Code Monkey home page Code Monkey logo

containers's Introduction

Containers

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)

Organisation

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).

Testing

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:

    1. create container
    2. insert test data (given in testdata.h)
    3. delete test data
    4. insert test data again
      1. at each step: look up every inserted key, ensure that all can be found and that the values match
    5. print whatever the fifth element contains
    6. enumerate keys by prefix
    7. look up a nonexistent key
    8. dump data
    9. dump data to Graphviz format
    10. remove every other key
    11. remove remaining keys
    12. dump data (which ought to be empty)
    13. destroy container
  • common prefix test:

    1. create container
    2. insert test data (strings with common prefixes)
    3. dump data
    4. dump data to Graphviz format
    5. destroy container
  • integer test:

    1. create container
    2. insert test data (mapping numbers to names)
      1. at each step: look up every inserted key, ensure that all can be found and that the values match
    3. print whatever the fifth element contains
    4. enumerate keys by prefix
    5. look up a nonexistent key
    6. dump data
    7. dump data to Graphviz format
    8. remove every other key
    9. remove remaining keys
    10. dump data (which ought to be empty)
    11. destroy container
  • character test:

    1. create container
    2. insert test data (mapping characters to their ordinals)
      1. at each step: look up every inserted key, ensure that all can be found and that the values match
    3. print whatever the fifth element contains
    4. enumerate keys by prefix
    5. look up a nonexistent key
    6. dump data
    7. dump data to Graphviz format
    8. remove every other key
    9. remove remaining keys
    10. dump data (which ought to be empty)
    11. destroy container

Graphs

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.

License

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:

  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 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.

containers's People

Contributors

dpt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

containers's Issues

Can't compile on Linux

Hi there,

I was trying to compile on Linux (x86_64) and I got stuck. First off, you might want to mention that this requires clang; it's pretty obvious after trying to run make the first time but still.
Second, I fixed a few errors by adding #included <stdint.h> to base/types.h, although I don't really understand why because the error was about intptr_t which was already defined in base/types.h.
Then I'm getting some more errors:

clang -c -std=c99  -Wall -Wundef -Wpointer-arith -Wuninitialized -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wunused -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Winline -Wno-unused -Wno-long-long -W -Wshadow -Iinclude -MMD -Os -DNDEBUG libraries/datastruct/queue/test/test.c -o libraries/datastruct/queue/test/test.o
libraries/datastruct/queue/test/test.c:181:7: warning: no previous prototype for
      function 'queuetest' [-Wmissing-prototypes]
error queuetest(void)
      ^
1 warning generated.
ar rc libcontainertest.a libraries/datastruct/queue/test/test.o
clang  -o container-test libcontainertest.a libcontainer.a
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11
[...]
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'

Do you happen to know what to do about these?
Cheers

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.