HashMap α β
is a key-value map which stores elements in an array using a hash function
to find the values. This allows it to have very good performance for lookups
(average O(1)
for a perfectly random hash function), but it is not a persistent data structure,
meaning that one should take care to use the map linearly when performing updates.
Copies are O(n)
.
Equations
- Batteries.HashMap α β = Std.HashMap α β
Instances For
Make a new hash map with the specified capacity.
Equations
- Batteries.mkHashMap capacity = { inner := ⟨Std.DHashMap.Raw.empty capacity, ⋯⟩ }
Instances For
Equations
- Batteries.HashMap.instInhabited = { default := Batteries.mkHashMap }
Equations
- Batteries.HashMap.instEmptyCollection = { emptyCollection := Batteries.mkHashMap }
Make a new empty hash map.
(empty : Batteries.HashMap Int Int).toList = []
Equations
Instances For
Is the map empty?
(empty : Batteries.HashMap Int Int).isEmpty = true
(ofList [("one", 1), ("two", 2)]).isEmpty = false
Equations
- self.isEmpty = Std.HashMap.isEmpty self
Instances For
Inserts key-value pair a, b
into the map.
If an element equal to a
is already in the map, it is replaced by b
.
def hashMap := ofList [("one", 1), ("two", 2)]
hashMap.insert "three" 3 = {"one" => 1, "two" => 2, "three" => 3}
hashMap.insert "two" 0 = {"one" => 1, "two" => 0}
Equations
- self.insert a b = Std.HashMap.insert self a b
Instances For
Similar to insert
, but also returns a boolean flag indicating whether an existing entry has been
replaced with a => b
.
def hashMap := ofList [("one", 1), ("two", 2)]
hashMap.insert' "three" 3 = ({"one" => 1, "two" => 2, "three" => 3}, false)
hashMap.insert' "two" 0 = ({"one" => 1, "two" => 0}, true)
Equations
- m.insert' a b = (Std.HashMap.containsThenInsert m a b).swap
Instances For
Removes key a
from the map. If it does not exist in the map, the map is returned unchanged.
def hashMap := ofList [("one", 1), ("two", 2)]
hashMap.erase "one" = {"two" => 2}
hashMap.erase "three" = {"one" => 1, "two" => 2}
Equations
- self.erase a = Std.HashMap.erase self a
Instances For
Performs an in-place edit of the value, ensuring that the value is used linearly.
The function f
is passed the original key of the entry, along with the value in the map.
(ofList [("one", 1), ("two", 2)]).modify "one" (fun _ v => v + 1) = {"one" => 2, "two" => 2}
(ofList [("one", 1), ("two", 2)]).modify "three" (fun _ v => v + 1) = {"one" => 1, "two" => 2}
Equations
- self.modify a f = Std.HashMap.modify self a (f a)
Instances For
Looks up an element in the map with key a
. Returns b₀
if the element is not found.
def hashMap := ofList [("one", 1), ("two", 2)]
hashMap.findD "one" 0 = 1
hashMap.findD "three" 0 = 0
Equations
- self.findD a b₀ = Std.HashMap.getD self a b₀
Instances For
Looks up an element in the map with key a
. Panics if the element is not found.
def hashMap := ofList [("one", 1), ("two", 2)]
hashMap.find! "one" = 1
hashMap.find! "three" => panic!
Equations
- self.find! a = Std.HashMap.getD self a (panicWithPosWithDecl "Batteries.Data.HashMap.Basic" "Batteries.HashMap.find!" 134 15 "key is not in the map")
Instances For
Equations
- Batteries.HashMap.instGetElemOptionTrue = { getElem := fun (m : Batteries.HashMap α β) (k : α) (x : True) => m[k]? }
Given a key a
, returns a key-value pair in the map whose key compares equal to a
.
Note that the returned key may not be identical to the input, if ==
ignores some part
of the value.
def hashMap := ofList [("one", 1), ("two", 2)]
hashMap.findEntry? "one" = some ("one", 1)
hashMap.findEntry? "three" = none
Equations
Instances For
Returns true if the element a
is in the map.
def hashMap := ofList [("one", 1), ("two", 2)]
hashMap.contains "one" = true
hashMap.contains "three" = false
Equations
- self.contains a = Std.HashMap.contains self a
Instances For
Folds a monadic function over the elements in the map (in arbitrary order).
def sumEven (sum: Nat) (k : String) (v : Nat) : Except String Nat :=
if v % 2 == 0 then pure (sum + v) else throw s!"value {v} at key {k} is not even"
foldM sumEven 0 (ofList [("one", 1), ("three", 3)]) =
Except.error "value 3 at key three is not even"
foldM sumEven 0 (ofList [("two", 2), ("four", 4)]) = Except.ok 6
Equations
- Batteries.HashMap.foldM f init self = Std.HashMap.foldM f init self
Instances For
Folds a function over the elements in the map (in arbitrary order).
fold (fun sum _ v => sum + v) 0 (ofList [("one", 1), ("two", 2)]) = 3
Equations
- Batteries.HashMap.fold f init self = Std.HashMap.fold f init self
Instances For
Combines two hashmaps using a monadic function f
to combine two values at a key.
def map1 := ofList [("one", 1), ("two", 2)]
def map2 := ofList [("two", 2), ("three", 3)]
def map3 := ofList [("two", 3), ("three", 3)]
def mergeIfNoConflict? (_ : String) (v₁ v₂ : Nat) : Option Nat :=
if v₁ != v₂ then none else some v₁
mergeWithM mergeIfNoConflict? map1 map2 = some {"one" => 1, "two" => 2, "three" => 3}
mergeWithM mergeIfNoConflict? map1 map3 = none
Equations
- One or more equations did not get rendered due to their size.
Instances For
Combines two hashmaps using function f
to combine two values at a key.
mergeWith (fun _ v₁ v₂ => v₁ + v₂ )
(ofList [("one", 1), ("two", 2)]) (ofList [("two", 2), ("three", 3)]) =
{"one" => 1, "two" => 4, "three" => 3}
Equations
- One or more equations did not get rendered due to their size.
Instances For
Runs a monadic function over the elements in the map (in arbitrary order).
def checkEven (k : String) (v : Nat) : Except String Unit :=
if v % 2 == 0 then pure () else throw s!"value {v} at key {k} is not even"
forM checkEven (ofList [("one", 1), ("three", 3)]) = Except.error "value 3 at key three is not even"
forM checkEven (ofList [("two", 2), ("four", 4)]) = Except.ok ()
Equations
- Batteries.HashMap.forM f self = Std.HashMap.forM f self
Instances For
The number of buckets in the hash map.
Equations
- self.numBuckets = Std.HashMap.Internal.numBuckets self
Instances For
Builds a HashMap
from a list of key-value pairs.
Values of duplicated keys are replaced by their respective last occurrences.
ofList [("one", 1), ("one", 2)] = {"one" => 2}
Equations
- Batteries.HashMap.ofList l = List.foldl (fun (m : Batteries.HashMap α β) (x : α × β) => match x with | (k, v) => m.insert k v) Batteries.HashMap.empty l
Instances For
Variant of ofList
which accepts a function that combines values of duplicated keys.
ofListWith [("one", 1), ("one", 2)] (fun v₁ v₂ => v₁ + v₂) = {"one" => 3}
Equations
- One or more equations did not get rendered due to their size.