Compile Time Generic Dynamic Lookup Table in C.
On it's own, no code is being compiled. You first have to include and implement a lookup table of your desires. For that, there are two macros.
#include "lutd.h"
LUTD_INCLUDE(N, A, T, M);
LUTD_IMPLEMENT(N, A, T, M, H, C, F);
N
- Name - the resulting name of the lookup table structA
- Abbreviation - functions get prefixed with thatT
- Type - the type of your elements within the lookup tableM
- Mode - storage type, eitherBY_VAL
(by value) orBY_REF
(by reference)H
- Hash - provide a hashing function for your elements (required)C
- Compare - provide a comparing function for your elements - if none provided, default tomemcmp
F
- Free - provide a freeing function for your elements, if available
The lookup table only stores how many times something with an equal hash has been added. If something was added a second time, it will only increase the count, and not copy the data into the table.
- example int
BY_VAL
, noC
and noF
provided - example person
BY_REF
,C
andF
provided - I used this lookup table in my Timers interpreter (
BY_REF
,C
provided but noF
provided) - This lookup table is extensively used in c-nexus and c-file-tagger and provides the base for my string c-string.
- to compile run
make
(have to be in the examples directory) - to clean run
make clean
(if you're on cygwin, runOS= make clean
- Both
BY_VAL
andBY_REF
can be used on either basic types or (complex) structs.
- Compiler optimized code
- Flexibility, reusability (generics / templates)
- Type safety
- Maintenance challenges
- Debugging difficulties
- Compilation overhead
The A##
means the A
specified in the two macros.
A##_init
initialize lookup tableA##_add
add a valueA##_add_count
add a specified number of counts to a valueA##_del
delete a valueA##_has
check if a value existsA##_find
find positioni
andj
of a value (to access in lookup table:N.buckets[i].items[j]
)A##_free
free lookup tableA##_clear
clear lookup table (reset but don't free)A##_dump
dump everything unsorted (arr
is required,counts
is optional)A##_clear
check if table is empty
A##_join
andA##_join_threaded
...