Code Monkey home page Code Monkey logo

componenttrie's Introduction

ComponentTrie

A simple component trie implementation that enables component-granularity longest prefix matching and exact match for strings with delimiters.

The same as piaodazhu/ttlHashMap, this stuff uses the hashmap provided by tidwall/hashmap.c.

It is header-only. Just include it.

run tests

gcc component_trie_test.c -o ctest
# run testbench (default N=5000000)
BENCH=1 N=1000000 ./ctest
# run multi-thread test (default M=4)
THEAD=1 M=4 ./ctest
# run normal test
./ctest

# or simpler cpp testfile
g++ cpp_test.cpp -o cpptest
./cpptest

APIs

// structure defination of component trie
typedef struct ct_trie {
	ct_node_t *root;
	char delimiter[MAX_DELIMITER_LEN];
	int delimiter_len;
	uint32_t version;
	pthread_rwlock_t lock;
} ct_trie_t;

// some macro defination
#define MAX_COMPONENT_LEN 16
#define MAX_DELIMITER_LEN 4

// new a trie with a specific delimiter. such as: '/', ': ', ', ', '::'
ct_trie_t* ct_trie_new(const char *delimiter, int delimiter_len);

// free a trie. not thread safe.
void ct_trie_free(ct_trie_t *t);

// insert a kv pair item (prefix, data)
int ct_trie_insert(ct_trie_t* t, const char *prefix, int prefix_len, void *udata);

// longest prefix match a prefix and get the corresponding data with udata.
int ct_trie_lpm(ct_trie_t* t, const char *name, int name_len, void **udata);

// exact prefix match a prefix and get the corresponding data with udata.
int ct_trie_em(ct_trie_t* t, const char *name, int name_len, void **udata);

// remove a kv pair and the the removed data with udata.
int ct_trie_remove(ct_trie_t* t, const char *prefix, int prefix_len, void **udata);

License

ComponentTrie source code is available under the MIT License.

componenttrie's People

Contributors

piaodazhu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.