From 95cb981380cc6761ac3609da00ccaa9c944a5616 Mon Sep 17 00:00:00 2001 From: Michael Heilman Date: Thu, 21 Aug 2014 08:42:22 -0400 Subject: [PATCH] copied minor changes from the most recent version of https://github.com/dcjones/hat-trie, including an updated pstdint.h for improved compatibility --- hat-trie/.gitignore | 21 ++++++++++ hat-trie/.travis.yml | 6 +++ hat-trie/README.md | 15 ++++++- hat-trie/src/ahtable.c | 2 +- hat-trie/src/ahtable.h | 4 +- hat-trie/src/hat-trie.c | 27 ++++++------ hat-trie/src/hat-trie.h | 2 - hat-trie/src/pstdint.h | 77 ++++++++++++++++++++--------------- hat-trie/test/check_hattrie.c | 7 +--- 9 files changed, 102 insertions(+), 59 deletions(-) create mode 100644 hat-trie/.gitignore create mode 100644 hat-trie/.travis.yml diff --git a/hat-trie/.gitignore b/hat-trie/.gitignore new file mode 100644 index 0000000..6a9dcba --- /dev/null +++ b/hat-trie/.gitignore @@ -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 diff --git a/hat-trie/.travis.yml b/hat-trie/.travis.yml new file mode 100644 index 0000000..4c10ab2 --- /dev/null +++ b/hat-trie/.travis.yml @@ -0,0 +1,6 @@ +language: c +compiler: + - clang + - gcc +before_script: autoreconf -i +script: ./configure && make && make check diff --git a/hat-trie/README.md b/hat-trie/README.md index 5941011..f0bee4f 100644 --- a/hat-trie/README.md +++ b/hat-trie/README.md @@ -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. @@ -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 diff --git a/hat-trie/src/ahtable.c b/hat-trie/src/ahtable.c index 01bb4a9..81aea64 100644 --- a/hat-trie/src/ahtable.c +++ b/hat-trie/src/ahtable.c @@ -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) { diff --git a/hat-trie/src/ahtable.h b/hat-trie/src/ahtable.h index bf6f782..5306094 100644 --- a/hat-trie/src/ahtable.h +++ b/hat-trie/src/ahtable.h @@ -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. * */ @@ -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; diff --git a/hat-trie/src/hat-trie.c b/hat-trie/src/hat-trie.c index 8b87752..8557b76 100644 --- a/hat-trie/src/hat-trie.c +++ b/hat-trie/src/hat-trie.c @@ -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; @@ -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)) { @@ -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; } @@ -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); } @@ -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); @@ -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; } @@ -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; @@ -654,6 +654,3 @@ value_t* hattrie_iter_val(hattrie_iter_t* i) return ahtable_iter_val(i->i); } - - - diff --git a/hat-trie/src/hat-trie.h b/hat-trie/src/hat-trie.h index d8439b6..1708d09 100644 --- a/hat-trie/src/hat-trie.h +++ b/hat-trie/src/hat-trie.h @@ -66,5 +66,3 @@ value_t* hattrie_iter_val (hattrie_iter_t*); #endif #endif - - diff --git a/hat-trie/src/pstdint.h b/hat-trie/src/pstdint.h index fa64dbe..18a26b5 100644 --- a/hat-trie/src/pstdint.h +++ b/hat-trie/src/pstdint.h @@ -3,13 +3,13 @@ * BSD License: **************************************************************************** * - * Copyright (c) 2005-2011 Paul Hsieh + * Copyright (c) 2005-2014 Paul Hsieh * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: - * + * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright @@ -17,7 +17,7 @@ * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -31,7 +31,7 @@ * **************************************************************************** * - * Version 0.1.12 + * Version 0.1.14 * * The ANSI C standard committee, for the C99 standard, specified the * inclusion of a new standard include file called stdint.h. This is @@ -179,6 +179,8 @@ * John Steele Scott * Dave Thorup * John Dill + * Florian Wobbe + * Christopher Sean Morrison * */ @@ -191,14 +193,23 @@ * do nothing else. On the Mac OS X version of gcc this is _STDINT_H_. */ -#if ((defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) || (defined (__WATCOMC__) && (defined (_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (defined(_STDINT_H) || defined(_STDINT_H_) || defined (__UINT_FAST64_TYPE__)) )) && !defined (_PSTDINT_H_INCLUDED) +#if ((defined(__STDC__) && __STDC__ && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined (__WATCOMC__) && (defined (_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (__GNUC__ > 3 || defined(_STDINT_H) || defined(_STDINT_H_) || defined (__UINT_FAST64_TYPE__)) )) && !defined (_PSTDINT_H_INCLUDED) #include #define _PSTDINT_H_INCLUDED -# ifndef PRINTF_INT64_MODIFIER -# define PRINTF_INT64_MODIFIER "ll" -# endif -# ifndef PRINTF_INT32_MODIFIER -# define PRINTF_INT32_MODIFIER "l" +# if defined(__GNUC__) && (defined(__x86_64__) || defined(__ppc64__)) +# ifndef PRINTF_INT64_MODIFIER +# define PRINTF_INT64_MODIFIER "l" +# endif +# ifndef PRINTF_INT32_MODIFIER +# define PRINTF_INT32_MODIFIER "" +# endif +# else +# ifndef PRINTF_INT64_MODIFIER +# define PRINTF_INT64_MODIFIER "ll" +# endif +# ifndef PRINTF_INT32_MODIFIER +# define PRINTF_INT32_MODIFIER "l" +# endif # endif # ifndef PRINTF_INT16_MODIFIER # define PRINTF_INT16_MODIFIER "h" @@ -304,7 +315,7 @@ #ifndef UINT8_MAX # define UINT8_MAX 0xff #endif -#ifndef uint8_t +#if !defined(uint8_t) && !defined(_UINT8_T) # if (UCHAR_MAX == UINT8_MAX) || defined (S_SPLINT_S) typedef unsigned char uint8_t; # define UINT8_C(v) ((uint8_t) v) @@ -319,7 +330,7 @@ #ifndef INT8_MIN # define INT8_MIN INT8_C(0x80) #endif -#ifndef int8_t +#if !defined(int8_t) && !defined(_INT8_T) # if (SCHAR_MAX == INT8_MAX) || defined (S_SPLINT_S) typedef signed char int8_t; # define INT8_C(v) ((int8_t) v) @@ -331,7 +342,7 @@ #ifndef UINT16_MAX # define UINT16_MAX 0xffff #endif -#ifndef uint16_t +#if !defined(uint16_t) && !defined(_UINT16_T) #if (UINT_MAX == UINT16_MAX) || defined (S_SPLINT_S) typedef unsigned int uint16_t; # ifndef PRINTF_INT16_MODIFIER @@ -355,7 +366,7 @@ #ifndef INT16_MIN # define INT16_MIN INT16_C(0x8000) #endif -#ifndef int16_t +#if !defined(int16_t) && !defined(_INT16_T) #if (INT_MAX == INT16_MAX) || defined (S_SPLINT_S) typedef signed int int16_t; # define INT16_C(v) ((int16_t) (v)) @@ -376,7 +387,7 @@ #ifndef UINT32_MAX # define UINT32_MAX (0xffffffffUL) #endif -#ifndef uint32_t +#if !defined(uint32_t) && !defined(_UINT32_T) #if (ULONG_MAX == UINT32_MAX) || defined (S_SPLINT_S) typedef unsigned long uint32_t; # define UINT32_C(v) v ## UL @@ -406,7 +417,7 @@ #ifndef INT32_MIN # define INT32_MIN INT32_C(0x80000000) #endif -#ifndef int32_t +#if !defined(int32_t) && !defined(_INT32_T) #if (LONG_MAX == INT32_MAX) || defined (S_SPLINT_S) typedef signed long int32_t; # define INT32_C(v) v ## L @@ -662,12 +673,12 @@ typedef uint_least32_t uint_fast32_t; * (u)intptr_t types and limits. */ -#if defined (_MSC_VER) && defined (_UINTPTR_T_DEFINED) +#if (defined (_MSC_VER) && defined (_UINTPTR_T_DEFINED)) || defined (_UINTPTR_T) # define STDINT_H_UINTPTR_T_DEFINED #endif #ifndef STDINT_H_UINTPTR_T_DEFINED -# if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (_WIN64) +# if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (_WIN64) || defined (__ppc64__) # define stdint_intptr_bits 64 # elif defined (__WATCOMC__) || defined (__TURBOC__) # if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__) @@ -675,10 +686,12 @@ typedef uint_least32_t uint_fast32_t; # else # define stdint_intptr_bits 32 # endif -# elif defined (__i386__) || defined (_WIN32) || defined (WIN32) +# elif defined (__i386__) || defined (_WIN32) || defined (WIN32) || defined (__ppc64__) # define stdint_intptr_bits 32 # elif defined (__INTEL_COMPILER) /* TODO -- what did Intel do about x86-64? */ +# else +/* #error "This platform might not be supported yet" */ # endif # ifdef stdint_intptr_bits @@ -730,25 +743,25 @@ typedef uint_least32_t uint_fast32_t; #if defined (__TEST_PSTDINT_FOR_CORRECTNESS) -/* +/* * Please compile with the maximum warning settings to make sure macros are not * defined more than once. */ - + #include #include #include - + #define glue3_aux(x,y,z) x ## y ## z #define glue3(x,y,z) glue3_aux(x,y,z) -#define DECLU(bits) glue3(uint,bits,_t) glue3(u,bits,=) glue3(UINT,bits,_C) (0); -#define DECLI(bits) glue3(int,bits,_t) glue3(i,bits,=) glue3(INT,bits,_C) (0); +#define DECLU(bits) glue3(uint,bits,_t) glue3(u,bits,) = glue3(UINT,bits,_C) (0); +#define DECLI(bits) glue3(int,bits,_t) glue3(i,bits,) = glue3(INT,bits,_C) (0); #define DECL(us,bits) glue3(DECL,us,) (bits) -#define TESTUMAX(bits) glue3(u,bits,=) glue3(~,u,bits); if (glue3(UINT,bits,_MAX) glue3(!=,u,bits)) printf ("Something wrong with UINT%d_MAX\n", bits) - +#define TESTUMAX(bits) glue3(u,bits,) = ~glue3(u,bits,); if (glue3(UINT,bits,_MAX) != glue3(u,bits,)) printf ("Something wrong with UINT%d_MAX\n", bits) + int main () { DECL(I,8) DECL(U,8) @@ -765,7 +778,7 @@ int main () { char str0[256], str1[256]; sprintf (str0, "%d %x\n", 0, ~0); - + sprintf (str1, "%d %x\n", i8, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i8 : %s\n", str1); sprintf (str1, "%u %x\n", u8, ~0); @@ -773,20 +786,20 @@ int main () { sprintf (str1, "%d %x\n", i16, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i16 : %s\n", str1); sprintf (str1, "%u %x\n", u16, ~0); - if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1); + if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1); sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n", i32, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i32 : %s\n", str1); sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n", u32, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with u32 : %s\n", str1); -#ifdef INT64_MAX +#ifdef INT64_MAX sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n", i64, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with i64 : %s\n", str1); #endif sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n", imax, ~0); if (0 != strcmp (str0, str1)) printf ("Something wrong with imax : %s\n", str1); sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n", umax, ~0); - if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1); - + if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1); + TESTUMAX(8); TESTUMAX(16); TESTUMAX(32); diff --git a/hat-trie/test/check_hattrie.c b/hat-trie/test/check_hattrie.c index 797a981..151b85a 100644 --- a/hat-trie/test/check_hattrie.c +++ b/hat-trie/test/check_hattrie.c @@ -187,7 +187,7 @@ void test_hattrie_sorted_iteration() ++count; key = hattrie_iter_key(i, &len); - + /* memory for key may be changed on iter, copy it */ strncpy(key_copy, key, len); @@ -266,8 +266,3 @@ int main() return 0; } - - - - -