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