This is a library of associative array data structures implemented as binary trees in C.
We implement the following types of binary tree:
- 'bstree' - a binary search tree
- 'dstree' - a digital search tree
- 'trie' - a Fredkin tree
- 'critbit' - a crit-bit tree
- 'patricia' - a PATRICIA trie
Each have their own strengths and (dis)advantages.
See 'Algorithms' by Sedgewick for details of the algorithms.
Two additional associative data structures are provided for comparison with the binary tree data structures:
- linkedlist - a linked list in which elements are kept sorted
- orderedarray - an ordered array (again, sorted)
The code is divided into two layers. You can either call the data structure manager functions directly using the interfaces defined in:
- bstree.h
- critbit.h
- dstree.h
- linkedlist.h
- orderedarray.h
- patricia.h
- trie.h
Or you can use the higher-level "Containers" interface defined in:
- icontainer.h
The benefit of the Containers interface is a regular interface which makes it possible to swap between data structures with little or no changes to the calling code. The cost is more code which may perform redundant work depending on your use case.
To create a container use one of:
- container-bstree.h::
container_create_bstree
- container-critbit.h::
container_create_critbit
- container-dstree.h::
container_create_dstree
- container-linkedlist.h::
container_create_linkedlist
- container-orderedarray.h::
container_create_orderedarray
- container-patricia.h::
container_create_patricia
- container-trie.h::
container_create_trie
These are all icontainer_maker
functions as defined in:
- icontainer-maker.h
Maker functions accept pointers to key and value interfaces then allocate and populate an icontainer_t
interface. Key and value interfaces are specified using icontainer_key_t
and icontainer_value_t
. They are respectively defined in:
- icontainer-key.h
- icontainer-value.h
(Interfaces common to both key and value icontainers live in icontainer-kv.h).
Pre-prepared key and value implementations are available in:
- char-kv.h
- int-kv.h
- string-kv.h
(Implementations common to these live in kv-common.h).
The containers layer was developed largely to simplify testing of the lower-level data structures. container-test.c uses the containers interface to perform the same tests across all available data structures:
-
string test:
- create container
- insert test data (given in testdata.h)
- delete test data
- insert test data again
- at each step: look up every inserted key, ensure that all can be found and that the values match
- print whatever the fifth element contains
- enumerate keys by prefix
- look up a nonexistent key
- dump data
- dump data to Graphviz format
- remove every other key
- remove remaining keys
- dump data (which ought to be empty)
- destroy container
-
common prefix test:
- create container
- insert test data (strings with common prefixes)
- dump data
- dump data to Graphviz format
- destroy container
-
integer test:
- create container
- insert test data (mapping numbers to names)
- at each step: look up every inserted key, ensure that all can be found and that the values match
- print whatever the fifth element contains
- enumerate keys by prefix
- look up a nonexistent key
- dump data
- dump data to Graphviz format
- remove every other key
- remove remaining keys
- dump data (which ought to be empty)
- destroy container
-
character test:
- create container
- insert test data (mapping characters to their ordinals)
- at each step: look up every inserted key, ensure that all can be found and that the values match
- print whatever the fifth element contains
- enumerate keys by prefix
- look up a nonexistent key
- dump data
- dump data to Graphviz format
- remove every other key
- remove remaining keys
- dump data (which ought to be empty)
- destroy container
The critbit, dstree, patricia and trie tests dump representations of their data structures to Graphviz format. The produced .gv files can be run through Graphviz's 'dot' tool, rendering the directed graph into boxes, lines and arrows.
To turn these into, for example, a PDF file:
dot -Tpdf -O dstree-string.gv
The above command will write out dstree-string.gv.pdf.
Copyright (c) 2009-2012, David Thomas. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.