std::bitset - how to iterate "set" (or "not set") bits? [duplicate] - c++

This question already has answers here:
Efficient way of iterating over true bits in std::bitset?
(7 answers)
Closed 8 years ago.
I'm using bitset in my code:
std::bitset<MAX_INSTRUMENTS_NUMBER_IN_SYSTEM> updatedInstruments;
Often I need to iterate only values that "set" (or "not set"), this is how I do that:
for (int instrumentId = 0; instrumentId < MAX_INSTRUMENTS_NUMBER_IN_SYSTEM; instrumentId++) {
if (!updatedInstruments[instrumentId]) {
continue;
}
// work
}
Can this iteration be improved to be more readable and possibly faster?

I don't think you can exploit the contiguity of set bits in your code using std::bitset: the interface doesn't provide anything to help in this task, and there's no legal way to access the underlying storage and work directly with it.1
If instead you can afford to change container, you can find several better alternatives. Here for example is a bitset-like structure that provides "enumerators" for active bits (it seems mostly like working with spans on images), and in the duplicate I linked above there are several other suggestions for data structures more specialized for this use case.
Previously I supposed that iterators might have yielded some performance advantage, but turns out std::bitset doesn't have iterators :-o Also, a similar test performed on std::vector<bool> (which should pack bits more or less the same way) gave a ~2x slowdown using iterators both with g++ and clang++.

Related

Does priority_queue provide a predictable order? [duplicate]

This question already has an answer here:
c++ ordered(stable) priority queue
(1 answer)
Closed 5 years ago.
wise guys
My question was like this:
I need to use priority_queue from std, everything works fine, until if there exists ties between my records, the order is no long consistent if I compile using clang compared to compiling on gcc.
my comparator function is simple:
bool comparator(const max_pair_t &lhs, const max_pair_t &rhs) {
return lhs.pval < rhs.pval;
}
that's it.
Is there a way to resolve this problem?
PS: I printed out all the records using two binary excutables, and compared the order side by side, the order is different, but the tied records are in the neighboring area
std::priority_queue gives no guarantees about sort stability. If you need sort stability, you'll have to provide it yourself, e.g. by storing a progressively increasing or decreasing value (doesn't really matter which, it just changes the direction of the fallback comparison) that is used when the primary comparison key is equal, and stripping it off when you pop off the queue.

In a low-latency application, Is unordered_map ever a better solution over vector?

Is it advisable to use unordered_map in place of vector while developing a low-latency application ?
I recently appeared for an interview with a financial company which worked on low-latency trading applications. I was asked a question for which I answered using an unordered_map which seemed pretty good efficiency-wise (0(n)) compared to If I had used a vector (O(n*n)). However, I know that it is advisable to use vector as much as possible and avoid unordered_map in order to utilize benefits of cache coherence. I just wanted to see If there is a better solution possible for this problem The problem I was asked was to check If two strings are a permutation of each other.
bool isPermutation(const std::string& first, const std::string& second) {
std::unordered_map<char, int> charDict;
if(first.length() != second.length())
return false;
for(auto it: first) {
charDict[it]++;
}
for(auto it: second) {
if(charDict.count(it) > 0) {
--charDict[it];
} else {
return false;
}
return true;
}
You can assume that both strings are equal length and the function is only assumed to return true If there is an exact number of occurrences of each character in second string as there are in the first string.
Sure, but it really depends on the problem you are trying to solve. If the domain of your key space is unknown, it would be difficult to come up with a generic solution that is faster than unordered_map.
In this case, the domain of your key space is known: it is limited to ASCII characters. This is convenient because you can instantly convert from item (char) to vector index (std::size_t). So you could just use the value of each character as an index into a vector rather than hashing it for every lookup.
But in general, don't optimize prematurely. If unordered_map is the most natural solution, I would start there, then profile, and if you find that performance does not meet your requirements, look at reworking your solution. (This isn't always the best advice; if you know you are working on a highly critical piece of code, there are certain design decisions you will want to take into consideration from the beginning. Coming back and refactoring later may be much more difficult if you start with an incompatible design.)
Since there are only 256 possible keys, you can use a stack-allocated array of 256 counts, which will be faster than a vector or an unordered_map. if first.size()+second.size() < 128, then only initialize the counts to 0 for keys that actually occur. Otherwise memset the whole array.

