-
Notifications
You must be signed in to change notification settings - Fork 3
/
prhash.c
172 lines (142 loc) · 3.59 KB
/
prhash.c
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
/* prhash.c */
/* symbol table handling */
#include <stdio.h>
#include <string.h>
#include "prmachine.h"
#include "prtypes.h"
#define HASHSIZE 103 /* a prime */
#define HASHNULL ((atom_ptr_t) NULL)
static atom_ptr_t Hashtable[HASHSIZE];
atom_ptr_t intern(), make_atom(); /* forward declaration */
/**********************************************************************
hash()
Find the index in the hash table of the bucket in which to store
atom corresponding to the string s;
**********************************************************************/
int hash(s)
char *s;
{
int ret = 0;
while(*s)ret += *s++;
return(ret % HASHSIZE);
}
/**********************************************************************
ini_hash()
Initialise the hash table.
Call this before any call on the functions below.
**********************************************************************/
void ini_hash()
{
register int i;
for(i = 0; i < HASHSIZE; i++)
Hashtable[i] = HASHNULL;
}
/******************************************************************************
hash_search()
See if a string is in the hash table and return the pointer to
the atom if it is and NULL otherwise.
******************************************************************************/
atom_ptr_t hash_search(s)
char *s;
{
int hashval;
atom_ptr_t the_el;
hashval = hash(s);
the_el = Hashtable[hashval];
if (the_el == HASHNULL)
{
return(HASHNULL);
}
else
{
while(the_el != HASHNULL && strcmp(s, the_el->name))
the_el = the_el->hash_link;
}
if(the_el == HASHNULL)
return(HASHNULL);
else
return(the_el);
}
/**********************************************************************
intern()
Add a new atom to the symbol table and return it.
**********************************************************************/
atom_ptr_t intern(char *s)
{
int hashval;
int status = PERMANENT;
atom_ptr_t the_el, previous = NULL;
ENTER("intern");
hashval = hash(s);
the_el = Hashtable[hashval];
if (the_el == HASHNULL)
{
the_el = make_atom(s, status);
Hashtable[hashval] = the_el;
return(the_el);
}
else
{
while(the_el!= HASHNULL && strcmp(s, the_el->name))
{
previous = the_el;
the_el = the_el->hash_link;
}
if(the_el == HASHNULL)
{
the_el = make_atom(s, status);
previous->hash_link = the_el;
// BUGHUNT(s);
return(the_el);
}
else
{
return(the_el);
}
}
}
/**********************************************************************
make_atom()
Makes an atom from a string. Does not insert it into hash table.
**********************************************************************/
atom_ptr_t make_atom(char* s,int status)
{
string_ptr_t get_string();
atom_ptr_t ret, get_atom();
char *s2;
ret = get_atom(status);
if(ret == HASHNULL)return(HASHNULL);
if(status == PERMANENT) status = PERM_STRING;
s2 = get_string((my_alloc_size_t)(strlen(s) + 1), status); /* alloue pour chaine et caractere '\0' */
if(ret == NULL)return(HASHNULL);
strcpy(s2, s);
ret->hash_link = HASHNULL;
ret->name = s2;
(ret->proc).clause = NULL;
// BUGHUNT(s);
return(ret);
}
#ifndef NDEBUG
void pr_bucket(atom_ptr_t a)
{
while(a != HASHNULL)
{
tty_pr_string(ATOMPTR_NAME(a));
tty_pr_string("\n");
a = a->hash_link;
}
}
void pr_hash_table()
{
int i;
for( i = 0; i < HASHSIZE; i++)
{
if(Hashtable[i] == NULL)
continue;
pr_bucket(Hashtable[i]);
more_y_n();
}
tty_pr_string("End of Hash Table!");
}
#endif
/* end of file */