github.com/moontrade/nogc@v0.1.7/collections/tree/art.h (about) 1 #ifndef ART_H 2 #define ART_H 3 4 #include "lock.h" 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 #include <stdint.h> 11 12 #define NODE4 1 13 #define NODE16 2 14 #define NODE48 3 15 #define NODE256 4 16 17 #define MAX_PREFIX_LEN 10 18 19 #if defined(__GNUC__) && !defined(__clang__) 20 # if __STDC_VERSION__ >= 199901L && 402 == (__GNUC__ * 100 + __GNUC_MINOR__) 21 /* 22 * GCC 4.2.2's C99 inline keyword support is pretty broken; avoid. Introduced in 23 * GCC 4.2.something, fixed in 4.3.0. So checking for specific major.minor of 24 * 4.2 is fine. 25 */ 26 # define BROKEN_GCC_C99_INLINE 27 # endif 28 #endif 29 30 struct art_lock_api; 31 extern const struct art_lock_api art_lock_api_unfair; 32 extern const struct art_lock_api art_lock_api_fair; 33 34 typedef struct art_value { 35 union { 36 void* value; 37 uint64_t uvalue; 38 }; 39 } art_value; 40 41 typedef struct art_search_result { 42 art_value value; 43 size_t found; 44 } art_search_result; 45 46 static const art_value ART_NULL_VALUE = { 47 .value=0 48 }; 49 50 static const art_search_result ART_NOT_FOUND = { 51 .found=0 52 }; 53 54 //#define ART_NULL_VALUE {.file=0,.offset=0,.value=NULL} 55 56 typedef int(*art_callback)(void *data, const unsigned char *key, uint32_t key_len, art_value value); 57 58 /** 59 * This struct is included as part 60 * of all the various node sizes 61 */ 62 typedef struct { 63 uint32_t partial_len; 64 uint8_t type; 65 uint8_t num_children; 66 unsigned char partial[MAX_PREFIX_LEN]; 67 } art_node; 68 69 /** 70 * Small node with only 4 children 71 */ 72 typedef struct { 73 art_node n; 74 unsigned char keys[4]; 75 art_node *children[4]; 76 } art_node4; 77 78 /** 79 * Node with 16 children 80 */ 81 typedef struct { 82 art_node n; 83 unsigned char keys[16]; 84 art_node *children[16]; 85 } art_node16; 86 87 /** 88 * Node with 48 children, but 89 * a full 256 byte field. 90 */ 91 typedef struct { 92 art_node n; 93 unsigned char keys[256]; 94 art_node *children[48]; 95 } art_node48; 96 97 /** 98 * Full node with 256 children 99 */ 100 typedef struct { 101 art_node n; 102 art_node *children[256]; 103 } art_node256; 104 105 /** 106 * Represents a leaf. These are 107 * of arbitrary size, as they include the key. 108 */ 109 typedef struct { 110 art_value value; 111 uint32_t key_len; 112 unsigned char key[0]; 113 } art_leaf; 114 115 /** 116 * Main struct, points to root. 117 */ 118 typedef struct art_tree { 119 art_node *root; 120 uint64_t size; 121 uint64_t memory; 122 int32_t free; 123 int32_t fair; 124 int32_t calc_memory; 125 uint32_t lock_; 126 void* lock; 127 } art_tree; 128 129 /** 130 * Initializes an ART tree 131 * @return 0 on success. 132 */ 133 int art_tree_init(art_tree *t); 134 135 /** 136 * Initializes a lock for an ART tree. 137 * Access to the tree will become thread-safe. 138 */ 139 void art_tree_init_lock(art_tree *t); 140 141 /** 142 * Destroys an ART tree 143 * @return 0 on success. 144 */ 145 int art_tree_destroy(art_tree *t); 146 147 /** 148 * Returns the size of the ART tree. 149 */ 150 #ifdef BROKEN_GCC_C99_INLINE 151 # define art_size(t) ((t)->size) 152 #else 153 inline uint64_t art_size(art_tree *t) { 154 return t->size; 155 } 156 #endif 157 158 void art_sleep(uint64_t nanos); 159 160 /** 161 * inserts a new value into the art tree 162 * @arg t the tree 163 * @arg key the key 164 * @arg key_len the length of the key 165 * @arg value opaque value. 166 * @return null if the item was newly inserted, otherwise 167 * the old value pointer is returned. 168 */ 169 art_value art_insert(art_tree *t, const unsigned char *key, int key_len, art_value value); 170 171 art_search_result art_insert_value(art_tree *t, const unsigned char *key, int key_len, art_value value); 172 173 /** 174 * inserts a new value into the art tree (not replacing) 175 * @arg t the tree 176 * @arg key the key 177 * @arg key_len the length of the key 178 * @arg value opaque value. 179 * @return null if the item was newly inserted, otherwise 180 * the old value pointer is returned. 181 */ 182 art_value art_insert_no_replace(art_tree *t, const unsigned char *key, int key_len, art_value value); 183 184 art_search_result art_insert_no_replace_value(art_tree *t, const unsigned char *key, int key_len, art_value value); 185 186 /** 187 * Deletes a value from the ART tree 188 * @arg t The tree 189 * @arg key The key 190 * @arg key_len The length of the key 191 * @return NULL if the item was not found, otherwise 192 * the value pointer is returned. 193 */ 194 art_value art_delete(art_tree *t, const unsigned char *key, int key_len); 195 196 art_search_result art_delete_value(art_tree *t, const unsigned char *key, int key_len); 197 198 /** 199 * Searches for a value in the ART tree 200 * @arg t The tree 201 * @arg key The key 202 * @arg key_len The length of the key 203 * @return NULL if the item was not found, otherwise 204 * the value pointer is returned. 205 */ 206 art_search_result art_search(const art_tree *t, const unsigned char *key, int key_len); 207 208 /** 209 * Returns the minimum valued leaf 210 * @return The minimum leaf or NULL 211 */ 212 art_leaf* art_minimum(art_tree *t); 213 214 /** 215 * Returns the maximum valued leaf 216 * @return The maximum leaf or NULL 217 */ 218 art_leaf* art_maximum(art_tree *t); 219 220 /** 221 * Iterates through the entries pairs in the map, 222 * invoking a callback for each. The call back gets a 223 * key, value for each and returns an integer stop value. 224 * If the callback returns non-zero, then the iteration stops. 225 * @arg t The tree to iterate over 226 * @arg cb The callback function to invoke 227 * @arg data Opaque handle passed to the callback 228 * @return 0 on success, or the return of the callback. 229 */ 230 int art_iter(art_tree *t, art_callback cb, void *data); 231 232 /** 233 * Iterates through the entries pairs in the map, 234 * invoking a callback for each that matches a given prefix. 235 * The call back gets a key, value for each and returns an integer stop value. 236 * If the callback returns non-zero, then the iteration stops. 237 * @arg t The tree to iterate over 238 * @arg prefix The prefix of keys to read 239 * @arg prefix_len The length of the prefix 240 * @arg cb The callback function to invoke 241 * @arg data Opaque handle passed to the callback 242 * @return 0 on success, or the return of the callback. 243 */ 244 int art_iter_prefix(art_tree *t, const unsigned char *prefix, int prefix_len, art_callback cb, void *data); 245 246 #ifdef __cplusplus 247 } 248 #endif 249 250 #endif