Iterator

All ordered containers have stateful iterators. Typically an iterator is obtained by Iterator() function of an ordered container. Once obtained, iterator’s Next() function moves the iterator to the next element and returns true if there was a next element. If there was an element, then element’s can be obtained by iterator’s Value() function. Depending on the ordering type, it’s position can be obtained by iterator’s Index() or Key() functions. Some containers even provide reversible iterators, essentially the same, but provide another extra Prev() function that moves the iterator to the previous element and returns true if there was a previous element.

Note: it is unsafe to remove elements from container while iterating.

IteratorWithIndex

An iterator whose elements are referenced by an index.

Typical usage:

  1. it := list.Iterator()
  2. for it.Next() {
  3. index, value := it.Index(), it.Value()
  4. ...
  5. }

Other usages:

  1. if it.First() {
  2. firstIndex, firstValue := it.Index(), it.Value()
  3. ...
  4. }
  1. for it.Begin(); it.Next(); {
  2. ...
  3. }

IteratorWithKey

An iterator whose elements are referenced by a key.

Typical usage:

  1. it := tree.Iterator()
  2. for it.Next() {
  3. key, value := it.Key(), it.Value()
  4. ...
  5. }

Other usages:

  1. if it.First() {
  2. firstKey, firstValue := it.Key(), it.Value()
  3. ...
  4. }
  1. for it.Begin(); it.Next(); {
  2. ...
  3. }

ReverseIteratorWithIndex

An iterator whose elements are referenced by an index. Provides all functions as IteratorWithIndex, but can also be used for reverse iteration.

Typical usage of iteration in reverse:

  1. it := list.Iterator()
  2. for it.End(); it.Prev(); {
  3. index, value := it.Index(), it.Value()
  4. ...
  5. }

Other usages:

  1. if it.Last() {
  2. lastIndex, lastValue := it.Index(), it.Value()
  3. ...
  4. }

ReverseIteratorWithKey

An iterator whose elements are referenced by a key. Provides all functions as IteratorWithKey, but can also be used for reverse iteration.

Typical usage of iteration in reverse:

  1. it := tree.Iterator()
  2. for it.End(); it.Prev(); {
  3. key, value := it.Key(), it.Value()
  4. ...
  5. }

Other usages:

  1. if it.Last() {
  2. lastKey, lastValue := it.Key(), it.Value()
  3. ...
  4. }