File: htable.c

package info (click to toggle)
lush 1.0%2Bfinal-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 19,256 kB
  • ctags: 4,254
  • sloc: ansic: 45,498; sh: 3,138; cpp: 2,456; makefile: 372; lisp: 79; python: 5
file content (779 lines) | stat: -rw-r--r-- 16,558 bytes parent folder | download | duplicates (2)
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
/***********************************************************************
 * 
 *  LUSH Lisp Universal Shell
 *    Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
 *  Includes parts of TL3:
 *    Copyright (C) 1987-1999 Leon Bottou and Neuristique.
 *  Includes selected parts of SN3.2:
 *    Copyright (C) 1991-2001 AT&T Corp.
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
 * 
 ***********************************************************************/

/***********************************************************************
 * $Id: htable.c,v 1.12 2003/02/28 18:39:23 leonb Exp $
 **********************************************************************/

/***********************************************************************
  This file contains functions for handling hash tables
********************************************************************** */

/* --- INITIALISATION SECTION --- */

#include "header.h"


struct hashelt
{
  struct hashelt *next;
  unsigned long hash;
  at *key;
  at *value;
};

struct hashtable
{
  int   size;
  int   nelems;
  int   pointerhashp;
  int   keylockp;
  int   rehashp;
  struct hashelt **table;
};



/* --- PRIMITIVE SECTION --- */



/* alloc_hashelt -- structure for allocating hash nodes */

static struct alloc_root alloc_hashelt = {
  NULL,
  NULL,
  sizeof(struct hashelt),
  1000,
};


/* HASHTABLE CLASS FUNCTIONS */

static void 
htable_dispose(at *p)
{
  int i;
  struct hashelt *n, *m;
  struct hashtable *htable = p->Object;

  if (htable->pointerhashp)
    del_finalizers(htable);
  
  for (i=0; i<htable->size; i++)
    {
      n=htable->table[i];
      while(n)
        {
          m = n->next;
          if (htable->keylockp)
            UNLOCK(n->key);
          UNLOCK(n->value);
          deallocate(&alloc_hashelt,(void*)n);
          n = m;
        }
    }
  free(htable->table);
  free(htable);
  p->Object = NIL;
}


static void 
htable_action(at *p, void (*action)(at *))
{
  int i;
  struct hashelt *n;
  struct hashtable *htable = p->Object;
  for (i=0; i<htable->size; i++)
    for (n=htable->table[i]; n; n=n->next)
      {
        if (htable->keylockp)
          (*action)(n->key);
        (*action)(n->value);
      }
}


static at *
htable_listeval(at *p, at *q)
{
  q = q->Cdr;
  if ((!CONSP(q)) ||
      (q->Cdr && ((!CONSP(q->Cdr)) || q->Cdr->Cdr)))
    error(NIL,"One or two arguments expected",q);
  q = eval_a_list(q);
  if (q->Cdr)
    {
      htable_set(p,q->Car,q->Cdr->Car);
      LOCK(p);
      UNLOCK(q);
      return p;
    }
  else
    {
      p = htable_get(p,q->Car);
      UNLOCK(q);
      return p;
    }
}


static void
htable_serialize(at **pp, int code)
{
  if (code == SRZ_READ)
    {
      int nelems, i;
      struct hashelt *n;
      struct hashtable *htable;
      serialize_int(&i, code);
      serialize_int(&nelems, code);
      (*pp) = new_htable(nelems, i);
      htable = (*pp)->Object;
      htable->rehashp = 1;
      htable->keylockp = 1;
      for (i=0; i<nelems; i++)
        {
          n = allocate(&alloc_hashelt);
          n->hash = 0;
          n->next = htable->table[0];
          serialize_atstar(&n->key, code);
          serialize_atstar(&n->value, code);
          htable->table[0] = n;
          htable->nelems++;
        }
    }
  else
    {
      int i;
      struct hashelt *n;
      struct hashtable *htable = (*pp)->Object;
      i = htable->pointerhashp;
      serialize_int(&i, code);
      i = htable->nelems;
      serialize_int(&i, code);
      for (i=0; i<htable->size; i++)
        for (n=htable->table[i]; n; n=n->next) {
          serialize_atstar(&n->key, code);
          serialize_atstar(&n->value, code);
        }
    }
}


static int
htable_compare(at *p, at *q, int order)
{
  int i, c;
  at *val;
  struct hashelt *n;
  struct hashtable *htable = p->Object;
  struct hashtable *htable2 = q->Object;
  if (order)
    error(NIL,"Cannot rank hashtables",NIL);
  /* check number of elements */
  if (htable->nelems != htable2->nelems)
    return 1;
  /* check elements */
  for (i=0; i<htable->size; i++)
    for (n=htable->table[i]; n; n=n->next)
    {
      val = htable_get(q, n->key);
      if (!val)
        return 1;
      c = eq_test(val,n->value);
      UNLOCK(val);
      if (!c)
        return 1;
    }
  return 0;
}


