- Notifications
You must be signed in to change notification settings - Fork2
ashinkarov/trie
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This a simple implementation oftriedata structure in C99. This implementation allows to store sequences ofint
-s, however one can easy trigger a type ofsymb
instruct trie
.
The library includes a simple test-case in the trie.c. In order to makeit work, runmake test
.
Currently an implemetation provides interfaces to storechar
based strings in thetrie, attaching to each word some information ofssize_t
type. Forexample, consider the following case:
#include<stdio.h>#include<string.h>#include<unistd.h>#include<stdlib.h>#include"trie.h"/* some values, we want to attach to every word in the trie data base. */enumtype_of_word {type_a,type_b};intmain (intargc,char*argv[]){intret=EXIT_SUCCESS;ssize_tres;/* Initialize new trie. */tructtrie*dict=trie_new ();if (argc <=1) { (void)fprintf (stderr,"usage: %s word to find\n",argv[0]);ret=EXIT_FAILURE; gotoout; }/* Add two words of two different types. */trie_add_word (dict,"hello",strlen ("hello"), (ssize_t)type_a);trie_add_word (dict,"hell",strlen ("hell"), (ssize_t)type_b);printf ("Printing the trie.\n");trie_print (dict);/* Search the argument in the trie and detect a type of it. */res=trie_search (dict,argv[1],strlen (argv[1]));printf ("searching '%s' in the database -- %s\n",argv[1],res!=TRIE_NOT_LAST ? (enumtype_of_word)res==type_a ?"yes, type A" :"yes, type B" :"no");out:trie_free (dict);returnret;}
As the type of information you attach is a transparentsize_t
we needto dedicate one symbol, whic would denote that a certain symbol isnotlast. MacroTRIE_NOT_LAST
serves this purpose.
Tha implementation is valgrinded, and is compiled at strictes gcc warninglevel.
Send your suggestions or patches.