User loginNavigation |
archivesA little insight on iterators/accumulatorsIf you've used a popular OO language chances are you've dealt with iterators. For example if I wanted to append to a list while traversing it back and fourth, 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 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. |
Browse archivesActive forum topics |
Recent comments
22 weeks 4 days ago
22 weeks 4 days ago
22 weeks 4 days ago
44 weeks 5 days ago
49 weeks 7 hours ago
50 weeks 4 days ago
50 weeks 4 days ago
1 year 1 week ago
1 year 5 weeks ago
1 year 5 weeks ago