]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/CodeTools/TianoTools/Pccts/antlr/hash.c
6 * The following functions are visible:
8 * char *mystrdup(char *); Make space and copy string
9 * Entry **newHashTable(); Create and return initialized hash table
10 * Entry *hash_add(Entry **, char *, Entry *)
11 * Entry *hash_get(Entry **, char *)
15 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
16 * Set (PCCTS) -- PCCTS is in the public domain. An individual or
17 * company may do whatever they wish with source code distributed with
18 * PCCTS or the code generated by PCCTS, including the incorporation of
19 * PCCTS, or its output, into commerical software.
21 * We encourage users to develop software with PCCTS. However, we do ask
22 * that credit is given to us for developing PCCTS. By "credit",
23 * we mean that if you incorporate our source code into one of your
24 * programs (commercial product, research project, or otherwise) that you
25 * acknowledge this fact somewhere in the documentation, research report,
26 * etc... If you like PCCTS and have developed a nice tool with the
27 * output, please mention that you developed it using PCCTS. In
28 * addition, we ask that this header remain intact in our source code.
29 * As long as these guidelines are kept, we expect to continue enhancing
30 * this system and expect to make other tools available as they are
35 * Parr Research Corporation
36 * with Purdue University and AHPCRC, University of Minnesota
58 {fprintf(stderr, "%s(%d):", __FILE__, __LINE__); \
59 fprintf(stderr, " %s\n", err); exit(PCCTS_EXIT_FAILURE);}
60 #define require(expr, err) {if ( !(expr) ) fatal(err);}
62 static unsigned size
= HashTableSize
;
63 static char *strings
= NULL
;
65 static unsigned strsize
= StrTableSize
;
67 /* create the hash table and string table for terminals (string table only once) */
77 table
= (Entry
**) calloc(size
, sizeof(Entry
*));
78 require( table
!= NULL
, "cannot allocate hash table");
79 if ( strings
== NULL
)
81 strings
= (char *) calloc(strsize
, sizeof(char));
82 require( strings
!= NULL
, "cannot allocate string table");
90 killHashTable( Entry
**table
)
92 killHashTable( table
)
96 /* for now, just free table, forget entries */
97 free( (char *) table
); /* MR10 cast */
100 /* Given a table, add 'rec' with key 'key' (add to front of list). return ptr to entry */
103 hash_add( Entry
**table
, char *key
, Entry
*rec
)
105 hash_add( table
, key
, rec
)
113 require(table
!=NULL
&& key
!=NULL
&& rec
!=NULL
, "add: invalid addition");
116 rec
->next
= table
[h
]; /* Add to singly-linked list */
121 /* Return ptr to 1st entry found in table under key (return NULL if none found) */
124 hash_get( Entry
**table
, char *key
)
126 hash_get( table
, key
)
134 /* require(table!=NULL && key!=NULL, "get: invalid table and/or key");*/
135 if ( !(table
!=NULL
&& key
!=NULL
) ) *((char *) 34) = 3;
138 for (q
= table
[h
]; q
!= NULL
; q
= q
->next
)
140 if ( strcmp(key
, q
->str
) == StrSame
) return( q
);
148 hashStat( Entry
**table
)
154 static unsigned short count
[20];
155 int i
,n
=0,low
=0, hi
=0;
159 for (i
=0; i
<20; i
++) count
[i
] = 0;
160 for (p
=table
; p
<&(table
[size
]); p
++)
165 if ( q
!= NULL
&& low
==0 ) low
= p
-table
;
167 if ( q
!= NULL
) fprintf(stderr
, "[%d]", p
-table
);
172 fprintf(stderr
, " %s", q
->str
);
174 if ( q
== NULL
) fprintf(stderr
, "\n");
177 if ( *p
!= NULL
) hi
= p
-table
;
180 fprintf(stderr
, "Storing %d recs used %d hash positions out of %d\n",
181 n
, size
-count
[0], size
);
182 fprintf(stderr
, "%f %% utilization\n",
183 ((float)(size
-count
[0]))/((float)size
));
188 avg
+= (((float)(i
*count
[i
]))/((float)n
)) * i
;
189 fprintf(stderr
, "Bucket len %d == %d (%f %% of recs)\n",
190 i
, count
[i
], ((float)(i
*count
[i
]))/((float)n
));
193 fprintf(stderr
, "Avg bucket length %f\n", avg
);
194 fprintf(stderr
, "Range of hash function: %d..%d\n", low
, hi
);
198 /* Add a string to the string table and return a pointer to it.
199 * Bump the pointer into the string table to next avail position.
210 require(s
!=NULL
, "mystrdup: NULL string");
214 require( strp
<= &(strings
[strsize
-2]),
215 "string table overflow\nIncrease StrTableSize in hash.h and recompile hash.c\n");