package kcas_data
Module Kcas_data.HashtblSource
Hash table.
The interface provides a subset of the OCaml Stdlib.Hashtbl module with some changes:
- The functorial interface of the
Stdlib.Hashtblis not provided. Instead the constructor functions,create,of_seq, andrebuild, take an optionalHashedTypemodule as an argument. By defaultcreatereturns a randomized hash table. - The
add_seqandreplace_seqoperations are not provided at all. - Non-instance specific operations related to randomization (e.g.
randomize,is_randomized) are not provided. - Non-instance specific operations related to hashing (e.g.
hash,seeded_hash,hash_param,seeded_hash_param) are not provided.
Compositional versions of find, to_seq, to_seq_keys, to_seq_values, rebuild, copy, iter, filter_map_inplace, fold, and stats are not provided.
Please note that the design is intentionally based on Stdlib.Hashtbl and copies its semantics as accurately as possible. Some of the operations come with warnings.
The hash table implementation is designed to avoid starvation. Read-only accesses can generally proceed in parallel without interference. Write accesses that do not change the number of bindings can proceed in parallel as long as they hit different internal buckets. Write accesses that change the number of bindings use a scalable Accumulator and only make infrequent random checks to determine whether the hash table should be resized.
Common interface
The type of hash tables from type 'k to type 'v.
First-class HashedType module type abbreviation.
val create :
?hashed_type:'k hashed_type ->
?min_buckets:int ->
?max_buckets:int ->
unit ->
('k, 'v) tcreate () returns a new empty hash table.
- The default
hashis computed asStdlib.Hashtbl.hash (Random.bits ()). - The default
equalis(=). - The default
min_bucketsis unspecified and a givenmin_bucketsmay be adjusted by the implementation. - The default
max_bucketsis the minimum of1 lsl 30and suitably adjustedSys.max_array_lengthand a givenmax_bucketsmay be adjusted by the implementation.
Hash tables are automatically internally resized.
hashed_type_of t returns a copy of the hashed type used when the hash table t was created.
min_buckets_of t returns the minimum number of buckets of the hash table t.
NOTE: The returned value may not be the same as given to create.
max_buckets_of t returns the maximum number of buckets of the hash table t.
NOTE: The returned value may not be the same as given to create.
val of_seq :
?hashed_type:'k hashed_type ->
?min_buckets:int ->
?max_buckets:int ->
('k * 'v) Seq.t ->
('k, 'v) tof_seq assoc creates a new hash table from the given association sequence assoc. The associations are added in the same order as they appear in the sequence, using replace, which means that if two pairs have the same key, only the latest one will appear in the table. See create for the optional arguments.
⚠️ of_seq (to_seq t) does not necessarily copy the bindings of a hash table correctly.
Compositional interface
Non-compositional interface
length t returns the number of bindings in the hash table t.
⚠️ The returned value may be greater than the number of distinct keys in the hash table.
reset t remove all bindings from the hash table t and shrinks the capacity of the table back to the minimum.
swap t1 t2 exchanges the contents of the hash tables t1 and t2.
remove t k removes the most recent existing binding of key k, if any, from the hash table t thereby revealing the earlier binding of k, if any.
add t k v adds a binding of key k to value v to the hash table shadowing the previous binding of the key k, if any.
⚠️ Consider using replace instead of add.
replace t k v adds a binding of key k to value v or replaces the most recent existing binding of key k in the hash table t.
find_opt t k returns the current binding of key k in the hash table t, or None if no such binding exists.
find_all t k returns a list of all the bindings of the key k in the hash table in reverse order of their introduction.
find t k returns the current binding of k in hash table t, or raises Not_found if no such binding exists.
to_seq t takes a snapshot of the keys and values in the hash table and returns them as an association sequence. Bindings of each individual key appear in the sequence in reverse order of their introduction.
⚠️ of_seq (to_seq t) does not necessarily copy the bindings of a hash table correctly.
to_seq_keys t is equivalent to to_seq t |> Seq.map fst.
⚠️ The sequence may include duplicates.
to_seq_values t is equivalent to to_seq t |> Seq.map snd.
⚠️ The sequence may include values of bindings that are hidden.
val rebuild :
?hashed_type:'k hashed_type ->
?min_buckets:int ->
?max_buckets:int ->
('k, 'v) t ->
('k, 'v) tcopy t is equivalent to rebuild t. In other words, the returned hash table uses the same hashed_type (and other parameters) as the given hash table t.
iter f t is equivalent to Seq.iter (fun (k, v) -> f k v) (to_seq t).
filter_map_inplace f t applies f to all bindings in the hash table t and updates each binding depending on the result of f. If f returns None, the binding is discarded. If f returns Some new_value, the binding is updated to associate the key to the new_value.
⚠️ The given f may be called multiple times for the same bindings from multiple domains in parallel.
fold f t a is equivalent to Seq.fold_left (fun a (k, v) -> f k v a) a (to_seq t).
stats t returns statistics about the hash table t.