Hash sets #
This module develops the type Std.HashSet of hash sets.
Lemmas about the operations on Std.HashSet are available in the
module Std.Data.HashSet.Lemmas.
See the module Std.Data.HashSet.Raw for a variant of this type which is safe to use in
nested inductive types.
Hash sets.
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.
These hash sets contain a bundled well-formedness invariant, which means that they cannot
be used in nested inductive types. For these use cases, Std.Data.HashSet.Raw and
Std.Data.HashSet.Raw.WF unbundle the invariant from the hash set. When in doubt, prefer
HashSet over HashSet.Raw.
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.
Instances For
Two hash sets are equivalent in the sense of Equiv iff all their values are equal.
Instances For
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
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.
Instances For
Returns true if the given key 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 ==.
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.
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.
Instances For
Computes the intersection of the given hash sets. The result will only contain entries from the first map.
This function always iterates through the smaller set, so the expected runtime is
O(min(m₁.size, m₂.size)).
Instances For
We currently do not provide lemmas for the functions below.
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.
Instances For
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.