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