remove old hash.[ch] implementation

Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Nov 14, 2013 at 20:23 UTC efc684245b81ae0fb8f0afbd06dc1c3101c4e5a0
6 files changed -299
Documentation/technical/api-hash.txt deleted
-52
@@ -1,52 +0,0 @@
1 -hash API
2 -========
3 -
4 -The hash API is a collection of simple hash table functions. Users are expected
5 -to implement their own hashing.
6 -
7 -Data Structures
8 ----------------
9 -
10 -`struct hash_table`::
11 -
12 - The hash table structure. The `array` member points to the hash table
13 - entries. The `size` member counts the total number of valid and invalid
14 - entries in the table. The `nr` member keeps track of the number of
15 - valid entries.
16 -
17 -`struct hash_table_entry`::
18 -
19 - An opaque structure representing an entry in the hash table. The `hash`
20 - member is the entry's hash key and the `ptr` member is the entry's
21 - value.
22 -
23 -Functions
24 ----------
25 -
26 -`init_hash`::
27 -
28 - Initialize the hash table.
29 -
30 -`free_hash`::
31 -
32 - Release memory associated with the hash table.
33 -
34 -`insert_hash`::
35 -
36 - Insert a pointer into the hash table. If an entry with that hash
37 - already exists, a pointer to the existing entry's value is returned.
38 - Otherwise NULL is returned. This allows callers to implement
39 - chaining, etc.
40 -
41 -`lookup_hash`::
42 -
43 - Lookup an entry in the hash table. If an entry with that hash exists
44 - the entry's value is returned. Otherwise NULL is returned.
45 -
46 -`for_each_hash`::
47 -
48 - Call a function for each entry in the hash table. The function is
49 - expected to take the entry's value as its only argument and return an
50 - int. If the function returns a negative int the loop is aborted
51 - immediately. Otherwise, the return value is accumulated and the sum
52 - returned upon completion of the loop.
Makefile
-2
@@ -677,7 +677,6 @@ LIB_H += git-compat-util.h
677 LIB_H += gpg-interface.h
678 LIB_H += graph.h
679 LIB_H += grep.h
680 -LIB_H += hash.h
680 LIB_H += hashmap.h
681 LIB_H += help.h
682 LIB_H += http.h
@@ -809,7 +808,6 @@ LIB_OBJS += gettext.o
808 LIB_OBJS += gpg-interface.o
809 LIB_OBJS += graph.o
810 LIB_OBJS += grep.o
812 -LIB_OBJS += hash.o
811 LIB_OBJS += hashmap.o
812 LIB_OBJS += help.o
813 LIB_OBJS += hex.o
cache.h
-1
@@ -3,7 +3,6 @@
3
4 #include "git-compat-util.h"
5 #include "strbuf.h"
6 -#include "hash.h"
6 #include "hashmap.h"
7 #include "advice.h"
8 #include "gettext.h"
hash.c deleted
-110
@@ -1,110 +0,0 @@
1 -/*
2 - * Some generic hashing helpers.
3 - */
4 -#include "cache.h"
5 -#include "hash.h"
6 -
7 -/*
8 - * Look up a hash entry in the hash table. Return the pointer to
9 - * the existing entry, or the empty slot if none existed. The caller
10 - * can then look at the (*ptr) to see whether it existed or not.
11 - */
12 -static struct hash_table_entry *lookup_hash_entry(unsigned int hash, const struct hash_table *table)
13 -{
14 - unsigned int size = table->size, nr = hash % size;
15 - struct hash_table_entry *array = table->array;
16 -
17 - while (array[nr].ptr) {
18 - if (array[nr].hash == hash)
19 - break;
20 - nr++;
21 - if (nr >= size)
22 - nr = 0;
23 - }
24 - return array + nr;
25 -}
26 -
27 -
28 -/*
29 - * Insert a new hash entry pointer into the table.
30 - *
31 - * If that hash entry already existed, return the pointer to
32 - * the existing entry (and the caller can create a list of the
33 - * pointers or do anything else). If it didn't exist, return
34 - * NULL (and the caller knows the pointer has been inserted).
35 - */
36 -static void **insert_hash_entry(unsigned int hash, void *ptr, struct hash_table *table)
37 -{
38 - struct hash_table_entry *entry = lookup_hash_entry(hash, table);
39 -
40 - if (!entry->ptr) {
41 - entry->ptr = ptr;
42 - entry->hash = hash;
43 - table->nr++;
44 - return NULL;
45 - }
46 - return &entry->ptr;
47 -}
48 -
49 -static void grow_hash_table(struct hash_table *table)
50 -{
51 - unsigned int i;
52 - unsigned int old_size = table->size, new_size;
53 - struct hash_table_entry *old_array = table->array, *new_array;
54 -
55 - new_size = alloc_nr(old_size);
56 - new_array = xcalloc(sizeof(struct hash_table_entry), new_size);
57 - table->size = new_size;
58 - table->array = new_array;
59 - table->nr = 0;
60 - for (i = 0; i < old_size; i++) {
61 - unsigned int hash = old_array[i].hash;
62 - void *ptr = old_array[i].ptr;
63 - if (ptr)
64 - insert_hash_entry(hash, ptr, table);
65 - }
66 - free(old_array);
67 -}
68 -
69 -void *lookup_hash(unsigned int hash, const struct hash_table *table)
70 -{
71 - if (!table->array)
72 - return NULL;
73 - return lookup_hash_entry(hash, table)->ptr;
74 -}
75 -
76 -void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table)
77 -{
78 - unsigned int nr = table->nr;
79 - if (nr >= table->size/2)
80 - grow_hash_table(table);
81 - return insert_hash_entry(hash, ptr, table);
82 -}
83 -
84 -int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data)
85 -{
86 - int sum = 0;
87 - unsigned int i;
88 - unsigned int size = table->size;
89 - struct hash_table_entry *array = table->array;
90 -
91 - for (i = 0; i < size; i++) {
92 - void *ptr = array->ptr;
93 - array++;
94 - if (ptr) {
95 - int val = fn(ptr, data);
96 - if (val < 0)
97 - return val;
98 - sum += val;
99 - }
100 - }
101 - return sum;
102 -}
103 -
104 -void free_hash(struct hash_table *table)
105 -{
106 - free(table->array);
107 - table->array = NULL;
108 - table->size = 0;
109 - table->nr = 0;
110 -}
hash.h deleted
-50
@@ -1,50 +0,0 @@
1 -#ifndef HASH_H
2 -#define HASH_H
3 -
4 -/*
5 - * These are some simple generic hash table helper functions.
6 - * Not necessarily suitable for all users, but good for things
7 - * where you want to just keep track of a list of things, and
8 - * have a good hash to use on them.
9 - *
10 - * It keeps the hash table at roughly 50-75% free, so the memory
11 - * cost of the hash table itself is roughly
12 - *
13 - * 3 * 2*sizeof(void *) * nr_of_objects
14 - *
15 - * bytes.
16 - *
17 - * FIXME: on 64-bit architectures, we waste memory. It would be
18 - * good to have just 32-bit pointers, requiring a special allocator
19 - * for hashed entries or something.
20 - */
21 -struct hash_table_entry {
22 - unsigned int hash;
23 - void *ptr;
24 -};
25 -
26 -struct hash_table {
27 - unsigned int size, nr;
28 - struct hash_table_entry *array;
29 -};
30 -
31 -extern void *lookup_hash(unsigned int hash, const struct hash_table *table);
32 -extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table);
33 -extern int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data);
34 -extern void free_hash(struct hash_table *table);
35 -
36 -static inline void init_hash(struct hash_table *table)
37 -{
38 - table->size = 0;
39 - table->nr = 0;
40 - table->array = NULL;
41 -}
42 -
43 -static inline void preallocate_hash(struct hash_table *table, unsigned int elts)
44 -{
45 - assert(table->size == 0 && table->nr == 0 && table->array == NULL);
46 - table->size = elts * 2;
47 - table->array = xcalloc(sizeof(struct hash_table_entry), table->size);
48 -}
49 -
50 -#endif
test-hashmap.c
-84
@@ -126,85 +126,6 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
126 }
127 }
128
129 -struct hash_entry
130 -{
131 - struct hash_entry *next;
132 - char key[FLEX_ARRAY];
133 -};
134 -
135 -/*
136 - * Test performance of hash.[ch]
137 - * Usage: time echo "perfhash method rounds" | test-hashmap
138 - */
139 -static void perf_hash(unsigned int method, unsigned int rounds)
140 -{
141 - struct hash_table map;
142 - char buf[16];
143 - struct hash_entry **entries, **res, *entry;
144 - unsigned int *hashes;
145 - unsigned int i, j;
146 -
147 - entries = malloc(TEST_SIZE * sizeof(struct hash_entry *));
148 - hashes = malloc(TEST_SIZE * sizeof(int));
149 - for (i = 0; i < TEST_SIZE; i++) {
150 - snprintf(buf, sizeof(buf), "%i", i);
151 - entries[i] = malloc(sizeof(struct hash_entry) + strlen(buf) + 1);
152 - strcpy(entries[i]->key, buf);
153 - hashes[i] = hash(method, i, entries[i]->key);
154 - }
155 -
156 - if (method & TEST_ADD) {
157 - /* test adding to the map */
158 - for (j = 0; j < rounds; j++) {
159 - init_hash(&map);
160 -
161 - /* add entries */
162 - for (i = 0; i < TEST_SIZE; i++) {
163 - res = (struct hash_entry **) insert_hash(
164 - hashes[i], entries[i], &map);
165 - if (res) {
166 - entries[i]->next = *res;
167 - *res = entries[i];
168 - } else {
169 - entries[i]->next = NULL;
170 - }
171 - }
172 -
173 - free_hash(&map);
174 - }
175 - } else {
176 - /* test map lookups */
177 - init_hash(&map);
178 -
179 - /* fill the map (sparsely if specified) */
180 - j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
181 - for (i = 0; i < j; i++) {
182 - res = (struct hash_entry **) insert_hash(hashes[i],
183 - entries[i], &map);
184 - if (res) {
185 - entries[i]->next = *res;
186 - *res = entries[i];
187 - } else {
188 - entries[i]->next = NULL;
189 - }
190 - }
191 -
192 - for (j = 0; j < rounds; j++) {
193 - for (i = 0; i < TEST_SIZE; i++) {
194 - entry = lookup_hash(hashes[i], &map);
195 - while (entry) {
196 - if (!strcmp(entries[i]->key, entry->key))
197 - break;
198 - entry = entry->next;
199 - }
200 - }
201 - }
202 -
203 - free_hash(&map);
204 -
205 - }
206 -}
207 -
129 #define DELIM " \t\r\n"
130
131 /*
@@ -218,7 +139,6 @@ static void perf_hash(unsigned int method, unsigned int rounds)
139 * size -> tablesize numentries
140 *
141 * perfhashmap method rounds -> test hashmap.[ch] performance
221 - * perfhash method rounds -> test hash.[ch] performance
142 */
143 int main(int argc, char *argv[])
144 {
@@ -324,10 +244,6 @@ int main(int argc, char *argv[])
244
245 perf_hashmap(atoi(p1), atoi(p2));
246
327 - } else if (!strcmp("perfhash", cmd) && l1 && l2) {
328 -
329 - perf_hash(atoi(p1), atoi(p2));
330 -
247 } else {
248
249 printf("Unknown command %s\n", cmd);