| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1988-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.IDENTIFICATION hash.c |
| 31 |
.AUTHOR Francois Ochsenbein [ESO-IPG] |
| 32 |
.LANGUAGE C |
| 33 |
.KEYWORDS Hash table |
| 34 |
.ENVIRONMENT Any |
| 35 |
.COMMENTS |
| 36 |
\begin{TeX} |
| 37 |
\end{TeX} |
| 38 |
.VERSION 1.0 18-Apr-1988: Creation |
| 39 |
|
| 40 |
090831 last modif |
| 41 |
-----------------------------------------------------------------------*/ |
| 42 |
|
| 43 |
#define DEBUG 1 /* For debugging only */ |
| 44 |
|
| 45 |
#define PM_LEVEL LEVEL_STR /* Only string-like functions */ |
| 46 |
|
| 47 |
#include <midas_def.h> |
| 48 |
#define PASCAL_DEF 0 /* Don't include pascalisation ... */ |
| 49 |
#include <stesodef.h> |
| 50 |
#include <hash.h> |
| 51 |
|
| 52 |
|
| 53 |
extern int pm_enter(), pm_ed_i(), pm_iexit(), pm_ed_tr2(); |
| 54 |
extern int mm_free(); |
| 55 |
|
| 56 |
|
| 57 |
#define FINISH goto FIN |
| 58 |
|
| 59 |
MID_STATIC int jndex; |
| 60 |
MID_STATIC H_ITEM *previous; |
| 61 |
MID_STATIC int factor = 2; /* seems to be the best factor... */ |
| 62 |
|
| 63 |
/*==========================================================================*/ |
| 64 |
int h_factor(n) |
| 65 |
/*+++ |
| 66 |
.PURPOSE Set factor for computation of index |
| 67 |
.RETURNS n |
| 68 |
.REMARKS To modify for better hashing function |
| 69 |
---*/ |
| 70 |
int n; /* IN: Factor */ |
| 71 |
{ |
| 72 |
factor = n; |
| 73 |
if (factor < 1) factor = 1; |
| 74 |
|
| 75 |
return(n); |
| 76 |
} |
| 77 |
|
| 78 |
/*==========================================================================*/ |
| 79 |
static int h_index(ht, symbol, len) |
| 80 |
/*+++ |
| 81 |
.PURPOSE Compute index from symbol |
| 82 |
.RETURNS The index (-1 for error) |
| 83 |
.REMARKS Simple addition of ASCII values. |
| 84 |
---*/ |
| 85 |
H_TABLE *ht; /* IN: Descriptor of Tex table */ |
| 86 |
char *symbol; /* IN: Symbol */ |
| 87 |
int len; /* IN: length of symbol */ |
| 88 |
{ |
| 89 |
register int i; |
| 90 |
register char *p; |
| 91 |
|
| 92 |
for (jndex = 0, p = symbol, i = len; --i >=0; p++) |
| 93 |
jndex = ((jndex * factor) + (*p & 077)) % ht->size; |
| 94 |
|
| 95 |
#if 0 |
| 96 |
for (jndex = 0, p = symbol, i = len; --i >=0; p++) |
| 97 |
jndex += (*p & 0177); |
| 98 |
jndex %= ht->size; |
| 99 |
#endif |
| 100 |
|
| 101 |
return(jndex); |
| 102 |
} |
| 103 |
|
| 104 |
/*==========================================================================*/ |
| 105 |
H_TABLE *h_create(size) |
| 106 |
/*+++ |
| 107 |
.PURPOSE Create a Hash table. |
| 108 |
.RETURNS Address of create table (NULL if failed) |
| 109 |
.REMARKS Size is adjusted to have no divisor smaller than 19. |
| 110 |
---*/ |
| 111 |
int size; /* IN: size (number of elements)*/ |
| 112 |
{ |
| 113 |
register H_TABLE *ht; |
| 114 |
register int l; |
| 115 |
|
| 116 |
ENTER("*h_create"); |
| 117 |
|
| 118 |
TRACE_ED_I("Proposed size is: ", size); |
| 119 |
|
| 120 |
/* Compute a number which has no divisor by 2,3,5,7,11,13,17,19 */ |
| 121 |
|
| 122 |
for ( l = (size & 1 ? size : size+1); ; l += 2) |
| 123 |
{ if ( (l% 3) == 0) continue; |
| 124 |
if ( (l% 5) == 0) continue; |
| 125 |
if ( (l% 7) == 0) continue; |
| 126 |
if ( (l%11) == 0) continue; |
| 127 |
if ( (l%13) == 0) continue; |
| 128 |
if ( (l%17) == 0) continue; |
| 129 |
if ( (l%19) == 0) continue; |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
TRACE_ED_I("Appropriate size is: ",l); |
| 134 |
|
| 135 |
ht = (H_TABLE *)mm_alloc(sizeof(H_TABLE) + (l-1)*sizeof(H_ITEM *)); |
| 136 |
if(ht) |
| 137 |
{ ht->size = l; |
| 138 |
ht->symbols = 0; |
| 139 |
ht->collisions = 0; |
| 140 |
while (--l >= 0) ht->start[l] = NULL_PTR(H_ITEM); |
| 141 |
} |
| 142 |
|
| 143 |
EXIT_PTR(H_TABLE, ht); |
| 144 |
} |
| 145 |
|
| 146 |
/*==========================================================================*/ |
| 147 |
int h_clear(ht) |
| 148 |
/*+++ |
| 149 |
.PURPOSE Clear a h-table |
| 150 |
.RETURNS OK |
| 151 |
.REMARKS |
| 152 |
---*/ |
| 153 |
H_TABLE *ht; /* IN: Descriptor of Hash table to clear */ |
| 154 |
{ |
| 155 |
register H_ITEM *pi, *pin; |
| 156 |
register int i; |
| 157 |
|
| 158 |
ENTER("h_clear"); |
| 159 |
|
| 160 |
for (i=0; i < ht->size; i++) |
| 161 |
{ for (pi = ht->start[i]; pi; pi = pin) |
| 162 |
pin = pi->next, MEM_FREE(pi); |
| 163 |
ht->start[i] = NULL_PTR(H_ITEM); |
| 164 |
} |
| 165 |
ht->collisions = 0; |
| 166 |
ht->symbols = 0; |
| 167 |
|
| 168 |
EXIT(OK); |
| 169 |
} |
| 170 |
|
| 171 |
/*==========================================================================*/ |
| 172 |
H_ITEM *h_look(ht, symbol, len) |
| 173 |
/*+++ |
| 174 |
.PURPOSE Look for a specified symbol in the table. |
| 175 |
.RETURNS Address of found item, or NULL |
| 176 |
.REMARKS Use hash-table |
| 177 |
---*/ |
| 178 |
H_TABLE *ht; /* IN: Descriptor of Hash table */ |
| 179 |
char *symbol; /* IN: symbol to retrieve */ |
| 180 |
int len; /* IN: Length of symbol */ |
| 181 |
{ |
| 182 |
register H_ITEM *pi; |
| 183 |
|
| 184 |
ENTER("*h_look"); |
| 185 |
|
| 186 |
#if DEBUG |
| 187 |
TRACE_ED_STR2("Looking for: ", symbol, len); |
| 188 |
#endif |
| 189 |
|
| 190 |
pi = ht->start[h_index(ht, symbol, len)]; /* Position */ |
| 191 |
for ( previous = NULL_PTR(H_ITEM); pi; previous = pi, pi = pi->next) |
| 192 |
{ if (pi->ls != len) continue; |
| 193 |
if (oscomp(symbol, &(pi->strings[0]), len) == 0) |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
EXIT_PTR(H_ITEM, pi); |
| 198 |
} |
| 199 |
|
| 200 |
/*==========================================================================*/ |
| 201 |
int h_remove(ht, symbol, len) |
| 202 |
/*+++ |
| 203 |
.PURPOSE Remove an item from table. |
| 204 |
.RETURNS OK / NOK (didn't exist) |
| 205 |
.REMARKS |
| 206 |
---*/ |
| 207 |
H_TABLE *ht; /* IN: Descriptor of Tex table */ |
| 208 |
char *symbol; /* IN: Symbol to remove */ |
| 209 |
int len; /* IN: Length of symbol */ |
| 210 |
{ |
| 211 |
register H_ITEM *pi; |
| 212 |
int st; |
| 213 |
|
| 214 |
ENTER("h_remove"); |
| 215 |
|
| 216 |
st = NOK; |
| 217 |
if_not(pi = h_look(ht, symbol, len)) FINISH; |
| 218 |
|
| 219 |
if (previous) /* Modify Links */ |
| 220 |
previous->next = pi->next; |
| 221 |
else ht->start[jndex] = pi->next; |
| 222 |
|
| 223 |
ht->symbols -= 1; |
| 224 |
if (ht->start[jndex]) ht->collisions -= 1; /* Update collisions */ |
| 225 |
st = OK; |
| 226 |
|
| 227 |
MEM_FREE(pi); /* Free Allocated Memory */ |
| 228 |
|
| 229 |
FIN: |
| 230 |
EXIT(st); |
| 231 |
} |
| 232 |
|
| 233 |
/*==========================================================================*/ |
| 234 |
H_ITEM *h_add(ht, symbol, ls, eq, leq) |
| 235 |
/*+++ |
| 236 |
.PURPOSE Insert the item (symboling, equivalence) in the table. |
| 237 |
.RETURNS Address of allocated item |
| 238 |
.REMARKS If symbol already exists, it's modified. |
| 239 |
---*/ |
| 240 |
H_TABLE *ht; /* IN: Descriptor of Tex table */ |
| 241 |
char *symbol; /* IN: Symbol to insert */ |
| 242 |
int ls; /* IN: Length of Symbol */ |
| 243 |
char *eq; /* IN: Equivalence string */ |
| 244 |
int leq; /* IN: Length of Equivalence String */ |
| 245 |
{ |
| 246 |
register H_ITEM *pi; |
| 247 |
register char *p; |
| 248 |
|
| 249 |
ENTER("*h_add"); |
| 250 |
|
| 251 |
#if DEBUG |
| 252 |
TRACE_ED_STR2("Insert: ", symbol, ls); |
| 253 |
TRACE_ED_STR2("Equate: ", eq, leq); |
| 254 |
#endif |
| 255 |
|
| 256 |
/* 1. Check if symbol already exists. |
| 257 |
If not, create it. */ |
| 258 |
|
| 259 |
pi = h_look(ht, symbol, ls); |
| 260 |
if(pi) |
| 261 |
{ if (pi->leq == leq) goto REPLACE; |
| 262 |
h_remove(ht, symbol, ls); |
| 263 |
pi = h_look(ht, symbol, ls); |
| 264 |
} |
| 265 |
|
| 266 |
/* 2. Allocate H_ITEM: strings are terminated with EOS */ |
| 267 |
|
| 268 |
if_not(pi = (H_ITEM *) mm_alloc(leq + ls + sizeof(H_ITEM))) |
| 269 |
FINISH; |
| 270 |
pi->next = NULL_PTR(H_ITEM); |
| 271 |
ht->symbols += 1; |
| 272 |
|
| 273 |
if (previous) |
| 274 |
previous->next = pi, ht->collisions += 1; |
| 275 |
else ht->start[jndex] = pi; |
| 276 |
|
| 277 |
|
| 278 |
/* 3. Fill H_ITEM, and copy strings */ |
| 279 |
|
| 280 |
REPLACE: |
| 281 |
pi->leq = leq; |
| 282 |
pi->ls = ls; |
| 283 |
p = &(pi->strings[0]); |
| 284 |
p += oscopy(p, symbol, ls); |
| 285 |
*(p++) = EOS; |
| 286 |
p += oscopy(p, eq, leq); |
| 287 |
*(p++) = EOS; |
| 288 |
|
| 289 |
FIN: |
| 290 |
EXIT_PTR(H_ITEM, pi); |
| 291 |
} |
| 292 |
|
| 293 |
/*==========================================================================*/ |
| 294 |
char *h_get(ht, symbol, len) |
| 295 |
/*+++ |
| 296 |
.PURPOSE Retrieve the string equated to a given symbol |
| 297 |
.RETURNS Address of equivalence string, or NULL |
| 298 |
.REMARKS |
| 299 |
---*/ |
| 300 |
H_TABLE *ht; /* IN: Descriptor of Tex table */ |
| 301 |
char *symbol; /* IN: symbol to retrieve */ |
| 302 |
int len; /* IN: Length of symbol */ |
| 303 |
{ |
| 304 |
register H_ITEM *pi; |
| 305 |
char *r; |
| 306 |
|
| 307 |
ENTER("*h_get"); |
| 308 |
|
| 309 |
#if DEBUG |
| 310 |
TRACE_ED_STR2("Looking for an equivalence to: ", symbol, len); |
| 311 |
#endif |
| 312 |
|
| 313 |
pi = h_look(ht, symbol, len); |
| 314 |
if(pi) |
| 315 |
r = &(pi->strings[1+pi->ls]); |
| 316 |
else r = NULL_PTR(char); |
| 317 |
|
| 318 |
#if DEBUG |
| 319 |
if(r) |
| 320 |
TRACE_ED_STR2("===================>", r, pi->leq); |
| 321 |
#endif |
| 322 |
|
| 323 |
EXITp(r); |
| 324 |
} |
| 325 |
|
| 326 |
/*==========================================================================*/ |
| 327 |
int h_log(ht) |
| 328 |
/*+++ |
| 329 |
.PURPOSE Log the statistics for a hash table. |
| 330 |
.RETURNS Number of free symbols. |
| 331 |
.REMARKS Not traced. |
| 332 |
---*/ |
| 333 |
H_TABLE *ht; /* IN: Descriptor of Tex table */ |
| 334 |
{ |
| 335 |
int n, i; |
| 336 |
|
| 337 |
for (n = 0, i = ht->size; --i >= 0; ) |
| 338 |
if_not(ht->start[i]) n++; /* Free entries */ |
| 339 |
|
| 340 |
LOG_ED_I("Size of h-table: ", ht->size); |
| 341 |
LOG_ED_I(" Unused entries: ", n); |
| 342 |
LOG_ED_I(" Total symbols: ", ht->symbols); |
| 343 |
LOG_ED_I(" Collisions: ", ht->collisions); |
| 344 |
|
| 345 |
return(n); |
| 346 |
} |
| 347 |
|