1 /* hash - hashing table processing. 2 Copyright (C) 1998-1999, 2001, 2003, 2009-2020 Free Software Foundation, 3 Inc. 4 Written by Jim Meyering <meyering@ascend.com>, 1998. 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 18 19 /* A generic hash table package. */ 20 21 /* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use 22 obstacks instead of malloc, and recompile 'hash.c' with same setting. */ 23 24 #ifndef HASH_H_ 25 # define HASH_H_ 26 27 # include <stdio.h> 28 # include <stdbool.h> 29 30 typedef size_t (*Hash_hasher) (const void *, size_t); 31 typedef bool (*Hash_comparator) (const void *, const void *); 32 typedef void (*Hash_data_freer) (void *); 33 typedef bool (*Hash_processor) (void *, void *); 34 35 struct hash_tuning 36 { 37 /* This structure is mainly used for 'hash_initialize', see the block 38 documentation of 'hash_reset_tuning' for more complete comments. */ 39 40 float shrink_threshold; /* ratio of used buckets to trigger a shrink */ 41 float shrink_factor; /* ratio of new smaller size to original size */ 42 float growth_threshold; /* ratio of used buckets to trigger a growth */ 43 float growth_factor; /* ratio of new bigger size to original size */ 44 bool is_n_buckets; /* if CANDIDATE really means table size */ 45 }; 46 47 typedef struct hash_tuning Hash_tuning; 48 49 struct hash_table; 50 51 typedef struct hash_table Hash_table; 52 53 /* Information and lookup. */ 54 size_t hash_get_n_buckets (const Hash_table *) _GL_ATTRIBUTE_PURE; 55 size_t hash_get_n_buckets_used (const Hash_table *) _GL_ATTRIBUTE_PURE; 56 size_t hash_get_n_entries (const Hash_table *) _GL_ATTRIBUTE_PURE; 57 size_t hash_get_max_bucket_length (const Hash_table *) _GL_ATTRIBUTE_PURE; 58 bool hash_table_ok (const Hash_table *) _GL_ATTRIBUTE_PURE; 59 void hash_print_statistics (const Hash_table *, FILE *); 60 void *hash_lookup (const Hash_table *, const void *); 61 62 /* Walking. */ 63 void *hash_get_first (const Hash_table *) _GL_ATTRIBUTE_PURE; 64 void *hash_get_next (const Hash_table *, const void *); 65 size_t hash_get_entries (const Hash_table *, void **, size_t); 66 size_t hash_do_for_each (const Hash_table *, Hash_processor, void *); 67 68 /* Allocation and clean-up. */ 69 #if 0 70 size_t hash_string (const char *, size_t) _GL_ATTRIBUTE_PURE; 71 #endif 72 void hash_reset_tuning (Hash_tuning *); 73 Hash_table *hash_initialize (size_t, const Hash_tuning *, 74 Hash_hasher, Hash_comparator, 75 Hash_data_freer) _GL_ATTRIBUTE_NODISCARD; 76 Hash_table *hash_xinitialize (size_t, const Hash_tuning *, 77 Hash_hasher, Hash_comparator, 78 Hash_data_freer) _GL_ATTRIBUTE_NODISCARD; 79 void hash_clear (Hash_table *); 80 void hash_free (Hash_table *); 81 82 /* Insertion and deletion. */ 83 bool hash_rehash (Hash_table *, size_t) _GL_ATTRIBUTE_NODISCARD; 84 void *hash_insert (Hash_table *, const void *) _GL_ATTRIBUTE_NODISCARD; 85 void *hash_xinsert (Hash_table *, const void *); 86 87 int hash_insert_if_absent (Hash_table *table, const void *entry, 88 const void **matched_ent); 89 void *hash_delete (Hash_table *, const void *); 90 91 #endif 92