projects
/
apps
/
pfixtools.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
More readable code.
[apps/pfixtools.git]
/
common
/
trie.c
diff --git
a/common/trie.c
b/common/trie.c
index
ea60f63
..
ef32c96
100644
(file)
--- a/
common/trie.c
+++ b/
common/trie.c
@@
-49,6
+49,9
@@
struct trie_entry_t {
#define TRIE_ENTRY_INIT { 0, 0, 0, 0 }
ARRAY(trie_entry_t)
#define TRIE_ENTRY_INIT { 0, 0, 0, 0 }
ARRAY(trie_entry_t)
+#define str(trie, entry) array_ptr((trie)->c, (entry)->c_offset)
+#define key(trie, id) array_ptr((trie)->keys, array_elt((trie)->keys_offset, (id)))
+
struct trie_t {
A(trie_entry_t) entries;
A(char) c;
struct trie_t {
A(trie_entry_t) entries;
A(char) c;
@@
-86,7
+89,7
@@
static inline bool trie_entry_c_match(const trie_t *trie,
const trie_entry_t *entry,
const char *key)
{
const trie_entry_t *entry,
const char *key)
{
- const char *c =
array_ptr(trie->c, entry->c_offset
);
+ const char *c =
str(trie, entry
);
int i = 0;
for (i = 0 ; i < entry->c_len ; ++i) {
if (key[i] != c[i]) {
int i = 0;
for (i = 0 ; i < entry->c_len ; ++i) {
if (key[i] != c[i]) {
@@
-99,17
+102,17
@@
static inline bool trie_entry_c_match(const trie_t *trie,
static inline bool trie_entry_match(const trie_t *trie,
const trie_entry_t *entry, const char *key)
{
static inline bool trie_entry_match(const trie_t *trie,
const trie_entry_t *entry, const char *key)
{
- return !!(strcmp(
array_ptr(trie->c, entry->c_offset
), key) == 0);
+ return !!(strcmp(
str(trie, entry
), key) == 0);
}
static inline bool trie_entry_prefix(const trie_t *trie,
const trie_entry_t *entry, const char *key)
{
int len = entry->c_len;
}
static inline bool trie_entry_prefix(const trie_t *trie,
const trie_entry_t *entry, const char *key)
{
int len = entry->c_len;
- if (len > 0 &&
array_elt(trie->c, entry->c_offset + len - 1)
== '\0') {
+ if (len > 0 &&
str(trie, entry)[len -1]
== '\0') {
--len;
}
--len;
}
- return !!(strncmp(
array_ptr(trie->c, entry->c_offset
), key, len) == 0);
+ return !!(strncmp(
str(trie, entry
), key, len) == 0);
}
static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
}
static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
@@
-132,7
+135,7
@@
static inline const trie_entry_t* trie_entry_child(const trie_t *trie,
while (start < end) {
uint32_t mid = (start + end) >> 1;
const trie_entry_t* child = array_ptr(trie->entries, mid);
while (start < end) {
uint32_t mid = (start + end) >> 1;
const trie_entry_t* child = array_ptr(trie->entries, mid);
- const char c2 =
array_elt(trie->c, child->c_offset)
;
+ const char c2 =
str(trie, child)[0]
;
if (c2 == c) {
return child;
if (c2 == c) {
return child;
@@
-157,16
+160,29
@@
static inline uint32_t trie_add_leaf(trie_t *trie, const char *key)
{
trie_entry_t *entry;
int len = m_strlen(key) + 1;
{
trie_entry_t *entry;
int len = m_strlen(key) + 1;
- entry = array_ptr(trie->entries, trie_entry_new(trie));
+ int id = trie_entry_new(trie);
+ entry = array_ptr(trie->entries, id);
entry->c_offset = trie->c.len;
entry->c_len = len;
entry->c_offset = trie->c.len;
entry->c_len = len;
+#ifdef CHECK_INTEGRITY
+ for (int i = 0 ; i < len - 1 ; ++i) {
+ if (key[i] == '\0') {
+ printf("Found a '\\0' in the string of the leaf\n");
+ abort();
+ }
+ }
+ if (key[len - 1] != '\0') {
+ printf("Key does not end with a '\\0'");
+ abort();
+ }
+#endif
array_append(trie->c, key, len);
return trie->entries.len - 1;
}
array_append(trie->c, key, len);
return trie->entries.len - 1;
}
-static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
- uint32_t pchild)
+static inline void trie_entry_insert_child(trie_t *trie, uint32_t id, uint32_t pchild)
{
{
+ trie_entry_t *entry = array_ptr(trie->entries, id);
if (entry->children_len == 0) {
entry->children_offset = pchild;
entry->children_len = 1;
if (entry->children_len == 0) {
entry->children_offset = pchild;
entry->children_len = 1;
@@
-180,16
+196,19
@@
static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
}
}
}
}
-static inline void trie_entry_split(trie_t *trie,
trie_entry_t *entry
, uint16_t pos)
+static inline void trie_entry_split(trie_t *trie,
uint32_t id
, uint16_t pos)
{
trie_entry_t *child;
{
trie_entry_t *child;
+ trie_entry_t *entry;
child = array_ptr(trie->entries, trie_entry_new(trie));
child = array_ptr(trie->entries, trie_entry_new(trie));
+ entry = array_ptr(trie->entries, id);
if (pos == 0) {
child->c_offset = entry->c_offset;
child->c_len = entry->c_len;
entry->c_offset = 0;
entry->c_len = 0;
} else {
if (pos == 0) {
child->c_offset = entry->c_offset;
child->c_len = entry->c_len;
entry->c_offset = 0;
entry->c_len = 0;
} else {
+ assert(pos <= entry->c_len);
child->c_offset = entry->c_offset + pos;
child->c_len = entry->c_len - pos;
entry->c_len = pos;
child->c_offset = entry->c_offset + pos;
child->c_len = entry->c_len - pos;
entry->c_len = pos;
@@
-218,18
+237,21
@@
static inline void trie_compile_aux(trie_t *trie, uint32_t id,
uint32_t fork_pos = 0;
char current = '\0';
uint32_t fork_pos = 0;
char current = '\0';
+#ifdef CHECK_INTEGRITY
+ assert(strcmp(key(trie, first_key) + offset, str(trie, entry)) == 0);
+#endif
+
for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) {
for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) {
- current =
array_elt(trie->keys, array_elt(trie->keys_offset, first_key) + offset)
;
+ current =
key(trie, first_key)[offset]
;
for (uint32_t i = first_key + 1 ; i < last_key ; ++i) {
for (uint32_t i = first_key + 1 ; i < last_key ; ++i) {
- const char *
str = array_ptr(trie->keys, array_elt(trie->keys_offset, i))
;
- const char c =
str[offset]
;
+ const char *
ckey = key(trie, i) + offset
;
+ const char c =
*ckey
;
if (c != current) {
array_ensure_capacity_delta(trie->entries, 2);
if (fork_pos == 0) {
if (c != current) {
array_ensure_capacity_delta(trie->entries, 2);
if (fork_pos == 0) {
- trie_entry_split(trie,
array_ptr(trie->entries, id)
, off_diff);
+ trie_entry_split(trie,
id
, off_diff);
}
}
- trie_entry_insert_child(trie, array_ptr(trie->entries, id),
- trie_add_leaf(trie, str + offset));
+ trie_entry_insert_child(trie, id, trie_add_leaf(trie, ckey));
forks[fork_pos++] = i;
current = c;
}
forks[fork_pos++] = i;
current = c;
}
@@
-262,8
+284,8
@@
void trie_compile(trie_t *trie, bool memlock)
# include "qsort.c"
}
# include "qsort.c"
}
- array_ensure_capacity(trie->entries, trie->keys.len);
- trie_compile_aux(trie, trie_add_leaf(trie,
trie->keys.data
),
+ array_ensure_capacity(trie->entries, trie->keys
_offset
.len);
+ trie_compile_aux(trie, trie_add_leaf(trie,
key(trie, 0)
),
0, trie->keys_offset.len, 0, 0);
trie_cleanup_build_data(trie);
array_adjust(trie->entries);
0, trie->keys_offset.len, 0, 0);
trie_cleanup_build_data(trie);
array_adjust(trie->entries);
@@
-375,7
+397,7
@@
static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
fputs(" ", stdout);
}
if (entry->c_len == 0) {
fputs(" ", stdout);
}
if (entry->c_len == 0) {
- fputs("(
nil
)", stdout);
+ fputs("(
0
)", stdout);
} else {
const char *c = array_ptr(trie->c, entry->c_offset);
printf("(%d) ", entry->c_len);
} else {
const char *c = array_ptr(trie->c, entry->c_offset);
printf("(%d) ", entry->c_len);