Re: Help with algorithm for iterating through a multidimensional array
On Jan 18, 11:47 pm, Martin Eisenberg <martin.eisenb...@udo.edu>
wrote:
Jesse Perla wrote:
Then I am creating a sequential_iterator which operates on the
underlying data pointer of the container and simultaneously
increments the mixed_radix counter. Last, to implement the
distance() concept of the sequential_multi_index_iterator, I will
subtract the two index arrays.
But you'd have to resum the difference tuple into an integer which
gives the same value as subtracting the pointers in the first place.
Martin
{ signature and clc++m banner removed. please remove them yourself. -mod }
I may not understand your concern. With a forward only iterator that
combines a linear data iterator with a multiradix counter, I can just
increment my data pointer with simple pointer arithmetic. Then for
the multiradix increment, for most cases it will only require a couple
of increment operations (one unless we are hitting the "nines" and a
few equality checks on the "nines"). This seems about as efficient as
I can imagine and doesn't require a bunch of relatively expensive
modulo operators for each dimension every time we ask for the current
index in multiradix form.
Of course, one major issue with this is that it only works for dense
storage of the data. However, it will work for fortran vs. C ordering
by choosing left vs. right first order for multiradix counting.
Assuming that I have implemented a multiradix counter with some of the
code here (with a ++ operator to increment, and a .digits() function
to get the current position), here is my code for the hardcoded
boost::multi_array<T, N> container:
template<class T, int N>
class sequential_multidim_iterator : public
boost::forward_iterator_helper<
sequential_multidim_iterator, T, std::ptrdiff_t, T*, T>
{
typedef sequential_multidim_iterator<T, N> self;
private:
T* data_; // The underlying data pointer
T* data_end_;
T* data_iter_; //The current position
mixedradix_counter<N> index_; //Current index for the position.
public:
explicit sequential_multidim_iterator(boost::multi_array<T, N>& c) :
data_(c.data())
{
data_end_ = c.data() + c.num_elements();
etk::lexicographic_order order;
if(c.storage_order().ordering(0) > c.storage_order().ordering(1)) //
i.e. c ordering
order = right_first;
else
order = left_first; //otherwise, fortran. Note we are not using
proper general orders here.
boost::array<int, N> shape;
for(int i = 0; i < N; ++i)
shape[i] = c.shape()[i];
index_.reset(shape, order); // Shape gives the extents
}
T& operator*()
{
return *data_iter_;
}
inline void first()
{
data_iter_ = data_;
index_.begin();
}
inline void end()
{
data_iter_ = data_end_;
index_.end();
}
self& operator++()
{
++data_iter_;
++index_;
return *this;
}
boost::array<int, N>& indices() const
{
return index_.digits();
}
friend bool operator==(const self& x, const self& y) {
return x.data_iter_ == y.data_iter_; } //Don't compare the
indicies.
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]