]>
Commit | Line | Data |
---|---|---|
aa32d841 JL |
1 | /* Header file for generic hash table support. |
2 | Copyright (C) 1993, 94 Free Software Foundation, Inc. | |
3 | Written by Steve Chamberlain <sac@cygnus.com> | |
4 | ||
5 | This file was lifted from BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #ifdef IN_GCC | |
22 | ||
23 | /* Add prototype support. */ | |
24 | #ifndef PROTO | |
25 | #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__) | |
26 | #define PROTO(ARGS) ARGS | |
27 | #else | |
28 | #define PROTO(ARGS) () | |
29 | #endif | |
30 | #endif | |
31 | ||
32 | #define PARAMS(ARGS) PROTO(ARGS) | |
33 | ||
34 | #ifdef __STDC__ | |
35 | #define PTR void * | |
36 | #else | |
37 | #ifndef const | |
38 | #define const | |
39 | #endif | |
40 | #define PTR char * | |
41 | #endif | |
42 | ||
43 | #else /* ! IN_GCC */ | |
44 | #include <ansidecl.h> | |
45 | #endif /* IN_GCC */ | |
46 | ||
47 | #include "obstack.h" | |
48 | ||
49 | typedef enum {false, true} boolean; | |
50 | ||
51 | /* Hash table routines. There is no way to free up a hash table. */ | |
52 | ||
53 | /* An element in the hash table. Most uses will actually use a larger | |
54 | structure, and an instance of this will be the first field. */ | |
55 | ||
56 | struct hash_entry | |
57 | { | |
58 | /* Next entry for this hash code. */ | |
59 | struct hash_entry *next; | |
60 | /* String being hashed. */ | |
61 | const char *string; | |
62 | /* Hash code. This is the full hash code, not the index into the | |
63 | table. */ | |
64 | unsigned long hash; | |
65 | }; | |
66 | ||
67 | /* A hash table. */ | |
68 | ||
69 | struct hash_table | |
70 | { | |
71 | /* The hash array. */ | |
72 | struct hash_entry **table; | |
73 | /* The number of slots in the hash table. */ | |
74 | unsigned int size; | |
75 | /* A function used to create new elements in the hash table. The | |
76 | first entry is itself a pointer to an element. When this | |
77 | function is first invoked, this pointer will be NULL. However, | |
78 | having the pointer permits a hierarchy of method functions to be | |
79 | built each of which calls the function in the superclass. Thus | |
80 | each function should be written to allocate a new block of memory | |
81 | only if the argument is NULL. */ | |
82 | struct hash_entry *(*newfunc) PARAMS ((struct hash_entry *, | |
83 | struct hash_table *, | |
84 | const char *)); | |
85 | /* An obstack for this hash table. */ | |
86 | struct obstack memory; | |
87 | }; | |
88 | ||
89 | /* Initialize a hash table. */ | |
90 | extern boolean hash_table_init | |
91 | PARAMS ((struct hash_table *, | |
92 | struct hash_entry *(*) (struct hash_entry *, | |
93 | struct hash_table *, | |
94 | const char *))); | |
95 | ||
96 | /* Initialize a hash table specifying a size. */ | |
97 | extern boolean hash_table_init_n | |
98 | PARAMS ((struct hash_table *, | |
99 | struct hash_entry *(*) (struct hash_entry *, | |
100 | struct hash_table *, | |
101 | const char *), | |
102 | unsigned int size)); | |
103 | ||
104 | /* Free up a hash table. */ | |
105 | extern void hash_table_free PARAMS ((struct hash_table *)); | |
106 | ||
107 | /* Look up a string in a hash table. If CREATE is true, a new entry | |
108 | will be created for this string if one does not already exist. The | |
109 | COPY argument must be true if this routine should copy the string | |
110 | into newly allocated memory when adding an entry. */ | |
111 | extern struct hash_entry *hash_lookup | |
112 | PARAMS ((struct hash_table *, const char *, boolean create, | |
113 | boolean copy)); | |
114 | ||
115 | /* Base method for creating a hash table entry. */ | |
116 | extern struct hash_entry *hash_newfunc | |
117 | PARAMS ((struct hash_entry *, struct hash_table *, | |
118 | const char *)); | |
119 | ||
120 | /* Grab some space for a hash table entry. */ | |
121 | extern PTR hash_allocate PARAMS ((struct hash_table *, | |
122 | unsigned int)); | |
123 | ||
124 | /* Traverse a hash table in a random order, calling a function on each | |
125 | element. If the function returns false, the traversal stops. The | |
126 | INFO argument is passed to the function. */ | |
127 | extern void hash_traverse PARAMS ((struct hash_table *, | |
128 | boolean (*) (struct hash_entry *, | |
129 | PTR), | |
130 | PTR info)); |