A map from Nat keys to values, backed by a hash map.
$$\text{IntMap}(v) \cong \text{HashMap}(\mathbb{N}, v)$$
Provides amortised $O(1)$ lookup and insert.
Equations
- Data.IntMap v = Std.HashMap Nat v
Instances For
The empty map. $$\text{empty} = \emptyset$$
Equations
Instances For
A map with a single key-value pair. $$\text{singleton}(k, v) = \{k \mapsto v\}$$
Equations
- Data.IntMap.singleton key val = Std.HashMap.insert ∅ key val
Instances For
Build a map from an association list. Later entries override earlier ones for duplicate keys. $$\text{fromList}([(k_1,v_1),\ldots,(k_n,v_n)])$$
Equations
Instances For
Look up a key. $$\text{lookup}(k, m) = \begin{cases} \text{some}(v) & \text{if } k \mapsto v \in m \\ \text{none} & \text{otherwise} \end{cases}$$
Equations
- Data.IntMap.lookup key m = Std.HashMap.get? m key
Instances For
Look up a key, returning a default if absent. $$\text{findWithDefault}(d, k, m) = \begin{cases} v & \text{if } k \mapsto v \in m \\ d & \text{otherwise} \end{cases}$$
Equations
- Data.IntMap.findWithDefault dflt key m = (Std.HashMap.get? m key).getD dflt
Instances For
Test whether a key is in the map. $$\text{member}(k, m) \iff k \in \text{dom}(m)$$
Equations
- Data.IntMap.member key m = Std.HashMap.contains m key
Instances For
Is the map empty? $$\text{null}(m) \iff m = \emptyset$$
Equations
- m.null = Std.HashMap.isEmpty m
Instances For
The number of entries. $$\text{size'}(m) = |m|$$
Equations
- m.size' = Std.HashMap.size m
Instances For
Insert a key-value pair. If the key already exists, the value is replaced. $$\text{insert}(k, v, m) = m[k \mapsto v]$$
Equations
- Data.IntMap.insert' key val m = Std.HashMap.insert m key val
Instances For
Delete a key from the map. $$\text{delete}(k, m) = m \setminus \{k\}$$
Equations
- Data.IntMap.delete key m = Std.HashMap.erase m key
Instances For
Adjust the value at a key, if present. $$\text{adjust}(f, k, m) = \begin{cases} m[k \mapsto f(v)] & \text{if } k \mapsto v \in m \\ m & \text{otherwise} \end{cases}$$
Equations
- Data.IntMap.adjust f key m = match Std.HashMap.get? m key with | some val => Std.HashMap.insert m key (f val) | none => m
Instances For
Left-biased union of two maps. $$\text{union}(m_1, m_2) = m_1 \cup m_2 \quad (\text{left-biased})$$
Equations
- m1.union m2 = Std.HashMap.fold (fun (acc : Data.IntMap v) (key : Nat) (val : v) => if Std.HashMap.contains acc key = true then acc else Std.HashMap.insert acc key val) m1 m2
Instances For
Union with a combining function. $$\text{unionWith}(f, m_1, m_2)[k] = \begin{cases} f(v_1, v_2) & \text{if } k \in m_1 \cap m_2 \\ v_1 & \text{if } k \in m_1 \text{ only} \\ v_2 & \text{if } k \in m_2 \text{ only} \end{cases}$$
Equations
- One or more equations did not get rendered due to their size.
Instances For
Intersection of two maps, keeping values from the first. $$\text{intersection}(m_1, m_2) = \{k \mapsto v \mid k \mapsto v \in m_1, k \in \text{dom}(m_2)\}$$
Equations
- m1.intersection m2 = Std.HashMap.filter (fun (key : Nat) (x : v) => Std.HashMap.contains m2 key) m1
Instances For
Intersection with a combining function. $$\text{intersectionWith}(f, m_1, m_2)[k] = f(v_1, v_2) \text{ for } k \in \text{dom}(m_1) \cap \text{dom}(m_2)$$
Equations
- One or more equations did not get rendered due to their size.
Instances For
Difference of two maps. $$\text{difference}(m_1, m_2) = \{k \mapsto v \mid k \mapsto v \in m_1, k \notin \text{dom}(m_2)\}$$
Equations
- m1.difference m2 = Std.HashMap.filter (fun (key : Nat) (x : v) => !Std.HashMap.contains m2 key) m1
Instances For
Left fold over key-value pairs (unspecified order). $$\text{foldlWithKey}(f, z, m)$$
Equations
- Data.IntMap.foldlWithKey f init m = Std.HashMap.fold (fun (acc : α) (key : Nat) (val : v) => f acc key val) init m
Instances For
Right fold over key-value pairs (unspecified order). $$\text{foldrWithKey}(f, z, m)$$
Equations
- Data.IntMap.foldrWithKey f init m = List.foldr (fun (x : Nat × v) (acc : α) => match x with | (key, val) => f key val acc) init (Std.HashMap.toList m)
Instances For
Map a function over all values. $$\text{mapValues}(f, m)[k] = f(m[k])$$
Equations
- Data.IntMap.mapValues f m = Std.HashMap.map (fun (x : Nat) (val : v) => f val) m
Instances For
Map a function over all key-value pairs. $$\text{mapWithKey}(f, m)[k] = f(k, m[k])$$
Equations
- Data.IntMap.mapWithKey f m = Std.HashMap.map (fun (key : Nat) (val : v) => f key val) m
Instances For
Filter entries by a predicate on keys and values. $$\text{filterWithKey}(p, m) = \{k \mapsto v \mid k \mapsto v \in m, p(k, v)\}$$
Equations
Instances For
Convert the map to a list of key-value pairs (unspecified order). $$\text{toList'}(m) = [(k_1, v_1), \ldots, (k_n, v_n)]$$
Equations
Instances For
Convert the map to an association list sorted by ascending key.
$$\text{toAscList}(m) = [(k_1, v_1), \ldots, (k_n, v_n)]$$ where $k_1 < \cdots < k_n$.
Note: requires $O(n \log n)$ sort since the backing HashMap is unordered.
Equations
Instances For
Restrict a map to only the keys in the given list. $$\text{restrictKeys}(m, ks) = \{k \mapsto v \mid k \mapsto v \in m, k \in ks\}$$
Equations
- m.restrictKeys ks = Std.HashMap.filter (fun (key : Nat) (x : v) => (List.foldl (fun (s : Std.HashMap Nat Unit) (key : Nat) => s.insert key ()) ∅ ks).contains key) m
Instances For
Remove all keys in the given list from the map. $$\text{withoutKeys}(m, ks) = \{k \mapsto v \mid k \mapsto v \in m, k \notin ks\}$$
Equations
- m.withoutKeys ks = Std.HashMap.filter (fun (key : Nat) (x : v) => !(List.foldl (fun (s : Std.HashMap Nat Unit) (key : Nat) => s.insert key ()) ∅ ks).contains key) m
Instances For
The smallest key-value pair, or none if the map is empty.
$$\text{lookupMin}(m) = \min_{k} \{(k, v) \mid k \mapsto v \in m\}$$
Note: $O(n)$ since the backing HashMap is unordered.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The largest key-value pair, or none if the map is empty.
$$\text{lookupMax}(m) = \max_{k} \{(k, v) \mid k \mapsto v \in m\}$$
Note: $O(n)$ since the backing HashMap is unordered.
Equations
- One or more equations did not get rendered due to their size.