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 /****************************************************************/
72 struct int_list *next;
79 struct int_list *depths;
80 struct st_bucket *next_st_bucket;
81 /* next bucket in the list */
82 struct st_bucket *all_next;
83 /* all buckets in symbol
84 table are linked together */
89 struct st_bucket *dictionary[DICTSIZE];
90 /* pointers to bucket lists */
92 /****************************************************************/
93 /* 4. Local functions. */
94 /****************************************************************/
95 struct int_list *add(struct int_list *,int);
96 int hash_pjw(char *id);
98 /****************************************************************/
99 /* 5. Definitions of functions to be exported. */
100 /****************************************************************/
102 struct st_bucket *all;
104 /* The following function initializes the symbol table to NULL */
105 void init_symbol_table()
107 struct st_bucket *st;
110 /* initialize the dictionary */
111 for (i = 0; i < DICTSIZE; i++)
112 dictionary[i] = NULL;
116 /* The following function searches the symbol table for an identifier */
117 /* and inserts it if it is not present.
118 /* The bucket associated with the given identifier */
119 /* becomes the first one in its list. */
121 search_bucket(id, where, depth)
128 /* value returned by the */
134 struct st_bucket *st;
136 /* apply the hash function */
137 dict_index = hash_pjw(id);
138 /* fprintf(stderr,"%d\n", dict_index); fflush(stdout); */
140 /* scan the bucket list indicated by the hash function */
141 prev = curr = dictionary[dict_index];
142 while ((curr != NULL) && (strcmp(id, curr->id)))
145 curr = curr->next_st_bucket;
148 /* the identifier is not in the list */
150 allocate_bucket(&st,id,where);
151 if (where == MAINCONCL)
152 st->main_depth = depth;
153 else if (where == MAINHYP)
154 st->depths = add(st->depths,depth);
155 move_bucket(st,dict_index);
160 fprintf(stderr,"uno=%s\n", id);
161 fprintf(stderr,"st=%s\n", curr->id); fflush(stdout) */
163 /* the identifier is already in the list */
166 curr->pos[where] = 1;
168 curr->pos[INBODY] = 0; /* it will never be set again to 1 */
169 if (where == MAINHYP)
170 curr->depths=add(curr->depths,depth);
171 else if (where == MAINCONCL)
172 curr->main_depth = depth;
174 /* the identifier is not in the first position */
176 prev->next_st_bucket = curr->next_st_bucket;
184 print_all(about,out,outrel,outsort)
192 struct st_bucket *curr;
196 for (i = 0; i < 5; ++i)
197 if ((curr->pos[i]) == 1)
200 print_mainhyp(about,out,outrel,outsort,curr->id,curr->depths);
201 else if (i == MAINCONCL)
202 print_mainconcl(about,out,outrel,outsort,curr->id,curr->main_depth);
204 print_one(out,curr->id,i);
206 curr = curr->all_next;
211 /****************************************************************/
212 /* 5. Definitions of functions local to the module. */
213 /****************************************************************/
215 struct int_list *add(l,m)
219 struct int_list *curr;
220 /* scan the list looking for m */
222 while ((curr != NULL) && (m != (curr->val)))
225 /* m is not in the list */
227 curr = (struct int_list *)malloc(sizeof(struct int_list));
237 print_mainhyp(about,out,outrel,outsort,uri,l)
245 struct int_list *curr;
247 if (!strcmp(uri,"Rel"))
252 fprintf(outrel,"\t<h:Object rdf:about=\"");
253 fprintf(outrel,"%s",about);
254 fprintf(outrel,"\">\n");
255 fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
256 fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
257 fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
258 fprintf(outrel,"\n\t\t</h:refRel>\n");
259 fprintf(outrel,"\t</h:Object>\n");
263 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
264 (!strcmp(uri,"Set")))
269 fprintf(outsort,"\t<h:Object rdf:about=\"");
270 fprintf(outsort,"%s",about);
271 fprintf(outsort,"\">\n");
272 fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
273 fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
274 fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
275 fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
276 fprintf(outsort,"\n\t\t</h:refSort>\n");
277 fprintf(outsort,"\t</h:Object>\n");
286 fprintf(out,"\t\t<h:refObj rdf:parseType=\"Resource\">");
287 fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
288 fprintf(out,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
289 fprintf(out,"\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
290 fprintf(out,"\n\t\t</h:refObj>\n");
296 print_mainconcl(about,out,outrel,outsort,uri,depth)
305 if (!strcmp(uri,"Rel"))
307 fprintf(outrel,"\t<h:Object rdf:about=\"");
308 fprintf(outrel,"%s",about);
309 fprintf(outrel,"\">\n");
310 fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
311 fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
312 fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
313 fprintf(outrel,"\n\t\t</h:refRel>\n");
314 fprintf(outrel,"\t</h:Object>\n");
316 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
317 (!strcmp(uri,"Set")))
319 fprintf(outsort,"\t<h:Object rdf:about=\"");
320 fprintf(outsort,"%s",about);
321 fprintf(outsort,"\">\n");
322 fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
323 fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
324 fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
325 fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
326 fprintf(outsort,"\n\t\t</h:refSort>\n");
327 fprintf(outsort,"\t</h:Object>\n");
331 fprintf(out,"\t\t<h:refObj rdf:parseType=\"Resource\">");
332 fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
333 fprintf(out,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
334 fprintf(out,"\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
335 fprintf(out,"\n\t\t</h:refObj>\n");
339 // dome: cambiata per usare il modello con position
340 print_one(out,uri,pos)
345 fprintf(out,"\t\t<h:refObj df:parseType=\"Resource\">");
346 fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;");
348 fprintf(out,"InBody");
349 else if (pos == MAINHYP)
350 fprintf(out,"MainHypothesis");
351 else if (pos == INHYP)
352 fprintf(out,"InHypothesis");
353 else if (pos == INCONCL)
354 fprintf(out,"InConclusion");
355 else if (pos == MAINCONCL)
356 fprintf(out,"MainConclusion");
357 fprintf(out,"\"/>\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>\n\t\t</h:refObj>\n", uri);
361 /* The following function allocates a bucket for an identifier. */
362 allocate_bucket(st, id, where)
365 /* pointer to the bucket to be */
373 *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
374 (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
375 strcpy((*st)->id,id);
376 (*st)->main_depth = 0;
377 (*st)->depths = NULL;
378 (*st)->next_st_bucket = NULL;
379 (*st)->all_next = all;
381 for (i = 0; i < 5; ++i)
383 (*st)->pos[where] = 1;
386 /* The following function moves a bucket to the head of the */
387 /* list in which it lies. */
388 move_bucket(st, dict_index)
391 /* pointer to the bucket to */
394 /* index corresponding to */
395 /* the list in which the */
398 st->next_st_bucket = dictionary[dict_index];
399 dictionary[dict_index] = st;
402 /* The following function implements Weinberger's hash function. */
406 /* identifier to be hashed */
411 for (h = 0; *id != EOS; id++)
413 h = (h << HASH1) + (*id);
415 h = h ^ (g >> HASH3) ^ g;
417 return(h % DICTSIZE);