195b7b453SJohn Marino /* hash - hashing table processing. 2*09d4459fSDaniel Fojt Copyright (C) 1998-1999, 2001, 2003, 2009-2020 Free Software Foundation, 395b7b453SJohn Marino Inc. 495b7b453SJohn Marino Written by Jim Meyering <meyering@ascend.com>, 1998. 595b7b453SJohn Marino 695b7b453SJohn Marino This program is free software: you can redistribute it and/or modify 795b7b453SJohn Marino it under the terms of the GNU General Public License as published by 895b7b453SJohn Marino the Free Software Foundation; either version 3 of the License, or 995b7b453SJohn Marino (at your option) any later version. 1095b7b453SJohn Marino 1195b7b453SJohn Marino This program is distributed in the hope that it will be useful, 1295b7b453SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of 1395b7b453SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1495b7b453SJohn Marino GNU General Public License for more details. 1595b7b453SJohn Marino 1695b7b453SJohn Marino You should have received a copy of the GNU General Public License 17*09d4459fSDaniel Fojt along with this program. If not, see <https://www.gnu.org/licenses/>. */ 1895b7b453SJohn Marino 1995b7b453SJohn Marino /* A generic hash table package. */ 2095b7b453SJohn Marino 2195b7b453SJohn Marino /* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use 22cf28ed85SJohn Marino obstacks instead of malloc, and recompile 'hash.c' with same setting. */ 2395b7b453SJohn Marino 2495b7b453SJohn Marino #ifndef HASH_H_ 2595b7b453SJohn Marino # define HASH_H_ 2695b7b453SJohn Marino 2795b7b453SJohn Marino # include <stdio.h> 2895b7b453SJohn Marino # include <stdbool.h> 2995b7b453SJohn Marino 30200fbe8dSJohn Marino /* The __attribute__ feature is available in gcc versions 2.5 and later. 31200fbe8dSJohn Marino The warn_unused_result attribute appeared first in gcc-3.4.0. */ 32200fbe8dSJohn Marino # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 33200fbe8dSJohn Marino # define _GL_ATTRIBUTE_WUR __attribute__ ((__warn_unused_result__)) 34200fbe8dSJohn Marino # else 35200fbe8dSJohn Marino # define _GL_ATTRIBUTE_WUR /* empty */ 3695b7b453SJohn Marino # endif 3795b7b453SJohn Marino 38cf28ed85SJohn Marino # ifndef _GL_ATTRIBUTE_DEPRECATED 39cf28ed85SJohn Marino /* The __attribute__((__deprecated__)) feature 40cf28ed85SJohn Marino is available in gcc versions 3.1 and newer. */ 41cf28ed85SJohn Marino # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1) 42cf28ed85SJohn Marino # define _GL_ATTRIBUTE_DEPRECATED /* empty */ 43cf28ed85SJohn Marino # else 44cf28ed85SJohn Marino # define _GL_ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__)) 45cf28ed85SJohn Marino # endif 46cf28ed85SJohn Marino # endif 47cf28ed85SJohn Marino 4895b7b453SJohn Marino typedef size_t (*Hash_hasher) (const void *, size_t); 4995b7b453SJohn Marino typedef bool (*Hash_comparator) (const void *, const void *); 5095b7b453SJohn Marino typedef void (*Hash_data_freer) (void *); 5195b7b453SJohn Marino typedef bool (*Hash_processor) (void *, void *); 5295b7b453SJohn Marino 5395b7b453SJohn Marino struct hash_tuning 5495b7b453SJohn Marino { 55cf28ed85SJohn Marino /* This structure is mainly used for 'hash_initialize', see the block 56cf28ed85SJohn Marino documentation of 'hash_reset_tuning' for more complete comments. */ 5795b7b453SJohn Marino 5895b7b453SJohn Marino float shrink_threshold; /* ratio of used buckets to trigger a shrink */ 5995b7b453SJohn Marino float shrink_factor; /* ratio of new smaller size to original size */ 6095b7b453SJohn Marino float growth_threshold; /* ratio of used buckets to trigger a growth */ 6195b7b453SJohn Marino float growth_factor; /* ratio of new bigger size to original size */ 6295b7b453SJohn Marino bool is_n_buckets; /* if CANDIDATE really means table size */ 6395b7b453SJohn Marino }; 6495b7b453SJohn Marino 6595b7b453SJohn Marino typedef struct hash_tuning Hash_tuning; 6695b7b453SJohn Marino 6795b7b453SJohn Marino struct hash_table; 6895b7b453SJohn Marino 6995b7b453SJohn Marino typedef struct hash_table Hash_table; 7095b7b453SJohn Marino 7195b7b453SJohn Marino /* Information and lookup. */ 72cf28ed85SJohn Marino size_t hash_get_n_buckets (const Hash_table *) _GL_ATTRIBUTE_PURE; 73cf28ed85SJohn Marino size_t hash_get_n_buckets_used (const Hash_table *) _GL_ATTRIBUTE_PURE; 74cf28ed85SJohn Marino size_t hash_get_n_entries (const Hash_table *) _GL_ATTRIBUTE_PURE; 75cf28ed85SJohn Marino size_t hash_get_max_bucket_length (const Hash_table *) _GL_ATTRIBUTE_PURE; 76cf28ed85SJohn Marino bool hash_table_ok (const Hash_table *) _GL_ATTRIBUTE_PURE; 7795b7b453SJohn Marino void hash_print_statistics (const Hash_table *, FILE *); 7895b7b453SJohn Marino void *hash_lookup (const Hash_table *, const void *); 7995b7b453SJohn Marino 8095b7b453SJohn Marino /* Walking. */ 81cf28ed85SJohn Marino void *hash_get_first (const Hash_table *) _GL_ATTRIBUTE_PURE; 8295b7b453SJohn Marino void *hash_get_next (const Hash_table *, const void *); 8395b7b453SJohn Marino size_t hash_get_entries (const Hash_table *, void **, size_t); 8495b7b453SJohn Marino size_t hash_do_for_each (const Hash_table *, Hash_processor, void *); 8595b7b453SJohn Marino 8695b7b453SJohn Marino /* Allocation and clean-up. */ 87cf28ed85SJohn Marino size_t hash_string (const char *, size_t) _GL_ATTRIBUTE_PURE; 8895b7b453SJohn Marino void hash_reset_tuning (Hash_tuning *); 8995b7b453SJohn Marino Hash_table *hash_initialize (size_t, const Hash_tuning *, 9095b7b453SJohn Marino Hash_hasher, Hash_comparator, 91200fbe8dSJohn Marino Hash_data_freer) _GL_ATTRIBUTE_WUR; 92*09d4459fSDaniel Fojt Hash_table *hash_xinitialize (size_t, const Hash_tuning *, 93*09d4459fSDaniel Fojt Hash_hasher, Hash_comparator, 94*09d4459fSDaniel Fojt Hash_data_freer) _GL_ATTRIBUTE_WUR; 9595b7b453SJohn Marino void hash_clear (Hash_table *); 9695b7b453SJohn Marino void hash_free (Hash_table *); 9795b7b453SJohn Marino 9895b7b453SJohn Marino /* Insertion and deletion. */ 99200fbe8dSJohn Marino bool hash_rehash (Hash_table *, size_t) _GL_ATTRIBUTE_WUR; 100200fbe8dSJohn Marino void *hash_insert (Hash_table *, const void *) _GL_ATTRIBUTE_WUR; 101cf28ed85SJohn Marino 102cf28ed85SJohn Marino int hash_insert_if_absent (Hash_table *table, const void *entry, 10395b7b453SJohn Marino const void **matched_ent); 10495b7b453SJohn Marino void *hash_delete (Hash_table *, const void *); 10595b7b453SJohn Marino 10695b7b453SJohn Marino #endif 107