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