This module defines pure hash tables. All operations are non-destructive:
Any "change" to a hash table (adding/deleting elements) produces a new hash
table. Copying is avoided as much as possible, i.e., the old and new tables
share values. This may lead to surprises if variables in the table are bound
later on (or if they are modified destructively).
- author
- - GergĂ¶ Barany <gergo@tud.at>
- license
- - LGPL
- empty_p_hashtbl(-Table) is det
- Unifies Table with a newly created empty hash table of a default size.
- empty_p_hashtbl(-Table, +Size) is det
- Unifies Table with a newly created empty hash table with the number of
buckets given by Size.
- throws
- - Type or domain error if Size is not a non-negative integer.
- p_hashtbl_put(+Table, +Key, +Value, -TableOut) is det
- TableOut is a hash table like Table, except that in TableOut Key is
mapped to Value. If Key is already mapped to some values in Table, those
mappings are present but shadowed by Value in TableOut.
- p_hashtbl_set(+Table, +Key, +Value, -TableOut) is det
- TableOut is a hash table like Table, except that in TableOut Key is
mapped to Value. If Key is already mapped to some values in Table, the
most recent one of those is replaced by Value. Other values for Key
remain shadowed in TableOut.
- p_hashtbl_get(+Table, +Key, -Value) is semidet
- Gets the most recently stored Value for Key in Table, if any. Fails if no
values are stored for Key. There is no separate predicate for membership
checks, use
p_hashtbl_get(Table, Key, _)
to test whether Key is
present in Table.
- p_hashtbl_get_all(+Table, +Key, -Value) is nondet
- On backtracking, enumerates every Value associated with Key in Table.
Fails if no values are stored for Key. The order of enumeration is the
most recently added value for Key first (the same as the solution for
p_hashtbl_get/3), then shadowed ones.
- p_hashtbl_get_default(+Table, +Key, ?Default, -Value, -TableOut) is det
- If Table contains an entry for Key, unifies Value with the corresponding
value as in p_hashtbl_get/3 and TableOut with Table. Otherwise, unifies
Value with Default and adds this value to the Table under Key (as by
p_hashtbl_put/4) and unifies TableOut with the resulting table.
- p_hashtbl_delete(!Table, +Key, -TableOut) is det
- Deletes the most recent value stored under Key in Table, if any. Does
nothing otherwise; succeeds always.
- p_hashtbl_enumerate(+Table, -Key, -Value) is nondet
- On backtracking, enumerates every Key-Value pair stored in the Table.
Fails if the table is empty. If several values are stored for the same
key, their order of enumeration is most recent first, as in
p_hashtbl_get_all/3. The ordering is otherwise unspecified.
If Key is ground, this behaves like p_hashtbl_get_all/3 for that Key.
However, p_hashtbl_get_all/3 is more efficient in this case.
- p_hashtbl_to_list(+Table, -Pairs) is det
- Unifies Pairs with a list of all Key-Value pairs in the order as
enumerated by p_hashtbl_enumerate/3.
- list_to_p_hashtbl(+Pairs, -Table) is semidet
- If Pairs is a list of Key-Value pairs, unifies Table with a corresponding
hash table of a default size containing all those pairs. If there are
several entries for the same Key in the list, later entries will shadow
earlier ones in the Table.
- list_to_p_hashtbl(+Pairs, -Table, +Size) is semidet
- As list_to_p_hashtbl/2, but the hash table has an initial number of
buckets given by Size.
- throws
- - Type or domain error if Size is not a non-negative integer.
- p_hashtbl_map(+Table, :Goal, -TableOut) is nondet
- For every Key and Value in Table, calls
Goal(Key, Value, Value1)
and
unifies TableOut with a hash table containing all Key-Value1 pairs.
Deterministic if Goal is deterministic. If Goal backtracks,
p_hashtbl_map/3 enumerates several TableOut tables accordingly.
- p_hashtbl_fold(+Table, :Goal, +Acc, -Result) is nondet
- Folds Goal over every Key and Value in the Table. Calls
Goal(Key,
Value, Acc, Result)
, using each call's Result as the next call's
accumulator Acc, and unifying Result with the final call's Result.
Deterministic if Goal is deterministic. If Goal backtracks, enumerates
several results accordingly.
- p_hashtbl_iter(+Table, :Goal) is nondet
- Calls
Goal(Key, Value)
for every Key and Value stored in Table. This
is useful to ensure that all entries satisfy some predicate, or for
Goal's side effects. Deterministic if Goal is deterministic.
- p_hashtbl_unfold(:Goal, -Table) is det
- Unfolds the binary goal Goal into the Table: Unifies Table with a hash
table containing a Key-Value entry for every solution of
Goal(Key,
Value)
. Table is empty if Goal has no solutions. Later solutions of
Goal will shadow earlier ones in Table.