+static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "_"; /* TODO: Numbers ... */
+
+static const size_t correct_alpha_index[0x80] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 52,
+ 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0
+};
+
+/*
+ * A fast space efficent trie for a dictionary of identifiers. This is
+ * faster than a hashtable for one reason. A hashtable itself may have
+ * fast constant lookup time, but the hash itself must be very fast. We
+ * have one of the fastest hash functions for strings, but if you do a
+ * lost of hashing (which we do, almost 3 million hashes per identifier)
+ * a hashtable becomes slow.
+ */
+correct_trie_t* correct_trie_new() {
+ correct_trie_t *t = (correct_trie_t*)mem_a(sizeof(correct_trie_t));
+ t->value = NULL;
+ t->entries = NULL;
+ return t;
+}
+
+static GMQCC_INLINE void correct_trie_del_sub(correct_trie_t *t) {
+ size_t i;
+ if (!t->entries)
+ return;
+ for (i = 0; i < sizeof(correct_alpha)-1; ++i) {
+ correct_trie_del_sub(&t->entries[i]);
+ }
+ mem_d(t->entries);
+}
+
+static GMQCC_INLINE void correct_trie_del(correct_trie_t *t) {
+ size_t i;
+ if (t->entries) {
+ for (i = 0; i < sizeof(correct_alpha)-1; ++i)
+ correct_trie_del_sub(&t->entries[i]);
+ mem_d(t->entries);
+ }
+ mem_d(t);
+}
+
+static GMQCC_INLINE void* correct_trie_get(const correct_trie_t *t, const char *key) {
+ const unsigned char *data = (const unsigned char*)key;
+
+ while (*data) {
+ if (!t->entries)
+ return NULL;
+ t = t->entries + correct_alpha_index[*data];
+ ++data;
+ }
+ return t->value;
+}
+
+static GMQCC_INLINE void correct_trie_set(correct_trie_t *t, const char *key, void * const value) {
+ const unsigned char *data = (const unsigned char*)key;
+ while (*data) {
+ if (!t->entries) {
+ t->entries = (correct_trie_t*)mem_a(sizeof(correct_trie_t)*(sizeof(correct_alpha)-1));
+ memset(t->entries, 0, sizeof(correct_trie_t)*(sizeof(correct_alpha)-1));
+ }
+ t = t->entries + correct_alpha_index[*data];
+ ++data;
+ }
+ t->value = value;
+}
+
+
+/*
+ * Implementation of the corrector algorithm commences. A very efficent
+ * brute-force attack (thanks to tries and mempool :-)).
+ */
+static GMQCC_INLINE size_t *correct_find(correct_trie_t *table, const char *word) {
+ return (size_t*)correct_trie_get(table, word);
+}
+
+static GMQCC_INLINE bool correct_update(correct_trie_t* *table, const char *word) {
+ size_t *data = correct_find(*table, word);
+ if (!data)
+ return false;
+
+ (*data)++;
+ return true;
+}
+
+void correct_add(correct_trie_t* table, size_t ***size, const char *ident) {
+ size_t *data = NULL;
+ const char *add = ident;
+
+ if (!correct_update(&table, add)) {
+ data = (size_t*)mem_a(sizeof(size_t));
+ *data = 1;
+
+ vec_push((*size), data);
+ correct_trie_set(table, add, data);
+ }
+}
+
+void correct_del(correct_trie_t* dictonary, size_t **data) {
+ size_t i;
+ const size_t vs = vec_size(data);
+
+ for (i = 0; i < vs; i++)
+ mem_d(data[i]);
+
+ vec_free(data);
+ correct_trie_del(dictonary);
+}