nostrdb

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

int_set.h (1369B)


      1 #ifndef INT_SET_H
      2 #define INT_SET_H
      3 
      4 #include "ptr_set.h"
      5 
      6 /*
      7  * The values 0, 1, and 2 are reserved so we map integers
      8  * before casting them to void *.
      9  *
     10  * Instead we disallow the largest positive integers.
     11  *
     12  * This is specfic to the implementation of ptr_set, so
     13  * if it changes, we may have to change here as well.
     14  */
     15 
     16 #define HT_INT_SET_OFFSET ((1 << (8 * sizeof(int) - 1)) - 2)
     17 #define HT_INT_TO_PTR(x) ((void *)(size_t)((x) - HT_INT_SET_OFFSET))
     18 #define HT_PTR_TO_INT(x) ((int)(size_t)(x) + HT_INT_SET_OFFSET)
     19 
     20 /* Return value helpers. */
     21 #define INT_SET_IS_MISSING(x) (HT_PTR_SET_MISSING(HT_INT_TO_PTR(x)))
     22 #define INT_SET_IS_ERROR(x) (HT_PTR_SET_IS_ERROR(HT_INT_TO_PTR(x)))
     23 #define INT_SET_IS_VALID(x) (HT_PTR_SET_IS_VALID(HT_INT_TO_PTR(x)))
     24 
     25 typedef ptr_set_t int_set_t;
     26 
     27 /* Returns 1 if already present, 0 otherwise. */
     28 static inline int int_set_add(int_set_t *S, int x)
     29 {
     30     return ptr_set_insert_item(S, HT_INT_TO_PTR(x), ht_keep) != 0;
     31 }
     32 
     33 /* Returns 1 if removed, 0 otherwise. */
     34 static inline int int_set_remove(int_set_t *S, int x)
     35 {
     36     return ptr_set_remove_item(S, HT_INT_TO_PTR(x)) != 0;
     37 }
     38 
     39 static inline int int_set_count(int_set_t *S)
     40 {
     41     return ptr_set_count(S);
     42 }
     43 
     44 /* Returns 1 if present, 0 otherwise. */
     45 static inline int int_set_exists(int_set_t *S, int x)
     46 {
     47     return ptr_set_exists(S, HT_INT_TO_PTR(x));
     48 }
     49 
     50 #endif /* INT_SET_H */