Which datastructure to use for finding an element [duplicate]

This question already has answers here:
How can I efficiently select a Standard Library container in C++11?
(4 answers)
Closed 4 years ago.
I have several items saved in a list. I would like to add items that have already been processed to a datastructure (this makes sense in my case even though you might wonder why). When processing the next item from the list I first want to make sure if it has been processed before so lets say something like this:
if(element_is_in_datastructure(current_element)) {
do this
}
else
{
do that
add_element_to_datastructure(current_element)
}
My question is, what is the ideal datastructure where checking if the element is in it won't take too long. At the moment I don't have too many elements (max 30) which will be added to the datastructure, but this number might increase and I don't want to lose performance.
You can use a map e.g std::unordered_map to store your elements as keys.
Then just check their presence e.g
if(!yourMap.count(element))
{
// your element is not in the structure
}
This finding takes logarithmic time in the map's size to finish.

same code, different order if tie exists in priority_queue, using clang and gcc [duplicate]

This question already has an answer here:
c++ ordered(stable) priority queue
(1 answer)
Closed 5 years ago.
wise guys
My question was like this:
I need to use priority_queue from std, everything works fine, until if there exists ties between my records, the order is no long consistent if I compile using clang compared to compiling on gcc.
my comparator function is simple:
bool comparator(const max_pair_t &lhs, const max_pair_t &rhs) {
return lhs.pval < rhs.pval;
}
that's it.
Is there a way to resolve this problem?
PS: I printed out all the records using two binary excutables, and compared the order side by side, the order is different, but the tied records are in the neighboring area
std::priority_queue gives no guarantees about sort stability. If you need sort stability, you'll have to provide it yourself, e.g. by storing a progressively increasing or decreasing value (doesn't really matter which, it just changes the direction of the fallback comparison) that is used when the primary comparison key is equal, and stripping it off when you pop off the queue.

Multidimensional Array Implementation [duplicate]

This question already has an answer here:
how to traverse a boost::multi_array
(1 answer)
Closed 8 years ago.
After searching for a while I did not find an answer to my question, so apologies in advance if this was already answered somewhere else.
I'm looking for a multi-dimensional data-structure in C++ that allows access not only as N-dimensional array, but also as 1-dimensional.
For an example assume a simple 2-dimensional matrix (it could go to higher dimensions, but in that case let's stick to this example). In most cases the member will be accessed in row-colum-form, e.g. matrix[x][y]. In other cases it might be desireable to access all members as a single list, e.g. for matrix addition using std-algorithms.
Standard-Approach would probably be something like std::array<std::array<double, 4>, 4> and write additionally an iterator with linear access to all members and maybe an extra accessor function.
A second approach is the other way around std::array<double, 16> with accessors in row-colum-form, but in this case it gets tricky to return whole columns .
Or maybe it is doable with boost MultiArray, but I think reducing the dimensions of a MultiArray always results in only getting slices of the MultiArray.
My question boils down to: Is there already an implementation in the standard-library or some well-known library, like boost, for this? If not, am I missing a point and there is a simpler method than the ones I wrote about?
EDIT: I was not looking for only iteration over all values, like in the mentioned question. But however from the pointed documentation I could find that MultiArray can be accessed as C-style array which is enough for my needs. This can then be closed and thanks for all answers
See boost::multi_array::data() and boost::multi_array::num_elements().
As with std::vector, it would appear you can just access it as a solid block of memory by index, if that's all you want. I've never done this, but looks like you can. Just because you can doesn't necessarily mean you should, but, well...
See this answer:
how to traverse a boost::multi_array
There is something like what you are looking for: std::valarray<T>. Well, the intend of the std::valarray<T> class template is to provide different views on the same array and to support potentially vectorized evaluation. That said, it doesn't really work and probably few people are using it.
However, from what you described you probably want to have something providing an array view on an existing array. I'd be pretty sure that this was implemented before, if nothing else as a replacement for std::valarray<T> but I can't point to an implementation.