Basic predicates for constants
This commit is contained in:
parent
f60bc9cd12
commit
523a682af5
28
critbit.c
28
critbit.c
|
@ -9,10 +9,10 @@
|
||||||
#include "critbit.h"
|
#include "critbit.h"
|
||||||
|
|
||||||
#define RET_IF_NO_PTR(v) \
|
#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) {
|
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;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
assert(tt_ptr_tag(t) == TT_TAG_DICT);
|
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) {
|
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;
|
return TT_NO_PTR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ static int set_walk(tt_arena_t *a,
|
||||||
if (first_differing_bit == -1) {
|
if (first_differing_bit == -1) {
|
||||||
if (*result == one) {
|
if (*result == one) {
|
||||||
*result = n;
|
*result = n;
|
||||||
} else if (*result == TT_NO_PTR) {
|
} else if (TT_NO_PTR_P(*result)) {
|
||||||
/* Do nothing. */
|
/* Do nothing. */
|
||||||
} else {
|
} else {
|
||||||
*result = tt_cons_node(a, index, TT_NODE_ZERO(a, n), *result);
|
*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;
|
return first_differing_bit;
|
||||||
} else {
|
} else {
|
||||||
*result = splice_key(a, key, trie, first_differing_bit, one);
|
*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);
|
*result = tt_cons_node(a, index, TT_NODE_ZERO(a, n), *result);
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -106,7 +106,7 @@ static int set_walk(tt_arena_t *a,
|
||||||
if (first_differing_bit == -1) {
|
if (first_differing_bit == -1) {
|
||||||
if (*result == zero) {
|
if (*result == zero) {
|
||||||
*result = n;
|
*result = n;
|
||||||
} else if (*result == TT_NO_PTR) {
|
} else if (TT_NO_PTR_P(*result)) {
|
||||||
/* Do nothing. */
|
/* Do nothing. */
|
||||||
} else {
|
} else {
|
||||||
*result = tt_cons_node(a, index, *result, TT_NODE_ONE(a, n));
|
*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;
|
return first_differing_bit;
|
||||||
} else {
|
} else {
|
||||||
*result = splice_key(a, key, trie, first_differing_bit, zero);
|
*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));
|
*result = tt_cons_node(a, index, *result, TT_NODE_ONE(a, n));
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -133,7 +133,7 @@ tt_node_ptr_t tt_dict_set(tt_arena_t *a,
|
||||||
tt_atom_t key,
|
tt_atom_t key,
|
||||||
tt_node_ptr_t trie)
|
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);
|
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) {
|
if (first_differing_bit != -1) {
|
||||||
result = splice_key(a, key, trie, first_differing_bit, old_root);
|
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;
|
return TT_NO_PTR;
|
||||||
} else if (result == old_root) {
|
} else if (result == old_root) {
|
||||||
return t;
|
return t;
|
||||||
|
@ -175,7 +175,7 @@ tt_node_ptr_t tt_dict_remove1(tt_arena_t *a,
|
||||||
if (bit_ref(key, index)) {
|
if (bit_ref(key, index)) {
|
||||||
tt_node_ptr_t n1 =
|
tt_node_ptr_t n1 =
|
||||||
RET_IF_NO_PTR(tt_dict_remove1(a, TT_NODE_ONE(a,n), key, removed_count));
|
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);
|
return TT_NODE_ZERO(a,n);
|
||||||
} else {
|
} else {
|
||||||
return tt_cons_node(a, index, TT_NODE_ZERO(a,n), n1);
|
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 {
|
} else {
|
||||||
tt_node_ptr_t n1 =
|
tt_node_ptr_t n1 =
|
||||||
RET_IF_NO_PTR(tt_dict_remove1(a, TT_NODE_ZERO(a,n), key, removed_count));
|
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);
|
return TT_NODE_ONE(a,n);
|
||||||
} else {
|
} else {
|
||||||
return tt_cons_node(a, index, n1, TT_NODE_ONE(a,n));
|
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_node_ptr_t t,
|
||||||
tt_atom_t key)
|
tt_atom_t key)
|
||||||
{
|
{
|
||||||
if (t == TT_EMPTY_DICT) {
|
if (TT_EMPTY_DICT_P(t)) {
|
||||||
return TT_EMPTY_DICT;
|
return TT_EMPTY_DICT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +209,7 @@ tt_node_ptr_t tt_dict_remove(tt_arena_t *a,
|
||||||
int removed_count = 0;
|
int removed_count = 0;
|
||||||
tt_node_ptr_t n =
|
tt_node_ptr_t n =
|
||||||
RET_IF_NO_PTR(tt_dict_remove1(a, TT_DICT_ROOT(a,t), key, &removed_count));
|
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;
|
return TT_EMPTY_DICT;
|
||||||
} else {
|
} else {
|
||||||
return tt_cons_dict(a, n, TT_DICT_SIZE(a,t) - removed_count);
|
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 *context,
|
||||||
void (*f)(void *, tt_atom_t key, tt_node_ptr_t trie))
|
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);
|
assert(tt_ptr_tag(t) == TT_TAG_DICT);
|
||||||
tt_dict_foreach1(a, TT_DICT_ROOT(a,t), context, f);
|
tt_dict_foreach1(a, TT_DICT_ROOT(a,t), context, f);
|
||||||
}
|
}
|
||||||
|
|
12
treetrie.c
12
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;
|
a->max_probe = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (candidate == TT_NO_PTR) {
|
if (TT_NO_PTR_P(candidate)) {
|
||||||
/* This slot in the table is free. */
|
/* This slot in the table is free. */
|
||||||
/* printf("slot free!\n"); */
|
/* printf("slot free!\n"); */
|
||||||
a->table[index].ptr = p;
|
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++) {
|
for (i = 0; i < old_table_length; i++) {
|
||||||
tt_node_ptr_t p = old_table[i].ptr;
|
tt_node_ptr_t p = old_table[i].ptr;
|
||||||
tt_hash_t h = old_table[i].hash;
|
tt_hash_t h = old_table[i].hash;
|
||||||
if (p != TT_NO_PTR) {
|
if (!TT_NO_PTR_P(p)) {
|
||||||
register_node(a, p, h);
|
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_ptr_t p = a->table[i].ptr;
|
||||||
tt_node_idx_t n = tt_ptr_idx(p);
|
tt_node_idx_t n = tt_ptr_idx(p);
|
||||||
|
|
||||||
if (p == TT_NO_PTR) {
|
if (TT_NO_PTR_P(p)) {
|
||||||
continue;
|
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;
|
tt_node_ptr_t candidate = a->table[index].ptr;
|
||||||
|
|
||||||
/* printf("hunting i=%d index=%d p=%d candidate=%d\n", i, index, p, candidate); */
|
/* 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) {
|
if (candidate == p) {
|
||||||
/* We found it. Now swap in elements. */
|
/* 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;
|
a->table[index].ptr = TT_NO_PTR;
|
||||||
|
|
||||||
if (next_p == TT_NO_PTR) {
|
if (TT_NO_PTR_P(next_p)) {
|
||||||
break;
|
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);
|
tt_node_idx_t candidate_i = tt_ptr_idx(candidate);
|
||||||
|
|
||||||
/* printf("cons at %d candidate %d\n", i, 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"); */
|
/* printf("cons empty cell\n"); */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_OK (tt_mkptr(TT_OK_IDX, TT_TAG_SPECIAL))
|
||||||
#define TT_EMPTY_DICT (tt_mkptr(TT_EMPTY_DICT_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_atom_t; /* Atom number 0 is the wildcard atom. */
|
||||||
|
|
||||||
typedef uint32_t tt_hash_t;
|
typedef uint32_t tt_hash_t;
|
||||||
|
|
Loading…
Reference in New Issue