1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#pragma once
#include <vector>
/// A class to help iterating over all values of multiple objects.
/// Templated over the type of the values. See test_multiindex.cc
/// for a sample use case.
template<class T>
class MultiIndex {
public:
typedef std::vector<T> values_type;
std::vector<values_type> values;
void start()
{
current_pos=std::vector<std::size_t>(values.size(), 0);
end_=false;
}
bool end() const
{
return end_;
}
MultiIndex& operator++()
{
current_pos[0]++;
std::size_t ci=0;
while(current_pos[ci] == values[ci].size()) {
if(ci==current_pos.size()-1) {
end_=true;
return *this;
}
current_pos[ci++]=0;
current_pos[ci]++;
}
return *this;
}
const T& operator[](std::size_t i)
{
return values[i][current_pos[i]];
}
private:
std::vector<std::size_t> current_pos;
bool end_;
};
|