1 /*********************************************************************/
2 /* Copyright (C) 2000, HELM Team */
4 /* This file is part of HELM, an Hypertextual, Electronic */
5 /* Library of Mathematics, developed at the Computer Science */
6 /* Department, University of Bologna, Italy. */
8 /* HELM is free software; you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation; either version 2 */
11 /* of the License, or (at your option) any later version. */
13 /* HELM is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU General Public License for more details. */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with HELM; if not, write to the Free Software */
20 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, */
21 /* MA 02111-1307, USA. */
23 /* For details, see the HELM World-Wide-Web page, */
24 /* http://cs.unibo.it/helm/. */
25 /*********************************************************************/
27 /****************************************************************/
29 /****************************************************************/
30 /* This module supplies routines for symbol table handling. */
31 /* - init_symbol_table(): it initializes the symbol table */
33 /* - search_bucket(): it searches the symbol table for the */
34 /* bucket containing a given identifier, and */
35 /* inserts it if it is not present; */
36 /****************************************************************/
37 /* First draft 11/12/2001, by Andrea Asperti */
38 /****************************************************************/
40 /****************************************************************/
41 /* 1. Inclusion of header files. */
42 /****************************************************************/
47 /****************************************************************/
49 /****************************************************************/
54 #define HASH2 0xf0000000
66 /****************************************************************/
68 /****************************************************************/
73 struct st_bucket *next_st_bucket;
74 /* next bucket in the list */
75 struct st_bucket *all_next;
76 /* all buckets in symbol
77 table are linked together */
82 struct st_bucket *dictionary[DICTSIZE];
83 /* pointers to bucket lists */
85 /****************************************************************/
86 /* 4. Definitions of functions to be exported. */
87 /****************************************************************/
89 struct st_bucket *all;
91 /* The following function initializes the symbol table to NULL */
92 void init_symbol_table()
97 /* initialize the dictionary */
98 for (i = 0; i < DICTSIZE; i++)
103 /* The following function searches the symbol table for an identifier */
104 /* and inserts it if it is not present.
105 /* The bucket associated with the given identifier */
106 /* becomes the first one in its list. */
108 search_bucket(id, where)
114 /* value returned by the */
120 struct st_bucket *st;
122 /* apply the hash function */
123 dict_index = hash_pjw(id);
124 /* printf( "%d\n", dict_index); */
126 /* scan the bucket list indicated by the hash function */
127 prev = curr = dictionary[dict_index];
128 while ((curr != NULL) && (strcmp(id, curr->id)))
131 curr = curr->next_st_bucket;
134 /* the identifier is not in the list */
136 allocate_bucket(&st,id,where);
137 move_bucket(st,dict_index);
141 /* printf("uno=%s\n", id);
142 printf("st=%s\n", curr->id); */
144 /* the identifier is already in the list */
147 curr->pos[where] = 1;
149 curr->pos[0] = 0; /* it will never be set again to 1 */
151 /* the identifier is not in the first position */
153 prev->next_st_bucket = curr->next_st_bucket;
164 struct st_bucket *curr;
169 for (i = 0; i < 5; ++i)
170 if (curr->pos[i] == 1)
171 print_one(curr->id,i);
172 curr = curr->all_next;
177 /****************************************************************/
178 /* 5. Definitions of functions local to the module. */
179 /****************************************************************/
185 printf("<h:refObj>\n");
186 printf("<h:Occurrence rdf:about=\"http://www.cs.unibo.it/helm/schemas/schema-h.rdf#");
189 else if (pos == MAINHYP)
190 printf("MainHypothesis");
191 else if (pos == INHYP)
192 printf("InHypothesis");
193 else if (pos == INCONCL)
194 printf("InConclusion");
195 else if (pos == MAINCONCL)
196 printf("MainConclusion");
197 printf("\" rdf:value=\"");
200 printf("</h:refObj>\n");
203 /* The following function allocates a bucket for an identifier. */
204 allocate_bucket(st, id, where)
207 /* pointer to the bucket to be */
215 *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
216 (*st)->id = (char *)malloc(sizeof('a')*strlen(id));
217 strcpy((*st)->id,id);
218 (*st)->next_st_bucket = NULL;
219 (*st)->all_next = all;
221 for (i = 0; i < 5; ++i)
223 (*st)->pos[where] = 1;
226 /* The following function moves a bucket to the head of the */
227 /* list in which it lies. */
228 move_bucket(st, dict_index)
231 /* pointer to the bucket to */
234 /* index corresponding to */
235 /* the list in which the */
238 st->next_st_bucket = dictionary[dict_index];
239 dictionary[dict_index] = st;
242 /* The following function implements Weinberger's hash function. */
246 /* identifier to be hashed */
251 for (h = 0; *id != EOS; id++)
253 h = (h << HASH1) + (*id);
255 h = h ^ (g >> HASH3) ^ g;
257 return(h % DICTSIZE);