Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright  2009,2012 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  *
     23  * Authors:
     24  *    Eric Anholt <eric (at) anholt.net>
     25  *
     26  */
     27 
     28 #ifndef _HASH_TABLE_H
     29 #define _HASH_TABLE_H
     30 
     31 #include <stdlib.h>
     32 #include <inttypes.h>
     33 #include <stdbool.h>
     34 #include "c99_compat.h"
     35 #include "macros.h"
     36 
     37 #ifdef __cplusplus
     38 extern "C" {
     39 #endif
     40 
     41 struct hash_entry {
     42    uint32_t hash;
     43    const void *key;
     44    void *data;
     45 };
     46 
     47 struct hash_table {
     48    struct hash_entry *table;
     49    uint32_t (*key_hash_function)(const void *key);
     50    bool (*key_equals_function)(const void *a, const void *b);
     51    const void *deleted_key;
     52    uint32_t size;
     53    uint32_t rehash;
     54    uint32_t max_entries;
     55    uint32_t size_index;
     56    uint32_t entries;
     57    uint32_t deleted_entries;
     58 };
     59 
     60 struct hash_table *
     61 _mesa_hash_table_create(void *mem_ctx,
     62                         uint32_t (*key_hash_function)(const void *key),
     63                         bool (*key_equals_function)(const void *a,
     64                                                     const void *b));
     65 void _mesa_hash_table_destroy(struct hash_table *ht,
     66                               void (*delete_function)(struct hash_entry *entry));
     67 void _mesa_hash_table_clear(struct hash_table *ht,
     68                             void (*delete_function)(struct hash_entry *entry));
     69 void _mesa_hash_table_set_deleted_key(struct hash_table *ht,
     70                                       const void *deleted_key);
     71 
     72 static inline uint32_t _mesa_hash_table_num_entries(struct hash_table *ht)
     73 {
     74    return ht->entries;
     75 }
     76 
     77 struct hash_entry *
     78 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data);
     79 struct hash_entry *
     80 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
     81                                    const void *key, void *data);
     82 struct hash_entry *
     83 _mesa_hash_table_search(struct hash_table *ht, const void *key);
     84 struct hash_entry *
     85 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
     86                                   const void *key);
     87 void _mesa_hash_table_remove(struct hash_table *ht,
     88                              struct hash_entry *entry);
     89 
     90 struct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht,
     91                                                struct hash_entry *entry);
     92 struct hash_entry *
     93 _mesa_hash_table_random_entry(struct hash_table *ht,
     94                               bool (*predicate)(struct hash_entry *entry));
     95 
     96 uint32_t _mesa_hash_data(const void *data, size_t size);
     97 uint32_t _mesa_hash_string(const char *key);
     98 bool _mesa_key_string_equal(const void *a, const void *b);
     99 bool _mesa_key_pointer_equal(const void *a, const void *b);
    100 
    101 static inline uint32_t _mesa_key_hash_string(const void *key)
    102 {
    103    return _mesa_hash_string((const char *)key);
    104 }
    105 
    106 static inline uint32_t _mesa_hash_pointer(const void *pointer)
    107 {
    108    return _mesa_hash_data(&pointer, sizeof(pointer));
    109 }
    110 
    111 enum {
    112    _mesa_fnv32_1a_offset_bias = 2166136261u,
    113 };
    114 
    115 static inline uint32_t
    116 _mesa_fnv32_1a_accumulate_block(uint32_t hash, const void *data, size_t size)
    117 {
    118    const uint8_t *bytes = (const uint8_t *)data;
    119 
    120    while (size-- != 0) {
    121       hash ^= *bytes;
    122       hash = hash * 0x01000193;
    123       bytes++;
    124    }
    125 
    126    return hash;
    127 }
    128 
    129 #define _mesa_fnv32_1a_accumulate(hash, expr) \
    130    _mesa_fnv32_1a_accumulate_block(hash, &(expr), sizeof(expr))
    131 
    132 /**
    133  * This foreach function is safe against deletion (which just replaces
    134  * an entry's data with the deleted marker), but not against insertion
    135  * (which may rehash the table, making entry a dangling pointer).
    136  */
    137 #define hash_table_foreach(ht, entry)                   \
    138    for (entry = _mesa_hash_table_next_entry(ht, NULL);  \
    139         entry != NULL;                                  \
    140         entry = _mesa_hash_table_next_entry(ht, entry))
    141 
    142 static inline void
    143 hash_table_call_foreach(struct hash_table *ht,
    144                         void (*callback)(const void *key,
    145                                          void *data,
    146                                          void *closure),
    147                         void *closure)
    148 {
    149    struct hash_entry *entry;
    150 
    151    hash_table_foreach(ht, entry)
    152       callback(entry->key, entry->data, closure);
    153 }
    154 
    155 #ifdef __cplusplus
    156 } /* extern C */
    157 #endif
    158 
    159 #endif /* _HASH_TABLE_H */
    160