blob: 4620e423c73dd58f58e53c96d5a53d8a09cbaeb5 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* hashlib.h -- the data structures used in hashing in Bash. */
Jari Aalto726f6381996-08-26 18:22:31 +00002
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1993-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aalto726f6381996-08-26 18:22:31 +000020
Jari Aaltoccc6cda1996-12-23 17:02:34 +000021#if !defined (_HASHLIB_H_)
22#define _HASHLIB_H_
Jari Aalto726f6381996-08-26 18:22:31 +000023
Jari Aaltobb706242000-03-17 21:46:59 +000024#include "stdc.h"
25
Jari Aalto7117c2d2002-07-17 14:10:11 +000026#ifndef PTR_T
27# ifdef __STDC__
28# define PTR_T void *
29# else
30# define PTR_T char *
31# endif
32#endif
33
Jari Aalto726f6381996-08-26 18:22:31 +000034typedef struct bucket_contents {
35 struct bucket_contents *next; /* Link to next hashed key in this bucket. */
36 char *key; /* What we look up. */
Jari Aalto7117c2d2002-07-17 14:10:11 +000037 PTR_T data; /* What we really want. */
38 unsigned int khash; /* What key hashes to */
Jari Aalto726f6381996-08-26 18:22:31 +000039 int times_found; /* Number of times this item has been found. */
40} BUCKET_CONTENTS;
41
42typedef struct hash_table {
43 BUCKET_CONTENTS **bucket_array; /* Where the data is kept. */
44 int nbuckets; /* How many buckets does this table have. */
45 int nentries; /* How many entries does this table have. */
46} HASH_TABLE;
47
Jari Aalto7117c2d2002-07-17 14:10:11 +000048typedef int hash_wfunc __P((BUCKET_CONTENTS *));
49
50/* Operations on tables as a whole */
51extern HASH_TABLE *hash_create __P((int));
52extern HASH_TABLE *hash_copy __P((HASH_TABLE *, sh_string_func_t *));
53extern void hash_flush __P((HASH_TABLE *, sh_free_func_t *));
54extern void hash_dispose __P((HASH_TABLE *));
55extern void hash_walk __P((HASH_TABLE *, hash_wfunc *));
56
57/* Operations to extract information from or pieces of tables */
58extern int hash_bucket __P((const char *, HASH_TABLE *));
59extern int hash_size __P((HASH_TABLE *));
60
61/* Operations on hash table entries */
62extern BUCKET_CONTENTS *hash_search __P((const char *, HASH_TABLE *, int));
63extern BUCKET_CONTENTS *hash_insert __P((char *, HASH_TABLE *, int));
64extern BUCKET_CONTENTS *hash_remove __P((const char *, HASH_TABLE *, int));
65
66/* Miscellaneous */
67extern unsigned int hash_string __P((const char *));
Jari Aalto726f6381996-08-26 18:22:31 +000068
69/* Redefine the function as a macro for speed. */
Jari Aalto7117c2d2002-07-17 14:10:11 +000070#define hash_items(bucket, table) \
Jari Aalto726f6381996-08-26 18:22:31 +000071 ((table && (bucket < table->nbuckets)) ? \
72 table->bucket_array[bucket] : \
73 (BUCKET_CONTENTS *)NULL)
74
75/* Default number of buckets in the hash table. */
Jari Aalto7117c2d2002-07-17 14:10:11 +000076#define DEFAULT_HASH_BUCKETS 64 /* was 107, then 53, must be power of two now */
Jari Aalto726f6381996-08-26 18:22:31 +000077
Jari Aaltof73dda02001-11-13 17:56:06 +000078#define HASH_ENTRIES(ht) ((ht) ? (ht)->nentries : 0)
Jari Aaltobb706242000-03-17 21:46:59 +000079
Jari Aalto7117c2d2002-07-17 14:10:11 +000080/* flags for hash_search and hash_insert */
81#define HASH_NOSRCH 0x01
82#define HASH_CREATE 0x02
83
Jari Aalto726f6381996-08-26 18:22:31 +000084#if !defined (NULL)
85# if defined (__STDC__)
86# define NULL ((void *) 0)
87# else
88# define NULL 0x0
89# endif /* !__STDC__ */
90#endif /* !NULL */
91
Jari Aaltoccc6cda1996-12-23 17:02:34 +000092#endif /* _HASHLIB_H */