]> matita.cs.unibo.it Git - helm.git/blob - helm/metadata/create4/METADATA/sthandler.c
Initial revision
[helm.git] / helm / metadata / create4 / METADATA / sthandler.c
1 /*********************************************************************/
2 /*  Copyright (C) 2000, HELM Team                                    */ 
3 /*                                                                   */
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.                         */
7 /*                                                                   */
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.            */
12 /*                                                                   */
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.                      */
17 /*                                                                   */
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.                                              */
22 /*                                                                   */
23 /* For details, see the HELM World-Wide-Web page,                    */
24 /* http://cs.unibo.it/helm/.                                         */
25  /*********************************************************************/
26
27 /****************************************************************/
28 /*                        STHANDLER.C                           */
29 /****************************************************************/
30 /* This module supplies routines for symbol table handling.     */
31 /* - init_symbol_table(): it initializes the symbol table       */
32 /*                        to void.                              */
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 /****************************************************************/
39
40 /****************************************************************/
41 /* 1. Inclusion of header files.                                */
42 /****************************************************************/
43
44 #include                <stdio.h>
45 #include                <malloc.h>
46
47 /****************************************************************/
48 /* 2. Declarations                                              */
49 /****************************************************************/
50
51
52 #define         DICTSIZE                        211
53 #define         HASH1                           4
54 #define         HASH2                           0xf0000000
55 #define         HASH3                           24
56 #define         EOS                             '\0'
57
58 #define                 INBODY    0
59 #define                 MAINHYP   1
60 #define                 INHYP     2
61 #define                 INCONCL   3
62 #define                 MAINCONCL 4
63 #define                 INTYPE    5
64 #define                 NOTFOUND  6
65
66 /****************************************************************/
67 /* 3. Types.                                                    */
68 /****************************************************************/
69
70 struct st_bucket {
71                 char                    *id;
72                                                 /* identifier */
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 */
78                 int                     pos[5];
79
80                   };                              
81
82 struct st_bucket    *dictionary[DICTSIZE];
83                                /* pointers to bucket lists */
84
85 /****************************************************************/
86 /* 4. Definitions of functions to be exported.                  */
87 /****************************************************************/
88
89 struct st_bucket        *all;
90
91  /* The following function initializes the symbol table to NULL */
92 void init_symbol_table()
93 {
94         struct st_bucket        *st;
95         int                     i;
96
97         /* initialize the dictionary */
98         for (i = 0; i < DICTSIZE; i++)
99                 dictionary[i] = NULL;
100         all = NULL;
101 }
102
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. */
107
108 search_bucket(id, where)
109         char            *id;
110                                         /* identifier */
111         int             where;
112 {
113         int             dict_index;
114                                         /* value returned by the */
115                                         /* hash function */
116         struct st_bucket
117                         *prev,
118                         *curr;
119
120         struct st_bucket *st;
121
122         /* apply the hash function */
123         dict_index = hash_pjw(id);
124         /* printf( "%d\n", dict_index); */
125         
126         /* scan the bucket list indicated by the hash function */
127         prev = curr = dictionary[dict_index];
128         while ((curr != NULL) && (strcmp(id, curr->id)))
129           {
130             prev = curr;
131             curr = curr->next_st_bucket;
132           }
133         if (curr == NULL)
134           /* the identifier is not in the list */
135           {
136             allocate_bucket(&st,id,where);
137             move_bucket(st,dict_index);
138             return NOTFOUND;
139           }
140         else
141           /* printf("uno=%s\n", id);
142              printf("st=%s\n", curr->id); */
143
144           /* the identifier is already in the list */
145           {
146             /* st = curr; */
147             curr->pos[where] = 1;
148             if (where >= 1) 
149               curr->pos[0] = 0; /* it will never be set again to 1 */
150             if (prev != curr)
151               /* the identifier is not in the first position */
152               {
153                 prev->next_st_bucket = curr->next_st_bucket;
154                 move_bucket(curr,
155                             dict_index);
156               };
157             return where;
158           }
159 }
160
161 print_all()
162 {
163         int i;
164         struct st_bucket *curr;
165         curr = all;
166
167         while (curr != NULL)
168           {
169             for (i = 0; i < 5; ++i)
170               if (curr->pos[i] == 1)
171                print_one(curr->id,i);
172             curr = curr->all_next;
173           }
174 }
175
176
177 /****************************************************************/
178 /* 5. Definitions of functions local to the module.             */
179 /****************************************************************/
180
181 print_one(uri,pos)
182      char    *uri;
183      int     pos;
184 {
185     printf("<h:refObj>\n");
186     printf("<h:Occurrence rdf:about=\"http://www.cs.unibo.it/helm/schemas/schema-h.rdf#");
187     if (pos == INBODY)
188        printf("InBody");
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=\"");
198     printf("%s", uri);
199     printf("\"/>\n");
200     printf("</h:refObj>\n");
201 }
202
203  /* The following function allocates a bucket for an identifier. */
204 allocate_bucket(st, id, where)
205         struct st_bucket
206                         **st;
207                                         /* pointer to the bucket to be */
208                                         /* allocated */
209         char            *id;
210                                         /* identifier */
211         int             where;
212 {
213         int i;
214
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;
220         all = *st;
221         for (i = 0; i < 5; ++i)
222           (*st)->pos[i] = 0;
223         (*st)->pos[where] = 1;
224 }
225
226  /* The following function moves a bucket to the head of the */
227  /* list in which it lies. */
228 move_bucket(st, dict_index)
229         struct st_bucket 
230                         *st;
231                                         /* pointer to the bucket to */
232                                         /* be moved */
233         int             dict_index;
234                                         /* index corresponding to */
235                                         /* the list in which the */
236                                         /* bucket lies */
237 {
238         st->next_st_bucket = dictionary[dict_index];
239         dictionary[dict_index] = st;
240 }
241
242  /* The following function implements Weinberger's hash function. */
243 int
244 hash_pjw(id)
245         char            *id;
246                                         /* identifier to be hashed */
247 {
248         unsigned        h,
249                         g;
250
251         for (h = 0; *id != EOS; id++)
252         {
253                 h = (h << HASH1) + (*id);
254                 if (g = h & HASH2)
255                         h = h ^ (g >> HASH3) ^ g;
256         }
257         return(h % DICTSIZE);
258 }
259
260
261
262
263