X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Frasterize;a=blobdiff_plain;f=map.h;h=20321f88c8dcc86600248c3af0af4c7340ea4cfe;hp=9b408b045ac708963e7cab387ce0ad30a44b3203;hb=09dd89d10e65029f0be313dd463ba1f43cac2fbb;hpb=c875478cdd823c7df8fdc859941bd9e5948c9315 diff --git a/map.h b/map.h index 9b408b0..20321f8 100644 --- a/map.h +++ b/map.h @@ -29,36 +29,54 @@ typedef rbtree_t map_t; */ -#define DECLARE_MAP_TYPE(K, V) DECLARE_MAP_TYPE(K##_t, V##_t, K##_##V) -#define DECLARE_MAP_TYPE2(K, V, N) \ -int map_##N##_search_fn(rbtree_node_t** np, void* data); \ +#define DEFINE_MAP_TYPE(K, V) DEFINE_MAP_TYPE2(K##_t, V##_t, K##_##V) +#define DEFINE_MAP_TYPE2(K, V, N) DEFINE_MAP_TYPE3(K, V, N, *a - *b) +#define DEFINE_MAP_TYPE3(K, V, N, C) \ typedef struct map_##N##_data \ { \ const K key; \ V val; \ } map_##N##_data_t; \ -INLINE_MAYBE map_t* map_##N##_alloc() \ +INLINE_MAYBE \ +int map_##N##_search_fn(rbtree_node_t** np, void* data) \ +{ \ + K* a = (K*)data; \ + rbtree_node_t* n = *np; \ + int c = -1; \ + while (n != rbtree_sentinel) \ + { \ + K* b = (K*)RBTREE_NODE_DATA(n); \ + *np = n; \ + c = (C); \ + if (c == 0) break; \ + else if (c < 0) n = n->left; \ + else n = n->right; \ + } \ + return c; \ +} \ +INLINE_MAYBE \ +map_t* map_##N##_alloc() \ { \ return rbtree_alloc(sizeof(map_##N##_data_t), map_##N##_search_fn); \ } \ -INLINE_MAYBE map_##N##_data_t* \ -map_##N##_insert(map_t* m, K k, V v) \ +INLINE_MAYBE \ +map_##N##_data_t* map_##N##_insert(map_t* m, K k, V v) \ { \ map_##N##_data_t d = {k, v}; \ rbtree_node_t* n = rbtree_insert(m, &d); \ if (!n) return NULL; \ return (map_##N##_data_t*)RBTREE_NODE_DATA(n); \ } \ -INLINE_MAYBE V* \ -map_##N##_delete(map_t* m, K k) \ +INLINE_MAYBE \ +V* map_##N##_delete(map_t* m, K k) \ { \ rbtree_node_t* n = rbtree_delete(m, &k); \ if (!n) return NULL; \ map_##N##_data_t* dp = (map_##N##_data_t*)RBTREE_NODE_DATA(n); \ return &(dp->val); \ } \ -INLINE_MAYBE V* \ -map_##N##_search(map_t* m, K k) \ +INLINE_MAYBE \ +V* map_##N##_search(map_t* m, K k) \ { \ rbtree_node_t* n = rbtree_search(m, &k); \ if (!n) return NULL; \ @@ -66,8 +84,8 @@ map_##N##_search(map_t* m, K k) \ return &(dp->val); \ } \ typedef void (*map_##N##_call_fn_t)(const K* k, V* v); \ -INLINE_MAYBE void \ -map_##N##_call_recurse(rbtree_node_t* n, map_##N##_call_fn_t fn) \ +INLINE_MAYBE \ +void map_##N##_call_recurse(rbtree_node_t* n, map_##N##_call_fn_t fn) \ { \ if (n == rbtree_sentinel) return; \ map_##N##_call_recurse(n->left, fn); \ @@ -75,46 +93,12 @@ map_##N##_call_recurse(rbtree_node_t* n, map_##N##_call_fn_t fn) \ fn(&d->key, &d->val); \ map_##N##_call_recurse(n->right, fn); \ } \ -INLINE_MAYBE void \ -map_##N##_call(const map_t* t, map_##N##_call_fn_t fn) \ +INLINE_MAYBE \ +void map_##N##_call(const map_t* t, map_##N##_call_fn_t fn) \ { \ map_##N##_call_recurse(t->root, fn); \ } -#define DEFINE_MAP_TYPE(K, V) DEFINE_MAP_TYPE2(K##_t, V##_t, K##_##V) -#define DEFINE_MAP_TYPE2(K, V, N) DEFINE_MAP_TYPE3(K, V, N, *a - *b) -#define DEFINE_MAP_TYPE3(K, V, N, C) \ -int map_##N##_search_fn(rbtree_node_t** np, void* data) \ -{ \ - K* a = (K*)data; \ - rbtree_node_t* n = *np; \ - int c = -1; \ - while (n != rbtree_sentinel) \ - { \ - K* b = (K*)RBTREE_NODE_DATA(n); \ - *np = n; \ - c = (C); \ - if (c == 0) break; \ - else if (c < 0) n = n->left; \ - else n = n->right; \ - } \ - return c; \ -} - - -#define DECLARE_AND_DEFINE_MAP_TYPE(K, V) \ - DECLARE_MAP_TYPE(K, V) \ - DEFINE_MAP_TYPE(K, V) - -#define DECLARE_AND_DEFINE_MAP_TYPE2(K, V, N) \ - DECLARE_MAP_TYPE2(K, V, N) \ - DEFINE_MAP_TYPE2(K, V, N) - -#define DECLARE_AND_DEFINE_MAP_TYPE3(K, V, N, C) \ - DECLARE_MAP_TYPE2(K, V, N) \ - DEFINE_MAP_TYPE3(K, V, N, C) - - #endif // _MAP_H_