More readable code.
[apps/pfixtools.git] / common / trie.c
index 5bf18ec..ef32c96 100644 (file)
@@ -33,8 +33,6 @@
  * Copyright © 2008 Florent Bruneau
  */
 
-#include <sys/mman.h>
-
 #include "array.h"
 #include "str.h"
 #include "trie.h"
 typedef struct trie_entry_t trie_entry_t;
 
 struct trie_entry_t {
-    int32_t  c_offset;
-    int32_t  c_len;
+    uint32_t c_offset;
+    uint32_t children_offset;
 
-    int32_t children_offset;
-    int32_t children_len;
+    uint16_t c_len;
+    uint16_t children_len;
 };
 #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;
@@ -86,28 +87,32 @@ void trie_delete(trie_t **trie)
  */
 static inline bool trie_entry_c_match(const trie_t *trie,
                                       const trie_entry_t *entry,
-                                      const char *key, int *pos)
+                                      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]) {
-            if (pos) {
-                *pos = i;
-            }
             return false;
         }
     }
-    if (pos) {
-        *pos = i;
-    }
     return true;
 }
 
 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;
+    if (len > 0 && str(trie, entry)[len -1] == '\0') {
+        --len;
+    }
+    return !!(strncmp(str(trie, entry), key, len) == 0);
 }
 
 static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
@@ -119,56 +124,65 @@ static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
  * Only the first character of the children is taken into account in the
  * lookup. The current entry is assumed to match the key.
  */