static unsigned long
htable_hash(at *p)
{
  int i;
  unsigned long x = 0;
  struct hashelt *n;
  struct hashtable *htable = p->Object;
  for (i=0; i<htable->size; i++)
    for (n=htable->table[i]; n; n=n->next)
      {
        unsigned long y = hash_value(n->key);
        y = (y<<6) ^ hash_value(n->value);
        x ^= y;  /* hash code is order independent */
      }
  return x;
}


class htable_class =
{
  htable_dispose,
  htable_action,
  generic_name,
  generic_eval,
  htable_listeval,
  htable_serialize,
  htable_compare,
  htable_hash
};



/* HASHTABLE UTILITIES */


/* hash_pointer -- hash pointer bits */

unsigned long 
hash_pointer(at *p)
{
  unsigned long x = (unsigned long)p;
  x = x ^ (x>>16);
  return x;
}



/* hash_value -- hash object pointed to by p */

unsigned long 
hash_value(at *p)
{
  unsigned long x = 0;
  struct recur_elt elt;
  char toggle = 0;
  at *slow = p;

again:
  x = (x<<1)|((long)x<0 ? 0 : 1);
  if (!p)
    {
      return x;
    }
  else if (p->flags & C_NUMBER)
    {
      x ^= 0x1010;
      x ^= ((unsigned long*)&p->Number)[0];
      if (sizeof(real) >= 2*sizeof(unsigned long))
        x ^= ((unsigned long*)&p->Number)[1];
    }
  else if (p->flags & C_CONS)
    {
      x ^= 0x2020;
      if (recur_push_ok(&elt, &hash_value, p->Car))
        {
          x ^= hash_value(p->Car);
          recur_pop(&elt);
        }
      /* go to next list element */
      p = p->Cdr;
      if (p == slow) /* circular list */
        return x;
      toggle ^= 1;
      if (!toggle)
        slow = slow->Cdr;
      goto again;
    }
  else if ((p->flags & C_EXTERN) && (p->Class->hash))
    {
      x ^= 0x3030;
      if (recur_push_ok(&elt, &hash_pointer, p))
        {
          x ^= (*p->Class->hash)(p);
          recur_pop(&elt);
        }
    }
  else if (p->flags & C_GPTR)
    {
      x ^= 0x4040;
      x ^= (unsigned long)(p->Gptr);
    }
  else
    {
      x ^= hash_pointer(p);
    }
  return x;
}


/* htable_resize -- extend hash table */

static void 
htable_resize(struct hashtable *htable, int newsize)
{
  int i, j;
  struct hashelt *n, *m;
  struct hashelt **newtable;
  /* allocate newtable */
  newtable = malloc(sizeof(struct hashelt*) * newsize);
  if (!newtable)
    error(NIL,"Out of memory",NIL);
  for (i=0; i<newsize; i++)
    newtable[i] = 0;
  /* relocate hashelt */
  for (i=0; i<htable->size; i++)
    {
      n = htable->table[i];
      while (n)
        {
          m = n->next;
          j = n->hash % newsize;
          n->next = newtable[j];
          newtable[j] = n;
          n = m;
        }
    }
  /* free and replace */
  free(htable->table);
  htable->table = newtable;
  htable->size = newsize;
}



/* hashelt_finalizer -- helper for pointerhashp case */

static void
hashelt_finalize(at *k, void *table)
{
  struct hashtable *htable = table;
  struct hashelt **np, *n;
  unsigned long hash;
  
  hash = hash_pointer(k);
  np = &(htable->table[hash % htable->size]);
  while (*np)
    {
      n = *np;
      if (n->key != k)
        {
          np = &(n->next);
        }
      else
        {
          *np = n->next;
          UNLOCK(n->value);
          deallocate(&alloc_hashelt,(struct empty_alloc*)n);
          htable->nelems -= 1;
        }
    }
}

/* htable_rehash -- rehash an existing hash table */

static void 
htable_rehash(struct hashtable *htable)
{
  int i;
  int pointerhashp;
  struct hashelt *n, **np;
  pointerhashp = htable->pointerhashp;
  /* turn off keylockp when pointerhashp is true */
  if (pointerhashp && htable->keylockp)
    {
      at *p = NIL;
      for (i=0; i<htable->size; i++)
        for (n=htable->table[i]; n; n=n->next)
          {
            p = cons(n->key, p); /* steal lock */
            add_finalizer(p, hashelt_finalize, htable);
          }
      htable->keylockp = 0;
      UNLOCK(p);
    }
  /* recompute hash numbers */
  for (i=0; i<htable->size; i++)
    {
      np = &htable->table[i];
      while ((n = *np))
        {
          /* zap zombies */
          if ( (n->key && (n->key->flags & X_ZOMBIE)) ||
               (n->value && (n->value->flags & X_ZOMBIE)) )
            {
              *np = n->next;
              UNLOCK(n->key);
              UNLOCK(n->value);
              deallocate(&alloc_hashelt,(void*)n);
              htable->nelems -= 1;
              continue;
            }
          /* recompute hash */
          if (pointerhashp)
            n->hash = hash_pointer(n->key);
          else
            n->hash = hash_value(n->key);
          np = &(n->next);
        }
    }
  /* reorganize hash table */
  htable_resize(htable, htable->size);
  htable->rehashp = FALSE;
}



