From 523a682af5207bb9c62888ab8662e37f1e7be4d0 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 1 Jul 2015 12:55:55 -0400 Subject: [PATCH] Basic predicates for constants --- critbit.c | 28 ++++++++++++++-------------- treetrie.c | 12 ++++++------ treetrie.h | 5 +++++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/critbit.c b/critbit.c index 4e7d100..dd32d5f 100644 --- a/critbit.c +++ b/critbit.c @@ -9,10 +9,10 @@ #include "critbit.h" #define RET_IF_NO_PTR(v) \ - ({ tt_node_ptr_t ___w = (v); if (___w == TT_NO_PTR) return TT_NO_PTR; ___w; }) + ({ tt_node_ptr_t ___w = (v); if (TT_NO_PTR_P(___w)) return TT_NO_PTR; ___w; }) int tt_dict_size(tt_arena_t *a, tt_node_ptr_t t) { - if (t == TT_EMPTY_DICT) { + if (TT_EMPTY_DICT_P(t)) { return 0; } else { assert(tt_ptr_tag(t) == TT_TAG_DICT); @@ -25,7 +25,7 @@ static inline int bit_ref(tt_atom_t key, unsigned int bit) { } tt_node_ptr_t tt_dict_get(tt_arena_t *a, tt_node_ptr_t t, tt_atom_t key) { - if (t == TT_EMPTY_DICT) { + if (TT_EMPTY_DICT_P(t)) { return TT_NO_PTR; } @@ -85,7 +85,7 @@ static int set_walk(tt_arena_t *a, if (first_differing_bit == -1) { if (*result == one) { *result = n; - } else if (*result == TT_NO_PTR) { + } else if (TT_NO_PTR_P(*result)) { /* Do nothing. */ } else { *result = tt_cons_node(a, index, TT_NODE_ZERO(a, n), *result); @@ -95,7 +95,7 @@ static int set_walk(tt_arena_t *a, return first_differing_bit; } else { *result = splice_key(a, key, trie, first_differing_bit, one); - if (*result != TT_NO_PTR) { + if (!TT_NO_PTR_P(*result)) { *result = tt_cons_node(a, index, TT_NODE_ZERO(a, n), *result); } return -1; @@ -106,7 +106,7 @@ static int set_walk(tt_arena_t *a, if (first_differing_bit == -1) { if (*result == zero) { *result = n; - } else if (*result == TT_NO_PTR) { + } else if (TT_NO_PTR_P(*result)) { /* Do nothing. */ } else { *result = tt_cons_node(a, index, *result, TT_NODE_ONE(a, n)); @@ -116,7 +116,7 @@ static int set_walk(tt_arena_t *a, return first_differing_bit; } else { *result = splice_key(a, key, trie, first_differing_bit, zero); - if (*result != TT_NO_PTR) { + if (!TT_NO_PTR_P(*result)) { *result = tt_cons_node(a, index, *result, TT_NODE_ONE(a, n)); } return -1; @@ -133,7 +133,7 @@ tt_node_ptr_t tt_dict_set(tt_arena_t *a, tt_atom_t key, tt_node_ptr_t trie) { - if (t == TT_EMPTY_DICT) { + if (TT_EMPTY_DICT_P(t)) { return tt_cons_dict(a, RET_IF_NO_PTR(tt_cons_leaf(a, trie, key)), 1); } @@ -146,7 +146,7 @@ tt_node_ptr_t tt_dict_set(tt_arena_t *a, if (first_differing_bit != -1) { result = splice_key(a, key, trie, first_differing_bit, old_root); } - if (result == TT_NO_PTR) { + if (TT_NO_PTR_P(result)) { return TT_NO_PTR; } else if (result == old_root) { return t; @@ -175,7 +175,7 @@ tt_node_ptr_t tt_dict_remove1(tt_arena_t *a, if (bit_ref(key, index)) { tt_node_ptr_t n1 = RET_IF_NO_PTR(tt_dict_remove1(a, TT_NODE_ONE(a,n), key, removed_count)); - if (n1 == TT_EMPTY_DICT) { + if (TT_EMPTY_DICT_P(n1)) { return TT_NODE_ZERO(a,n); } else { return tt_cons_node(a, index, TT_NODE_ZERO(a,n), n1); @@ -183,7 +183,7 @@ tt_node_ptr_t tt_dict_remove1(tt_arena_t *a, } else { tt_node_ptr_t n1 = RET_IF_NO_PTR(tt_dict_remove1(a, TT_NODE_ZERO(a,n), key, removed_count)); - if (n1 == TT_EMPTY_DICT) { + if (TT_EMPTY_DICT_P(n1)) { return TT_NODE_ONE(a,n); } else { return tt_cons_node(a, index, n1, TT_NODE_ONE(a,n)); @@ -199,7 +199,7 @@ tt_node_ptr_t tt_dict_remove(tt_arena_t *a, tt_node_ptr_t t, tt_atom_t key) { - if (t == TT_EMPTY_DICT) { + if (TT_EMPTY_DICT_P(t)) { return TT_EMPTY_DICT; } @@ -209,7 +209,7 @@ tt_node_ptr_t tt_dict_remove(tt_arena_t *a, int removed_count = 0; tt_node_ptr_t n = RET_IF_NO_PTR(tt_dict_remove1(a, TT_DICT_ROOT(a,t), key, &removed_count)); - if (n == TT_EMPTY_DICT) { + if (TT_EMPTY_DICT_P(n)) { return TT_EMPTY_DICT; } else { return tt_cons_dict(a, n, TT_DICT_SIZE(a,t) - removed_count); @@ -242,7 +242,7 @@ void tt_dict_foreach(tt_arena_t *a, void *context, void (*f)(void *, tt_atom_t key, tt_node_ptr_t trie)) { - if (t != TT_EMPTY_DICT) { + if (!TT_EMPTY_DICT_P(t)) { assert(tt_ptr_tag(t) == TT_TAG_DICT); tt_dict_foreach1(a, TT_DICT_ROOT(a,t), context, f); } diff --git a/treetrie.c b/treetrie.c index 3d83367..85489d6 100644 --- a/treetrie.c +++ b/treetrie.c @@ -143,7 +143,7 @@ static void register_node(tt_arena_t *a, tt_node_ptr_t p, tt_hash_t initial_hash a->max_probe = i; } - if (candidate == TT_NO_PTR) { + if (TT_NO_PTR_P(candidate)) { /* This slot in the table is free. */ /* printf("slot free!\n"); */ a->table[index].ptr = p; @@ -219,7 +219,7 @@ static int tt_grow(tt_arena_t *a) { for (i = 0; i < old_table_length; i++) { tt_node_ptr_t p = old_table[i].ptr; tt_hash_t h = old_table[i].hash; - if (p != TT_NO_PTR) { + if (!TT_NO_PTR_P(p)) { register_node(a, p, h); } } @@ -274,7 +274,7 @@ void tt_dump_arena(tt_arena_t *a) { tt_node_ptr_t p = a->table[i].ptr; tt_node_idx_t n = tt_ptr_idx(p); - if (p == TT_NO_PTR) { + if (TT_NO_PTR_P(p)) { continue; } @@ -387,7 +387,7 @@ static void recycle_node(tt_arena_t *a, tt_node_ptr_t p) { tt_node_ptr_t candidate = a->table[index].ptr; /* printf("hunting i=%d index=%d p=%d candidate=%d\n", i, index, p, candidate); */ - assert(candidate != TT_NO_PTR); /* Internal error if node not in table */ + assert(!TT_NO_PTR_P(candidate)); /* Internal error if node not in table */ if (candidate == p) { /* We found it. Now swap in elements. */ @@ -399,7 +399,7 @@ static void recycle_node(tt_arena_t *a, tt_node_ptr_t p) { a->table[index].ptr = TT_NO_PTR; - if (next_p == TT_NO_PTR) { + if (TT_NO_PTR_P(next_p)) { break; } @@ -453,7 +453,7 @@ tt_node_ptr_t tt_arena_cons(tt_arena_t *a, tt_node_idx_t candidate_i = tt_ptr_idx(candidate); /* printf("cons at %d candidate %d\n", i, candidate); */ - if (candidate == TT_NO_PTR) { + if (TT_NO_PTR_P(candidate)) { /* printf("cons empty cell\n"); */ break; } diff --git a/treetrie.h b/treetrie.h index e50a746..48389e1 100644 --- a/treetrie.h +++ b/treetrie.h @@ -32,6 +32,11 @@ typedef uint32_t tt_node_ptr_t; /* An index shifted left 2 with tag or'd in low #define TT_OK (tt_mkptr(TT_OK_IDX, TT_TAG_SPECIAL)) #define TT_EMPTY_DICT (tt_mkptr(TT_EMPTY_DICT_IDX, TT_TAG_SPECIAL)) +#define TT_NO_PTR_P(x) ((x) == TT_NO_PTR) +#define TT_EMPTY_P(x) ((x) == TT_EMPTY) +#define TT_OK_P(x) ((x) == TT_OK) +#define TT_EMPTY_DICT_P(x) ((x) == TT_EMPTY_DICT) + typedef uint32_t tt_atom_t; /* Atom number 0 is the wildcard atom. */ typedef uint32_t tt_hash_t;