71bcd0f14f331f8eb1c07d5478bc46c029ee71de
[pspp-builds.git] / src / libpspp / hash.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <stdbool.h>
22 #include "hash.h"
23 #include "message.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <limits.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include "array.h"
30 #include "alloc.h"
31 #include "compiler.h"
32 #include "misc.h"
33 #include "str.h"
34 #include "pool.h"
35
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* Note for constructing hash functions:
41
42    You can store the hash values in the records, then compare hash
43    values (in the compare function) before bothering to compare keys.
44    Hash values can simply be returned from the records instead of
45    recalculating when rehashing. */
46
47 /* Debugging note:
48
49    Since hash_probe and hash_find take void * pointers, it's easy to
50    pass a void ** to your data by accidentally inserting an `&'
51    reference operator where one shouldn't go.  It took me an hour to
52    hunt down a bug like that once. */
53 \f
54 /* Prime numbers and hash functions. */
55
56 /* Returns smallest power of 2 greater than X. */
57 static size_t
58 next_power_of_2 (size_t x) 
59 {
60   assert (x != 0);
61
62   for (;;) 
63     {
64       /* Turn off rightmost 1-bit in x. */
65       size_t y = x & (x - 1);
66
67       /* If y is 0 then x only had a single 1-bit. */
68       if (y == 0)
69         return 2 * x;
70
71       /* Otherwise turn off the next. */
72       x = y;
73     }
74 }
75
76 /* Fowler-Noll-Vo hash constants, for 32-bit word sizes. */
77 #define FNV_32_PRIME 16777619u
78 #define FNV_32_BASIS 2166136261u
79
80 /* Fowler-Noll-Vo 32-bit hash, for bytes. */
81 unsigned
82 hsh_hash_bytes (const void *buf_, size_t size)
83 {
84   const unsigned char *buf = (const unsigned char *) buf_;
85   unsigned hash;
86
87   assert (buf != NULL);
88
89   hash = FNV_32_BASIS;
90   while (size-- > 0)
91     hash = (hash * FNV_32_PRIME) ^ *buf++;
92
93   return hash;
94
95
96 /* Fowler-Noll-Vo 32-bit hash, for strings. */
97 unsigned
98 hsh_hash_string (const char *s_) 
99 {
100   const unsigned char *s = (const unsigned char *) s_;
101   unsigned hash;
102
103   assert (s != NULL);
104
105   hash = FNV_32_BASIS;
106   while (*s != '\0')
107     hash = (hash * FNV_32_PRIME) ^ *s++;
108
109   return hash;
110 }
111
112 /* Fowler-Noll-Vo 32-bit hash, for case-insensitive strings. */
113 unsigned
114 hsh_hash_case_string (const char *s_) 
115 {
116   const unsigned char *s = (const unsigned char *) s_;
117   unsigned hash;
118
119   assert (s != NULL);
120
121   hash = FNV_32_BASIS;
122   while (*s != '\0')
123     hash = (hash * FNV_32_PRIME) ^ toupper (*s++);
124
125   return hash;
126 }
127
128 /* Hash for ints. */
129 unsigned
130 hsh_hash_int (int i) 
131 {
132   return hsh_hash_bytes (&i, sizeof i);
133 }
134
135 /* Hash for double. */
136 unsigned
137 hsh_hash_double (double d) 
138 {
139   if (!isnan (d))
140     return hsh_hash_bytes (&d, sizeof d);
141   else
142     return 0;
143 }
144 \f
145 /* Hash tables. */
146
147 /* Hash table. */
148 struct hsh_table
149   {
150     size_t used;                /* Number of filled entries. */
151     size_t size;                /* Number of entries (a power of 2). */
152     void **entries;             /* Hash table proper. */
153
154     const void *aux;            /* Auxiliary data for comparison functions. */
155     hsh_compare_func *compare;
156     hsh_hash_func *hash;
157     hsh_free_func *free;
158     
159 #ifndef NDEBUG
160     /* Set to false if hsh_data() or hsh_sort() has been called,
161        so that most hsh_*() functions may no longer be called. */
162     bool hash_ordered;
163 #endif
164
165     struct pool *pool;         /* The pool used for this hash table */
166   };
167
168 struct hsh_table *
169 hsh_create (int size, hsh_compare_func *compare, hsh_hash_func *hash,
170             hsh_free_func *free, const void *aux)
171 {
172   return hsh_create_pool (NULL, size, compare, hash, free, aux);
173 }
174
175
176
177 /* Creates a hash table with at least M entries.  COMPARE is a
178    function that compares two entries and returns 0 if they are
179    identical, nonzero otherwise; HASH returns a nonnegative hash value
180    for an entry; FREE destroys an entry. */
181 struct hsh_table *
182 hsh_create_pool (struct pool *pool, int size, 
183                  hsh_compare_func *compare, hsh_hash_func *hash,
184                  hsh_free_func *free, const void *aux)
185 {
186   struct hsh_table *h;
187   int i;
188
189   assert (compare != NULL);
190   assert (hash != NULL);
191   
192   h = pool_malloc (pool, sizeof *h);
193   h->pool = pool;
194   h->used = 0;
195   if (size < 4)
196     size = 4;
197   h->size = next_power_of_2 (size);
198   h->entries = pool_nmalloc (pool, h->size, sizeof *h->entries);
199   for (i = 0; i < h->size; i++)
200     h->entries[i] = NULL;
201   h->aux = aux;
202   h->compare = compare;
203   h->hash = hash;
204   h->free = free;
205 #ifndef NDEBUG
206   h->hash_ordered = true;
207 #endif
208   return h;
209 }
210
211 /* Destroys the contents of table H. */
212 void
213 hsh_clear (struct hsh_table *h)
214 {
215   int i;
216
217   assert (h != NULL);
218   if (h->free)
219     for (i = 0; i < h->size; i++)
220       if (h->entries[i] != NULL)
221         h->free (h->entries[i], h->aux);
222
223   for (i = 0; i < h->size; i++)
224     h->entries[i] = NULL;
225
226   h->used = 0;
227
228 #ifndef NDEBUG
229   h->hash_ordered = true;
230 #endif
231 }
232
233 /* Destroys table H and all its contents. */
234 void
235 hsh_destroy (struct hsh_table *h)
236 {
237   int i;
238
239   if (h != NULL) 
240     {
241       if (h->free)
242         for (i = 0; i < h->size; i++)
243           if (h->entries[i] != NULL)
244             h->free (h->entries[i], h->aux);
245       pool_free (h->pool, h->entries);
246       pool_free (h->pool, h);
247     }
248 }
249
250 /* Locates an entry matching TARGET.  Returns the index for the
251    entry, if found, or the index of an empty entry that indicates
252    where TARGET should go, otherwise. */
253 static inline unsigned
254 locate_matching_entry (struct hsh_table *h, const void *target) 
255 {
256   unsigned i = h->hash (target, h->aux);
257
258   assert (h->hash_ordered);
259   for (;;)
260     {
261       void *entry;
262       i &= h->size - 1;
263       entry = h->entries[i];
264       if (entry == NULL || !h->compare (entry, target, h->aux))
265         return i;
266       i--;
267     }
268 }
269
270 /* Returns the index of an empty entry that indicates
271    where TARGET should go, assuming that TARGET is not equal to
272    any item already in the hash table. */
273 static inline unsigned
274 locate_empty_entry (struct hsh_table *h, const void *target) 
275 {
276   unsigned i = h->hash (target, h->aux);
277
278   assert (h->hash_ordered);
279   for (;;)
280     {
281       i &= h->size - 1;
282       if (h->entries[i] == NULL)
283         return i;
284       i--;
285     }
286 }
287
288 /* Changes the capacity of H to NEW_SIZE, which must be a
289    positive power of 2 at least as large as the number of
290    elements in H. */
291 static void
292 rehash (struct hsh_table *h, size_t new_size)
293 {
294   void **begin, **end, **table_p;
295   int i;
296
297   assert (h != NULL);
298   assert (new_size >= h->used);
299
300   /* Verify that NEW_SIZE is a positive power of 2. */
301   assert (new_size > 0 && (new_size & (new_size - 1)) == 0);
302
303   begin = h->entries;
304   end = begin + h->size;
305
306   h->size = new_size;
307   h->entries = pool_nmalloc (h->pool, h->size, sizeof *h->entries);
308   for (i = 0; i < h->size; i++)
309     h->entries[i] = NULL;
310   for (table_p = begin; table_p < end; table_p++) 
311     {
312       void *entry = *table_p;
313       if (entry != NULL)
314         h->entries[locate_empty_entry (h, entry)] = entry;
315     }
316   pool_free (h->pool, begin);
317
318 #ifndef NDEBUG
319   h->hash_ordered = true;
320 #endif
321 }
322
323 /* A "algo_predicate_func" that returns true if DATA points
324    to a non-null void. */
325 static bool
326 not_null (const void *data_, const void *aux UNUSED) 
327 {
328   void *const *data = data_;
329
330   return *data != NULL;
331 }
332
333 /* Compacts hash table H and returns a pointer to its data.  The
334    returned data consists of hsh_count(H) non-null pointers, in
335    no particular order, followed by a null pointer.
336
337    After calling this function, only hsh_destroy() and
338    hsh_count() should be applied to H.  hsh_first() and
339    hsh_next() could also be used, but you're better off just
340    iterating through the returned array.
341
342    This function is intended for use in situations where data
343    processing occurs in two phases.  In the first phase, data is
344    added, removed, and searched for within a hash table.  In the
345    second phase, the contents of the hash table are output and
346    the hash property itself is no longer of interest.
347
348    Use hsh_sort() instead, if the second phase wants data in
349    sorted order.  Use hsh_data_copy() or hsh_sort_copy() instead,
350    if the second phase still needs to search the hash table. */
351 void *const *
352 hsh_data (struct hsh_table *h) 
353 {
354   size_t n;
355
356   assert (h != NULL);
357   n = partition (h->entries, h->size, sizeof *h->entries, not_null, NULL);
358   assert (n == h->used);
359 #ifndef NDEBUG
360   h->hash_ordered = false;
361 #endif
362   return h->entries;
363 }
364
365 /* Dereferences void ** pointers and passes them to the hash
366    comparison function. */
367 static int
368 comparison_helper (const void *a_, const void *b_, const void *h_) 
369 {
370   void *const *a = a_;
371   void *const *b = b_;
372   const struct hsh_table *h = h_;
373
374   assert(a);
375   assert(b);
376
377   return h->compare (*a, *b, h->aux);
378 }
379
380 /* Sorts hash table H based on hash comparison function.  The
381    returned data consists of hsh_count(H) non-null pointers,
382    sorted in order of the hash comparison function, followed by a
383    null pointer.
384
385    After calling this function, only hsh_destroy() and
386    hsh_count() should be applied to H.  hsh_first() and
387    hsh_next() could also be used, but you're better off just
388    iterating through the returned array.
389
390    This function is intended for use in situations where data
391    processing occurs in two phases.  In the first phase, data is
392    added, removed, and searched for within a hash table.  In the
393    second phase, the contents of the hash table are output and
394    the hash property itself is no longer of interest.
395
396    Use hsh_data() instead, if the second phase doesn't need the
397    data in any particular order.  Use hsh_data_copy() or
398    hsh_sort_copy() instead, if the second phase still needs to
399    search the hash table. */
400 void *const *
401 hsh_sort (struct hsh_table *h)
402 {
403   assert (h != NULL);
404
405   hsh_data (h);
406   sort (h->entries, h->used, sizeof *h->entries, comparison_helper, h);
407   return h->entries;
408 }
409
410 /* Makes and returns a copy of the pointers to the data in H.
411    The returned data consists of hsh_count(H) non-null pointers,
412    in no particular order, followed by a null pointer.  The hash
413    table is not modified.  The caller is responsible for freeing
414    the allocated data.
415
416    If you don't need to search or modify the hash table, then
417    hsh_data() is a more efficient choice. */
418 void **
419 hsh_data_copy (struct hsh_table *h) 
420 {
421   void **copy;
422
423   assert (h != NULL);
424   copy = pool_nmalloc (h->pool, (h->used + 1), sizeof *copy);
425   copy_if (h->entries, h->size, sizeof *h->entries, copy, not_null, NULL);
426   copy[h->used] = NULL;
427   return copy;
428 }
429
430 /* Makes and returns a copy of the pointers to the data in H.
431    The returned data consists of hsh_count(H) non-null pointers,
432    sorted in order of the hash comparison function, followed by a
433    null pointer.  The hash table is not modified.  The caller is
434    responsible for freeing the allocated data.
435
436    If you don't need to search or modify the hash table, then
437    hsh_sort() is a more efficient choice. */
438 void **
439 hsh_sort_copy (struct hsh_table *h) 
440 {
441   void **copy;
442
443   assert (h != NULL);
444   copy = hsh_data_copy (h);
445   sort (copy, h->used, sizeof *copy, comparison_helper, h);
446   return copy;
447 }
448 \f
449 /* Hash entries. */
450
451 /* Searches hash table H for TARGET.  If found, returns a pointer
452    to a pointer to that entry; otherwise returns a pointer to a
453    NULL entry which *must* be used to insert a new entry having
454    the same key data.  */
455 inline void **
456 hsh_probe (struct hsh_table *h, const void *target)
457 {
458   unsigned i;
459   
460   assert (h != NULL);
461   assert (target != NULL);
462   assert (h->hash_ordered);
463
464   if (h->used > h->size / 2)
465     rehash (h, h->size * 2);
466   i = locate_matching_entry (h, target);
467   if (h->entries[i] == NULL)
468     h->used++;
469   return &h->entries[i];
470 }
471
472 /* Searches hash table H for TARGET.  If not found, inserts
473    TARGET and returns a null pointer.  If found, returns the
474    match, without replacing it in the table. */
475 void *
476 hsh_insert (struct hsh_table *h, void *target) 
477 {
478   void **entry;
479
480   assert (h != NULL);
481   assert (target != NULL);
482
483   entry = hsh_probe (h, target);
484   if (*entry == NULL) 
485     {
486       *entry = target;
487       return NULL;
488     }
489   else
490     return *entry;
491 }
492
493 /* Searches hash table H for TARGET.  If not found, inserts
494    TARGET and returns a null pointer.  If found, returns the
495    match, after replacing it in the table by TARGET. */
496 void *
497 hsh_replace (struct hsh_table *h, void *target) 
498 {
499   void **entry = hsh_probe (h, target);
500   void *old = *entry;
501   *entry = target;
502   return old;
503 }
504
505 /* Returns the entry in hash table H that matches TARGET, or NULL
506    if there is none. */
507 void *
508 hsh_find (struct hsh_table *h, const void *target)
509 {
510   return h->entries[locate_matching_entry (h, target)];
511 }
512
513 /* Deletes the entry in hash table H that matches TARGET.
514    Returns true if an entry was deleted.
515
516    Uses Knuth's Algorithm 6.4R (Deletion with linear probing).
517    Because our load factor is at most 1/2, the average number of
518    moves that this algorithm makes should be at most 2 - ln 2 ~=
519    1.65. */
520 bool
521 hsh_delete (struct hsh_table *h, const void *target) 
522 {
523   unsigned i = locate_matching_entry (h, target);
524   if (h->entries[i] != NULL) 
525     {
526       h->used--;
527       if (h->free != NULL)
528         h->free (h->entries[i], h->aux);
529
530       for (;;) 
531         {
532           unsigned r;
533           ptrdiff_t j;
534
535           h->entries[i] = NULL;
536           j = i;
537           do 
538             {
539               i = (i - 1) & (h->size - 1);
540               if (h->entries[i] == NULL)
541                 return true;
542               
543               r = h->hash (h->entries[i], h->aux) & (h->size - 1);
544             }
545           while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
546           h->entries[j] = h->entries[i]; 
547         }
548     }
549   else
550     return false;
551 }
552 \f
553 /* Iteration. */
554
555 /* Finds and returns an entry in TABLE, and initializes ITER for
556    use with hsh_next().  If TABLE is empty, returns a null
557    pointer. */
558 void *
559 hsh_first (struct hsh_table *h, struct hsh_iterator *iter) 
560 {
561   assert (h != NULL);
562   assert (iter != NULL);
563
564   iter->next = 0;
565   return hsh_next (h, iter);
566 }
567
568 /* Iterates through TABLE with iterator ITER.  Returns the next
569    entry in TABLE, or a null pointer after the last entry.
570
571    Entries are returned in an undefined order.  Modifying TABLE
572    during iteration may cause some entries to be returned
573    multiple times or not at all. */
574 void *
575 hsh_next (struct hsh_table *h, struct hsh_iterator *iter)
576 {
577   size_t i;
578
579   assert (h != NULL);
580   assert (iter != NULL);
581   assert (iter->next <= h->size);
582
583   for (i = iter->next; i < h->size; i++)
584     if (h->entries[i])
585       {
586         iter->next = i + 1;
587         return h->entries[i];
588       }
589
590   iter->next = h->size;
591   return NULL;
592 }
593 \f
594 /* Returns the number of items in H. */
595 size_t 
596 hsh_count (struct hsh_table *h) 
597 {
598   assert (h != NULL);
599   
600   return h->used;
601 }
602 \f
603 /* Debug helpers. */
604
605 #if DEBUGGING
606 #undef NDEBUG
607 #include "message.h"
608 #include <stdio.h>
609
610 /* Displays contents of hash table H on stdout. */
611 void
612 hsh_dump (struct hsh_table *h)
613 {
614   void **entry = h->entries;
615   int i;
616
617   printf (_("hash table:"));
618   for (i = 0; i < h->size; i++)
619     printf (" %p", *entry++);
620   printf ("\n");
621 }
622
623 /* This wrapper around hsh_probe() assures that it returns a pointer
624    to a NULL pointer.  This function is used when it is known that the
625    entry to be inserted does not already exist in the table. */
626 void
627 hsh_force_insert (struct hsh_table *h, void *p)
628 {
629   void **pp = hsh_probe (h, p);
630   assert (*pp == NULL);
631   *pp = p;
632 }
633
634 /* This wrapper around hsh_find() assures that it returns non-NULL.
635    This function is for use when it is known that the entry being
636    searched for must exist in the table. */
637 void *
638 hsh_force_find (struct hsh_table *h, const void *target)
639 {
640   void *found = hsh_find (h, target);
641   assert (found != NULL);
642   return found;
643 }
644
645 /* This wrapper for hsh_delete() verifies that an item really was
646    deleted. */
647 void
648 hsh_force_delete (struct hsh_table *h, const void *target)
649 {
650   int found = hsh_delete (h, target);
651   assert (found != 0);
652 }
653 #endif