annotate lib/hash.h @ 1311:20f35a1b786a

Lots of minor spec and name changes, and new comments. From François Pinard.
author Jim Meyering <jim@meyering.net>
date Mon, 06 Apr 1998 08:09:36 +0000
parents 8de2d981b46f
children 99255b403d74
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
1 /* hash - hashing table processing.
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
2 Copyright (C) 1998 Free Software Foundation, Inc.
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
3 Written by Jim Meyering <meyering@ascend.com>, 1998.
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
4
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
5 This program is free software; you can redistribute it and/or modify
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
6 it under the terms of the GNU General Public License as published by
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
8 any later version.
1032
0b6b7e10fe5f use HASH_H, not _hash_h_ in #ifndef
Jim Meyering <jim@meyering.net>
parents: 1031
diff changeset
9
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
10 This program is distributed in the hope that it will be useful,
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
13 GNU General Public License for more details.
1170
8de2d981b46f Use PARAMS, not __P.
Jim Meyering <jim@meyering.net>
parents: 1168
diff changeset
14
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
15 You should have received a copy of the GNU General Public License
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
19 /* A generic hash table package. */
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
21 /* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
22 obstacks instead of malloc, and recompile `hash.c' with same setting. */
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
24 #ifndef PARAMS
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
25 # if PROTOTYPES || __STDC__
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
26 # define PARAMS(Args) Args
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
27 # else
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
28 # define PARAMS(Args) ()
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
29 # endif
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
30 #endif
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
31
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
32 typedef unsigned int (*Hash_hasher) PARAMS ((const void *, unsigned int));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
33 typedef bool (*Hash_comparator) PARAMS ((const void *, const void *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
34 typedef void (*Hash_data_freer) PARAMS ((void *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
35 typedef bool (*Hash_processor) PARAMS ((void *, void *));
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
36
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
37 struct hash_entry
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
38 {
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
39 void *data;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
40 struct hash_entry *next;
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
41 };
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
42
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
43 struct hash_table
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
44 {
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
45 /* The array of buckets starts at BUCKET and extends to BUCKET_LIMIT-1,
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
46 for a possibility of N_BUCKETS. Among those, N_BUCKETS_USED buckets
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
47 are not empty, there are N_ENTRIES active entries in the table. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
48 struct hash_entry *bucket;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
49 struct hash_entry *bucket_limit;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
50 unsigned int n_buckets;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
51 unsigned int n_buckets_used;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
52 unsigned int n_entries;
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
54 /* Three functions are given to `hash_initialize', see the documentation
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
55 block for this function. In a word, HASHER randomizes a user entry
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
56 into a number up from 0 up to some maximum minus 1; COMPARATOR returns
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
57 true if two user entries compare equally; and DATA_FREER is the cleanup
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
58 function for a user entry. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
59 Hash_hasher hasher;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
60 Hash_comparator comparator;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
61 Hash_data_freer data_freer;
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
62
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
63 /* A linked list of freed struct hash_entry structs. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
64 struct hash_entry *free_entry_list;
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
66 #if USE_OBSTACK
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
67 /* Whenever obstacks are used, it is possible to allocate all overflowed
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
68 entries into a single stack, so they all can be freed in a single
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
69 operation. It is not clear if the speedup is worth the trouble. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
70 struct obstack entry_stack;
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
71 #endif
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
72 };
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
73
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
74 typedef struct hash_table Hash_table;
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
75
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
76 /* Information and lookup. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
77 unsigned int hash_get_n_buckets PARAMS ((const Hash_table *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
78 unsigned int hash_get_n_buckets_used PARAMS ((const Hash_table *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
79 unsigned int hash_get_n_entries PARAMS ((const Hash_table *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
80 unsigned int hash_get_max_bucket_length PARAMS ((const Hash_table *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
81 bool hash_table_ok PARAMS ((const Hash_table *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
82 void hash_print_statistics PARAMS ((const Hash_table *, FILE *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
83 void *hash_lookup PARAMS ((const Hash_table *, const void *));
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
84
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
85 /* Walking. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
86 void *hash_get_first PARAMS ((const Hash_table *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
87 void *hash_get_next PARAMS ((const Hash_table *, const void *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
88 unsigned int hash_get_entries PARAMS ((const Hash_table *, void **,
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
89 unsigned int));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
90 unsigned int hash_do_for_each PARAMS ((const Hash_table *, Hash_processor,
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
91 void *));
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
92
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
93 /* Allocation and clean-up. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
94 unsigned int hash_string PARAMS ((const char *, unsigned int));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
95 Hash_table *hash_initialize PARAMS ((unsigned int, Hash_hasher,
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
96 Hash_comparator, Hash_data_freer));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
97 void hash_clear PARAMS ((Hash_table *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
98 void hash_free PARAMS ((Hash_table *));
1031
e34b8e81638f from ti/hdlsv
Jim Meyering <jim@meyering.net>
parents:
diff changeset
99
1311
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
100 /* Insertion and deletion. */
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
101 bool hash_rehash PARAMS ((Hash_table *, unsigned int));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
102 void *hash_insert PARAMS ((Hash_table *, const void *, bool *));
20f35a1b786a Lots of minor spec and name changes, and new comments.
Jim Meyering <jim@meyering.net>
parents: 1170
diff changeset
103 void *hash_delete PARAMS ((Hash_table *, const void *));