In-memory indexed store and fetch library
This library makes use of dep to manage it's dependencies and exports.
dep add finwo/mindex
dep install
After that, simply add include lib/.dep/config.mk
in your makefile and include
the header file by adding #include "finwo/mindex.h
.
- Consistent interface, always working with pointers
- ANSI C (C99)
- Purge to be called when an item is removed API
struct mindex_t
The main handle of the mindex instance
struct mindex_t {
int (*compare)(const void *a, const void *b, void *udata);
void (*purge)(void *item, void *udata);
void *udata;
size_t length;
size_t max;
void **items;
};
mindex_init(cmp, purge, udata)
Initialize a new in-memory index
struct mindex_t * mindex_init(int (*compare)(const void *a, const void *b, void *udata), void (*purge)(void *item, void *udata), void *udata);
mindex_find(mindex, pattern, items, length)
Intended for internal use or advanced usage (like fetching both index & the pointer of the result)
struct mindex_find_response * mindex_find(const struct mindex_t *mindex, const void *pattern, void **items, int length);
mindex_set(mindex, item)
Insert or replace an existing item
void mindex_set(struct mindex_t *mindex, void *item);
mindex_get(mindex, pattern)
Simple query, fetch an entry in the index matching the pattern
void * mindex_get(struct mindex_t *mindex, const void *pattern);
mindex_nth(mindex, index)
Fetch the nth entry in the index
void * mindex_nth(struct mindex_t *mindex, int index);
mindex_rand(mindex)
Retrieve a single random entry from the index
void * mindex_rand(struct mindex_t *mindex);
mindex_delete(mindex, pattern)
Delete a single entry from the index matching the pattern
void mindex_delete(struct mindex_t *mindex, const void *pattern);
mindex_length(mindex)
The current amount of entries in the index
size_t mindex_length(struct mindex_t *mindex);
mindex_free(mindex)
Purge all entries from the index and free the memory used
void mindex_free(struct mindex_t *mindex);
If you want to run the library's tests, simply run make test
to compile
the testing binary, and then ./test
to run the actual tests.
mindex source code is available under the MIT license.