File: pl-table.c

package info (click to toggle)
swi-prolog 3.1.0-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,772 kB
  • ctags: 12,869
  • sloc: ansic: 43,657; perl: 12,577; lisp: 4,359; sh: 1,534; makefile: 798; awk: 14
file content (217 lines) | stat: -rw-r--r-- 4,575 bytes parent folder | download | duplicates (3)
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
/*  $Id: pl-table.c,v 1.14 1997/08/07 07:58:45 jan Exp $

    Copyright (c) 1990 Jan Wielemaker. All rights reserved.
    See ../LICENCE to find out about your rights.
    jan@swi.psy.uva.nl

    Purpose: generic support for (numeric) hashTables
*/

/*#define O_DEBUG 1*/
#include "pl-incl.h"

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Anonymous hash-tables.  This  is  rather  straightforward.   A  peculiar
thing  on these and a number of dedicated hashtables build in the system
is that the last symbol of  a  specific  hash  value  is  not  the  NULL
pointer,  but  a reference pointer to the next entry.  This allows us to
enumerate  all  entries  of  the  table  with  only  one   word   status
information.   This  is  more  efficient  and  simple to handle with the
interface for non-deterministic C functions (current_predicate/2, ...).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

static void
allocHTableEntries(Table ht)
{ int n;
  Symbol *p;

  ht->entries = allocHeap(ht->buckets * sizeof(Symbol));

  for(n=0, p = &ht->entries[0]; n < ht->buckets-1; n++, p++)
    *p = makeTableRef(p+1);
  *p = (Symbol) NULL;
}


Table
newHTable(int buckets)
{ Table ht;

  ht = (Table) allocHeap(sizeof(struct table));
  ht->buckets = buckets;
  ht->size    = 0;
  ht->locked  = 0;

  allocHTableEntries(ht);
  return ht;
}


void
destroyHTable(Table ht)
{ clearHTable(ht);
  freeHeap(ht->entries, ht->buckets * sizeof(Symbol));
  freeHeap(ht, sizeof(struct table));
}


#if O_DEBUG || O_HASHSTAT
#define HASHSTAT(c) c
static int lookups;
static int cmps;

void
exitTables(int status, void *arg)
{ Sdprintf("hashstat: Anonymous tables: %d lookups using %d compares\n",
	   lookups, cmps);
}
#else
#define HASHSTAT(c)
#endif /*O_DEBUG*/


void
initTables()
{ HASHSTAT(PL_on_halt(exitTables, NULL));
}


Symbol
lookupHTable(Table ht, Void name)
{ Symbol s = ht->entries[pointerHashValue(name, ht->buckets)];

  HASHSTAT(lookups++);
  for(;s && !isTableRef(s); s = s->next)
  { HASHSTAT(cmps++);
    if (s->name == (word)name)
    { DEBUG(9, Sdprintf("lookupHTable(0x%x, 0x%x --> 0x%x\n",
			ht, name, s->value));
      return s;
    }
  }

  DEBUG(9, Sdprintf("lookupHTable(0x%x, 0x%x --> FAIL\n", ht, name));
  return (Symbol) NULL;
}


static void
rehashHTable(Table ht)
{ Symbol *oldtab  = ht->entries;
  int    oldbucks = ht->buckets;
  Symbol s, n;
  int done = 0;
  int i = 0;

  startCritical;
  ht->buckets *= 2;
  allocHTableEntries(ht);

  DEBUG(1, Sdprintf("Rehashing table 0x%x to %d entries\n",
		    ht, ht->buckets));

  for(s = oldtab[0]; s; s = n)
  { int v;

    while( isTableRef(s) )
    { s = unTableRef(Symbol, s);
      assert(s == oldtab[++i]);
      if ( !s )
	goto out;
    }
    done++;
    n = s->next;
    v = pointerHashValue(s->name, ht->buckets);
    s->next = ht->entries[v];
    ht->entries[v] = s;
  }

out:
  assert(done == ht->size);
  freeHeap(oldtab, oldbucks * sizeof(Symbol));
  endCritical;
}


bool
addHTable(Table ht, Void name, Void value)
{ Symbol s;
  int v = pointerHashValue(name, ht->buckets);

  if (lookupHTable(ht, name) != (Symbol) NULL)
    fail;
  s = (Symbol) allocHeap(sizeof(struct symbol));
  s->name = (word)name;
  s->value = (word)value;
  s->next = ht->entries[v];
  ht->entries[v] = s;
  ht->size++;
  DEBUG(9, Sdprintf("addHTable(0x%x, 0x%x, 0x%x) --> size = %d\n",
		    ht, name, value, ht->size));

  if ( ht->buckets * 2 < ht->size && !ht->locked )
    rehashHTable(ht);

  succeed;
}  


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Note: s must be in the table!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

void
deleteSymbolHTable(Table ht, Symbol s)
{ int v = pointerHashValue(s->name, ht->buckets);
  Symbol *h = &ht->entries[v];

  for( ; *h != s; h = &(*h)->next )
  { if ( *h == s )
    { *h = (*h)->next;

      freeHeap(s, sizeof(struct symbol));
      ht->size--;

      return;
    }
  }
}


Symbol
nextHTable(Table ht, Symbol s)
{ s = s->next;
  while(s != (Symbol) NULL && isTableRef(s) )
    s = unTableRef(Symbol, s);

  return s;
}

Symbol
firstHTable(Table ht)
{ Symbol s = ht->entries[0];

  while(s != (Symbol) NULL && isTableRef(s) )
    s = unTableRef(Symbol, s);

  return s;
}  

void
clearHTable(Table ht)
{ int n;
  Symbol s;

  for(n=0; n < ht->buckets; n++)
  { s = ht->entries[n];
    while(s && !isTableRef(s))
    { Symbol q = s->next;
      freeHeap(s, sizeof(struct symbol));
      s = q;
    }
    ht->entries[n] = s;
  }

  ht->size = 0;
}