This module assumes that hits in the scan process are at random, and are very unlikely.
machine.h
which optionally defines:
sp_init
function calls to calloc
(3) into larger cached
slabs. Since we never free the memory we allocate for the space this
saves a lot of malloc headers and such. Suggested value of 4096 for
a medium application hit rate, smaller for very rare hit rates.
By default no cache is enabled.
sp_walk
, rather than
the iterative one when set to non-zero.
The recursive one is more complex and slower, as far as I can tell.
#include "machine.h" #include "sparse.h"
These functions are limited to construction of the sparse space,
not mechanism (short of calling exit
) is provided to
remove any allocated space. This is fine for the use intended,
and an implemenation via AVL trees would trivially
provide a "clean" implementation.
More modern allocation system will also fix this issue.
extern sp_key sp_fibs[48];
extern size_t sp_unfib(sp_key wN);
sp_key
array which
contains the Fibonacci that is closest the wN
without
going over.
extern void **sp_init(sp_key wRange);
(void **)
which can represent unsigned integers
upto and including wRange
(starting at zero).
extern void **sp_index(void **ppvFrom, sp_key wFind);
(void **)
which is bound to
the integer wFind
in the sparse space ppcFrom
. To mark the value as
used set the (void *) indexed by the return value to a non-NULL
value (any one works).
Later, when sp_walk
ing the space this
(void *)
is the second parameter passed to
the Map
function.
extern int sp_walk(void **ppvFrom, sp_key wLow, sp_key wHi, int (*pfiMap)(sp_key, void *));
From
from wLow
to
wHi
calling Map
on each integer that
has a non-NULL (void *)
bound to it.
(sp_key wN, void *pvData)
Map
parameter to
sp_walk
expects.
extern int sp_exists(void **ppvFrom, sp_key wTest);
Test
in the sparse space
given by From
. Return zero for "not in the space"
and non-zero for "included".
This test driver inserts a few (unsigned long) integers into a sparse space instance with alternating colors ("red" and "blue"), then walks the space to output each integer and the color it was recorded with.
The integers in the test data were selected to test the corner cases in the walk and index functions.
$Id: sparse.html,v 1.9 2010/08/13 17:26:00 ksb Exp $