nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

hash_table_def.h (9160B)


      1 /*
      2  * The MIT License (MIT)
      3  *
      4  * Copyright (c) 2015 Mikkel F. Jørgensen, dvide.com
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in all
     14  * copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  * SOFTWARE.
     23  */
     24 
     25 #ifndef HASH_TABLE_DEF_H
     26 #define HASH_TABLE_DEF_H
     27 
     28 #include "ht_hash_function.h"
     29 #ifndef HT_HASH_FUNCTION
     30 /*
     31  * If the default hash function is used, make sure to link with the
     32  * appropriate hash implementation file.
     33  */
     34 #define HT_HASH_FUNCTION ht_default_hash_function
     35 #endif
     36 
     37 #ifndef HT_LOAD_FACTOR
     38 #define HT_LOAD_FACTOR 0.7
     39 #endif
     40 
     41 #define HT_LOAD_FACTOR_FRAC ((size_t)((float)(HT_LOAD_FACTOR)*256))
     42 
     43 #ifndef HT_PANIC
     44 #include <stdio.h>
     45 #define HT_PANIC(s) { fprintf(stderr, "aborting on panic: %s\n", s); exit(1); }
     46 #endif
     47 
     48 #ifndef HT_MISSING
     49 #define HT_MISSING ((ht_item_t)0)
     50 #endif
     51 
     52 #ifndef HT_NOMEM
     53 #define HT_NOMEM ((ht_item_t)1)
     54 #endif
     55 
     56 #ifndef HT_DELETED
     57 #define HT_DELETED ((ht_item_t)2)
     58 #endif
     59 
     60 #define DEFINE_HASH_TABLE(HT_NAME)                                          \
     61                                                                             \
     62 typedef HT_NAME##_item_t ht_item_t;                                         \
     63 typedef HT_NAME##_visitor_f ht_visitor_f;                                   \
     64                                                                             \
     65 /* User supplied. */                                                        \
     66 static inline int ht_match(const void *key, size_t len, ht_item_t item);    \
     67 static inline const void *ht_key(ht_item_t item);                           \
     68 static inline size_t ht_key_len(ht_item_t item);                            \
     69                                                                             \
     70 /* Implementation supplied. */                                              \
     71 static ht_item_t ht_insert(hash_table_t *ht,                                \
     72         const void *key, size_t len, ht_item_t new_item, int mode);         \
     73 static ht_item_t ht_find(hash_table_t *ht, const void *key, size_t len);    \
     74 static ht_item_t ht_remove(hash_table_t *ht, const void *key, size_t len);  \
     75 static int ht_init(hash_table_t *ht, size_t count);                         \
     76 static int ht_resize(hash_table_t *ht, size_t count);                       \
     77 static void ht_clear(hash_table_t *ht);                                     \
     78 static void ht_visit(hash_table_t *ht,                                      \
     79         ht_visitor_f *visitor, void *context);                              \
     80                                                                             \
     81 const ht_item_t HT_NAME##_missing = HT_MISSING;                             \
     82 const ht_item_t HT_NAME##_nomem = HT_NOMEM;                                 \
     83 const ht_item_t HT_NAME##_deleted = HT_DELETED;                             \
     84                                                                             \
     85 HT_PRIV void HT_NAME##_clear(HT_NAME##_t *ht)                               \
     86 {                                                                           \
     87     ht_clear(ht);                                                           \
     88 }                                                                           \
     89                                                                             \
     90 HT_PRIV void HT_NAME##_destroy(HT_NAME##_t *ht,                             \
     91         HT_NAME##_visitor_f *destructor, void *context)                     \
     92 {                                                                           \
     93     if (destructor) {                                                       \
     94         ht_visit(ht, destructor, context);                                  \
     95     }                                                                       \
     96     ht_clear(ht);                                                           \
     97 }                                                                           \
     98                                                                             \
     99 HT_PRIV int HT_NAME##_init(HT_NAME##_t *ht, size_t count)                   \
    100 {                                                                           \
    101     return ht_init(ht, count);                                              \
    102 }                                                                           \
    103                                                                             \
    104 HT_PRIV int HT_NAME##_resize(HT_NAME##_t *ht, size_t count)                 \
    105 {                                                                           \
    106     return ht_resize(ht, count);                                            \
    107 }                                                                           \
    108                                                                             \
    109 HT_PRIV ht_item_t HT_NAME##_insert(HT_NAME##_t *ht,                         \
    110         const void *key, size_t len, ht_item_t new_item, int mode)          \
    111 {                                                                           \
    112     return ht_insert(ht, key, len, new_item, mode);                         \
    113 }                                                                           \
    114                                                                             \
    115 HT_PRIV ht_item_t HT_NAME##_insert_item(HT_NAME##_t *ht,                    \
    116         ht_item_t item, int mode)                                           \
    117 {                                                                           \
    118     return ht_insert(ht,                                                    \
    119             ht_key(item),                                                   \
    120             ht_key_len(item),                                               \
    121             item, mode);                                                    \
    122 }                                                                           \
    123                                                                             \
    124 HT_PRIV ht_item_t HT_NAME##_find(HT_NAME##_t *ht,                           \
    125         const void *key, size_t len)                                        \
    126 {                                                                           \
    127     return ht_find(ht, key, len);                                           \
    128 }                                                                           \
    129                                                                             \
    130 HT_PRIV ht_item_t HT_NAME##_find_item(HT_NAME##_t *ht, ht_item_t item)      \
    131 {                                                                           \
    132     return ht_find(ht,                                                      \
    133             ht_key(item),                                                   \
    134             ht_key_len(item));                                              \
    135 }                                                                           \
    136                                                                             \
    137 HT_PRIV ht_item_t HT_NAME##_remove(HT_NAME##_t *ht,                         \
    138         const void *key, size_t len)                                        \
    139 {                                                                           \
    140     return ht_remove(ht, key, len);                                         \
    141 }                                                                           \
    142                                                                             \
    143 HT_PRIV ht_item_t HT_NAME##_remove_item(HT_NAME##_t *ht, ht_item_t item)    \
    144 {                                                                           \
    145     return ht_remove(ht, ht_key(item), ht_key_len(item));                   \
    146 }                                                                           \
    147                                                                             \
    148 HT_PRIV void HT_NAME##_visit(HT_NAME##_t *ht,                               \
    149         HT_NAME##_visitor_f *visitor, void *context)                        \
    150 {                                                                           \
    151     ht_visit(ht, visitor, context);                                         \
    152 }                                                                           \
    153 
    154 #endif /* HASH_TABLE_DEF_H */