10#define NEW(p) ((p) = (void *) getmem((long)sizeof *(p)))
11#define FREE(ptr) ((void)(freemem((ptr)), (ptr) = 0))
32 unsigned (*
hash)(
const void *key);
48static int cmpatom(
const void *
x,
const void *y) {
return x != y; }
49static unsigned hashatom(
const void *key) {
return (
unsigned long)key >> 2; }
51 int cmp(
const void *
x,
const void *y),
52 unsigned hash(
const void *key))
56 static int primes[] = {
57 509, 509, 1021, 2053, 4093, 8191, 16381, 32771, 65521, INT_MAX};
59 for (i = 1; primes[i] < hint; i++)
62 (
T *)
getmem(
sizeof(*table) + primes[i - 1] *
sizeof(table->
buckets[0]));
63 table->
size = primes[i - 1];
66 table->
buckets = (
struct binding **)(table + 1);
67 for (i = 0; i < table->
size; i++) table->
buckets[i] = NULL;
78 i = (*table->
hash)(key) % table->
size;
80 if ((*table->
cmp)(key,
p->key) == 0)
break;
81 return p ?
p->value : NULL;
90 i = (*table->
hash)(key) % table->
size;
92 if ((*table->
cmp)(key,
p->key) == 0)
break;
114 void apply(
const void *key,
void **value,
void *cl),
127 for (i = 0; i < table->
size; i++)
130 apply(
p->key, &
p->value, cl);
141 i = (*table->
hash)(key) % table->
size;
142 for (pp = &table->
buckets[i]; *pp; pp = &(*pp)->
link)
143 if ((*table->
cmp)(key, (*pp)->key) == 0)
145 struct binding *
p = *pp;
146 void *value =
p->value;
160 array = (
const void **)
getmem((2 * table->
length + 1) *
sizeof(*array));
161 for (i = 0; i < table->
size; i++)
164 array[j++] = (
const void *)
p->key;
165 array[j++] =
p->value;
172 assert(table && *table);
173 if ((*table)->length > 0)
176 struct binding *
p, *q;
177 for (i = 0; i < (*table)->size; i++)
178 for (
p = (*table)->buckets[i];
p;
p = q)
Engine-wide GC allocator surface (getmem / getmem_atomic) and debug-allocation trap.
TermIterator< Nterm > end(Nterm *)
Singly linked-list node holding one (key, value) pair in a hash bucket chain.
unsigned(* hash)(const void *key)
int(* cmp)(const void *x, const void *y)
struct T::binding ** buckets
int Table_length(T *table)
void Table_free(T **table)
static int cmpatom(const void *x, const void *y)
const void ** Table_toArray(T *table, void *end)
void * Table_get(T *table, const void *key)
static unsigned hashatom(const void *key)
void * Table_put(T *table, const void *key, void *value)
void * Table_remove(T *table, const void *key)
T * Table_new(int hint, int cmp(const void *x, const void *y), unsigned hash(const void *key))
void Table_map(T *table, void apply(const void *key, void **value, void *cl), void *cl)