-static inline trie_entry_t* trie_entry_child(const trie_t *trie,
-                                             const trie_entry_t* entry,
-                                             const char *key)
+static inline const trie_entry_t* trie_entry_child(const trie_t *trie,
+                                                   const trie_entry_t* entry,
+                                                   const char *key)
 {
-    int start = entry->children_offset;
-    int end   = start + entry->children_len;
+    uint32_t start = entry->children_offset;
+    uint32_t end   = start + entry->children_len;
     const char c = *key;
 
     while (start < end) {
-        int mid = (start + end) / 2;
-        trie_entry_t* child = array_ptr(trie->entries, mid);
-        const char c2 = array_elt(trie->c, child->c_offset);
+        uint32_t mid = (start + end) >> 1;
+        const trie_entry_t* child = array_ptr(trie->entries, mid);
+        const char c2 = str(trie, child)[0];
 
-        if (child->c_len) {
-            if (c2 == c) {
-                return child;
-            }
-            if (c < c2) {
-                end = mid;
-            } else {
-                start = mid + 1;
-            }
+        if (c2 == c) {
+          return child;
+        }
+        if (c < c2) {
+          end = mid;
         } else {
-            abort();
+          start = mid + 1;
         }
     }
     return NULL;
 }
 
-static inline int trie_entry_new(trie_t *trie)
+static inline uint32_t trie_entry_new(trie_t *trie)
 {
     const trie_entry_t e = TRIE_ENTRY_INIT;
     array_add(trie->entries, e);
-    return trie->entries.len;
+    return trie->entries.len - 1;
 }
 
-static inline int trie_add_leaf(trie_t *trie, const char *key)
+static inline uint32_t trie_add_leaf(trie_t *trie, const char *key)
 {
     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;
+#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;
 }
 
-static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
-                                           int 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;
@@ -182,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, int pos)
+static inline void trie_entry_split(trie_t *trie, uint32_t id, uint16_t pos)
 {
     trie_entry_t *child;
+    trie_entry_t *entry;
     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 {
+        assert(pos <= entry->c_len);
         child->c_offset = entry->c_offset + pos;
         child->c_len    = entry->c_len - pos;
         entry->c_len    = pos;
@@ -212,26 +229,29 @@ void trie_insert(trie_t *trie, const char* key)
 }
 
 
-static inline void trie_compile_aux(trie_t *trie, int id,
-                                    int first_key, int last_key, int offset,
-                                    int initial_diff)
+static inline void trie_compile_aux(trie_t *trie, uint32_t id,
+                                    uint32_t first_key, uint32_t last_key,
+                                    int offset, int initial_diff)
 {
-    int forks[256];
-    int fork_pos = 0;
+    uint32_t forks[256];
+    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) {
-        current = array_elt(trie->keys, array_elt(trie->keys_offset, first_key) + offset);
-        for (int 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];
+        current = key(trie, first_key)[offset];
+        for (uint32_t i = first_key + 1 ; i < last_key ; ++i) {
+            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) {
-                    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;
             }
@@ -242,8 +262,8 @@ static inline void trie_compile_aux(trie_t *trie, int id,
     }
     forks[fork_pos] = last_key;
 
-    const int children_len = array_elt(trie->entries, id).children_len;
-    for (int i = 0 ; i < children_len ; ++i) {
+    const uint8_t children_len = array_elt(trie->entries, id).children_len;
+    for (uint16_t i = 0 ; i < children_len ; ++i) {
         int child = array_elt(trie->entries, id).children_offset + i;
         if (forks[i] - 1 > first_key) {
             trie_compile_aux(trie, child, first_key, forks[i], offset, 1);
@@ -264,8 +284,8 @@ void trie_compile(trie_t *trie, bool memlock)
 #       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);
@@ -281,13 +301,35 @@ bool trie_lookup(const trie_t *trie, const char *key)
     if (trie->entries.len == 0) {
         return false;
     } else {
-        trie_entry_t *current = array_ptr(trie->entries, 0);
+        const trie_entry_t *current = array_ptr(trie->entries, 0);
         while (true) {
-            int pos = 0;
             if (trie_entry_is_leaf(current)) {
                 return trie_entry_match(trie, current, key);
-            } else if (trie_entry_c_match(trie, current, key, &pos)) {
-                key += pos;
+            } else if (trie_entry_c_match(trie, current, key)) {
+                key += current->c_len;
+                current = trie_entry_child(trie, current, key);
+                if (current == NULL) {
+                    return false;
+                }
+            } else {
+                return false;
+            }
+        }
+    }
+}
+
+bool trie_prefix(const trie_t *trie, const char *key)
+{
+    assert(trie->keys.len == 0L && "Can't lookup: trie not compiled");
+    if (trie->entries.len == 0) {
+        return false;
+    } else {
+        const trie_entry_t *current = array_ptr(trie->entries, 0);
+        while (true) {
+            if (trie_entry_is_leaf(current)) {
+                return trie_entry_prefix(trie, current, key);
+            } else if (trie_entry_c_match(trie, current, key)) {
+                key += current->c_len;
                 current = trie_entry_child(trie, current, key);
                 if (current == NULL) {
                     return false;
@@ -304,13 +346,14 @@ void trie_lock(trie_t *trie)
     if (trie->locked) {
         return;
     }
-    if (mlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len) != 0) {
+    if (!array_lock(trie->entries)) {
         UNIXERR("mlock");
-        return;
     }
-    if (mlock(trie->c.data, trie->c.len) != 0) {
+    if (!array_lock(trie->c)) {
+        UNIXERR("mlock");
+    }
+    if (mlock(trie, sizeof(trie_t)) != 0) {
         UNIXERR("mlock");
-        munlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len);
         return;
     }
     trie->locked = true;
@@ -321,8 +364,9 @@ void trie_unlock(trie_t *trie)
     if (!trie->locked) {
         return;
     }
-    munlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len);
-    munlock(trie->c.data, trie->c.len);
+    array_unlock(trie->entries);
+    array_unlock(trie->c);
+    munlock(trie, sizeof(trie_t));
     trie->locked = false;
 }
 
@@ -336,6 +380,11 @@ static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
     static int leaves    = 0;
     static int depth_sum = 0;
 
+    if (entry == array_ptr(trie->entries, 0)) {
+      max_depth = 0;
+      leaves    = 0;
+      depth_sum = 0;
+    }
     if (trie_entry_is_leaf(entry)) {
         if (level > max_depth) {
             max_depth = level;
@@ -348,7 +397,7 @@ static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
             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);
@@ -362,7 +411,7 @@ static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
         }
         fputs("\n", stdout);
     }
-    for (int i = entry->children_offset ;
+    for (uint32_t i = entry->children_offset ;
           i < entry->children_offset + entry->children_len ; ++i) {
         trie_entry_inspect(trie, show_content, array_ptr(trie->entries, i), level + 1);
     }
@@ -372,7 +421,7 @@ static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
         printf("Number of leaves: %d\n", leaves);
         printf("Max depth: %d\n", max_depth);
         printf("Average leaf depth: %d\n", depth_sum / leaves);
-        printf("Memory used: %d\n", (trie->entries.size * sizeof(trie_entry_t))
+        printf("Memory used: %zd\n", (trie->entries.size * sizeof(trie_entry_t))
                                   + (trie->c.size) + sizeof(trie_t));
     }
 }