[ library(hash) | Reference Manual | Alphabetic Index ]
hash_update(+Table, ++Key, -OldValue, +NewValue)
Lookup and replace the value stored under Key
- Table
- A hash table
- Key
- a ground term
- OldValue
- Any term
- NewValue
- Any term
Description
A combination of hash_get/3 and hash_set/3, but more efficient
because there is only one hash lookup.
Modes and Determinism
- hash_update(+, ++, -, +) is semidet
Fail Conditions
No entry for Key
Examples
?- hash_create(H),
hash_set(H, k, hello),
hash_update(H, k, Old, world),
hash_get(H, k, New).
Old = hello
New = world
Yes (0.00s cpu)
% Example: increment a numerical table entry
hash_inc(H, Key) :-
hash_update(H, Key, N0, N1),
N1 is N0+1.
% Example: add element to a list entry
hash_prepend(H, Key, Value) :-
hash_update(H, Key, Values, [Value|Values]).
See Also
hash_update / 5, hash_get / 3, hash_set / 3