Reference Guide: Associative Array Iterators

By virtue of polymorphism, all Map implementations use a single MapIterator that hides complexity of actual solution used (eg: HashMap). To make this possible, each class implementing Map needs to come with a MapIterator implementation adapted to the data structure they are using and keep it hidden to end users so they only work with abstractions.

Usage example:

HashMap<long,long> map; map.set(1,2); map.set(3,4); // This uses MapIterator<long,long>* for MapIterator<long,long>*, instead of MapIterator<long,long> for(auto it = map.begin(); *it!=*(map.end()); ++(*it)) { std::cout << (*(*it)).first << ":" << (*(*it)).second << std::endl; }

Whereas STL equivalent is:

std::unordered_map<long,long> map; map[1]=2; map[3]=4; // This uses iterator for std::unordered_map<long,long> directly for(auto it = map.begin(); it!=map.end(); ++it) { std::cout << it->first << << it->last << std::endl; }

As one can see above, because API iterators are polymorphic, they require an extra pointer compared to those of STL. Unlike STL, currently only forward non-const iterators are supported, but these will be added in the near future!


Forward iterator prototype for Map, returned by latter's begin and end methods


template <typename K, typename V> class MapIterator


Method Signature Description
MapIterator MapIterator () Constructs iterator, setting offset
~MapIterator virtual ~MapIterator () Virtual destructor used when iterator is deallocated
operator* virtual const std::pair<K, V> operator* () = 0 Dereferencing operator required when accessing current element
operator++ virtual void operator++ () = 0 Advance operator required when iterating
operator!= bool operator!= (const MapIterator<K,V>& it) Difference operator required when iterating