dartora

IterationMap<K, V>

IterationMap is an abstract base class that combines the windowing capabilities of IterationConstruct<V> with the standard Map<K, V> API. It views a sequence of key–value pairs as a list‑like structure that can be sliced, iterated and mutated while maintaining a deterministic order of keys.

Purpose

While a normal Map in Dart does not guarantee a particular order beyond insertion order, IterationMap formalises the concept of an ordered map view. It exposes startIndex and indexTake offsets (from IterationConstruct) to hide elements at the beginning or end of the map and provides index‑based operations in addition to key‑based look‑ups.

Concrete subclasses (such as IterationItemMap) supply the underlying storage and define how keys and values are stored and updated. IterationMap itself focuses on index manipulation and implements most of the Map interface in terms of abstract hooks.

Construction

IterationMap({
  int startIndex = 0,
  int indexTake = 0,
})

Subclasses must initialise the internal _startIndex and _indexTake fields and provide a concrete implementation of the sourceEntries getter.

Key fields and getters

Member Description
sourceEntries Abstract iterable of all MapEntry<K,V> objects in the backing map, in their full order (without applying startIndex/indexTake). Subclasses must implement this to drive iteration and look‑ups.
startIndex / indexTake Offsets inherited from IterationConstruct determining which portion of sourceEntries is exposed.
sourceLength Returns sourceEntries.length. Can be overridden for efficiency.
length Number of visible entries: (sourceLength − startIndex) − indexTake.
entries An IterationItem<MapEntry<K,V>> wrapping sourceEntries with the current window applied.
keys entries.map((entry) ⇒ entry.key).
values entries.map((entry) ⇒ entry.value).
operator [](key) / get(key) Returns the value associated with key or null if not found. Delegated to subclass implementation.
getEntry(index) Returns the visible MapEntry at index. Performs a range check and uses offset(index) to account for startIndex.
sourceGet(sourceIndex) Implements the IterationConstruct requirement by returning sourceEntries.elementAt(sourceIndex).value.
getKey(index) Returns the key of the entry at the given index.
indexOfKey(key,[start]) Searches from start (default 0) for the first index where the key matches. Returns −1 if not found.

Presence checks

IterationMap forwards the standard containsKey() and containsValue() methods to its keys and values iterables. Note that these operations perform a linear search unless overridden.

Modification hooks

Subclasses must implement two key methods that power all modifications:

Mutation methods

IterationMap implements the rest of the Map interface in terms of the above hooks:

Slicing and mapping

Like Iteration, IterationMap supports producing new views rather than materialising a whole map:

Remarks

IterationMap does not specify how keys are stored or how ordering is maintained; these responsibilities are left to subclasses. However, it does guarantee that operations using indices respect startIndex and indexTake. The concrete implementation IterationItemMap uses a list of keys to preserve order and an underlying map for storage.