/* HASHTABLE ACCESS FUNCTIONS */


/* htable_set -- set element by key */

LUSHAPI void 
htable_set(at *ht, at *key, at *value)
{
  int i;
  unsigned long hash;
  struct hashelt *n,**np;
  struct hashtable *htable;
  int pointerhashp;
  
  if (!EXTERNP(ht, &htable_class))
    error(NIL,"not a hash table", ht);
  if ( value && (value->flags & X_ZOMBIE))
    value = NIL;
  if ( key && (key->flags & X_ZOMBIE))
    return;
  /* check hash table */
  htable = ht->Object;
  if (htable->rehashp)
    htable_rehash(htable);
  /* hash */
  pointerhashp = htable->pointerhashp;
  if (pointerhashp)
    hash = hash_pointer(key);
  else
    hash = hash_value(key);
  /* search */
  np = &(htable->table[hash % htable->size]);
  while ((n = *np))
    {
      if (key == n->key)
        break;
      if (!pointerhashp)
        if (hash == n->hash)
          if (eq_test(key, n->key))
            break;
      np = &(n->next);
    }
  /* change */
  if (value && n)
    {
      LOCK(value);
      UNLOCK(n->value);
      n->value = value;
    }
  else if (value)
    {
      while (2 * htable->size < 3 * htable->nelems)
        htable_resize(htable, 2 * htable->size - 1);
      n = allocate(&alloc_hashelt);
      if (!htable->keylockp)
        add_finalizer(key, hashelt_finalize, htable);
      else
        LOCK(key);
      LOCK(value);
      n->hash = hash;
      n->value = value;
      n->key = key;
      i = hash % htable->size;
      n->next = htable->table[i];
      htable->table[i] = n;
      htable->nelems += 1;
    }
  else if (n)
    {
      *np = n->next;
      if (htable->keylockp)
        UNLOCK(n->key);
      UNLOCK(n->value);
      deallocate(&alloc_hashelt,(void*)n);
      htable->nelems -= 1;
    }
}


/* htable_get -- get element by key */

LUSHAPI at *
htable_get(at *ht, at *key)
{
  struct hashelt *n, **np;
  struct hashtable *htable;
  unsigned long hash;
  int pointerhashp;

  if (!EXTERNP(ht, &htable_class))
    error(NIL,"not a hash table", ht);
  /* check hash table */
  htable = ht->Object;
  if (htable->rehashp)
    htable_rehash(htable);
  /* hash */
  pointerhashp = htable->pointerhashp;
  if (pointerhashp)
    hash = hash_pointer(key);
  else
    hash = hash_value(key);
  /* search chain */
  np = &htable->table[hash % htable->size];
  while ((n = *np))
    {
      if (key == n->key)
        break;
      if (!pointerhashp)
        if (hash == n->hash)
          if (eq_test(key, n->key))
            break;
      np = &(n->next);
    }
  if (n)
    {
      LOCK(n->value);
      return n->value;
    }
  return NIL;
}



/* HASHTABLE OPERATIONS */

/* hashcode */

DX(xhashcode)
{
  static char format[8];
  char buffer[24];
  unsigned long x;
  ARG_NUMBER(1);
  ARG_EVAL(1);
  x = hash_value(APOINTER(1));
  if (! format[0])
    sprintf(format,"%%0%dlx", (int)(2*sizeof(unsigned long)));
  sprintf(buffer,format, x);
  return new_string(buffer);
}


/* new_htable -- hashtable creation */

LUSHAPI at *
new_htable(int nelems, int pointerhashp)
{ 
  int i;
  int size;
  struct hashtable *htable;
  struct hashelt **table;
  size = (nelems<16 ? 31 : nelems * 2 - 1);
  table = malloc(size*sizeof(struct hashelt*));
  ifn (table)
    error(NIL,"Out of memory",NIL);
  htable = malloc(sizeof(struct hashtable));
  ifn (htable)
    error(NIL,"Out of memory",NIL);
  for (i=0; i<size; i++)
    table[i] = NIL;
  htable->nelems = 0;
  htable->size = size;
  htable->pointerhashp = pointerhashp;
  htable->keylockp = !pointerhashp;
  htable->rehashp = FALSE;
  htable->table = table;
  return new_extern(&htable_class,htable);
}

