Blame lib/hash.h

Packit 709fb3
/* hash - hashing table processing.
Packit 709fb3
   Copyright (C) 1998-1999, 2001, 2003, 2009-2017 Free Software Foundation,
Packit 709fb3
   Inc.
Packit 709fb3
   Written by Jim Meyering <meyering@ascend.com>, 1998.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* A generic hash table package.  */
Packit 709fb3
Packit 709fb3
/* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use
Packit 709fb3
   obstacks instead of malloc, and recompile 'hash.c' with same setting.  */
Packit 709fb3
Packit 709fb3
#ifndef HASH_H_
Packit 709fb3
# define HASH_H_
Packit 709fb3
Packit 709fb3
# include <stdio.h>
Packit 709fb3
# include <stdbool.h>
Packit 709fb3
Packit 709fb3
/* The __attribute__ feature is available in gcc versions 2.5 and later.
Packit 709fb3
   The warn_unused_result attribute appeared first in gcc-3.4.0.  */
Packit 709fb3
# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
Packit 709fb3
#  define _GL_ATTRIBUTE_WUR __attribute__ ((__warn_unused_result__))
Packit 709fb3
# else
Packit 709fb3
#  define _GL_ATTRIBUTE_WUR /* empty */
Packit 709fb3
# endif
Packit 709fb3
Packit 709fb3
# ifndef _GL_ATTRIBUTE_DEPRECATED
Packit 709fb3
/* The __attribute__((__deprecated__)) feature
Packit 709fb3
   is available in gcc versions 3.1 and newer.  */
Packit 709fb3
#  if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1)
Packit 709fb3
#   define _GL_ATTRIBUTE_DEPRECATED /* empty */
Packit 709fb3
#  else
Packit 709fb3
#   define _GL_ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__))
Packit 709fb3
#  endif
Packit 709fb3
# endif
Packit 709fb3
Packit 709fb3
typedef size_t (*Hash_hasher) (const void *, size_t);
Packit 709fb3
typedef bool (*Hash_comparator) (const void *, const void *);
Packit 709fb3
typedef void (*Hash_data_freer) (void *);
Packit 709fb3
typedef bool (*Hash_processor) (void *, void *);
Packit 709fb3
Packit 709fb3
struct hash_tuning
Packit 709fb3
  {
Packit 709fb3
    /* This structure is mainly used for 'hash_initialize', see the block
Packit 709fb3
       documentation of 'hash_reset_tuning' for more complete comments.  */
Packit 709fb3
Packit 709fb3
    float shrink_threshold;     /* ratio of used buckets to trigger a shrink */
Packit 709fb3
    float shrink_factor;        /* ratio of new smaller size to original size */
Packit 709fb3
    float growth_threshold;     /* ratio of used buckets to trigger a growth */
Packit 709fb3
    float growth_factor;        /* ratio of new bigger size to original size */
Packit 709fb3
    bool is_n_buckets;          /* if CANDIDATE really means table size */
Packit 709fb3
  };
Packit 709fb3
Packit 709fb3
typedef struct hash_tuning Hash_tuning;
Packit 709fb3
Packit 709fb3
struct hash_table;
Packit 709fb3
Packit 709fb3
typedef struct hash_table Hash_table;
Packit 709fb3
Packit 709fb3
/* Information and lookup.  */
Packit 709fb3
size_t hash_get_n_buckets (const Hash_table *) _GL_ATTRIBUTE_PURE;
Packit 709fb3
size_t hash_get_n_buckets_used (const Hash_table *) _GL_ATTRIBUTE_PURE;
Packit 709fb3
size_t hash_get_n_entries (const Hash_table *) _GL_ATTRIBUTE_PURE;
Packit 709fb3
size_t hash_get_max_bucket_length (const Hash_table *) _GL_ATTRIBUTE_PURE;
Packit 709fb3
bool hash_table_ok (const Hash_table *) _GL_ATTRIBUTE_PURE;
Packit 709fb3
void hash_print_statistics (const Hash_table *, FILE *);
Packit 709fb3
void *hash_lookup (const Hash_table *, const void *);
Packit 709fb3
Packit 709fb3
/* Walking.  */
Packit 709fb3
void *hash_get_first (const Hash_table *) _GL_ATTRIBUTE_PURE;
Packit 709fb3
void *hash_get_next (const Hash_table *, const void *);
Packit 709fb3
size_t hash_get_entries (const Hash_table *, void **, size_t);
Packit 709fb3
size_t hash_do_for_each (const Hash_table *, Hash_processor, void *);
Packit 709fb3
Packit 709fb3
/* Allocation and clean-up.  */
Packit 709fb3
size_t hash_string (const char *, size_t) _GL_ATTRIBUTE_PURE;
Packit 709fb3
void hash_reset_tuning (Hash_tuning *);
Packit 709fb3
Hash_table *hash_initialize (size_t, const Hash_tuning *,
Packit 709fb3
                             Hash_hasher, Hash_comparator,
Packit 709fb3
                             Hash_data_freer) _GL_ATTRIBUTE_WUR;
Packit 709fb3
void hash_clear (Hash_table *);
Packit 709fb3
void hash_free (Hash_table *);
Packit 709fb3
Packit 709fb3
/* Insertion and deletion.  */
Packit 709fb3
bool hash_rehash (Hash_table *, size_t) _GL_ATTRIBUTE_WUR;
Packit 709fb3
void *hash_insert (Hash_table *, const void *) _GL_ATTRIBUTE_WUR;
Packit 709fb3
Packit 709fb3
int hash_insert_if_absent (Hash_table *table, const void *entry,
Packit 709fb3
                           const void **matched_ent);
Packit 709fb3
void *hash_delete (Hash_table *, const void *);
Packit 709fb3
Packit 709fb3
#endif