]> matita.cs.unibo.it Git - helm.git/blob - helm/metadata/create_V7_mowgli/METADATA/sthandler.c
added ocaml-http 0.0.1
[helm.git] / helm / metadata / create_V7_mowgli / 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[INBODY] = 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
182 // dome: cambiata per usare il modello con position
183 print_one(uri,pos)
184      char    *uri;
185      int     pos;
186 {
187     printf("\t\t<h:refObj>\n\t\t\t<h:Occurrence>\n\t\t\t\t<h:position>");
188     if (pos == INBODY)
189        printf("InBody");
190     else if (pos == MAINHYP)
191        printf("MainHypothesis");
192     else if (pos == INHYP)
193        printf("InHypothesis");
194     else if (pos == INCONCL)
195        printf("InConclusion");
196     else if (pos == MAINCONCL)
197        printf("MainConclusion");
198     printf("</h:position>\n\t\t\t\t<h:occurrence>%s</h:occurrence>\n\t\t\t</h:Occurrence>\n\t\t</h:refObj>\n", uri);
199 }
200
201  /* The following function allocates a bucket for an identifier. */
202 allocate_bucket(st, id, where)
203         struct st_bucket
204                         **st;
205                                         /* pointer to the bucket to be */
206                                         /* allocated */
207         char            *id;
208                                         /* identifier */
209         int             where;
210 {
211         int i;
212
213         *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
214         (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
215         strcpy((*st)->id,id);
216         (*st)->next_st_bucket = NULL;
217         (*st)->all_next = all;
218         all = *st;
219         for (i = 0; i < 5; ++i)
220           (*st)->pos[i] = 0;
221         (*st)->pos[where] = 1;
222 }
223
224  /* The following function moves a bucket to the head of the */
225  /* list in which it lies. */
226 move_bucket(st, dict_index)
227         struct st_bucket 
228                         *st;
229                                         /* pointer to the bucket to */
230                                         /* be moved */
231         int             dict_index;
232                                         /* index corresponding to */
233                                         /* the list in which the */
234                                         /* bucket lies */
235 {
236         st->next_st_bucket = dictionary[dict_index];
237         dictionary[dict_index] = st;
238 }
239
240  /* The following function implements Weinberger's hash function. */
241 int
242 hash_pjw(id)
243         char            *id;
244                                         /* identifier to be hashed */
245 {
246         unsigned        h,
247                         g;
248
249         for (h = 0; *id != EOS; id++)
250         {
251                 h = (h << HASH1) + (*id);
252                 if (g = h & HASH2)
253                         h = h ^ (g >> HASH3) ^ g;
254         }
255         return(h % DICTSIZE);
256 }
257
258
259
260
261