Skip to content

Commit

Permalink
copied minor changes from the most recent version of https://github.c…
Browse files Browse the repository at this point in the history
…om/dcjones/hat-trie, including an updated pstdint.h for improved compatibility
  • Loading branch information
mheilman committed Aug 21, 2014
1 parent b30b294 commit 95cb981
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 59 deletions.
21 changes: 21 additions & 0 deletions hat-trie/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*.la
*.lo
*.o
*~
.DS_Store
.deps
.libs
Makefile
Makefile.in
aclocal.m4
autom4te.cache
config.*
configure
depcomp
hat-trie-*.pc
hat-trie-*.tar.gz
install-sh
libtool
ltmain.sh
m4
missing
6 changes: 6 additions & 0 deletions hat-trie/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: c
compiler:
- clang
- gcc
before_script: autoreconf -i
script: ./configure && make && make check
15 changes: 14 additions & 1 deletion hat-trie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Hat-Trie
========

[![Build Status](https://travis-ci.org/dcjones/hat-trie.svg)](https://travis-ci.org/dcjones/hat-trie)

This a ANSI C99 implementation of the HAT-trie data structure of Askitis and
Sinha, an extremely efficient (space and time) modern variant of tries.

Expand Down Expand Up @@ -29,6 +31,17 @@ Installation
./configure
make install

To use the library, include `hat-trie.h` and link using `lhat-trie`.
To use the library, include `hat-trie.h` and link using `-lhat-trie`.


Tests
-----

Build and run the tests:

make check

Other Language Bindings
-----------------------
* Ruby - https://github.com/luikore/triez
* Python - https://github.com/kmike/hat-trie
2 changes: 1 addition & 1 deletion hat-trie/src/ahtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


const double ahtable_max_load_factor = 100000.0; /* arbitrary large number => don't resize */
const const size_t ahtable_initial_size = 4096;
const size_t ahtable_initial_size = 4096;
static const uint16_t LONG_KEYLEN_MASK = 0x7fff;

static size_t keylen(slot_t s) {
Expand Down
4 changes: 2 additions & 2 deletions hat-trie/src/ahtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* Briefly, the idea is, as opposed to separate chaining with linked lists, to
* store keys contiguously in one big array, thereby improving the caching
* behavior, and reducing space requirments.
* behavior, and reducing space requirements.
*
*/

Expand All @@ -38,7 +38,7 @@ typedef struct ahtable_t_
unsigned char c1;

size_t n; // number of slots
size_t m; // numbur of key/value pairs stored
size_t m; // number of key/value pairs stored
size_t max_m; // number of stored keys before we resize

size_t* slot_sizes;
Expand Down
27 changes: 12 additions & 15 deletions hat-trie/src/hat-trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ struct hattrie_t_
size_t m; // number of stored keys
};

/* Create a new trie node with all pointer pointing to the given child (which
/* Create a new trie node with all pointers pointing to the given child (which
* can be NULL). */
static trie_node_t* alloc_trie_node(hattrie_t* T, node_ptr child)
{
trie_node_t* node = malloc_or_die(sizeof(trie_node_t));
node->flag = NODE_TYPE_TRIE;
node->val = 0;

/* pass T to allow custom allocator for trie. */
HT_UNUSED(T); /* unused now */

size_t i;
for (i = 0; i < NODE_CHILDS; ++i) node->xs[i] = child;
return node;
Expand Down Expand Up @@ -120,7 +120,7 @@ static node_ptr hattrie_find(hattrie_t* T, const char **key, size_t *len)
if (*len == 0) return parent;

node_ptr node = hattrie_consume(&parent, key, len, 1);

/* if the trie node consumes value, use it */
if (*node.flag & NODE_TYPE_TRIE) {
if (!(node.t->flag & NODE_HAS_VAL)) {
Expand All @@ -131,10 +131,10 @@ static node_ptr hattrie_find(hattrie_t* T, const char **key, size_t *len)

/* pure bucket holds only key suffixes, skip current char */
if (*node.flag & NODE_TYPE_PURE_BUCKET) {
*key += 1;
*key += 1;
*len -= 1;
}

/* do not scan bucket, it's not needed for this operation */
return node;
}
Expand Down Expand Up @@ -391,12 +391,12 @@ value_t* hattrie_tryget(hattrie_t* T, const char* key, size_t len)
if (node.flag == NULL) {
return NULL;
}

/* if the trie node consumes value, use it */
if (*node.flag & NODE_TYPE_TRIE) {
return &node.t->val;
}

return ahtable_tryget(node.b, key, len);
}

Expand All @@ -411,7 +411,7 @@ int hattrie_del(hattrie_t* T, const char* key, size_t len)
if (node.flag == NULL) {
return -1;
}

/* if consumed on a trie node, clear the value */
if (*node.flag & NODE_TYPE_TRIE) {
return hattrie_clrval(T, node);
Expand All @@ -421,10 +421,10 @@ int hattrie_del(hattrie_t* T, const char* key, size_t len)
size_t m_old = ahtable_size(node.b);
int ret = ahtable_del(node.b, key, len);
T->m -= (m_old - ahtable_size(node.b));

/* merge empty buckets */
/*! \todo */

return ret;
}

Expand Down Expand Up @@ -507,7 +507,7 @@ static void hattrie_iter_nextnode(hattrie_iter_t* i)
/* push all child nodes from right to left */
int j;
for (j = NODE_MAXCHAR; j >= 0; --j) {

/* skip repeated pointers to hybrid bucket */
if (j < NODE_MAXCHAR && node.t->xs[j].t == node.t->xs[j + 1].t) continue;

Expand Down Expand Up @@ -654,6 +654,3 @@ value_t* hattrie_iter_val(hattrie_iter_t* i)

return ahtable_iter_val(i->i);
}



2 changes: 0 additions & 2 deletions hat-trie/src/hat-trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,3 @@ value_t* hattrie_iter_val (hattrie_iter_t*);
#endif

#endif


Loading

0 comments on commit 95cb981

Please sign in to comment.