nostrdb

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

ptr_set.c (1535B)


      1 /*
      2  * Creates a set of stored pointers by using the pointer itself as key.
      3  *
      4  * (void *)0 (HT_MISSING) cannot be stored.
      5  * (void *)1 (HT_DELETED) also cannot be stored.
      6  *
      7  * ht_item, ht_key, ht_key_len, and ht_match are required.
      8  *
      9  * In this case HT_HASH_FUNCTION is also required because
     10  * we do not read the content of the key but use the pointer
     11  * itself as a key. The default behavior would crash.
     12  *
     13  * Only one hash table can be defined in a single compilation unit
     14  * because of static function names in the generic implementation.
     15  */
     16 
     17 #include "ptr_set.h"
     18 
     19 static inline size_t ptr_set_hash_function(const void *s, size_t len);
     20 #define HT_HASH_FUNCTION ptr_set_hash_function
     21 
     22 #define HT_LOAD_FACTOR 0.7
     23 #include "hash_table_def.h"
     24 DEFINE_HASH_TABLE(ptr_set)
     25 
     26 #if defined(PTR_SET_RH)
     27 #include "hash_table_impl_rh.h"
     28 #else
     29 #include "hash_table_impl.h"
     30 #endif
     31 
     32 static inline const void *ht_key(ht_item_t x)
     33 {
     34     return (const void *)x;
     35 }
     36 
     37 static inline size_t ht_key_len(ht_item_t x)
     38 {
     39     return sizeof(x);
     40 }
     41 
     42 static inline int ht_match(const void *key, size_t len, ht_item_t x)
     43 {
     44     (void)len;
     45     return (size_t)key == (size_t)x;
     46 }
     47 
     48 static inline size_t ptr_set_hash_function(const void *s, size_t len)
     49 {
     50 #if defined (PTR_SET_PTR_HASH)
     51     /* Murmur hash like finalization step. */
     52     return ht_ptr_hash_function(s, len);
     53 #elif defined (PTR_SET_INT_HASH)
     54     /* Knuths multiplication. */
     55     return ht_int_hash_function(s, len);
     56 #else
     57     (void)len;
     58     return ht_default_hash_function(&s, sizeof(char *));
     59 #endif
     60 }