DX(xnew_htable)
{
  int nelems = 0;
  int pointerhashp = 0;

  ALL_ARGS_EVAL;
  if (arg_number>=1)
    nelems = AINTEGER(1);
  if (arg_number>=2)
    pointerhashp = (APOINTER(2) ? 1 : 0);
  if (arg_number>2)
    error(NIL,"At most two arguments expected",NIL);
  return new_htable(nelems,pointerhashp);
}



/* htable_alist -- returns contents as an alist */

DX(xhtable_alist)
{
  int i;
  at *ans;
  struct hashtable *htable;
  struct hashelt *n;
  ARG_NUMBER(1);
  ARG_EVAL(1);
  ans = APOINTER(1);
  if (!EXTERNP(ans, &htable_class))
    error(NIL,"not a hash table", ans);
  htable = ans->Object;
  ans = NIL;
  for (i=0; i<htable->size; i++)
    for (n=htable->table[i]; n; n=n->next)
      ans = cons(new_cons(n->key, n->value),ans);
  return ans;
}


/* htable_keys -- returns list of keys */

DX(xhtable_keys)
{
  int i;
  at *ans;
  struct hashtable *htable;
  struct hashelt *n;
  ARG_NUMBER(1);
  ARG_EVAL(1);
  ans = APOINTER(1);
  if (!EXTERNP(ans, &htable_class))
    error(NIL,"not a hash table", ans);
  htable = ans->Object;
  ans = NIL;
  for (i=0; i<htable->size; i++)
    for (n=htable->table[i]; n; n=n->next)
    {
      LOCK(n->key);
      ans = cons(n->key,ans);
    }
  return ans;
}




/* htable_rehash -- recompute hash numbers */

DX(xhtable_rehash)
{
  at *p;
  struct hashtable *htable;
  /* Get hash table */
  ARG_NUMBER(1);
  ARG_EVAL(1);
  p = APOINTER(1);
  if (!EXTERNP(p, &htable_class))
    error(NIL,"Not a hash table", p);
  htable = p->Object;
  htable->rehashp = TRUE;
  return NEW_NUMBER(htable->nelems);
}




/* htable_size -- returns htable number of elements */

DX(xhtable_size)
{
  at *p;
  struct hashtable *htable;
  ARG_NUMBER(1);
  ARG_EVAL(1);
  p = APOINTER(1);
  if (!EXTERNP(p, &htable_class))
    error(NIL,"Not a hash table", p);
  htable = p->Object;
  return NEW_NUMBER(htable->nelems);
}




/* htable_info -- return hashtable statistics */

#define MAXHITDEPTH 32

DX(xhtable_info)
{
  at *ans;
  struct hashtable *htable;
  struct hashelt *n;
  int hit[MAXHITDEPTH];
  int i,j,maxhit;
  /* Get hash table */
  ARG_NUMBER(1);
  ARG_EVAL(1);
  ans = APOINTER(1);
  if (!EXTERNP(ans, &htable_class))
    error(NIL,"Not a hash table", ans);
  htable = ans->Object;
  if (htable->rehashp)
    htable_rehash(htable);
  /* Initialize */
  maxhit = -1;
  for (j=0; j<MAXHITDEPTH; j++)
    hit[j] = 0;
  /* Iterate and count */
  for (i=0; i<htable->size; i++)
    for (n=htable->table[i],j=0; n; n=n->next,j++)
    {
      if (j<MAXHITDEPTH)
        hit[j] += 1;
      if (j>maxhit)
        maxhit = j;
    }
  /* Build result */
  ans = NIL;
  if (maxhit>=MAXHITDEPTH)
    ans = cons(cons(NEW_NUMBER(maxhit),NIL),NIL);
  for (j=MAXHITDEPTH-1; j>=0; j--)
    if (hit[j]) break;
  for (;j>=0; j--)
    ans = cons(NEW_NUMBER(hit[j]),ans);
  ans = cons(cons(named("hits"),ans),NIL);
  ans = cons(cons(named("buckets"),NEW_NUMBER(htable->size)),ans);
  ans = cons(cons(named("size"),NEW_NUMBER(htable->nelems)),ans);
  return ans;
}





/* --- INITIALISATION SECTION --- */

void init_htable(void)
{
  class_define("HTABLE",&htable_class);
  dx_define("hashcode",xhashcode);
  dx_define("htable",xnew_htable);
  dx_define("htable-alist",xhtable_alist);
  dx_define("htable-keys", xhtable_keys);
  dx_define("htable-rehash", xhtable_rehash);
  dx_define("htable-size",xhtable_size);
  dx_define("htable-info",xhtable_info);
}