1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2020, VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(hashtable,
[ ht_new/1, % --HT
ht_is_hashtable/1, % @HT
ht_size/2, % +HT, -Size
ht_put/3, % !HT, +Key, +Value
ht_update/4, % +HT, +Key, ?Old, +New
ht_put_new/3, % !HT, +Key, +Value
ht_put/5, % !HT, +Key, +Value, +IfNew, -Old
ht_del/3, % !HT, +Key, -Value
ht_get/3, % +HT, +Key, -Value
ht_gen/3, % +HT, ?Key, ?Value
ht_pairs/2, % ?HT, ?Pairs
ht_keys/2 % +HT, -Keys
]).
:- autoload(library(error), [must_be/2]).
/** <module> Hash tables
Hash tables are one of the many key-value representations available to
SWI-Prolog.
This module implements a hash table as a _mutable_ and _backtrackable_
data structure. The hash table is implemented as a _closed hash table_,
where the _buckets_ array is implemented using an unbounded arity
compound term. Elements in this array are manipulated using setarg/3.
Hash tables allow for any Prolog data types as keys or values, except
that the key cannot be a variable. Applications that require a plain
variable as key can do so by wrapping all keys in a compound, e.g.,
k(Var).
*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Data structure
ht(Load, Size, Table)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
%! ht_new(--HT)
%
% Create a new hash table.
ht_new(ht(0,0,[](_))).
%! ht_is_hashtable(@HT) is semidet.
%
% True when HT is a hash table.
ht_is_hashtable(HT) :-
nonvar(HT),
HT = ht(Load, Size, Buckets),
integer(Load),
integer(Size),
compound_name_arity(Buckets, [], Arity),
Arity =:= Size*2+1.
%! ht_size(+HT, -Count) is det.
%
% True when Size is the number of key-value pairs in HT.
ht_size(ht(Count, _Size, _Buckets), Count).
%! ht_put(!HT, +Key, +Value) is det.
%
% Add a Key-Value to HT. The binding is undone on backtracking.
ht_put(HT, Key, Value) :-
must_be(nonvar, Key),
ht_put(HT, Key, Value, _, _, _).
%! ht_put_new(!HT, +Key, +Value) is semidet.
%
% As ht_put/3, but fails if Key is already in HT instead of updating
% the associated value.
ht_put_new(HT, Key, Value) :-
must_be(nonvar, Key),
ht_put(HT, Key, Value, _, _, true).
%! ht_update(+HT, +Key, ?Old, +New) is semidet.
%
% True when HT holds Key-Old before and Key-New after this call. Note
% that it is possible to update to a variable and the instantiate
% this. For example, a word-count update could be implemented as:
%
% ```
% update_word_count(HT, Word) :-
% ( ht_update(HT, Word, Old, New)
% -> New is Old+1
% ; ht_put(HT, Word, 1)
% ).
% ```
ht_update(HT, Key, Old, New) :-
must_be(nonvar, Key),
ht_put(HT, Key, New, _, Old, false).
%! ht_put(!HT, +Key, +Value, +IfNew, -Old) is det.
%
% Add Key-Value to HT. Old is unified with the old value associated
% with Key or, if Key is new, with IfNew. This can be used to
% bootstrap managing a list of values, e.g.
%
% ht_put_list(HT, Key, Value) :-
% ht_put(HT, Key, [Value|Tail], [], Tail).
ht_put(HT, Key, Value, IfNew, Old) :-
must_be(nonvar, Key),
ht_put(HT, Key, Value, IfNew, Old, _).
ht_put(HT, Key, Value, IfNew, Old, IsNew) :-
HT = ht(Load, Size, Buckets),
( Load >= Size//2
-> ht_resize(HT),
ht_put(HT, Key, Value, IfNew, Old, IsNew)
; variant_hash(Key, I0),
I is I0 mod Size,
put_(Buckets, I, Size, Key, Old, IfNew, Value, IsNew),
( IsNew == true
-> Load2 is Load+1,
setarg(1, HT, Load2)
; true
)
).
put_(Buckets, I, Size, Key, Old, IfNew, Value, IsNew) :-
ht_kv(Buckets, I, K, V),
( var(K)
-> IsNew = true,
Old = IfNew,
K = Key,
V = Value
; K == Key
-> IsNew = false,
Old = V,
ht_put_v(Buckets, I, Value)
; I2 is (I+1) mod Size,
put_(Buckets, I2, Size, Key, Old, IfNew, Value, IsNew)
).
ht_resize(HT) :-
HT = ht(_Load, Size, Buckets),
NewSize is max(4, Size*2),
NewArity is NewSize*2+1,
compound_name_arity(NewBuckets, [], NewArity),
copy_members(0, Size, Buckets, NewSize, NewBuckets),
setarg(2, HT, NewSize),
setarg(3, HT, NewBuckets).
copy_members(I, OSize, OBuckets, NSize, NBuckets) :-
I < OSize,
!,
ht_kv(OBuckets, I, K, V),
( nonvar(K)
-> variant_hash(K, I0),
NI is I0 mod NSize,
copy_(NBuckets, NI, NSize, K, V)
; true
),
I2 is I+1,
copy_members(I2, OSize, OBuckets, NSize, NBuckets).
copy_members(_, _, _, _, _).
copy_(Buckets, I, Size, Key, Value) :-
ht_kv(Buckets, I, K, V),
( var(K)
-> K = Key,
V = Value
; I2 is (I+1) mod Size,
copy_(Buckets, I2, Size, Key, Value)
).
%! ht_del(!HT, +Key, -Value) is semidet.
%
% Delete Key-Value from HT. Fails if Key does not appear in HT or
% Value does not unify with the old associated value.
ht_del(HT, Key, Value) :-
must_be(nonvar, Key),
HT = ht(Load, Size, Buckets),
Load > 0,
variant_hash(Key, I0),
I is I0 mod Size,
del_(Buckets, I, Size, Key, Value),
Load2 is Load - 1,
setarg(1, HT, Load2).
del_(Buckets, I, Size, Key, Value) :-
ht_kv(Buckets, I, K, V),
( var(K)
-> fail
; K == Key
-> V = Value,
ht_put_kv(Buckets, I, _, _),
del_shift(Buckets, I, I, Size)
; I2 is (I+1) mod Size,
del_(Buckets, I2, Size, Key, Value)
).
del_shift(Buckets, I0, J, Size) :-
I is (I0+1) mod Size,
ht_kv(Buckets, I, K, V),
( var(K)
-> true
; variant_hash(K, Hash),
R is Hash mod Size,
( ( I >= R, R > J
; R > J, J > I
; J > I, I >= R
)
-> del_shift(Buckets, I, J, Size)
; ht_put_kv(Buckets, J, K, V),
ht_put_kv(Buckets, I, _, _),
del_shift(Buckets, I, I, Size)
)
).
%! ht_get(+HT, +Key, -Value) is semidet.
%
% True when Key is in HT and associated with Value.
ht_get(ht(Load, Size, Buckets), Key, Value) :-
Load > 0,
must_be(nonvar, Key),
variant_hash(Key, I0),
I is I0 mod Size,
get_(Buckets, I, Size, Key, Value).
get_(Buckets, I, Size, Key, Value) :-
ht_kv(Buckets, I, K, V),
( Key == K
-> Value = V
; nonvar(K)
-> I2 is (I+1) mod Size,
get_(Buckets, I2, Size, Key, Value)
).
ht_k(Buckets, I, K) :-
IK is I*2+1,
arg(IK, Buckets, K).
ht_kv(Buckets, I, K, V) :-
IK is I*2+1,
IV is IK+1,
arg(IK, Buckets, K),
arg(IV, Buckets, V).
ht_put_kv(Buckets, I, K, V) :-
IK is I*2+1,
IV is IK+1,
setarg(IK, Buckets, K),
setarg(IV, Buckets, V).
ht_put_v(Buckets, I, V) :-
IV is I*2+2,
setarg(IV, Buckets, V).
%! ht_gen(+HT, ?Key, ?Value) is nondet.
%
% True when Key-Value is in HT. Pairs are enumerated on backtracking
% using the hash table order.
ht_gen(HT, Key, Value) :-
HT = ht(_, Size, Buckets),
End is Size - 1,
between(0, End, I),
ht_kv(Buckets, I, K, V),
nonvar(K),
K = Key,
V = Value.
%! ht_pairs(?HT, ?Pairs) is det.
%
% True when Pairs and HT represent the same association. When used in
% mode (+,-), Pairs is an ordered set.
ht_pairs(HT, Pairs) :-
ht_is_hashtable(HT),
!,
HT = ht(_Load, Size, Buckets),
pairs_(0, Size, Buckets, Pairs0),
sort(Pairs0, Pairs).
ht_pairs(HT, Pairs) :-
must_be(list(pair), Pairs),
ht_new(HT),
ht_fill(Pairs, HT).
pairs_(I, Size, Buckets, Pairs) :-
( I < Size
-> ht_kv(Buckets, I, K, V),
( nonvar(K)
-> Pairs = [K-V|T],
I2 is I+1,
pairs_(I2, Size, Buckets, T)
; I2 is I+1,
pairs_(I2, Size, Buckets, Pairs)
)
; Pairs = []
).
ht_fill([], _).
ht_fill([K-V|T], HT) :-
ht_put(HT, K, V),
ht_fill(T, HT).
%! ht_keys(+HT, -Keys) is det.
%
% True when Keys is an ordered set of all keys in HT.
ht_keys(HT, Keys) :-
HT = ht(_Load, Size, Buckets),
keys_(0, Size, Buckets, Keys0),
sort(Keys0, Keys).
keys_(I, Size, Buckets, Keys) :-
( I < Size
-> ht_k(Buckets, I, K),
( nonvar(K)
-> Keys = [K|T],
I2 is I+1,
keys_(I2, Size, Buckets, T)
; I2 is I+1,
keys_(I2, Size, Buckets, Keys)
)
; Keys = []
).
|