archives

A little insight on iterators/accumulators

If you've used a popular OO language chances are you've dealt with iterators.
As it stands a language like C++ supports three - Normal, Bidirectional and
Random Access. While this seems enough, the usual developer only makes use
of the normal iterator. I certainly think theres room for improvement

For example if I wanted to append to a list while traversing it back and fourth,
I'd need to write additional code. One concept to this is a scan style iterator:

template 
  operator ++ ()
    m.fwd = m.pos == m_cont.size() - 1 ? 0 : m_pos == 0 ? 1 : m_fwd; 
    return m_fwd ? *m_cont[(m.pos++)] : *m.cont[(m_pos--)];  

The key to this iterator is that whenever the upper bounds or lower bounds is met the iterator simply traverses in its the opposite direction. Decent in situations where one would want to compare array values having added or removed new ones - the drawback however is knowing when to stop. In addition, one must be somewhat aware of its length throughout. Otherwise it can lead
to unexpected results.

Another example would be the nthElement iterator. This basically says that after each iteration we sort its indice in the array. Consquently sample dataset:

set = [n, n - 1, n + 1]
*set ++; // returns n 
set.at(0) // returns n - 1 
// [n - 1, n, n + 1]

Lastly consider a usage iterater. In a nutshell this says every time a resource uses an indice it's usage count increments. This practice involves keeping a pointer to the next ideal location in the array. Consider:

dataset = [1,2, .. , n]
consume(dataset[1])
*dataset ++; // returns 2 given we've used its indice the most

In this example the array itself needn't be rearranged the iterator could simply keep a pointer to the next location.

Anyway for brevity that's it for now.
I would appreciate feedback on this.