Hash sets without a bundled well-formedness invariant, suitable for use in nested
inductive types. The well-formedness invariant is called Raw.WF
. When in doubt, prefer HashSet
over HashSet.Raw
. Lemmas about the operations on Std.Data.HashSet.Raw
are available in the
module Std.Data.HashSet.RawLemmas
.
This is a simple separate-chaining hash table. The data of the hash set consists of a cached size and an array of buckets, where each bucket is a linked list of keys. The number of buckets is always a power of two. The hash set doubles its size upon inserting an element such that the number of elements is more than 75% of the number of buckets.
The hash table is backed by an Array
. Users should make sure that the hash set is used linearly to
avoid expensive copies.
The hash set uses ==
(provided by the BEq
typeclass) to compare elements and hash
(provided by
the Hashable
typeclass) to hash them. To ensure that the operations behave as expected, ==
should be an equivalence relation and a == b
should imply hash a = hash b
(see also the
EquivBEq
and LawfulHashable
typeclasses). Both of these conditions are automatic if the BEq
instance is lawful, i.e., if a == b
implies a = b
.
- inner : Std.HashMap.Raw α Unit
Internal implementation detail of the hash set.
Instances For
Creates a new empty hash set. The optional parameter capacity
can be supplied to presize the set
so that it can hold the given number of elements without reallocating. It is also possible to use
the empty collection notations ∅
and {}
to create an empty hash set with the default capacity.
Equations
- Std.HashSet.Raw.empty capacity = { inner := Std.HashMap.Raw.empty capacity }
Instances For
Equations
- Std.HashSet.Raw.instEmptyCollection = { emptyCollection := Std.HashSet.Raw.empty }
Inserts the given element into the set. If the hash set already contains an element that is
equal (with regard to ==
) to the given element, then the hash set is returned unchanged.
Note: this non-replacement behavior is true for HashSet
and HashSet.Raw
.
The insert
function on HashMap
, DHashMap
, HashMap.Raw
and DHashMap.Raw
behaves
differently: it will overwrite an existing mapping.
Instances For
Equations
- Std.HashSet.Raw.instSingletonOfBEqOfHashable = { singleton := fun (a : α) => Std.HashSet.Raw.empty.insert a }
Equations
- Std.HashSet.Raw.instInsertOfBEqOfHashable = { insert := fun (a : α) (s : Std.HashSet.Raw α) => s.insert a }
Checks whether an element is present in a set and inserts the element if it was not found.
If the hash set already contains an element that is equal (with regard to ==
) to the given
element, then the hash set is returned unchanged.
Equivalent to (but potentially faster than) calling contains
followed by insert
.
Equations
Instances For
Returns true
if the given element is present in the set. There is also a Prop
-valued version
of this: a ∈ m
is equivalent to m.contains a = true
.
Observe that this is different behavior than for lists: for lists, ∈
uses =
and contains
use
==
for comparisons, while for hash sets, both use ==
.
Equations
- m.contains a = m.inner.contains a
Instances For
Equations
- Std.HashSet.Raw.instMembershipOfBEqOfHashable = { mem := fun (m : Std.HashSet.Raw α) (a : α) => a ∈ m.inner }
Equations
- Std.HashSet.Raw.instDecidableMem = inferInstanceAs (Decidable (a ∈ m.inner))
Removes the element if it exists.
Equations
- m.erase a = { inner := m.inner.erase a }
Instances For
Checks if given key is contained and returns the key if it is, otherwise none
.
The result in the some
case is guaranteed to be pointer equal to the key in the map.
Equations
- m.get? a = m.inner.getKey? a
Instances For
Retrieves the key from the set that matches a
. Ensures that such a key exists by requiring a proof
of a ∈ m
. The result is guaranteed to be pointer equal to the key in the set.
Equations
- m.get a h = m.inner.getKey a h
Instances For
Checks if given key is contained and returns the key if it is, otherwise fallback
.
If they key is contained the result is guaranteed to be pointer equal to the key in the set.
Equations
- m.getD a fallback = m.inner.getKeyD a fallback
Instances For
Checks if given key is contained and returns the key if it is, otherwise panics. If no panic occurs the result is guaranteed to be pointer equal to the key in the set.
Equations
- m.get! a = m.inner.getKey! a
Instances For
Transforms the hash set into a list of elements in some order.
Equations
- m.toList = m.inner.keys
Instances For
We currently do not provide lemmas for the functions below.
We currently do not provide lemmas for the functions below.
Removes all elements from the hash set for which the given function returns false
.
Equations
- Std.HashSet.Raw.filter f m = { inner := Std.HashMap.Raw.filter (fun (a : α) (x : Unit) => f a) m.inner }
Instances For
Monadically computes a value by folding the given function over the elements in the hash set in some order.
Equations
- Std.HashSet.Raw.foldM f init b = Std.HashMap.Raw.foldM (fun (b : β) (a : α) (x : Unit) => f b a) init b.inner
Instances For
Folds the given function over the elements of the hash set in some order.
Equations
- Std.HashSet.Raw.fold f init m = Std.HashMap.Raw.fold (fun (b : β) (a : α) (x : Unit) => f b a) init m.inner
Instances For
Carries out a monadic action on each element in the hash set in some order.
Equations
- Std.HashSet.Raw.forM f b = Std.HashMap.Raw.forM (fun (a : α) (x : Unit) => f a) b.inner
Instances For
Support for the for
loop construct in do
blocks.
Equations
- Std.HashSet.Raw.forIn f init b = Std.HashMap.Raw.forIn (fun (a : α) (x : Unit) (acc : β) => f a acc) init b.inner
Instances For
Equations
- Std.HashSet.Raw.instForM = { forM := fun [Monad m] (m_1 : Std.HashSet.Raw α) (f : α → m PUnit) => Std.HashSet.Raw.forM f m_1 }
Equations
- Std.HashSet.Raw.instForIn = { forIn := fun {β : Type ?u.24} [Monad m] (m_1 : Std.HashSet.Raw α) (init : β) (f : α → β → m (ForInStep β)) => Std.HashSet.Raw.forIn f init m_1 }
Check if all elements satisfy the predicate, short-circuiting if a predicate fails.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Check if any element satisfies the predicate, short-circuiting if a predicate succeeds.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Transforms the hash set into an array of elements in some order.
Equations
- m.toArray = m.inner.keysArray
Instances For
Inserts multiple mappings into the hash set by iterating over the given collection and calling
insert
. If the same key appears multiple times, the first occurrence takes precedence.
Note: this precedence behavior is true for HashSet
and HashSet.Raw
. The insertMany
function on
HashMap
, DHashMap
, HashMap.Raw
and DHashMap.Raw
behaves differently: it will prefer the last
appearance.
Equations
- m.insertMany l = { inner := m.inner.insertManyIfNewUnit l }
Instances For
Creates a hash set from a list of elements. Note that unlike repeatedly calling insert
, if the
collection contains multiple elements that are equal (with regard to ==
), then the last element
in the collection will be present in the returned hash set.
Equations
- Std.HashSet.Raw.ofList l = { inner := Std.HashMap.Raw.unitOfList l }
Instances For
Creates a hash set from an array of elements. Note that unlike repeatedly calling insert
, if the
collection contains multiple elements that are equal (with regard to ==
), then the last element
in the collection will be present in the returned hash set.
Equations
- Std.HashSet.Raw.ofArray l = { inner := Std.HashMap.Raw.unitOfArray l }
Instances For
Computes the union of the given hash sets, by traversing m₂
and inserting its elements into m₁
.
Equations
- m₁.union m₂ = Std.HashSet.Raw.fold (fun (acc : Std.HashSet.Raw α) (x : α) => acc.insert x) m₁ m₂
Instances For
Equations
- Std.HashSet.Raw.instUnionOfBEqOfHashable = { union := Std.HashSet.Raw.union }
Returns the number of buckets in the internal representation of the hash set. This function may be useful for things like monitoring system health, but it should be considered an internal implementation detail.
Equations
Instances For
Equations
- Std.HashSet.Raw.instRepr = { reprPrec := fun (m : Std.HashSet.Raw α) (prec : Nat) => Repr.addAppParen (Std.Format.text "Std.HashSet.Raw.ofList " ++ reprArg m.toList) prec }
Well-formedness predicate for hash sets. Users of HashSet
will not need to interact with this.
Users of HashSet.Raw
will need to provide proofs of WF
to lemmas and should use lemmas like
WF.empty
and WF.insert
(which are always named exactly like the operations they are about) to
show that set operations preserve well-formedness.
- out : m.inner.WF
Internal implementation detail of the hash set