[cc2b09]: src / c / hash.d Maximize Restore History

Download this file

hash.d    811 lines (742 with data), 21.3 kB

  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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* -*- mode: c; c-basic-offset: 8 -*- */
/*
hash.d -- Hash tables.
*/
/*
Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
Copyright (c) 1990, Giuseppe Attardi.
Copyright (c) 2001, Juan Jose Garcia Ripoll.
ECL is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
See file '../Copyright' for full details.
*/
/* for ECL_MATHERR_* */
#define ECL_INCLUDE_MATH_H
#include <ecl/ecl.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <ecl/ecl-inl.h>
#include <ecl/internal.h>
#include "newhash.h"
static void corrupted_hash(cl_object hashtable) /*__attribute__((noreturn))*/;
#define SYMBOL_NAME(x) (Null(x)? Cnil_symbol->symbol.name : (x)->symbol.name)
static void ECL_INLINE
assert_type_hash_table(cl_object function, cl_narg narg, cl_object p)
{
unlikely_if (!ECL_HASH_TABLE_P(p))
FEwrong_type_nth_arg(function, narg, p, @[hash-table]);
}
static void
corrupted_hash(cl_object hashtable)
{
FEerror("internal error, corrupted hashtable ~S", 1, hashtable);
}
static cl_hashkey
_hash_eql(cl_hashkey h, cl_object x)
{
switch (type_of(x)) {
case t_bignum:
return hash_string(h, (unsigned char*)x->big.big_limbs,
labs(x->big.big_size) * sizeof(mp_limb_t));
case t_ratio:
h = _hash_eql(h, x->ratio.num);
return _hash_eql(h, x->ratio.den);
case t_singlefloat:
return hash_string(h, (unsigned char*)&sf(x), sizeof(sf(x)));
case t_doublefloat:
return hash_string(h, (unsigned char*)&df(x), sizeof(df(x)));
#ifdef ECL_LONG_FLOAT
case t_longfloat: {
/* We coerce to double because long double has extra bits
* that give rise to different hash key and are not
* meaningful */
struct { double mantissa; int exponent; int sign; } aux;
aux.mantissa = frexpl(ecl_long_float(x), &aux.exponent);
aux.sign = (ecl_long_float(x) < 0)? -1: 1;
return hash_string(h, (unsigned char*)&aux, sizeof(aux));
}
#endif
case t_complex:
h = _hash_eql(h, x->complex.real);
return _hash_eql(h, x->complex.imag);
case t_character:
return hash_word(h, CHAR_CODE(x));
#ifdef ECL_SSE2
case t_sse_pack:
return hash_string(h, x->sse.data.b8, 16);
#endif
default:
return hash_word(h, ((cl_hashkey)x >> 2));
}
}
static cl_hashkey
_hash_equal(int depth, cl_hashkey h, cl_object x)
{
switch (type_of(x)) {
case t_list:
if (Null(x)) {
return _hash_equal(depth, h, Cnil_symbol->symbol.name);
}
if (--depth == 0) {
return h;
} else {
h = _hash_equal(depth, h, ECL_CONS_CAR(x));
return _hash_equal(depth, h, ECL_CONS_CDR(x));
}
case t_symbol:
x = x->symbol.name;
#ifdef ECL_UNICODE
case t_base_string:
return hash_base_string((ecl_base_char *)x->base_string.self,
x->base_string.fillp, h);
case t_string:
return hash_full_string(x->string.self, x->string.fillp, h);
#else
case t_base_string:
return hash_string(h, (ecl_base_char *)x->base_string.self,
x->base_string.fillp);
#endif
case t_pathname:
h = _hash_equal(0, h, x->pathname.directory);
h = _hash_equal(0, h, x->pathname.name);
h = _hash_equal(0, h, x->pathname.type);
h = _hash_equal(0, h, x->pathname.host);
h = _hash_equal(0, h, x->pathname.device);
return _hash_equal(0, h, x->pathname.version);
case t_bitvector:
/* Notice that we may round out some bits. We must do this
* because the fill pointer may be set in the middle of a byte.
* If so, the extra bits _must_ _not_ take part in the hash,
* because otherwise two bit arrays which are EQUAL might
* have different hash keys. */
return hash_string(h, x->vector.self.bc, x->vector.fillp / 8);
case t_random:
return _hash_equal(0, h, x->random.value);
#ifdef ECL_SIGNED_ZERO
case t_singlefloat: {
float f = sf(x);
if (f == 0.0) f = 0.0;
return hash_string(h, (unsigned char*)&f, sizeof(f));
}
case t_doublefloat: {
double f = df(x);
if (f == 0.0) f = 0.0;
return hash_string(h, (unsigned char*)&f, sizeof(f));
}
# ifdef ECL_LONG_FLOAT
case t_longfloat: {
/* We coerce to double because long double has extra bits
* that give rise to different hash key and are not
* meaningful */
struct { double mantissa; int exponent; int sign; } aux;
aux.mantissa = frexpl(ecl_long_float(x), &aux.exponent);
aux.sign = (ecl_long_float(x) < 0)? -1: 1;
if (aux.mantissa == 0.0) aux.mantissa = 0.0;
return hash_string(h, (unsigned char*)&aux, sizeof(aux));
}
# endif
case t_complex: {
h = _hash_equal(depth, h, x->complex.real);
return _hash_equal(depth, h, x->complex.imag);
}
#endif
default:
return _hash_eql(h, x);
}
}
static cl_hashkey
_hash_equalp(int depth, cl_hashkey h, cl_object x)
{
cl_index i, len;
switch (type_of(x)) {
case t_character:
return hash_word(h, ecl_char_upcase(CHAR_CODE(x)));
case t_list:
if (Null(x)) {
return _hash_equalp(depth, h, Cnil_symbol->symbol.name);
}
if (--depth == 0) {
return h;
} else {
h = _hash_equalp(depth, h, ECL_CONS_CAR(x));
return _hash_equalp(depth, h, ECL_CONS_CDR(x));
}
#ifdef ECL_UNICODE
case t_string:
#endif
case t_base_string:
case t_vector:
case t_bitvector:
len = x->vector.fillp;
goto SCAN;
case t_array:
len = x->vector.dim;
SCAN: if (--depth) {
for (i = 0; i < len; i++) {
h = _hash_equalp(depth, h, ecl_aref_unsafe(x, i));
}
}
return h;
case t_fixnum:
return hash_word(h, fix(x));
case t_singlefloat:
/* FIXME! We should be more precise here! */
return hash_word(h, (cl_index)sf(x));
case t_doublefloat:
/* FIXME! We should be more precise here! */
return hash_word(h, (cl_index)df(x));
case t_bignum:
/* FIXME! We should be more precise here! */
case t_ratio:
h = _hash_equalp(0, h, x->ratio.num);
return _hash_equalp(0, h, x->ratio.den);
case t_complex:
h = _hash_equalp(0, h, x->complex.real);
return _hash_equalp(0, h, x->complex.imag);
case t_instance:
case t_hashtable:
/* FIXME! We should be more precise here! */
return hash_word(h, 42);
default:
return _hash_equal(depth, h, x);
}
}
#define HASH_TABLE_LOOP(hkey,hvalue,h,HASH_TABLE_LOOP_TEST) { \
cl_index hsize = hashtable->hash.size; \
cl_index i = h % hsize, j = hsize, k; \
struct ecl_hashtable_entry *first_hole = 0; \
for (k = 0; k < hsize; i = (i + 1) % hsize, k++) { \
struct ecl_hashtable_entry *e = hashtable->hash.data + i; \
cl_object hkey = e->key, hvalue = e->value; \
if (hkey == OBJNULL) { \
if (e->value == OBJNULL) { \
if (j == hsize) \
return e; \
else \
return hashtable->hash.data + j; \
} else { \
if (j == hsize) \
j = i; \
else if (j == i) \
return e; \
} \
continue; \
} \
if (HASH_TABLE_LOOP_TEST) return hashtable->hash.data + i; \
} \
return hashtable->hash.data + j; \
}
#if 0
#define HASH_TABLE_SET(h,loop,compute_key,store_key)
#else
#define HASH_TABLE_SET(h,loop,compute_key,store_key) { \
cl_hashkey h = compute_key; \
struct ecl_hashtable_entry *e; \
AGAIN: \
e = loop(h, key, hashtable); \
if (e->key == OBJNULL) { \
cl_index i = hashtable->hash.entries + 1; \
if (i >= hashtable->hash.limit) { \
hashtable = ecl_extend_hashtable(hashtable); \
goto AGAIN; \
} \
hashtable->hash.entries = i; \
e->key = store_key; \
} \
e->value = value; \
return hashtable; \
}
#endif
/*
* EQ HASHTABLES
*/
#if 0
#define _hash_eq(k) ((cl_hashkey)(k) ^ ((cl_hashkey)(k) >> 16))
#else
#define _hash_eq(k) ((cl_hashkey)(k) >> 2)
#endif
static struct ecl_hashtable_entry *
_ecl_hash_loop_eq(cl_hashkey h, cl_object key, cl_object hashtable)
{
HASH_TABLE_LOOP(hkey, hvalue, h, key == hkey);
}
struct ecl_hashtable_entry *
_ecl_gethash_eq(cl_object key, cl_object hashtable)
{
cl_hashkey h = _hash_eq(key);
return _ecl_hash_loop_eq(h, key, hashtable);
}
cl_object
_ecl_sethash_eq(cl_object key, cl_object hashtable, cl_object value)
{
HASH_TABLE_SET(h, _ecl_hash_loop_eq, _hash_eq(key), key);
}
/*
* EQL HASHTABLES
*/
static struct ecl_hashtable_entry *
_ecl_hash_loop_eql(cl_hashkey h, cl_object key, cl_object hashtable)
{
HASH_TABLE_LOOP(hkey, hvalue, h, ecl_eql(key, hkey));
}
static struct ecl_hashtable_entry *
_ecl_gethash_eql(cl_object key, cl_object hashtable)
{
cl_hashkey h = _hash_eql(0, key);
return _ecl_hash_loop_eql(h, key, hashtable);
}
static cl_object
_ecl_sethash_eql(cl_object key, cl_object hashtable, cl_object value)
{
HASH_TABLE_SET(h, _ecl_hash_loop_eql, _hash_eql(0, key), key);
}
/*
* EQUAL HASHTABLES
*/
static struct ecl_hashtable_entry *
_ecl_hash_loop_equal(cl_hashkey h, cl_object key, cl_object hashtable)
{
HASH_TABLE_LOOP(hkey, hvalue, h, ecl_equal(key, hkey));
}
static struct ecl_hashtable_entry *
_ecl_gethash_equal(cl_object key, cl_object hashtable)
{
cl_hashkey h = _hash_equal(3, 0, key);
return _ecl_hash_loop_equal(h, key, hashtable);
}
static cl_object
_ecl_sethash_equal(cl_object key, cl_object hashtable, cl_object value)
{
HASH_TABLE_SET(h, _ecl_hash_loop_equal, _hash_equal(3, 0, key), key);
}
/*
* EQUALP HASHTABLES
*/
static struct ecl_hashtable_entry *
_ecl_hash_loop_equalp(cl_hashkey h, cl_object key, cl_object hashtable)
{
HASH_TABLE_LOOP(hkey, hvalue, h, ecl_equalp(key, hkey));
}
static struct ecl_hashtable_entry *
_ecl_gethash_equalp(cl_object key, cl_object hashtable)
{
cl_hashkey h = _hash_equalp(3, 0, key);
return _ecl_hash_loop_equalp(h, key, hashtable);
}
static cl_object
_ecl_sethash_equalp(cl_object key, cl_object hashtable, cl_object value)
{
HASH_TABLE_SET(h, _ecl_hash_loop_equalp, _hash_equalp(3, 0, key), key);
}
/*
* PACKAGE HASHTABLES
*/
static struct ecl_hashtable_entry *
_ecl_hash_loop_pack(cl_hashkey h, cl_object key, cl_object hashtable)
{
cl_object ho = MAKE_FIXNUM(h & 0xFFFFFFF);
HASH_TABLE_LOOP(hkey, hvalue, h, (ho==hkey) && ecl_string_eq(key,SYMBOL_NAME(hvalue)));
}
static struct ecl_hashtable_entry *
_ecl_gethash_pack(cl_object key, cl_object hashtable)
{
cl_hashkey h = _hash_equal(3, 0, key);
return _ecl_hash_loop_pack(h, key, hashtable);
}
static cl_object
_ecl_sethash_pack(cl_object key, cl_object hashtable, cl_object value)
{
HASH_TABLE_SET(h, _ecl_hash_loop_pack, _hash_equal(3, 0, key), MAKE_FIXNUM(h & 0xFFFFFFF));
}
/*
* HIGHER LEVEL INTERFACE
*/
struct ecl_hashtable_entry *
_ecl_gethash(cl_object key, cl_object hashtable)
{
return hashtable->hash.get(key, hashtable);
}
cl_object
ecl_gethash(cl_object key, cl_object hashtable)
{
cl_object output;
assert_type_hash_table(@[gethash], 2, hashtable);
output = hashtable->hash.get(key, hashtable)->value;
return output;
}
cl_object
ecl_gethash_safe(cl_object key, cl_object hashtable, cl_object def)
{
struct ecl_hashtable_entry *e;
assert_type_hash_table(@[gethash], 2, hashtable);
e = hashtable->hash.get(key, hashtable);
if (e->key != OBJNULL)
def = e->value;
return def;
}
cl_object
_ecl_sethash(cl_object key, cl_object hashtable, cl_object value)
{
return hashtable->hash.set(key, hashtable, value);
}
cl_object
ecl_sethash(cl_object key, cl_object hashtable, cl_object value)
{
assert_type_hash_table(@[si::hash-set], 2, hashtable);
hashtable = hashtable->hash.set(key, hashtable, value);
return hashtable;
}
cl_object
ecl_extend_hashtable(cl_object hashtable)
{
cl_object old, new, key;
cl_index old_size, new_size, i;
cl_object new_size_obj;
assert_type_hash_table(@[si::hash-set], 2, hashtable);
old_size = hashtable->hash.size;
/* We do the computation with lisp datatypes, just in case the sizes contain
* weird numbers */
if (FIXNUMP(hashtable->hash.rehash_size)) {
new_size_obj = ecl_plus(hashtable->hash.rehash_size, MAKE_FIXNUM(old_size));
} else {
new_size_obj = ecl_times(hashtable->hash.rehash_size, MAKE_FIXNUM(old_size));
new_size_obj = ecl_ceiling1(new_size_obj);
}
if (!FIXNUMP(new_size_obj)) {
/* New size is too large */
new_size = old_size * 2;
} else {
new_size = fix(new_size_obj);
}
if (hashtable->hash.test == htt_pack) {
new = ecl_alloc_object(t_hashtable);
new->hash = hashtable->hash;
old = hashtable;
} else {
old = ecl_alloc_object(t_hashtable);
old->hash = hashtable->hash;
new = hashtable;
}
new->hash.data = NULL; /* for GC sake */
new->hash.entries = 0;
new->hash.size = new_size;
new->hash.limit = new->hash.size * new->hash.factor;
new->hash.data = (struct ecl_hashtable_entry *)
ecl_alloc(new_size * sizeof(struct ecl_hashtable_entry));
for (i = 0; i < new_size; i++) {
new->hash.data[i].key = OBJNULL;
new->hash.data[i].value = OBJNULL;
}
for (i = 0; i < old_size; i++) {
if ((key = old->hash.data[i].key) != OBJNULL) {
if (new->hash.test == htt_pack)
key = SYMBOL_NAME(old->hash.data[i].value);
new = new->hash.set(key, new, old->hash.data[i].value);
}
}
return new;
}
@(defun make_hash_table (&key (test @'eql')
(size MAKE_FIXNUM(1024))
(rehash_size cl_core.rehash_size)
(rehash_threshold cl_core.rehash_threshold))
@
@(return cl__make_hash_table(test, size, rehash_size, rehash_threshold))
@)
static void
do_clrhash(cl_object ht)
{
/*
* Fill a hash with null pointers and ensure it does not have
* any entry. We separate this routine because it is needed
* both by clrhash and hash table initialization.
*/
cl_index i;
ht->hash.entries = 0;
for(i = 0; i < ht->hash.size; i++) {
ht->hash.data[i].key = OBJNULL;
ht->hash.data[i].value = OBJNULL;
}
}
ecl_def_ct_single_float(min_threshold, 0.1, static, const);
cl_object
cl__make_hash_table(cl_object test, cl_object size, cl_object rehash_size,
cl_object rehash_threshold)
{
int htt;
cl_index hsize;
cl_object h;
struct ecl_hashtable_entry *(*get)(cl_object, cl_object);
cl_object (*set)(cl_object, cl_object, cl_object);
/*
* Argument checking
*/
if (test == @'eq' || test == SYM_FUN(@'eq')) {
htt = htt_eq;
get = _ecl_gethash_eq;
set = _ecl_sethash_eq;
} else if (test == @'eql' || test == SYM_FUN(@'eql')) {
htt = htt_eql;
get = _ecl_gethash_eql;
set = _ecl_sethash_eql;
} else if (test == @'equal' || test == SYM_FUN(@'equal')) {
htt = htt_equal;
get = _ecl_gethash_equal;
set = _ecl_sethash_equal;
} else if (test == @'equalp' || test == SYM_FUN(@'equalp')) {
htt = htt_equalp;
get = _ecl_gethash_equalp;
set = _ecl_sethash_equalp;
} else if (test == @'package') {
htt = htt_pack;
get = _ecl_gethash_pack;
set = _ecl_sethash_pack;
} else {
FEerror("~S is an illegal hash-table test function.",
1, test);
}
if (ecl_unlikely(!ECL_FIXNUMP(size) ||
ecl_fixnum_minusp(size) ||
ecl_fixnum_geq(size,MAKE_FIXNUM(ATOTLIM)))) {
FEwrong_type_key_arg(@[make-hash-table], @[:size], size,
ecl_make_integer_type(MAKE_FIXNUM(0),
MAKE_FIXNUM(ATOTLIM)));
}
hsize = fix(size);
if (hsize < 16) {
hsize = 16;
}
AGAIN:
if (ecl_minusp(rehash_size)) {
ERROR1:
rehash_size =
ecl_type_error(@'make-hash-table',"rehash-size",
rehash_size,
ecl_read_from_cstring("(OR (INTEGER 1 *) (FLOAT 0 (1)))"));
goto AGAIN;
}
if (floatp(rehash_size)) {
if (ecl_number_compare(rehash_size, MAKE_FIXNUM(1)) < 0 ||
ecl_minusp(rehash_size)) {
goto ERROR1;
}
rehash_size = ecl_make_doublefloat(ecl_to_double(rehash_size));
} else if (!FIXNUMP(rehash_size)) {
goto ERROR1;
}
while (!ecl_numberp(rehash_threshold) ||
ecl_minusp(rehash_threshold) ||
ecl_number_compare(rehash_threshold, MAKE_FIXNUM(1)) > 0)
{
rehash_threshold =
ecl_type_error(@'make-hash-table',"rehash-threshold",
rehash_threshold,
ecl_read_from_cstring("(REAL 0 1)"));
}
/*
* Build actual hash.
*/
h = ecl_alloc_object(t_hashtable);
h->hash.test = htt;
h->hash.get = get;
h->hash.set = set;
h->hash.size = hsize;
h->hash.entries = 0;
h->hash.rehash_size = rehash_size;
h->hash.threshold = rehash_threshold;
rehash_threshold = cl_max(2, min_threshold, rehash_threshold);
h->hash.factor = ecl_to_double(rehash_threshold);
h->hash.limit = h->hash.size * h->hash.factor;
h->hash.data = NULL; /* for GC sake */
h->hash.data = (struct ecl_hashtable_entry *)
ecl_alloc(hsize * sizeof(struct ecl_hashtable_entry));
do_clrhash(h);
return h;
}
cl_object
cl_hash_table_p(cl_object ht)
{
@(return (ECL_HASH_TABLE_P(ht) ? Ct : Cnil))
}
@(defun gethash (key ht &optional (no_value Cnil))
struct ecl_hashtable_entry e;
@
assert_type_hash_table(@[gethash], 2, ht);
e = *(ht->hash.get(key, ht));
if (e.key != OBJNULL)
@(return e.value Ct)
else
@(return no_value Cnil)
@)
cl_object
si_hash_set(cl_object key, cl_object ht, cl_object val)
{
/* INV: ecl_sethash() checks the type of hashtable */
ecl_sethash(key, ht, val);
@(return val)
}
bool
ecl_remhash(cl_object key, cl_object hashtable)
{
struct ecl_hashtable_entry *e;
bool output;
assert_type_hash_table(@[remhash], 2, hashtable);
e = hashtable->hash.get(key, hashtable);
if (e->key == OBJNULL) {
output = FALSE;
} else {
e->key = OBJNULL;
e->value = Cnil;
hashtable->hash.entries--;
output = TRUE;
}
return output;
}
cl_object
cl_remhash(cl_object key, cl_object ht)
{
/* INV: _ecl_remhash() checks the type of hashtable */
@(return (ecl_remhash(key, ht)? Ct : Cnil));
}
cl_object
cl_clrhash(cl_object ht)
{
assert_type_hash_table(@[clrhash], 1, ht);
if (ht->hash.entries) {
do_clrhash(ht);
}
@(return ht)
}
cl_object
cl_hash_table_test(cl_object ht)
{
cl_object output;
assert_type_hash_table(@[hash-table-test], 1, ht);
switch(ht->hash.test) {
case htt_eq: output = @'eq'; break;
case htt_eql: output = @'eql'; break;
case htt_equal: output = @'equal'; break;
case htt_equalp: output = @'equalp'; break;
case htt_pack:
default: output = @'equal';
}
@(return output)
}
cl_object
cl_hash_table_size(cl_object ht)
{
assert_type_hash_table(@[hash-table-size], 1, ht);
@(return MAKE_FIXNUM(ht->hash.size))
}
cl_object
cl_hash_table_count(cl_object ht)
{
assert_type_hash_table(@[hash-table-count], 1, ht);
@(return (MAKE_FIXNUM(ht->hash.entries)))
}
static cl_object
si_hash_table_iterate(cl_narg narg)
{
const cl_env_ptr the_env = ecl_process_env();
cl_object env = the_env->function->cclosure.env;
cl_object index = CAR(env);
cl_object ht = CADR(env);
cl_fixnum i;
if (!Null(index)) {
i = fix(index);
if (i < 0)
i = -1;
for (; ++i < ht->hash.size; ) {
struct ecl_hashtable_entry e = ht->hash.data[i];
if (e.key != OBJNULL) {
cl_object ndx = MAKE_FIXNUM(i);
ECL_RPLACA(env, ndx);
@(return ndx e.key e.value)
}
}
ECL_RPLACA(env, Cnil);
}
@(return Cnil)
}
cl_object
si_hash_table_iterator(cl_object ht)
{
assert_type_hash_table(@[si::hash-table-iterator], 1, ht);
@(return ecl_make_cclosure_va((cl_objectfn)si_hash_table_iterate,
cl_list(2, MAKE_FIXNUM(-1), ht),
@'si::hash-table-iterator'))
}
cl_object
cl_hash_table_rehash_size(cl_object ht)
{
assert_type_hash_table(@[hash-table-rehash-size], 1, ht);
@(return ht->hash.rehash_size)
}
cl_object
cl_hash_table_rehash_threshold(cl_object ht)
{
assert_type_hash_table(@[hash-table-rehash-threshold], 1, ht);
@(return ht->hash.threshold)
}
cl_object
cl_sxhash(cl_object key)
{
cl_index output = _hash_equal(3, 0, key);
const cl_index mask = ((cl_index)1 << (FIXNUM_BITS - 3)) - 1;
@(return MAKE_FIXNUM(output & mask))
}
@(defun si::hash-eql (&rest args)
cl_index h;
@
for (h = 0; narg; narg--) {
cl_object o = cl_va_arg(args);
h = _hash_eql(h, o);
}
@(return MAKE_FIXNUM(h))
@)
@(defun si::hash-equal (&rest args)
cl_index h;
@
for (h = 0; narg; narg--) {
cl_object o = cl_va_arg(args);
h = _hash_equal(3, h, o);
}
@(return MAKE_FIXNUM(h))
@)
@(defun si::hash-equalp (&rest args)
cl_index h;
@
for (h = 0; narg; narg--) {
cl_object o = cl_va_arg(args);
h = _hash_equalp(3, h, o);
}
@(return MAKE_FIXNUM(h))
@)
cl_object
cl_maphash(cl_object fun, cl_object ht)
{
cl_index i;
assert_type_hash_table(@[maphash], 2, ht);
for (i = 0; i < ht->hash.size; i++) {
struct ecl_hashtable_entry e = ht->hash.data[i];
if(e.key != OBJNULL)
funcall(3, fun, e.key, e.value);
}
@(return Cnil)
}
cl_object
si_copy_hash_table(cl_object orig)
{
cl_object hash;
hash = cl__make_hash_table(cl_hash_table_test(orig),
cl_hash_table_size(orig),
cl_hash_table_rehash_size(orig),
cl_hash_table_rehash_threshold(orig));
memcpy(hash->hash.data, orig->hash.data,
orig->hash.size * sizeof(*orig->hash.data));
hash->hash.entries = orig->hash.entries;
@(return hash)
}