Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass vector position in std::for_each

Tags:

c++

stl

I have a data structure in sparse compressed column format.

For my given algorithm, I need to iterate over all the values in a "column" of data and do a bunch of stuff. Currently, it is working nicely using a regular for loop. The boss wants me to re-code this as a for_each loop for future parallelization.

For those not familiar with sparse compressed column, it use 2 (or 3) vectors to represent the data. One vector is just a long list of values. The second vector is the index of where each column starts.

The current version // for processing data in column 5 vector values; vector colIndex; vector rowIndex;

int column = 5;
for(int i = conIndex[5]; i != colIndex[6]; i++){
    value = values[i];
    row = rowIndex[i];
    // do stuff
}

The key is that I need to know the location(as an integer) in my values column in order to lookup the row position (And a bunch of other stuff I'm not bothering to list here.)

If I use the std::for_each() function, I get the value at the position, not the position. I need the position itself.

One thought, and clearly not efficient, would be to create a vector of integers the same length as my data. That way, I could pass an iterator over that dummy vector to the function in for_each and the value passed to my function would be the postion. However, this seems like the least efficient way.

Any thoughts?

My challenge is that I need to know the position in the vector. for_each takes an iterator and sends the value of that iterator to the function.

like image 800
Noah Avatar asked Apr 23 '26 21:04

Noah


2 Answers

Use boost::counting_iterator<int>, or implement your own.

like image 86
n. 1.8e9-where's-my-share m. Avatar answered Apr 25 '26 12:04

n. 1.8e9-where's-my-share m.


@n.m.'s answer is probably the best, but it is possible with only what the standard library provides, though painfully slow I assume:

void your_loop_func(const T& val){
    iterator it = values.find(val);
    std::ptrdiff_t index = it - values.begin();
    value = val;
    row = rowIndices[index];
 }

And after writing that, I really can only recommend the Boost counting_iterator version. ;)

like image 26
Xeo Avatar answered Apr 25 '26 11:04

Xeo