dartora

IterationItemMap<K, V>

IterationItemMap is the standard concrete implementation of IterationMap. It wraps an existing Map<K,V> and maintains a parallel list of keys to preserve a deterministic order. This design allows the map to behave like a list of key–value pairs while supporting the full suite of Map operations and index‑based manipulations.

Constructor

IterationItemMap({
  required Map<K, V> map,
  int startIndex = 0,
  int indexTake = 0,
})

On initialisation the class copies map.keys into _keys, preserving insertion order. Subsequent insertions and removals update both _keys and _map so that the order of keys remains consistent.

Internal state

Field Description
_map The underlying Map<K,V> used for storage. Reads and writes operate directly on this map.
_keys A List<K> storing the keys of _map in deterministic order. All index‑based operations (such as getEntry(), removeAt(), insert()) manipulate this list to maintain order.
startIndex / indexTake Offsets inherited from IterationConstruct. They hide a number of entries at the beginning/end of _keys.

Core overrides

IterationItemMap implements the abstract hooks of IterationMap as follows:

Other methods of IterationMap (such as addAll, update, remove, removeAt, clear, getRange, sublist, reversed, skip, take) reuse these overrides and the _keys list to provide deterministic behaviour.

Notes on efficiency

Because IterationItemMap stores keys in a list, index‑based operations such as removeAt() and insert() run in O(n) time to shift subsequent elements. However, key‑based look‑ups (get(key), containsKey(key)) remain O(1) on average thanks to the underlying map. This trade‑off makes IterationItemMap suitable for moderately sized maps where deterministic ordering and windowed views are important.

Example usage

final backing = <String,int>{'a':1, 'b':2, 'c':3};
final iterMap = IterationItemMap(map: backing);
print(iterMap.keys);    // [a, b, c]
iterMap['b'] = 5;       // updates value without moving key
iterMap.insert(0,'d',0);// inserts new key 'd' at position 0
print(iterMap.keys);    // [d, a, b, c]
iterMap.remove('a');    // removes key 'a'
print(iterMap.values);  // [0, 5, 3]