1 /* Copyright (C) 1995, 2000-2003, 2005-2006 Free Software Foundation, Inc.
2 
3    This program is free software: you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 3 of the License, or
6    (at your option) any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
15 
16 #ifndef _HASH_H
17 #define _HASH_H
18 
19 #include "obstack.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 struct hash_entry;
26 
27 typedef struct hash_table
28 {
29   unsigned long int size;   /* Number of allocated entries.  */
30   unsigned long int filled; /* Number of used entries.  */
31   struct hash_entry *first; /* Pointer to head of list of entries.  */
32   struct hash_entry *table; /* Pointer to array of entries.  */
33   struct obstack mem_pool;  /* Memory pool holding the keys.  */
34 }
35 hash_table;
36 
37 /* Initialize a hash table.  INIT_SIZE > 1 is the initial number of available
38    entries.
39    Return 0 upon successful completion, -1 upon memory allocation error.  */
40 extern int hash_init (hash_table *htab, unsigned long int init_size);
41 
42 /* Delete a hash table's contents.
43    Return 0 always.  */
44 extern int hash_destroy (hash_table *htab);
45 
46 /* Look up the value of a key in the given table.
47    If found, return 0 and set *RESULT to it.  Otherwise return -1.  */
48 extern int hash_find_entry (hash_table *htab,
49                             const void *key, size_t keylen,
50                             void **result);
51 
52 /* Try to insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table.
53    Return non-NULL (more precisely, the address of the KEY inside the table's
54    memory pool) if successful, or NULL if there is already an entry with the
55    given key.  */
56 extern const void * hash_insert_entry (hash_table *htab,
57                                        const void *key, size_t keylen,
58                                        void *data);
59 
60 /* Insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table.
61    Return 0.  */
62 extern int hash_set_value (hash_table *htab,
63                            const void *key, size_t keylen,
64                            void *data);
65 
66 /* Steps *PTR forward to the next used entry in the given hash table.  *PTR
67    should be initially set to NULL.  Store information about the next entry
68    in *KEY, *KEYLEN, *DATA.
69    Return 0 normally, -1 when the whole hash table has been traversed.  */
70 extern int hash_iterate (hash_table *htab, void **ptr,
71                          const void **key, size_t *keylen,
72                          void **data);
73 
74 /* Steps *PTR forward to the next used entry in the given hash table.  *PTR
75    should be initially set to NULL.  Store information about the next entry
76    in *KEY, *KEYLEN, *DATAP.  *DATAP is set to point to the storage of the
77    value; modifying **DATAP will modify the value of the entry.
78    Return 0 normally, -1 when the whole hash table has been traversed.  */
79 extern int hash_iterate_modify (hash_table *htab, void **ptr,
80                                 const void **key, size_t *keylen,
81                                 void ***datap);
82 
83 /* Given SEED > 1, return the smallest odd prime number >= SEED.  */
84 extern unsigned long int next_prime (unsigned long int seed);
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #endif /* not _HASH_H */
91