| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* hash.d - hash tables |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* Copyright (c) 2017 Daniel Kochmanski |
| 11 |
* |
| 12 |
* See file 'LICENSE' for the copyright details. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <ecl/ecl.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
#include <ctype.h> |
| 20 |
#include <ecl/ecl-inl.h> |
| 21 |
#include <ecl/internal.h> |
| 22 |
#include "newhash.h" |
| 23 |
|
| 24 |
#define SYMBOL_NAME(x) (Null(x)? ECL_NIL_SYMBOL->symbol.name : (x)->symbol.name) |
| 25 |
|
| 26 |
static void ECL_INLINE |
| 27 |
assert_type_hash_table(cl_object function, cl_narg narg, cl_object p) |
| 28 |
{ |
| 29 |
unlikely_if (!ECL_HASH_TABLE_P(p)) |
| 30 |
FEwrong_type_nth_arg(function, narg, p, @[hash-table]); |
| 31 |
} |
| 32 |
|
| 33 |
static cl_hashkey |
| 34 |
_hash_eql(cl_hashkey h, cl_object x) |
| 35 |
{ |
| 36 |
switch (ecl_t_of(x)) { |
| 37 |
case t_bignum: |
| 38 |
return hash_string(h, (unsigned char*)ECL_BIGNUM_LIMBS(x), |
| 39 |
ECL_BIGNUM_USIZE(x) * sizeof(ecl_limb_t)); |
| 40 |
case t_ratio: |
| 41 |
h = _hash_eql(h, x->ratio.num); |
| 42 |
return _hash_eql(h, x->ratio.den); |
| 43 |
case t_singlefloat: |
| 44 |
return hash_string(h, (unsigned char*)&ecl_single_float(x), sizeof(ecl_single_float(x))); |
| 45 |
case t_doublefloat: |
| 46 |
return hash_string(h, (unsigned char*)&ecl_double_float(x), sizeof(ecl_double_float(x))); |
| 47 |
case t_longfloat: { |
| 48 |
/* We coerce to double because long double has extra bits that |
| 49 |
* give rise to different hash key and are not meaningful. */ |
| 50 |
struct { double mantissa; int exponent; int sign; } aux; |
| 51 |
aux.mantissa = frexpl(ecl_long_float(x), &aux.exponent); |
| 52 |
aux.sign = (ecl_long_float(x) < 0)? -1: 1; |
| 53 |
return hash_string(h, (unsigned char*)&aux, sizeof(aux)); |
| 54 |
} |
| 55 |
case t_complex: |
| 56 |
h = _hash_eql(h, x->gencomplex.real); |
| 57 |
return _hash_eql(h, x->gencomplex.imag); |
| 58 |
#ifdef ECL_COMPLEX_FLOAT |
| 59 |
case t_csfloat: return hash_string(h, (unsigned char*)&ecl_csfloat(x), sizeof(ecl_csfloat(x))); |
| 60 |
case t_cdfloat: return hash_string(h, (unsigned char*)&ecl_cdfloat(x), sizeof(ecl_cdfloat(x))); |
| 61 |
case t_clfloat: { |
| 62 |
/* We coerce to _Complex double because _Complex long double has |
| 63 |
* extra bits that give rise to different hash key and are not |
| 64 |
* meaningful. */ |
| 65 |
struct { |
| 66 |
double mantissa1, mantissa2; |
| 67 |
int exponent1, exponent2; |
| 68 |
int sign1, sign2; } aux; |
| 69 |
long double realpart = creall(ecl_clfloat(x)); |
| 70 |
long double imagpart = cimagl(ecl_clfloat(x)); |
| 71 |
aux.mantissa1 = frexpl(realpart, &aux.exponent1); |
| 72 |
aux.mantissa2 = frexpl(imagpart, &aux.exponent2); |
| 73 |
aux.sign1 = (realpart < 0)? -1: 1; |
| 74 |
aux.sign2 = (imagpart < 0)? -1: 1; |
| 75 |
return hash_string(h, (unsigned char*)&aux, sizeof(aux)); |
| 76 |
} |
| 77 |
#endif |
| 78 |
case t_character: |
| 79 |
return hash_word(h, ECL_CHAR_CODE(x)); |
| 80 |
#ifdef ECL_SSE2 |
| 81 |
case t_sse_pack: |
| 82 |
return hash_string(h, x->sse.data.b8, 16); |
| 83 |
#endif |
| 84 |
default: |
| 85 |
return hash_word(h, ((cl_hashkey)x >> 2)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
static cl_hashkey |
| 90 |
_hash_equal(int depth, cl_hashkey h, cl_object x) |
| 91 |
{ |
| 92 |
switch (ecl_t_of(x)) { |
| 93 |
case t_list: |
| 94 |
if (Null(x)) { |
| 95 |
return _hash_equal(depth, h, ECL_NIL_SYMBOL->symbol.name); |
| 96 |
} |
| 97 |
if (--depth == 0) { |
| 98 |
return h; |
| 99 |
} else { |
| 100 |
h = _hash_equal(depth, h, ECL_CONS_CAR(x)); |
| 101 |
return _hash_equal(depth, h, ECL_CONS_CDR(x)); |
| 102 |
} |
| 103 |
case t_symbol: |
| 104 |
x = x->symbol.name; |
| 105 |
#ifdef ECL_UNICODE |
| 106 |
case t_base_string: |
| 107 |
return hash_base_string((ecl_base_char *)x->base_string.self, |
| 108 |
x->base_string.fillp, h); |
| 109 |
case t_string: |
| 110 |
return hash_full_string(x->string.self, x->string.fillp, h); |
| 111 |
#else |
| 112 |
case t_base_string: |
| 113 |
return hash_string(h, (ecl_base_char *)x->base_string.self, |
| 114 |
x->base_string.fillp); |
| 115 |
#endif |
| 116 |
case t_pathname: |
| 117 |
h = _hash_equal(0, h, x->pathname.directory); |
| 118 |
h = _hash_equal(0, h, x->pathname.name); |
| 119 |
h = _hash_equal(0, h, x->pathname.type); |
| 120 |
h = _hash_equal(0, h, x->pathname.host); |
| 121 |
h = _hash_equal(0, h, x->pathname.device); |
| 122 |
return _hash_equal(0, h, x->pathname.version); |
| 123 |
case t_bitvector: |
| 124 |
/* Notice that we may round out some bits. We must do this |
| 125 |
* because the fill pointer may be set in the middle of a byte. |
| 126 |
* If so, the extra bits _must_ _not_ take part in the hash, |
| 127 |
* because otherwise two bit arrays which are EQUAL might |
| 128 |
* have different hash keys. */ |
| 129 |
return hash_string(h, x->vector.self.bc, x->vector.fillp / 8); |
| 130 |
case t_random: { |
| 131 |
cl_object array = x->random.value; |
| 132 |
return hash_string |
| 133 |
(h, (unsigned char*)array->vector.self.b8, 4*624); |
| 134 |
} |
| 135 |
#ifdef ECL_SIGNED_ZERO |
| 136 |
/* According to 3.2.4.2.2 Definition of Similarity two numbers are |
| 137 |
"similar" if they are of the same type and represent the same |
| 138 |
mathematical value. -- jd 2019-05-06*/ |
| 139 |
case t_singlefloat: { |
| 140 |
float f = ecl_single_float(x); |
| 141 |
if (f == 0.0) f = 0.0; |
| 142 |
return hash_string(h, (unsigned char*)&f, sizeof(f)); |
| 143 |
} |
| 144 |
case t_doublefloat: { |
| 145 |
double f = ecl_double_float(x); |
| 146 |
if (f == 0.0) f = 0.0; |
| 147 |
return hash_string(h, (unsigned char*)&f, sizeof(f)); |
| 148 |
} |
| 149 |
case t_longfloat: { |
| 150 |
/* We coerce to double because long double has extra bits |
| 151 |
* that give rise to different hash key and are not |
| 152 |
* meaningful */ |
| 153 |
struct { double mantissa; int exponent; int sign; } aux; |
| 154 |
aux.mantissa = frexpl(ecl_long_float(x), &aux.exponent); |
| 155 |
aux.sign = (ecl_long_float(x) < 0)? -1: 1; |
| 156 |
if (aux.mantissa == 0.0) aux.mantissa = 0.0; |
| 157 |
return hash_string(h, (unsigned char*)&aux, sizeof(aux)); |
| 158 |
} |
| 159 |
case t_complex: { |
| 160 |
h = _hash_equal(depth, h, x->gencomplex.real); |
| 161 |
return _hash_equal(depth, h, x->gencomplex.imag); |
| 162 |
} |
| 163 |
# ifdef ECL_COMPLEX_FLOAT |
| 164 |
case t_csfloat: { |
| 165 |
_Complex float f = ecl_csfloat(x); |
| 166 |
if (crealf(f) == 0.0) f = 0.0 + I * cimagf(f); |
| 167 |
if (cimagf(f) == 0.0) f = crealf(f) + I * 0.0; |
| 168 |
return hash_string(h, (unsigned char*)&(f), sizeof(f)); |
| 169 |
} |
| 170 |
case t_cdfloat: { |
| 171 |
_Complex double f = ecl_cdfloat(x); |
| 172 |
if (creal(f) == 0.0) f = 0.0 + I * cimag(f); |
| 173 |
if (cimag(f) == 0.0) f = creal(f) + I * 0.0; |
| 174 |
return hash_string(h, (unsigned char*)&(f), sizeof(f)); |
| 175 |
} |
| 176 |
case t_clfloat: { |
| 177 |
/* We coerce to _Complex double because _Complex long double has |
| 178 |
* extra bits that give rise to different hash key and are not |
| 179 |
* meaningful. */ |
| 180 |
struct { |
| 181 |
double mantissa1, mantissa2; |
| 182 |
int exponent1, exponent2; |
| 183 |
int sign1, sign2; } aux; |
| 184 |
long double realpart = creall(ecl_clfloat(x)); |
| 185 |
long double imagpart = cimagl(ecl_clfloat(x)); |
| 186 |
aux.mantissa1 = frexpl(realpart, &aux.exponent1); |
| 187 |
aux.mantissa2 = frexpl(imagpart, &aux.exponent2); |
| 188 |
aux.sign1 = (realpart < 0)? -1: 1; |
| 189 |
aux.sign2 = (imagpart < 0)? -1: 1; |
| 190 |
if (aux.mantissa1 == 0.0) aux.mantissa1 = 0.0; |
| 191 |
if (aux.mantissa2 == 0.0) aux.mantissa2 = 0.0; |
| 192 |
return hash_string(h, (unsigned char*)&aux, sizeof(aux)); |
| 193 |
} |
| 194 |
# endif |
| 195 |
#endif |
| 196 |
default: |
| 197 |
return _hash_eql(h, x); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
static cl_hashkey |
| 202 |
_hash_equalp(int depth, cl_hashkey h, cl_object x) |
| 203 |
{ |
| 204 |
cl_index i, len; |
| 205 |
switch (ecl_t_of(x)) { |
| 206 |
case t_character: |
| 207 |
return hash_word(h, ecl_char_upcase(ECL_CHAR_CODE(x))); |
| 208 |
case t_list: |
| 209 |
if (Null(x)) { |
| 210 |
return _hash_equalp(depth, h, ECL_NIL_SYMBOL->symbol.name); |
| 211 |
} |
| 212 |
if (--depth == 0) { |
| 213 |
return h; |
| 214 |
} else { |
| 215 |
h = _hash_equalp(depth, h, ECL_CONS_CAR(x)); |
| 216 |
return _hash_equalp(depth, h, ECL_CONS_CDR(x)); |
| 217 |
} |
| 218 |
#ifdef ECL_UNICODE |
| 219 |
case t_string: |
| 220 |
#endif |
| 221 |
case t_base_string: |
| 222 |
case t_vector: |
| 223 |
case t_bitvector: |
| 224 |
len = x->vector.fillp; |
| 225 |
goto SCAN; |
| 226 |
case t_array: |
| 227 |
len = x->vector.dim; |
| 228 |
SCAN: if (--depth) { |
| 229 |
for (i = 0; i < len; i++) { |
| 230 |
h = _hash_equalp(depth, h, ecl_aref_unsafe(x, i)); |
| 231 |
} |
| 232 |
} |
| 233 |
return h; |
| 234 |
case t_fixnum: |
| 235 |
return hash_word(h, ecl_fixnum(x)); |
| 236 |
case t_singlefloat: |
| 237 |
/* FIXME! We should be more precise here! */ |
| 238 |
return hash_word(h, (cl_index)ecl_single_float(x)); |
| 239 |
case t_doublefloat: |
| 240 |
/* FIXME! We should be more precise here! */ |
| 241 |
return hash_word(h, (cl_index)ecl_double_float(x)); |
| 242 |
case t_bignum: |
| 243 |
/* FIXME! We should be more precise here! */ |
| 244 |
return hash_string(h, (unsigned char*)ecl_bignum(x)->_mp_d, |
| 245 |
abs(ecl_bignum(x)->_mp_size) * |
| 246 |
sizeof(ecl_limb_t)); |
| 247 |
case t_ratio: |
| 248 |
h = _hash_equalp(0, h, x->ratio.num); |
| 249 |
return _hash_equalp(0, h, x->ratio.den); |
| 250 |
case t_complex: |
| 251 |
h = _hash_equalp(0, h, x->gencomplex.real); |
| 252 |
return _hash_equalp(0, h, x->gencomplex.imag); |
| 253 |
#ifdef ECL_COMPLEX_FLOAT |
| 254 |
/* FIXME! We should be more precise here! */ |
| 255 |
case t_csfloat: return hash_word(h, (cl_index)ecl_csfloat(x)); |
| 256 |
case t_cdfloat: return hash_word(h, (cl_index)ecl_cdfloat(x)); |
| 257 |
case t_clfloat: return hash_word(h, (cl_index)ecl_clfloat(x)); |
| 258 |
#endif |
| 259 |
case t_instance: |
| 260 |
case t_hashtable: |
| 261 |
/* FIXME! We should be more precise here! */ |
| 262 |
return hash_word(h, 42); |
| 263 |
default: |
| 264 |
return _hash_equal(depth, h, x); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
static cl_hashkey _hash_generic(cl_object ht, cl_object key) { |
| 269 |
cl_object hash_fun = ht->hash.generic_hash; |
| 270 |
cl_object h_object = _ecl_funcall2(hash_fun, key); |
| 271 |
if (!ECL_FIXNUMP(h_object) || ecl_fixnum_minusp(h_object)) { |
| 272 |
FEwrong_type_argument(@'fixnum', h_object); |
| 273 |
} |
| 274 |
return ecl_fixnum(h_object); |
| 275 |
} |
| 276 |
|
| 277 |
#define HASH_TABLE_LOOP(hkey,hval,h,HASH_TABLE_LOOP_TEST) { \ |
| 278 |
cl_index i, hsize = hashtable->hash.size; \ |
| 279 |
/* INV there is at least one empty bucket (loop terminates). */ \ |
| 280 |
for (i = h % hsize; ; i = (i + 1) % hsize) { \ |
| 281 |
struct ecl_hashtable_entry *e = hashtable->hash.data + i; \ |
| 282 |
cl_object hkey = e->key, hval = e->value; \ |
| 283 |
(void)hval; /* silence unused-variable compiler warning. */ \ |
| 284 |
if (hkey == OBJNULL || (HASH_TABLE_LOOP_TEST)) return e; \ |
| 285 |
} \ |
| 286 |
} |
| 287 |
|
| 288 |
#define HASH_TABLE_SET(h,loop,compute_key,store_key,store_val) { \ |
| 289 |
cl_hashkey h = compute_key; \ |
| 290 |
struct ecl_hashtable_entry *e; \ |
| 291 |
AGAIN: \ |
| 292 |
e = loop(h, key, hashtable); \ |
| 293 |
if (e->key == OBJNULL) { \ |
| 294 |
cl_index i = hashtable->hash.entries + 1; \ |
| 295 |
if (i >= hashtable->hash.limit) { \ |
| 296 |
hashtable = ecl_extend_hashtable(hashtable); \ |
| 297 |
goto AGAIN; \ |
| 298 |
} \ |
| 299 |
hashtable->hash.entries = i; \ |
| 300 |
e->key = store_key; \ |
| 301 |
} \ |
| 302 |
e->value = store_val; \ |
| 303 |
return hashtable; \ |
| 304 |
} |
| 305 |
|
| 306 |
/* HASH_TABLE_REMOVE tries to fills up holes generated by deleting |
| 307 |
* entries from a hashtable as follows. Iterate through all entries f |
| 308 |
* to the right of the deleted entry e (the hole). If the distance |
| 309 |
* between f's current and its optimal location is greater than the |
| 310 |
* distance between e and f, then we can put f into the hole. Repeat |
| 311 |
* with the new hole at the location of f until the holes are all |
| 312 |
* filled. */ |
| 313 |
|
| 314 |
#define HASH_TABLE_REMOVE(hkey,hval,h,HASH_TABLE_LOOP_TEST,compute_key) { \ |
| 315 |
cl_index i, hsize = hashtable->hash.size; \ |
| 316 |
/* INV there is at least one empty bucket (loop terminates). */ \ |
| 317 |
for (i = h % hsize; ; i = (i + 1) % hsize) { \ |
| 318 |
struct ecl_hashtable_entry *e = hashtable->hash.data + i; \ |
| 319 |
cl_object hkey = e->key, hval = e->value; \ |
| 320 |
(void)hval; /* silence unused-variable compiler warning */ \ |
| 321 |
if (hkey == OBJNULL) return 0; \ |
| 322 |
if (HASH_TABLE_LOOP_TEST) { \ |
| 323 |
cl_index j = (i+1) % hsize, k; \ |
| 324 |
for (k = 1; k <= hsize; j = (j+1) % hsize, k++) { \ |
| 325 |
struct ecl_hashtable_entry *f = hashtable->hash.data + j; \ |
| 326 |
hkey = f->key; \ |
| 327 |
hval = f->value; \ |
| 328 |
if (hkey == OBJNULL) { \ |
| 329 |
e->key = OBJNULL; \ |
| 330 |
e->value = OBJNULL; \ |
| 331 |
break; \ |
| 332 |
} \ |
| 333 |
cl_hashkey hf = compute_key; \ |
| 334 |
cl_index m = hf % hsize; \ |
| 335 |
/* d: distance of f from the optimal position */ \ |
| 336 |
cl_index d = (j >= m) ? (j - m) : (j + hsize - m); \ |
| 337 |
if (k <= d) { \ |
| 338 |
e->key = hkey; \ |
| 339 |
e->value = hval; \ |
| 340 |
e = f; \ |
| 341 |
i = j; \ |
| 342 |
k = 0; \ |
| 343 |
} \ |
| 344 |
} \ |
| 345 |
hashtable->hash.entries--; \ |
| 346 |
return 1; \ |
| 347 |
} \ |
| 348 |
} \ |
| 349 |
} |
| 350 |
|
| 351 |
/* |
| 352 |
* EQ HASHTABLES |
| 353 |
*/ |
| 354 |
|
| 355 |
#define _hash_eq(k) ((cl_hashkey)(k) >> 2) |
| 356 |
|
| 357 |
static struct ecl_hashtable_entry * |
| 358 |
_ecl_hash_loop_eq(cl_hashkey h, cl_object key, cl_object hashtable) |
| 359 |
{ |
| 360 |
HASH_TABLE_LOOP(hkey, hval, h, key == hkey); |
| 361 |
} |
| 362 |
|
| 363 |
static cl_object |
| 364 |
_ecl_gethash_eq(cl_object key, cl_object hashtable, cl_object def) |
| 365 |
{ |
| 366 |
cl_hashkey h = _hash_eq(key); |
| 367 |
struct ecl_hashtable_entry *e = _ecl_hash_loop_eq(h, key, hashtable); |
| 368 |
return (e->key == OBJNULL)? def : e->value; |
| 369 |
} |
| 370 |
|
| 371 |
static cl_object |
| 372 |
_ecl_sethash_eq(cl_object key, cl_object hashtable, cl_object value) |
| 373 |
{ |
| 374 |
HASH_TABLE_SET(h, _ecl_hash_loop_eq, _hash_eq(key), key, value); |
| 375 |
} |
| 376 |
|
| 377 |
static bool |
| 378 |
_ecl_remhash_eq(cl_object key, cl_object hashtable) |
| 379 |
{ |
| 380 |
HASH_TABLE_REMOVE(hkey, hval, _hash_eq(key), key == hkey, _hash_eq(hkey)); |
| 381 |
} |
| 382 |
|
| 383 |
/* |
| 384 |
* EQL HASHTABLES |
| 385 |
*/ |
| 386 |
|
| 387 |
static struct ecl_hashtable_entry * |
| 388 |
_ecl_hash_loop_eql(cl_hashkey h, cl_object key, cl_object hashtable) |
| 389 |
{ |
| 390 |
HASH_TABLE_LOOP(hkey, hval, h, ecl_eql(key, hkey)); |
| 391 |
} |
| 392 |
|
| 393 |
static cl_object |
| 394 |
_ecl_gethash_eql(cl_object key, cl_object hashtable, cl_object def) |
| 395 |
{ |
| 396 |
cl_hashkey h = _hash_eql(0, key); |
| 397 |
struct ecl_hashtable_entry *e = _ecl_hash_loop_eql(h, key, hashtable); |
| 398 |
return (e->key == OBJNULL)? def : e->value; |
| 399 |
} |
| 400 |
|
| 401 |
static cl_object |
| 402 |
_ecl_sethash_eql(cl_object key, cl_object hashtable, cl_object value) |
| 403 |
{ |
| 404 |
HASH_TABLE_SET(h, _ecl_hash_loop_eql, _hash_eql(0, key), key, value); |
| 405 |
} |
| 406 |
|
| 407 |
static bool |
| 408 |
_ecl_remhash_eql(cl_object key, cl_object hashtable) |
| 409 |
{ |
| 410 |
HASH_TABLE_REMOVE(hkey, hval, _hash_eql(0, key), ecl_eql(key, hkey), |
| 411 |
_hash_eql(0, hkey)); |
| 412 |
} |
| 413 |
|
| 414 |
/* |
| 415 |
* EQUAL HASHTABLES |
| 416 |
*/ |
| 417 |
|
| 418 |
static struct ecl_hashtable_entry * |
| 419 |
_ecl_hash_loop_equal(cl_hashkey h, cl_object key, cl_object hashtable) |
| 420 |
{ |
| 421 |
HASH_TABLE_LOOP(hkey, hval, h, ecl_equal(key, hkey)); |
| 422 |
} |
| 423 |
|
| 424 |
static cl_object |
| 425 |
_ecl_gethash_equal(cl_object key, cl_object hashtable, cl_object def) |
| 426 |
{ |
| 427 |
cl_hashkey h = _hash_equal(3, 0, key); |
| 428 |
struct ecl_hashtable_entry *e = _ecl_hash_loop_equal(h, key, hashtable); |
| 429 |
return (e->key == OBJNULL)? def : e->value; |
| 430 |
} |
| 431 |
|
| 432 |
static cl_object |
| 433 |
_ecl_sethash_equal(cl_object key, cl_object hashtable, cl_object value) |
| 434 |
{ |
| 435 |
HASH_TABLE_SET(h, _ecl_hash_loop_equal, _hash_equal(3, 0, key), key, value); |
| 436 |
} |
| 437 |
|
| 438 |
static bool |
| 439 |
_ecl_remhash_equal(cl_object key, cl_object hashtable) |
| 440 |
{ |
| 441 |
HASH_TABLE_REMOVE(hkey, hval, _hash_equal(3, 0, key), |
| 442 |
ecl_equal(key, hkey), _hash_equal(3, 0, hkey)); |
| 443 |
} |
| 444 |
|
| 445 |
/* |
| 446 |
* EQUALP HASHTABLES |
| 447 |
*/ |
| 448 |
|
| 449 |
static struct ecl_hashtable_entry * |
| 450 |
_ecl_hash_loop_equalp(cl_hashkey h, cl_object key, cl_object hashtable) |
| 451 |
{ |
| 452 |
HASH_TABLE_LOOP(hkey, hval, h, ecl_equalp(key, hkey)); |
| 453 |
} |
| 454 |
|
| 455 |
static cl_object |
| 456 |
_ecl_gethash_equalp(cl_object key, cl_object hashtable, cl_object def) |
| 457 |
{ |
| 458 |
cl_hashkey h = _hash_equalp(3, 0, key); |
| 459 |
struct ecl_hashtable_entry *e = _ecl_hash_loop_equalp(h, key, hashtable); |
| 460 |
return (e->key == OBJNULL)? def : e->value; |
| 461 |
} |
| 462 |
|
| 463 |
static cl_object |
| 464 |
_ecl_sethash_equalp(cl_object key, cl_object hashtable, cl_object value) |
| 465 |
{ |
| 466 |
HASH_TABLE_SET(h, _ecl_hash_loop_equalp, _hash_equalp(3, 0, key),key, value); |
| 467 |
} |
| 468 |
|
| 469 |
static bool |
| 470 |
_ecl_remhash_equalp(cl_object key, cl_object hashtable) |
| 471 |
{ |
| 472 |
HASH_TABLE_REMOVE(hkey, hval, _hash_equalp(3, 0, key), |
| 473 |
ecl_equalp(key, hkey), _hash_equalp(3, 0, hkey)); |
| 474 |
} |
| 475 |
|
| 476 |
/* |
| 477 |
* PACKAGE HASHTABLES |
| 478 |
*/ |
| 479 |
|
| 480 |
static struct ecl_hashtable_entry * |
| 481 |
_ecl_hash_loop_pack(cl_hashkey h, cl_object key, cl_object hashtable) |
| 482 |
{ |
| 483 |
cl_object ho = ecl_make_fixnum(h & 0xFFFFFFF); |
| 484 |
HASH_TABLE_LOOP(hkey, hval, h, |
| 485 |
(ho==hkey) && ecl_string_eq(key,SYMBOL_NAME(hval))); |
| 486 |
} |
| 487 |
|
| 488 |
static cl_object |
| 489 |
_ecl_gethash_pack(cl_object key, cl_object hashtable, cl_object def) |
| 490 |
{ |
| 491 |
cl_hashkey h = _hash_equal(3, 0, key); |
| 492 |
struct ecl_hashtable_entry *e = _ecl_hash_loop_pack(h, key, hashtable); |
| 493 |
return (e->key == OBJNULL)? def : e->value; |
| 494 |
} |
| 495 |
|
| 496 |
static cl_object |
| 497 |
_ecl_sethash_pack(cl_object key, cl_object hashtable, cl_object value) |
| 498 |
{ |
| 499 |
HASH_TABLE_SET(h, _ecl_hash_loop_pack, _hash_equal(3, 0, key), |
| 500 |
ecl_make_fixnum(h & 0xFFFFFFF), value); |
| 501 |
} |
| 502 |
|
| 503 |
static bool |
| 504 |
_ecl_remhash_pack(cl_object key, cl_object hashtable) |
| 505 |
{ |
| 506 |
cl_hashkey h = _hash_equal(3, 0, key); |
| 507 |
cl_object ho = ecl_make_fixnum(h & 0xFFFFFFF); |
| 508 |
HASH_TABLE_REMOVE(hkey, hval, h, |
| 509 |
(ho==hkey) && ecl_string_eq(key,SYMBOL_NAME(hval)), |
| 510 |
_hash_equal(3, 0, SYMBOL_NAME(hval))); |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
* Generic HASHTABLES |
| 515 |
*/ |
| 516 |
|
| 517 |
static bool |
| 518 |
_ecl_generic_hash_test(cl_object fun, cl_object key, cl_object hkey) { |
| 519 |
return (_ecl_funcall3(fun, key, hkey) != ECL_NIL); |
| 520 |
} |
| 521 |
|
| 522 |
static struct ecl_hashtable_entry * |
| 523 |
_ecl_hash_loop_generic(cl_hashkey h, cl_object key, cl_object hashtable) |
| 524 |
{ |
| 525 |
cl_object test_fun = hashtable->hash.generic_test; |
| 526 |
HASH_TABLE_LOOP(hkey, hval, h, _ecl_generic_hash_test(test_fun, key, hkey)); |
| 527 |
} |
| 528 |
|
| 529 |
static cl_object |
| 530 |
_ecl_gethash_generic(cl_object key, cl_object hashtable, cl_object def) |
| 531 |
{ |
| 532 |
cl_hashkey h = _hash_generic(hashtable, key); |
| 533 |
struct ecl_hashtable_entry *e = _ecl_hash_loop_generic(h, key, hashtable); |
| 534 |
return (e->key == OBJNULL)? def : e->value; |
| 535 |
} |
| 536 |
|
| 537 |
static cl_object |
| 538 |
_ecl_sethash_generic(cl_object key, cl_object hashtable, cl_object value) |
| 539 |
{ |
| 540 |
HASH_TABLE_SET(h, _ecl_hash_loop_generic, _hash_generic(hashtable, key), |
| 541 |
key, value); |
| 542 |
} |
| 543 |
|
| 544 |
static bool |
| 545 |
_ecl_remhash_generic(cl_object key, cl_object hashtable) |
| 546 |
{ |
| 547 |
cl_object test_fun = hashtable->hash.generic_test; |
| 548 |
HASH_TABLE_REMOVE(hkey, hval, _hash_generic(hashtable, key), |
| 549 |
_ecl_generic_hash_test(test_fun, key, hkey), |
| 550 |
_hash_generic(hashtable, hkey)); |
| 551 |
} |
| 552 |
|
| 553 |
/* |
| 554 |
* WEAK HASH TABLES |
| 555 |
* |
| 556 |
* Entries in a weak hash table may disappear without explicit REMHASH. Our |
| 557 |
* implementation handles collisions with open addressing, that is we put the |
| 558 |
* colliding element at the first free spot starting from its hash. |
| 559 |
* |
| 560 |
* Until recently we've implemented REMHASH by inserting "tombstones", that is |
| 561 |
* by simply invalidating the entry. Currently after removing the hash we fill |
| 562 |
* the hole by shifting elements after it to left and that yields some benefits |
| 563 |
* for scenarios where we frequently add and remove elements. |
| 564 |
* |
| 565 |
* Since weak entries may disappear at any time, we need to either fill holes in |
| 566 |
* GETHASH/SETHASH too, or we need to revert back to inserting "tombstones". |
| 567 |
* Notice that some functions are common to hash tables with and without weak |
| 568 |
* entries - for example MAPHASH. These functions assume that entry indexes do |
| 569 |
* not change while iterating, so we can't shift values in copy_entry unless we |
| 570 |
* customize these functions too. |
| 571 |
* |
| 572 |
* For reasons above weak entries are not normalized to OBJNULL but rather we |
| 573 |
* leave the weak entry in the same place as a tombstone. SETHASH reuses these |
| 574 |
* entries while REMHASH behaves the same for all hash tables. |
| 575 |
* |
| 576 |
* [key=OBJNULL, value=OBJNULL] - free bucket |
| 577 |
* [key=ECL_NIL, value=OBJNULL] - tombstone |
| 578 |
* [key=OBJNULL, value=ECL_NIL] - tombstone copy |
| 579 |
* |
| 580 |
*/ |
| 581 |
|
| 582 |
#ifndef ECL_WEAK_HASH |
| 583 |
#define copy_entry(e,h) *(e) |
| 584 |
#endif |
| 585 |
|
| 586 |
#ifdef ECL_WEAK_HASH |
| 587 |
static cl_hashkey |
| 588 |
_ecl_hash_key(cl_object h, cl_object o) { |
| 589 |
switch (h->hash.test) { |
| 590 |
case ecl_htt_eq: return _hash_eq(o); |
| 591 |
case ecl_htt_eql: return _hash_eql(0, o); |
| 592 |
case ecl_htt_equal: return _hash_equal(3, 0, o); |
| 593 |
case ecl_htt_equalp: return _hash_equalp(3, 0, o); |
| 594 |
case ecl_htt_pack: return _hash_equal(3, 0, o); |
| 595 |
case ecl_htt_generic: return _hash_generic(h, o); |
| 596 |
default: |
| 597 |
ecl_internal_error("Unknown hash test."); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
static bool |
| 602 |
_ecl_hash_test(cl_object hashtable, cl_object key, cl_object hkey) { |
| 603 |
switch (hashtable->hash.test) { |
| 604 |
case ecl_htt_eq: return (key == hkey); |
| 605 |
case ecl_htt_eql: return ecl_eql(key, hkey); |
| 606 |
case ecl_htt_equal: return ecl_equal(key, hkey); |
| 607 |
case ecl_htt_equalp: return ecl_equalp(key, hkey); |
| 608 |
case ecl_htt_generic: |
| 609 |
return _ecl_generic_hash_test(hashtable->hash.generic_test, key, hkey); |
| 610 |
default: |
| 611 |
ecl_internal_error("Unknown hash test."); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
static void * |
| 616 |
normalize_weak_key_entry(struct ecl_hashtable_entry *e) { |
| 617 |
return (void*)(e->key = e->key->weak.value); |
| 618 |
} |
| 619 |
|
| 620 |
static void * |
| 621 |
normalize_weak_value_entry(struct ecl_hashtable_entry *e) { |
| 622 |
return (void*)(e->value = e->value->weak.value); |
| 623 |
} |
| 624 |
|
| 625 |
static void * |
| 626 |
normalize_weak_key_and_value_entry(struct ecl_hashtable_entry *e) { |
| 627 |
if ((e->key = e->key->weak.value) && (e->value = e->value->weak.value)) |
| 628 |
return (void*)e; |
| 629 |
else |
| 630 |
return 0; |
| 631 |
} |
| 632 |
|
| 633 |
static void * |
| 634 |
normalize_weak_key_or_value_entry(struct ecl_hashtable_entry *e) { |
| 635 |
e->key = e->key->weak.value; |
| 636 |
e->value = e->value->weak.value; |
| 637 |
if (e->key || e->value) |
| 638 |
return (void*)e; |
| 639 |
else |
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
static cl_object |
| 644 |
_ecl_store_key (cl_object hashtable, cl_object key) { |
| 645 |
switch (hashtable->hash.weak) { |
| 646 |
case ecl_htt_weak_key: |
| 647 |
case ecl_htt_weak_key_and_value: |
| 648 |
case ecl_htt_weak_key_or_value: |
| 649 |
return si_make_weak_pointer(key); |
| 650 |
default: |
| 651 |
return key; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
static cl_object |
| 657 |
_ecl_store_val (cl_object hashtable, cl_object val) { |
| 658 |
switch (hashtable->hash.weak) { |
| 659 |
case ecl_htt_weak_value: |
| 660 |
case ecl_htt_weak_key_and_value: |
| 661 |
case ecl_htt_weak_key_or_value: |
| 662 |
return si_make_weak_pointer(val); |
| 663 |
default: |
| 664 |
return val; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
/* This function normalizes entries. That means that si_weak_pointer_value |
| 669 |
shouldn't be called on resulting entry key and value. -- jd 2019-05-28 */ |
| 670 |
static struct ecl_hashtable_entry |
| 671 |
copy_entry(struct ecl_hashtable_entry *e, cl_object h) |
| 672 |
{ |
| 673 |
if (e->key == OBJNULL) { |
| 674 |
return *e; |
| 675 |
} else if (e->value == OBJNULL) { |
| 676 |
struct ecl_hashtable_entry output = *e; |
| 677 |
output.key = OBJNULL; |
| 678 |
output.value = ECL_NIL; |
| 679 |
return output; |
| 680 |
} else { |
| 681 |
struct ecl_hashtable_entry output = *e; |
| 682 |
switch (h->hash.weak) { |
| 683 |
case ecl_htt_weak_key: |
| 684 |
if (GC_call_with_alloc_lock |
| 685 |
((GC_fn_type)normalize_weak_key_entry, &output)) { |
| 686 |
return output; |
| 687 |
} |
| 688 |
break; |
| 689 |
case ecl_htt_weak_value: |
| 690 |
if (GC_call_with_alloc_lock |
| 691 |
((GC_fn_type)normalize_weak_value_entry, &output)) { |
| 692 |
return output; |
| 693 |
} |
| 694 |
break; |
| 695 |
case ecl_htt_weak_key_and_value: |
| 696 |
if (GC_call_with_alloc_lock |
| 697 |
((GC_fn_type)normalize_weak_key_and_value_entry, &output)) { |
| 698 |
return output; |
| 699 |
} |
| 700 |
break; |
| 701 |
case ecl_htt_weak_key_or_value: |
| 702 |
if (GC_call_with_alloc_lock |
| 703 |
((GC_fn_type)normalize_weak_key_or_value_entry, &output)) { |
| 704 |
return output; |
| 705 |
} |
| 706 |
break; |
| 707 |
case ecl_htt_not_weak: |
| 708 |
default: |
| 709 |
return output; |
| 710 |
} |
| 711 |
output.key = OBJNULL; |
| 712 |
output.value = ECL_NIL; |
| 713 |
e->key = ECL_NIL; |
| 714 |
e->value = OBJNULL; |
| 715 |
return output; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
static cl_object |
| 720 |
_ecl_gethash_weak(cl_object key, cl_object hashtable, cl_object def) |
| 721 |
{ |
| 722 |
cl_index i, hsize = hashtable->hash.size; |
| 723 |
cl_hashkey h = _ecl_hash_key(hashtable, key); |
| 724 |
struct ecl_hashtable_entry *p, e; |
| 725 |
for (i = h % hsize; ; i = (i + 1) % hsize) { |
| 726 |
p = hashtable->hash.data + i; |
| 727 |
e = copy_entry(p, hashtable); |
| 728 |
if (p->key == OBJNULL) return def; |
| 729 |
if (p->value == OBJNULL) continue; /* skip the tombstone */ |
| 730 |
if (_ecl_hash_test(hashtable, key, e.key)) return e.value; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
static cl_object |
| 735 |
_ecl_sethash_weak(cl_object key, cl_object hashtable, cl_object value) |
| 736 |
{ |
| 737 |
cl_index i, hsize = hashtable->hash.size; |
| 738 |
cl_hashkey h = _ecl_hash_key(hashtable, key); |
| 739 |
struct ecl_hashtable_entry e, *p, *f = NULL; |
| 740 |
AGAIN: |
| 741 |
for (i = h % hsize; ; i = (i + 1) % hsize) { |
| 742 |
p = hashtable->hash.data + i; |
| 743 |
e = copy_entry(p, hashtable); |
| 744 |
if (p->key == OBJNULL) { break; } |
| 745 |
if (p->value == OBJNULL) { f = p; continue; } |
| 746 |
if (_ecl_hash_test(hashtable, key, e.key)) { |
| 747 |
f = p; |
| 748 |
break; |
| 749 |
} |
| 750 |
} |
| 751 |
if (p->key == OBJNULL && f == NULL) { |
| 752 |
cl_index i = hashtable->hash.entries + 1; |
| 753 |
if (i >= hashtable->hash.limit) { |
| 754 |
hashtable = ecl_extend_hashtable(hashtable); |
| 755 |
goto AGAIN; |
| 756 |
} |
| 757 |
hashtable->hash.entries = i; |
| 758 |
f = p; |
| 759 |
} |
| 760 |
f->key = _ecl_store_key(hashtable, key); |
| 761 |
f->value = _ecl_store_val(hashtable, value); |
| 762 |
return hashtable; |
| 763 |
} |
| 764 |
|
| 765 |
static bool |
| 766 |
_ecl_remhash_weak(cl_object key, cl_object hashtable) |
| 767 |
{ |
| 768 |
cl_index i, hsize = hashtable->hash.size; |
| 769 |
cl_hashkey h = _ecl_hash_key(hashtable, key); |
| 770 |
struct ecl_hashtable_entry *p, e; |
| 771 |
for (i = h % hsize; ; i = (i + 1) % hsize) { |
| 772 |
p = hashtable->hash.data + i; |
| 773 |
e = copy_entry(p, hashtable); |
| 774 |
if (p->key == OBJNULL) return 0; |
| 775 |
/* We could try to shift consecutive tombstones here(!) */ |
| 776 |
if (e.key == OBJNULL) continue; |
| 777 |
if (_ecl_hash_test(hashtable, key, e.key)) { |
| 778 |
cl_index j = (i+1) % hsize, k; |
| 779 |
struct ecl_hashtable_entry *q, f; |
| 780 |
for (k = 1; k <= hsize; j = (j+1) % hsize, k++) { |
| 781 |
q = hashtable->hash.data + j; |
| 782 |
f = copy_entry(q, hashtable); |
| 783 |
if (f.key == OBJNULL) { |
| 784 |
p->key = OBJNULL; |
| 785 |
p->value = OBJNULL; |
| 786 |
break; |
| 787 |
} |
| 788 |
cl_hashkey hf = _ecl_hash_key(hashtable, f.key); |
| 789 |
cl_index m = hf % hsize; |
| 790 |
cl_index d = (j >= m) ? (j - m) : (j + hsize - m); |
| 791 |
if (k <= d) { |
| 792 |
p->key = _ecl_store_key(hashtable, f.key); |
| 793 |
p->value = _ecl_store_val(hashtable, f.value); |
| 794 |
p = q; |
| 795 |
i = j; |
| 796 |
k = 0; |
| 797 |
} |
| 798 |
} |
| 799 |
hashtable->hash.entries--; |
| 800 |
return 1; |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
#endif |
| 805 |
|
| 806 |
/* SYNCHRONIZED HASH TABLES */ |
| 807 |
#ifdef ECL_THREADS |
| 808 |
static cl_object |
| 809 |
_ecl_sethash_sync(cl_object key, cl_object hashtable, cl_object value) |
| 810 |
{ |
| 811 |
cl_object output = ECL_NIL; |
| 812 |
cl_object sync_lock = hashtable->hash.sync_lock; |
| 813 |
mp_get_rwlock_write_wait(sync_lock); |
| 814 |
ECL_UNWIND_PROTECT_BEGIN(ecl_process_env()) { |
| 815 |
output = hashtable->hash.set_unsafe(key, hashtable, value); |
| 816 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 817 |
mp_giveup_rwlock_write(sync_lock); |
| 818 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 819 |
return output; |
| 820 |
} |
| 821 |
|
| 822 |
static cl_object |
| 823 |
_ecl_gethash_sync(cl_object key, cl_object hashtable, cl_object def) |
| 824 |
{ |
| 825 |
cl_object output = ECL_NIL; |
| 826 |
cl_object sync_lock = hashtable->hash.sync_lock; |
| 827 |
mp_get_rwlock_read_wait(sync_lock); |
| 828 |
ECL_UNWIND_PROTECT_BEGIN(ecl_process_env()) { |
| 829 |
output = hashtable->hash.get_unsafe(key, hashtable, def); |
| 830 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 831 |
mp_giveup_rwlock_read(sync_lock); |
| 832 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 833 |
return output; |
| 834 |
} |
| 835 |
|
| 836 |
static bool |
| 837 |
_ecl_remhash_sync(cl_object key, cl_object hashtable) |
| 838 |
{ |
| 839 |
bool output = 0; |
| 840 |
cl_object sync_lock = hashtable->hash.sync_lock; |
| 841 |
mp_get_rwlock_write_wait(sync_lock); |
| 842 |
ECL_UNWIND_PROTECT_BEGIN(ecl_process_env()) { |
| 843 |
output = hashtable->hash.rem_unsafe(key, hashtable); |
| 844 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 845 |
mp_giveup_rwlock_write(sync_lock); |
| 846 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 847 |
return output; |
| 848 |
} |
| 849 |
#endif |
| 850 |
|
| 851 |
/* |
| 852 |
* HIGHER LEVEL INTERFACE |
| 853 |
*/ |
| 854 |
|
| 855 |
cl_object |
| 856 |
ecl_gethash(cl_object key, cl_object hashtable) |
| 857 |
{ |
| 858 |
assert_type_hash_table(@[gethash], 2, hashtable); |
| 859 |
return hashtable->hash.get(key, hashtable, OBJNULL); |
| 860 |
} |
| 861 |
|
| 862 |
cl_object |
| 863 |
ecl_gethash_safe(cl_object key, cl_object hashtable, cl_object def) |
| 864 |
{ |
| 865 |
assert_type_hash_table(@[gethash], 2, hashtable); |
| 866 |
return hashtable->hash.get(key, hashtable, def); |
| 867 |
} |
| 868 |
|
| 869 |
cl_object |
| 870 |
_ecl_sethash(cl_object key, cl_object hashtable, cl_object value) |
| 871 |
{ |
| 872 |
return hashtable->hash.set(key, hashtable, value); |
| 873 |
} |
| 874 |
|
| 875 |
cl_object |
| 876 |
ecl_sethash(cl_object key, cl_object hashtable, cl_object value) |
| 877 |
{ |
| 878 |
assert_type_hash_table(@[si::hash-set], 2, hashtable); |
| 879 |
hashtable = hashtable->hash.set(key, hashtable, value); |
| 880 |
return hashtable; |
| 881 |
} |
| 882 |
|
| 883 |
cl_object |
| 884 |
ecl_extend_hashtable(cl_object hashtable) |
| 885 |
{ |
| 886 |
cl_object old, new; |
| 887 |
cl_index old_size, new_size, i; |
| 888 |
cl_object new_size_obj; |
| 889 |
|
| 890 |
assert_type_hash_table(@[si::hash-set], 2, hashtable); |
| 891 |
old_size = hashtable->hash.size; |
| 892 |
/* We do the computation with lisp datatypes, just in case the sizes contain |
| 893 |
* weird numbers */ |
| 894 |
if (ECL_FIXNUMP(hashtable->hash.rehash_size)) { |
| 895 |
new_size_obj = ecl_plus(hashtable->hash.rehash_size, |
| 896 |
ecl_make_fixnum(old_size)); |
| 897 |
} else { |
| 898 |
new_size_obj = ecl_times(hashtable->hash.rehash_size, |
| 899 |
ecl_make_fixnum(old_size)); |
| 900 |
new_size_obj = ecl_ceiling1(new_size_obj); |
| 901 |
} |
| 902 |
if (!ECL_FIXNUMP(new_size_obj)) { |
| 903 |
/* New size is too large */ |
| 904 |
new_size = old_size * 2; |
| 905 |
} else { |
| 906 |
new_size = ecl_fixnum(new_size_obj); |
| 907 |
} |
| 908 |
if (hashtable->hash.test == ecl_htt_pack) { |
| 909 |
new = ecl_alloc_object(t_hashtable); |
| 910 |
new->hash = hashtable->hash; |
| 911 |
old = hashtable; |
| 912 |
} else { |
| 913 |
old = ecl_alloc_object(t_hashtable); |
| 914 |
old->hash = hashtable->hash; |
| 915 |
new = hashtable; |
| 916 |
} |
| 917 |
new->hash.data = NULL; /* for GC sake */ |
| 918 |
new->hash.entries = 0; |
| 919 |
new->hash.size = new_size; |
| 920 |
new->hash.limit = new->hash.size * new->hash.factor; |
| 921 |
if (new->hash.limit >= new_size) { |
| 922 |
new->hash.limit = new_size - 1; |
| 923 |
} |
| 924 |
new->hash.data = (struct ecl_hashtable_entry *) |
| 925 |
ecl_alloc(new_size * sizeof(struct ecl_hashtable_entry)); |
| 926 |
for (i = 0; i < new_size; i++) { |
| 927 |
new->hash.data[i].key = OBJNULL; |
| 928 |
new->hash.data[i].value = OBJNULL; |
| 929 |
} |
| 930 |
for (i = 0; i < old_size; i++) { |
| 931 |
struct ecl_hashtable_entry e = copy_entry(old->hash.data + i, old); |
| 932 |
if (e.key != OBJNULL) { |
| 933 |
new = (new->hash.test == ecl_htt_pack) |
| 934 |
? new->hash.set_unsafe(SYMBOL_NAME(e.value), new, e.value) |
| 935 |
: new->hash.set_unsafe(e.key, new, e.value); |
| 936 |
} |
| 937 |
} |
| 938 |
return new; |
| 939 |
} |
| 940 |
|
| 941 |
@(defun make_hash_table (&key (test @'eql') |
| 942 |
(hash_function ECL_NIL) |
| 943 |
(weakness ECL_NIL) |
| 944 |
(synchronized ECL_NIL) |
| 945 |
(size ecl_make_fixnum(1024)) |
| 946 |
(rehash_size cl_core.rehash_size) |
| 947 |
(rehash_threshold cl_core.rehash_threshold)) |
| 948 |
@ { |
| 949 |
cl_object hash = cl__make_hash_table(test, size, rehash_size, rehash_threshold); |
| 950 |
if (hash->hash.test == ecl_htt_generic) { |
| 951 |
/* Normally we would make hash_function an argument to cl__make_hash_table |
| 952 |
and put this test in there and void unnecessary object allocation, but |
| 953 |
we do not want to compromise the API. -- jd 2019-05-23 */ |
| 954 |
if (hash_function == ECL_NIL) { |
| 955 |
FEerror("~S is an illegal hash-table test function.", 1, test); |
| 956 |
} |
| 957 |
hash_function = si_coerce_to_function(hash_function); |
| 958 |
hash->hash.generic_hash = hash_function; |
| 959 |
} |
| 960 |
/* FIXME we should have separate getters and setters for each predicate. */ |
| 961 |
#ifdef ECL_WEAK_HASH |
| 962 |
if (!Null(weakness)) { |
| 963 |
if (weakness == @':key') { |
| 964 |
hash->hash.weak = ecl_htt_weak_key; |
| 965 |
} else if (weakness == @':value') { |
| 966 |
hash->hash.weak = ecl_htt_weak_value; |
| 967 |
} else if (weakness == @':key-and-value') { |
| 968 |
hash->hash.weak = ecl_htt_weak_key_and_value; |
| 969 |
} else if (weakness == @':key-or-value') { |
| 970 |
hash->hash.weak = ecl_htt_weak_key_or_value; |
| 971 |
} else { |
| 972 |
FEwrong_type_key_arg(@[make-hash-table], |
| 973 |
@[:weakness], |
| 974 |
cl_list(5, @'member', |
| 975 |
ECL_NIL, @':key', @':value', |
| 976 |
@':key-and-value', @':key-or-value'), |
| 977 |
weakness); |
| 978 |
} |
| 979 |
hash->hash.get = _ecl_gethash_weak; |
| 980 |
hash->hash.set = _ecl_sethash_weak; |
| 981 |
hash->hash.rem = _ecl_remhash_weak; |
| 982 |
} |
| 983 |
#endif |
| 984 |
/* Always bind unsafe variants. */ |
| 985 |
hash->hash.get_unsafe = hash->hash.get; |
| 986 |
hash->hash.set_unsafe = hash->hash.set; |
| 987 |
hash->hash.rem_unsafe = hash->hash.rem; |
| 988 |
if (!Null(synchronized)) { |
| 989 |
#ifdef ECL_THREADS |
| 990 |
hash->hash.sync_lock = ecl_make_rwlock(ECL_NIL); |
| 991 |
hash->hash.get = _ecl_gethash_sync; |
| 992 |
hash->hash.set = _ecl_sethash_sync; |
| 993 |
hash->hash.rem = _ecl_remhash_sync; |
| 994 |
#else |
| 995 |
/* for hash-table-synchronized-p predicate */ |
| 996 |
hash->hash.sync_lock = ECL_T; |
| 997 |
#endif |
| 998 |
} |
| 999 |
|
| 1000 |
@(return hash); |
| 1001 |
} @) |
| 1002 |
|
| 1003 |
static void |
| 1004 |
do_clrhash(cl_object ht) |
| 1005 |
{ |
| 1006 |
/* |
| 1007 |
* Fill a hash with null pointers and ensure it does not have |
| 1008 |
* any entry. We separate this routine because it is needed |
| 1009 |
* both by clrhash and hash table initialization. |
| 1010 |
*/ |
| 1011 |
cl_index i; |
| 1012 |
ht->hash.entries = 0; |
| 1013 |
for(i = 0; i < ht->hash.size; i++) { |
| 1014 |
ht->hash.data[i].key = OBJNULL; |
| 1015 |
ht->hash.data[i].value = OBJNULL; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
ecl_def_ct_single_float(min_threshold, 0.1, static, const); |
| 1020 |
|
| 1021 |
cl_object |
| 1022 |
cl__make_hash_table(cl_object test, cl_object size, cl_object rehash_size, |
| 1023 |
cl_object rehash_threshold) |
| 1024 |
{ |
| 1025 |
int htt; |
| 1026 |
cl_index hsize; |
| 1027 |
cl_object h; |
| 1028 |
cl_object hash_test = ECL_NIL, hash_func = ECL_NIL; |
| 1029 |
cl_object (*get)(cl_object, cl_object, cl_object); |
| 1030 |
cl_object (*set)(cl_object, cl_object, cl_object); |
| 1031 |
bool (*rem)(cl_object, cl_object); |
| 1032 |
/* |
| 1033 |
* Argument checking |
| 1034 |
*/ |
| 1035 |
if (test == @'eq' || test == ECL_SYM_FUN(@'eq')) { |
| 1036 |
htt = ecl_htt_eq; |
| 1037 |
get = _ecl_gethash_eq; |
| 1038 |
set = _ecl_sethash_eq; |
| 1039 |
rem = _ecl_remhash_eq; |
| 1040 |
} else if (test == @'eql' || test == ECL_SYM_FUN(@'eql')) { |
| 1041 |
htt = ecl_htt_eql; |
| 1042 |
get = _ecl_gethash_eql; |
| 1043 |
set = _ecl_sethash_eql; |
| 1044 |
rem = _ecl_remhash_eql; |
| 1045 |
} else if (test == @'equal' || test == ECL_SYM_FUN(@'equal')) { |
| 1046 |
htt = ecl_htt_equal; |
| 1047 |
get = _ecl_gethash_equal; |
| 1048 |
set = _ecl_sethash_equal; |
| 1049 |
rem = _ecl_remhash_equal; |
| 1050 |
} else if (test == @'equalp' || test == ECL_SYM_FUN(@'equalp')) { |
| 1051 |
htt = ecl_htt_equalp; |
| 1052 |
get = _ecl_gethash_equalp; |
| 1053 |
set = _ecl_sethash_equalp; |
| 1054 |
rem = _ecl_remhash_equalp; |
| 1055 |
} else if (test == @'package') { |
| 1056 |
htt = ecl_htt_pack; |
| 1057 |
get = _ecl_gethash_pack; |
| 1058 |
set = _ecl_sethash_pack; |
| 1059 |
rem = _ecl_remhash_pack; |
| 1060 |
} else { |
| 1061 |
htt = ecl_htt_generic; |
| 1062 |
get = _ecl_gethash_generic; |
| 1063 |
set = _ecl_sethash_generic; |
| 1064 |
rem = _ecl_remhash_generic; |
| 1065 |
hash_test = si_coerce_to_function(test); |
| 1066 |
} |
| 1067 |
if (ecl_unlikely(!ECL_FIXNUMP(size) || |
| 1068 |
ecl_fixnum_minusp(size) || |
| 1069 |
ecl_fixnum_geq(size,ecl_make_fixnum(ECL_ARRAY_TOTAL_LIMIT)))) { |
| 1070 |
FEwrong_type_key_arg(@[make-hash-table], @[:size], size, |
| 1071 |
ecl_make_integer_type(ecl_make_fixnum(0), |
| 1072 |
ecl_make_fixnum(ECL_ARRAY_TOTAL_LIMIT))); |
| 1073 |
} |
| 1074 |
hsize = ecl_fixnum(size); |
| 1075 |
if (hsize < 16) { |
| 1076 |
hsize = 16; |
| 1077 |
} |
| 1078 |
AGAIN: |
| 1079 |
if (ecl_minusp(rehash_size)) { |
| 1080 |
ERROR1: |
| 1081 |
rehash_size = |
| 1082 |
ecl_type_error(@'make-hash-table',"rehash-size", |
| 1083 |
rehash_size, |
| 1084 |
ecl_read_from_cstring("(OR (INTEGER 1 *) (FLOAT 0 (1)))")); |
| 1085 |
goto AGAIN; |
| 1086 |
} |
| 1087 |
if (floatp(rehash_size)) { |
| 1088 |
if (ecl_number_compare(rehash_size, ecl_make_fixnum(1)) < 0 || |
| 1089 |
ecl_minusp(rehash_size)) { |
| 1090 |
goto ERROR1; |
| 1091 |
} |
| 1092 |
rehash_size = ecl_make_double_float(ecl_to_double(rehash_size)); |
| 1093 |
} else if (!ECL_FIXNUMP(rehash_size)) { |
| 1094 |
goto ERROR1; |
| 1095 |
} |
| 1096 |
while (!ecl_numberp(rehash_threshold) || |
| 1097 |
ecl_minusp(rehash_threshold) || |
| 1098 |
ecl_number_compare(rehash_threshold, ecl_make_fixnum(1)) > 0) |
| 1099 |
{ |
| 1100 |
rehash_threshold = |
| 1101 |
ecl_type_error(@'make-hash-table',"rehash-threshold", |
| 1102 |
rehash_threshold, |
| 1103 |
ecl_read_from_cstring("(REAL 0 1)")); |
| 1104 |
} |
| 1105 |
/* |
| 1106 |
* Build actual hash. |
| 1107 |
*/ |
| 1108 |
h = ecl_alloc_object(t_hashtable); |
| 1109 |
h->hash.test = htt; |
| 1110 |
h->hash.weak = ecl_htt_not_weak; |
| 1111 |
h->hash.generic_test = hash_test; |
| 1112 |
h->hash.generic_hash = hash_func; |
| 1113 |
h->hash.get = h->hash.get_unsafe = get; |
| 1114 |
h->hash.set = h->hash.set_unsafe = set; |
| 1115 |
h->hash.rem = h->hash.rem_unsafe = rem; |
| 1116 |
h->hash.size = hsize; |
| 1117 |
h->hash.entries = 0; |
| 1118 |
h->hash.rehash_size = rehash_size; |
| 1119 |
h->hash.threshold = rehash_threshold; |
| 1120 |
rehash_threshold = cl_max(2, min_threshold, rehash_threshold); |
| 1121 |
h->hash.factor = ecl_to_double(rehash_threshold); |
| 1122 |
h->hash.limit = h->hash.size * h->hash.factor; |
| 1123 |
if (h->hash.limit >= hsize) { |
| 1124 |
h->hash.limit = hsize - 1; |
| 1125 |
} |
| 1126 |
h->hash.data = NULL; /* for GC sake */ |
| 1127 |
h->hash.data = (struct ecl_hashtable_entry *) |
| 1128 |
ecl_alloc(hsize * sizeof(struct ecl_hashtable_entry)); |
| 1129 |
do_clrhash(h); |
| 1130 |
return h; |
| 1131 |
} |
| 1132 |
|
| 1133 |
#ifdef ECL_EXTERNALIZABLE |
| 1134 |
void |
| 1135 |
ecl_reconstruct_serialized_hashtable(cl_object h) { |
| 1136 |
switch (h->hash.test) { |
| 1137 |
case ecl_htt_eq: |
| 1138 |
h->hash.get = _ecl_gethash_eq; |
| 1139 |
h->hash.set = _ecl_sethash_eq; |
| 1140 |
h->hash.rem = _ecl_remhash_eq; |
| 1141 |
break; |
| 1142 |
case ecl_htt_eql: |
| 1143 |
h->hash.get = _ecl_gethash_eql; |
| 1144 |
h->hash.set = _ecl_sethash_eql; |
| 1145 |
h->hash.rem = _ecl_remhash_eql; |
| 1146 |
break; |
| 1147 |
case ecl_htt_equal: |
| 1148 |
h->hash.get = _ecl_gethash_equal; |
| 1149 |
h->hash.set = _ecl_sethash_equal; |
| 1150 |
h->hash.rem = _ecl_remhash_equal; |
| 1151 |
break; |
| 1152 |
case ecl_htt_equalp: |
| 1153 |
h->hash.get = _ecl_gethash_equalp; |
| 1154 |
h->hash.set = _ecl_sethash_equalp; |
| 1155 |
h->hash.rem = _ecl_remhash_equalp; |
| 1156 |
break; |
| 1157 |
case ecl_htt_pack: |
| 1158 |
h->hash.get = _ecl_gethash_pack; |
| 1159 |
h->hash.set = _ecl_sethash_pack; |
| 1160 |
h->hash.rem = _ecl_remhash_pack; |
| 1161 |
break; |
| 1162 |
case ecl_htt_generic: |
| 1163 |
h->hash.get = _ecl_gethash_generic; |
| 1164 |
h->hash.set = _ecl_sethash_generic; |
| 1165 |
h->hash.rem = _ecl_remhash_generic; |
| 1166 |
break; |
| 1167 |
} |
| 1168 |
/* FIXME we should have separate getters and setters for each predicate. */ |
| 1169 |
if (h->hash.weak != ecl_htt_not_weak) { |
| 1170 |
h->hash.get = _ecl_gethash_weak; |
| 1171 |
h->hash.set = _ecl_sethash_weak; |
| 1172 |
h->hash.rem = _ecl_remhash_weak; |
| 1173 |
} |
| 1174 |
/* Always bind unsafe variants. */ |
| 1175 |
h->hash.get_unsafe = h->hash.get; |
| 1176 |
h->hash.set_unsafe = h->hash.set; |
| 1177 |
h->hash.rem_unsafe = h->hash.rem; |
| 1178 |
if (h->hash.sync_lock != OBJNULL |
| 1179 |
&& (ecl_t_of(h->hash.sync_lock) == t_lock |
| 1180 |
|| ecl_t_of(h->hash.sync_lock) == t_rwlock)) { |
| 1181 |
h->hash.get = _ecl_gethash_sync; |
| 1182 |
h->hash.set = _ecl_sethash_sync; |
| 1183 |
h->hash.rem = _ecl_remhash_sync; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
#endif |
| 1187 |
|
| 1188 |
cl_object |
| 1189 |
cl_hash_table_p(cl_object ht) |
| 1190 |
{ |
| 1191 |
@(return (ECL_HASH_TABLE_P(ht) ? ECL_T : ECL_NIL)); |
| 1192 |
} |
| 1193 |
|
| 1194 |
cl_object |
| 1195 |
si_hash_table_weakness(cl_object ht) |
| 1196 |
{ |
| 1197 |
cl_object output = ECL_NIL; |
| 1198 |
#ifdef ECL_WEAK_HASH |
| 1199 |
switch (ht->hash.weak) { |
| 1200 |
case ecl_htt_weak_key: output = @':key'; break; |
| 1201 |
case ecl_htt_weak_value: output = @':value'; break; |
| 1202 |
case ecl_htt_weak_key_and_value: output = @':key-and-value'; break; |
| 1203 |
case ecl_htt_weak_key_or_value: output = @':key-or-value'; break; |
| 1204 |
case ecl_htt_not_weak: default: output = ECL_NIL; break; |
| 1205 |
} |
| 1206 |
#endif |
| 1207 |
@(return output); |
| 1208 |
} |
| 1209 |
|
| 1210 |
cl_object |
| 1211 |
si_hash_table_synchronized_p(cl_object ht) |
| 1212 |
{ |
| 1213 |
|
| 1214 |
if (!Null(ht->hash.sync_lock)) { |
| 1215 |
return ECL_T; |
| 1216 |
} |
| 1217 |
return ECL_NIL; |
| 1218 |
} |
| 1219 |
|
| 1220 |
@(defun gethash (key ht &optional (no_value ECL_NIL)) |
| 1221 |
@ |
| 1222 |
{ |
| 1223 |
assert_type_hash_table(@[gethash], 2, ht); |
| 1224 |
{ |
| 1225 |
cl_object v = ht->hash.get(key, ht, OBJNULL); |
| 1226 |
if (v != OBJNULL) { |
| 1227 |
@(return v ECL_T); |
| 1228 |
} else { |
| 1229 |
@(return no_value ECL_NIL); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
@) |
| 1234 |
|
| 1235 |
cl_object |
| 1236 |
si_hash_set(cl_object key, cl_object ht, cl_object val) |
| 1237 |
{ |
| 1238 |
/* INV: ecl_sethash() checks the type of hashtable */ |
| 1239 |
ecl_sethash(key, ht, val); |
| 1240 |
@(return val); |
| 1241 |
} |
| 1242 |
|
| 1243 |
bool |
| 1244 |
ecl_remhash(cl_object key, cl_object hashtable) |
| 1245 |
{ |
| 1246 |
assert_type_hash_table(@[remhash], 2, hashtable); |
| 1247 |
return hashtable->hash.rem(key, hashtable); |
| 1248 |
} |
| 1249 |
|
| 1250 |
cl_object |
| 1251 |
cl_remhash(cl_object key, cl_object ht) |
| 1252 |
{ |
| 1253 |
/* INV: _ecl_remhash() checks the type of hashtable */ |
| 1254 |
@(return (ecl_remhash(key, ht)? ECL_T : ECL_NIL)); |
| 1255 |
} |
| 1256 |
|
| 1257 |
cl_object |
| 1258 |
cl_clrhash(cl_object ht) |
| 1259 |
{ |
| 1260 |
assert_type_hash_table(@[clrhash], 1, ht); |
| 1261 |
if (ht->hash.entries) { |
| 1262 |
do_clrhash(ht); |
| 1263 |
} |
| 1264 |
@(return ht); |
| 1265 |
} |
| 1266 |
|
| 1267 |
cl_object |
| 1268 |
cl_hash_table_test(cl_object ht) |
| 1269 |
{ |
| 1270 |
cl_object output; |
| 1271 |
assert_type_hash_table(@[hash-table-test], 1, ht); |
| 1272 |
switch(ht->hash.test) { |
| 1273 |
case ecl_htt_eq: output = @'eq'; break; |
| 1274 |
case ecl_htt_eql: output = @'eql'; break; |
| 1275 |
case ecl_htt_equal: output = @'equal'; break; |
| 1276 |
case ecl_htt_equalp: output = @'equalp'; break; |
| 1277 |
case ecl_htt_pack: output = @'equal'; break; |
| 1278 |
case ecl_htt_generic: output = ht->hash.generic_test; break; |
| 1279 |
default: FEerror("hash-table-test: unknown test.", 0); |
| 1280 |
} |
| 1281 |
@(return output); |
| 1282 |
} |
| 1283 |
|
| 1284 |
cl_object |
| 1285 |
cl_hash_table_size(cl_object ht) |
| 1286 |
{ |
| 1287 |
assert_type_hash_table(@[hash-table-size], 1, ht); |
| 1288 |
@(return ecl_make_fixnum(ht->hash.size)); |
| 1289 |
} |
| 1290 |
|
| 1291 |
cl_index |
| 1292 |
ecl_hash_table_count(cl_object ht) |
| 1293 |
{ |
| 1294 |
if (ht->hash.weak == ecl_htt_not_weak) { |
| 1295 |
return ht->hash.entries; |
| 1296 |
} else if (ht->hash.size) { |
| 1297 |
cl_index i, j; |
| 1298 |
for (i = j = 0; i < ht->hash.size; i++) { |
| 1299 |
struct ecl_hashtable_entry output = copy_entry(ht->hash.data + i, ht); |
| 1300 |
if (output.key != OBJNULL) { |
| 1301 |
if (++j == ht->hash.size) |
| 1302 |
break; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
return ht->hash.entries = j; |
| 1306 |
} else { |
| 1307 |
return 0; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
cl_object |
| 1313 |
cl_hash_table_count(cl_object ht) |
| 1314 |
{ |
| 1315 |
assert_type_hash_table(@[hash-table-count], 1, ht); |
| 1316 |
@(return (ecl_make_fixnum(ecl_hash_table_count(ht)))); |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
/* HASH TABLE ITERATION |
| 1321 |
* |
| 1322 |
* We iterate from right to left across each group of consecutive |
| 1323 |
* non-empty buckets. This allows removing the current iteration |
| 1324 |
* element without iterating over elements twice or missing elements |
| 1325 |
* because we only change elements to the right of the current |
| 1326 |
* element when removing an element. For example, a hashtable of |
| 1327 |
* size 10 with 5 filled buckets is iterated over as follows: |
| 1328 |
* |
| 1329 |
* a..bc...de |
| 1330 |
* ^ |
| 1331 |
* a..bc...de |
| 1332 |
* ^ |
| 1333 |
* a..bc...de |
| 1334 |
* ^ |
| 1335 |
* a..bc...de |
| 1336 |
* ^ |
| 1337 |
* a..bc...de |
| 1338 |
* ^ |
| 1339 |
* |
| 1340 |
* If for example the element `e` is removed and the element `a` moves |
| 1341 |
* up at the place that `e` previously occupied, we don't iterate |
| 1342 |
* twice over `a`. |
| 1343 |
*/ |
| 1344 |
|
| 1345 |
static cl_object |
| 1346 |
si_hash_table_iterate(cl_narg narg, ...) |
| 1347 |
{ |
| 1348 |
const cl_env_ptr the_env = ecl_process_env(); |
| 1349 |
cl_object env = the_env->function->cclosure.env; |
| 1350 |
cl_object index = CAR(env); |
| 1351 |
cl_object endpoint = CADR(env); |
| 1352 |
cl_object ht = CADDR(env); |
| 1353 |
cl_fixnum i, j; |
| 1354 |
if (!Null(index)) { |
| 1355 |
i = ecl_fixnum(endpoint); |
| 1356 |
j = ecl_fixnum(index); |
| 1357 |
do { |
| 1358 |
j = (j == 0) ? ht->hash.size-1 : j - 1; |
| 1359 |
struct ecl_hashtable_entry e = copy_entry(ht->hash.data + j, ht); |
| 1360 |
if (e.key != OBJNULL) { |
| 1361 |
cl_object ndx = ecl_make_fixnum(j); |
| 1362 |
ECL_RPLACA(env, ndx); |
| 1363 |
@(return ndx e.key e.value); |
| 1364 |
} |
| 1365 |
} while (j != i); |
| 1366 |
ECL_RPLACA(env, ECL_NIL); |
| 1367 |
} |
| 1368 |
@(return ECL_NIL); |
| 1369 |
} |
| 1370 |
|
| 1371 |
cl_object |
| 1372 |
si_hash_table_iterator(cl_object ht) |
| 1373 |
{ |
| 1374 |
cl_fixnum i; |
| 1375 |
assert_type_hash_table(@[si::hash-table-iterator], 1, ht); |
| 1376 |
/* Make sure we don't start in the middle of a group of consecutive |
| 1377 |
* filled buckets. */ |
| 1378 |
for (i = ht->hash.size-1; ht->hash.data[i].key != OBJNULL; i--); |
| 1379 |
@(return ecl_make_cclosure_va(si_hash_table_iterate, |
| 1380 |
cl_list(3, ecl_make_fixnum(i), |
| 1381 |
ecl_make_fixnum(i), |
| 1382 |
ht), |
| 1383 |
@'si::hash-table-iterator', |
| 1384 |
0)); |
| 1385 |
} |
| 1386 |
|
| 1387 |
cl_object |
| 1388 |
cl_hash_table_rehash_size(cl_object ht) |
| 1389 |
{ |
| 1390 |
assert_type_hash_table(@[hash-table-rehash-size], 1, ht); |
| 1391 |
@(return ht->hash.rehash_size); |
| 1392 |
} |
| 1393 |
|
| 1394 |
cl_object |
| 1395 |
cl_hash_table_rehash_threshold(cl_object ht) |
| 1396 |
{ |
| 1397 |
assert_type_hash_table(@[hash-table-rehash-threshold], 1, ht); |
| 1398 |
@(return ht->hash.threshold); |
| 1399 |
} |
| 1400 |
|
| 1401 |
cl_object |
| 1402 |
cl_sxhash(cl_object key) |
| 1403 |
{ |
| 1404 |
cl_index output = _hash_equal(3, 0, key); |
| 1405 |
const cl_index mask = ((cl_index)1 << (ECL_FIXNUM_BITS - 3)) - 1; |
| 1406 |
@(return ecl_make_fixnum(output & mask)); |
| 1407 |
} |
| 1408 |
|
| 1409 |
@(defun si::hash-eql (&rest args) |
| 1410 |
cl_index h; |
| 1411 |
@ |
| 1412 |
for (h = 0; narg; narg--) { |
| 1413 |
cl_object o = ecl_va_arg(args); |
| 1414 |
h = _hash_eql(h, o); |
| 1415 |
} |
| 1416 |
@(return ecl_make_fixnum(h)); |
| 1417 |
@) |
| 1418 |
|
| 1419 |
@(defun si::hash-equal (&rest args) |
| 1420 |
cl_index h; |
| 1421 |
@ |
| 1422 |
for (h = 0; narg; narg--) { |
| 1423 |
cl_object o = ecl_va_arg(args); |
| 1424 |
h = _hash_equal(3, h, o); |
| 1425 |
} |
| 1426 |
@(return ecl_make_fixnum(h)); |
| 1427 |
@) |
| 1428 |
|
| 1429 |
@(defun si::hash-equalp (&rest args) |
| 1430 |
cl_index h; |
| 1431 |
@ |
| 1432 |
for (h = 0; narg; narg--) { |
| 1433 |
cl_object o = ecl_va_arg(args); |
| 1434 |
h = _hash_equalp(3, h, o); |
| 1435 |
} |
| 1436 |
@(return ecl_make_fixnum(h)); |
| 1437 |
@) |
| 1438 |
|
| 1439 |
cl_object |
| 1440 |
cl_maphash(cl_object fun, cl_object ht) |
| 1441 |
{ |
| 1442 |
cl_index i, j, hsize; |
| 1443 |
|
| 1444 |
assert_type_hash_table(@[maphash], 2, ht); |
| 1445 |
if (ht->hash.entries == 0) { |
| 1446 |
@(return ECL_NIL); |
| 1447 |
} |
| 1448 |
hsize = ht->hash.size; |
| 1449 |
/* Make sure we don't start in the middle of a group of consecutive |
| 1450 |
* filled buckets. */ |
| 1451 |
for (i = hsize-1; ht->hash.data[i].key != OBJNULL; i--); |
| 1452 |
j = i; |
| 1453 |
do { |
| 1454 |
j = (j == 0) ? hsize-1 : j - 1; |
| 1455 |
struct ecl_hashtable_entry e = copy_entry(ht->hash.data + j, ht); |
| 1456 |
if (e.key != OBJNULL) { |
| 1457 |
cl_object key = e.key; |
| 1458 |
cl_object val = e.value; |
| 1459 |
funcall(3, fun, key, val); |
| 1460 |
} |
| 1461 |
} while (j != i); |
| 1462 |
@(return ECL_NIL); |
| 1463 |
} |
| 1464 |
|
| 1465 |
cl_object |
| 1466 |
si_hash_table_content(cl_object ht) |
| 1467 |
{ |
| 1468 |
cl_index i; |
| 1469 |
cl_object output = ECL_NIL; |
| 1470 |
assert_type_hash_table(@[ext::hash-table-content], 2, ht); |
| 1471 |
for (i = 0; i < ht->hash.size; i++) { |
| 1472 |
struct ecl_hashtable_entry e = copy_entry(ht->hash.data + i, ht); |
| 1473 |
if (e.key != OBJNULL) |
| 1474 |
output = ecl_cons(ecl_cons(e.key, e.value), output); |
| 1475 |
} |
| 1476 |
@(return output); |
| 1477 |
} |
| 1478 |
|
| 1479 |
cl_object |
| 1480 |
si_hash_table_fill(cl_object ht, cl_object values) |
| 1481 |
{ |
| 1482 |
assert_type_hash_table(@[ext::hash-table-fill], 2, ht); |
| 1483 |
while (!Null(values)) { |
| 1484 |
cl_object pair = ecl_car(values); |
| 1485 |
cl_object key = ecl_car(pair); |
| 1486 |
cl_object value = ECL_CONS_CDR(pair); |
| 1487 |
values = ECL_CONS_CDR(values); |
| 1488 |
ecl_sethash(key, ht, value); |
| 1489 |
} |
| 1490 |
@(return ht); |
| 1491 |
} |
| 1492 |
|
| 1493 |
cl_object |
| 1494 |
si_copy_hash_table(cl_object orig) |
| 1495 |
{ |
| 1496 |
cl_object hash; |
| 1497 |
hash = cl__make_hash_table(cl_hash_table_test(orig), |
| 1498 |
cl_hash_table_size(orig), |
| 1499 |
cl_hash_table_rehash_size(orig), |
| 1500 |
cl_hash_table_rehash_threshold(orig)); |
| 1501 |
hash->hash.generic_hash = orig->hash.generic_hash, |
| 1502 |
memcpy(hash->hash.data, orig->hash.data, |
| 1503 |
orig->hash.size * sizeof(*orig->hash.data)); |
| 1504 |
hash->hash.entries = orig->hash.entries; |
| 1505 |
@(return hash); |
| 1506 |
} |