bitvector

Determining a string has all unique characters without using additional data structures and without the lowercase characters assumption

你离开我真会死。 提交于 2019-11-30 14:06:01
This is one of the questions in the Cracking the Coding Interview book by Gayle Laakmann McDowell: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? The author wrote: We can reduce our space usage a little bit by using a bit vector. We will assume, in the below code, that the string is only lower case 'a' through 'z' . This will allow us to use just a single int. The author has this implementation: public static boolean isUniqueChars(String str) { int checker = 0; for (int i = 0; i < str.length(); ++i) { int val = str

Are std::fill, std::copy specialized for std::vector<bool>?

喜夏-厌秋 提交于 2019-11-30 12:24:22
When thinking about this question I start to wondering if std::copy() and/or std::fill are specialized (I really mean optimized) for std::vector<bool> . Is this required by C++ standard or, perhaps, it is common approach by C++ std library vendors? Simple speaking, I wonder to know if the following code: std::vector<bool> v(10, false); std::fill(v.begin(), v.end(), true); is in any way better/different than that: std::vector<bool> v(10, false); for (auto it = v.begin(); it != v.end(); ++it) *it = true; To be very strict - can, let say: std::fill<std::vector<bool>::iterator>() go into internal

bitwise operations on vector<bool>

只愿长相守 提交于 2019-11-30 08:55:38
问题 what's the best way to perform bitwise operations on vector<bool> ? as i understand, vector<bool> is a specialisation that uses one bit per boolean. I chose vector<bool> for memory saving reasons. I know that there are some problems with vector<bool> but for my needs it's apropriate. now - what's the most performant way of aplying bitwise operations to whole such vectors? if i do it in a for loop and readout each single bool and store it back, the way I understand it a lot more operations are

Could multiple proxy classes make up a STL-proof bitvector?

你。 提交于 2019-11-29 09:36:20
It's well known that std::vector<bool> does not satisfy the Standard's container requirements, mainly because the packed representation prevents T* x = &v[i] from returning a pointer to a bool. My question is: can this be remedied/mitigated when the reference_proxy overloads the address-of operator& to return a pointer_proxy? The pointer-proxy could contain the same data as the reference_proxy in most implementations, namely a pointer into the packed data and a mask to isolate the particular bit inside the block pointed to. Indirection of the pointer_proxy would then yield the reference_proxy.

bitwise operations on vector<bool>

戏子无情 提交于 2019-11-29 09:14:27
what's the best way to perform bitwise operations on vector<bool> ? as i understand, vector<bool> is a specialisation that uses one bit per boolean. I chose vector<bool> for memory saving reasons. I know that there are some problems with vector<bool> but for my needs it's apropriate. now - what's the most performant way of aplying bitwise operations to whole such vectors? if i do it in a for loop and readout each single bool and store it back, the way I understand it a lot more operations are performed inside in order to access the actual values. thanks! If the number of bits are fixed at

C++11 vector<bool> performance issue (with code example)

那年仲夏 提交于 2019-11-29 09:07:43
I notice that vector is much slower than bool array when running the following code. int main() { int count = 0; int n = 1500000; // slower with c++ vector<bool> /*vector<bool> isPrime; isPrime.reserve(n); isPrime.assign(n, true); */ // faster with bool array bool* isPrime = new bool[n]; for (int i = 0; i < n; ++i) isPrime[i] = true; for (int i = 2; i< n; ++i) { if (isPrime[i]) count++; for (int j =2; i*j < n; ++j ) isPrime[i*j] = false; } cout << count << endl; return 0; } Is there some way that I can do to make vector<bool> faster ? Btw, both std::vector::push_back and std::vector::emplace

Fast code for searching bit-array for contiguous set/clear bits?

大憨熊 提交于 2019-11-29 07:04:12
Is there some reasonably fast code out there which can help me quickly search a large bitmap (a few megabytes) for runs of contiguous zero or one bits? By "reasonably fast" I mean something that can take advantage of the machine word size and compare entire words at once, instead of doing bit-by-bit analysis which is horrifically slow (such as one does with vector<bool> ). It's very useful for e.g. searching the bitmap of a volume for free space (for defragmentation, etc.). Windows has an RTL_BITMAP data structure one can use along with its APIs. But I needed the code for this sometime ago,

Bit vector and bitset

本秂侑毒 提交于 2019-11-28 11:36:12
问题 What is the difference between bit-vector and bitset container of stl ? Please explain. To my understanding bitset is the implementation of the concept of bitvector am I right or wrong? What are the other ways to implement bit vector? 回答1: bit_vector has the same interface as an std::vector , and is optimised for space. It not a part of standard C++. This documentation claims it is close to an STL vector<bool> , which presumably is quite close to a standard C++ std::vector<bool> . std::bitset

Could multiple proxy classes make up a STL-proof bitvector?

流过昼夜 提交于 2019-11-28 02:56:48
问题 It's well known that std::vector<bool> does not satisfy the Standard's container requirements, mainly because the packed representation prevents T* x = &v[i] from returning a pointer to a bool. My question is: can this be remedied/mitigated when the reference_proxy overloads the address-of operator& to return a pointer_proxy? The pointer-proxy could contain the same data as the reference_proxy in most implementations, namely a pointer into the packed data and a mask to isolate the particular

Fast code for searching bit-array for contiguous set/clear bits?

蓝咒 提交于 2019-11-28 00:37:37
问题 Is there some reasonably fast code out there which can help me quickly search a large bitmap (a few megabytes) for runs of contiguous zero or one bits? By "reasonably fast" I mean something that can take advantage of the machine word size and compare entire words at once, instead of doing bit-by-bit analysis which is horrifically slow (such as one does with vector<bool> ). It's very useful for e.g. searching the bitmap of a volume for free space (for defragmentation, etc.). 回答1: Windows has