turing/table.h

35 lines
1.2 KiB
C

#ifndef __TABLE_H
#define __TABLE_H
#include <stdbool.h>
typedef void *(*item_clone_f)(void *);
typedef void (*item_free_f)(void *);
typedef int (*item_compare_f)(void *, void *);
typedef void (*item_repr_f)(void *, char *, int);
struct table
{
int size; // Number of entries actually used
int capacity; // Total allocated number of entries
int increment; // How many additional entries to allocate when growing the table
item_clone_f clone_f;
item_free_f free_f;
item_compare_f compare_f;
item_repr_f repr_f;
void **items; // Table entries, sorted
int *indexes; // Maps entried ids to their index inside the items array
void **lookup; // Cached lookup results
bool dirty; // Cached lookup entries are not up to date
};
struct table *table_new(int capacity, int increment,
item_clone_f clone_f, item_free_f free_f, item_compare_f compare_f, item_repr_f repr_f);
void table_free(struct table *t);
int table_find(struct table *t, void *entry);
int table_insert(struct table *t, void *entry);
void *table_lookup(struct table *t, int idx);
void table_print(struct table *t);
#endif