Project

General

Profile

Feature #5789 » st_pack_tables.patch

allow usual tables to be packed (+tiny fix) - funny_falcon (Yura Sokolov), 12/22/2011 07:02 PM

View differences:

st.c
st_table_entry *fore, *back;
};
#define ST_DEFAULT_MAX_DENSITY 5
#define ST_DEFAULT_INIT_TABLE_SIZE 11
#define ST_DEFAULT_MAX_DENSITY 3
#define ST_DEFAULT_INIT_TABLE_SIZE 9
#define MAX_PACKED_HASH 12
/*
* DEFAULT_MAX_DENSITY is the default for the largest we allow the
......
/* remove cast to unsigned int in the future */
#define do_hash(key,table) (unsigned int)(st_index_t)(*(table)->type->hash)((key))
#define do_hash_bin(key,table) (do_hash((key), (table))%(table)->num_bins)
#define PKEY_POS(i, num_bins) ((num_bins)-(i)*2-2) //((num_bins)/3 + (i)*2)
#define PVAL_POS(i, num_bins) ((num_bins)-(i)*2-1) //((num_bins)/3 + (i)*2 + 1)
#define PHASH_POS(i, num_bins) (i) //((num_bins)-(i)*3-1) //(i)
#define PKEY(table, i) (st_data_t)(table)->bins[PKEY_POS(i, (table)->num_bins)]
#define PVAL(table, i) (st_data_t)(table)->bins[PVAL_POS(i, (table)->num_bins)]
#define PHASH(table, i) (st_data_t)(table)->bins[PHASH_POS(i, (table)->num_bins)]
#define PKEY_SET(table, i, v) do{ (table)->bins[PKEY_POS(i, (table)->num_bins)] = (st_table_entry *)(v); } while(0)
#define PVAL_SET(table, i, v) do{ (table)->bins[PVAL_POS(i, (table)->num_bins)] = (st_table_entry *)(v); } while(0)
#define PHASH_SET(table, i, v) do{ (table)->bins[PHASH_POS(i, (table)->num_bins)] = (st_table_entry *)(v); } while(0)
//#define ST_PACKED_REFERENCE
#ifdef ST_PACKED_REFERENCE
static inline void
resize_packed_table(st_table *table, st_index_t new_num_bins)
{
st_index_t ij;
st_table_entry **new_bins = (st_table_entry **) calloc((new_num_bins), sizeof(st_table_entry *));
for(ij = 0; ij < table->num_entries; ij++) {
new_bins[PKEY_POS(ij, new_num_bins)] = (table)->bins[PKEY_POS(ij, table->num_bins)];
new_bins[PVAL_POS(ij, new_num_bins)] = (table)->bins[PVAL_POS(ij, table->num_bins)];
new_bins[PHASH_POS(ij, new_num_bins)] = (table)->bins[PHASH_POS(ij, table->num_bins)];
}
free((table)->bins);
(table)->bins = new_bins;
(table)->num_bins = (new_num_bins);
}
static inline void
remove_packed_entry(st_table *table, st_index_t i)
{
table->num_entries--;
for(;i < table->num_entries; i++) {
PVAL_SET(table, i, PVAL(table, i+1));
PKEY_SET(table, i, PKEY(table, i+1));
PHASH_SET(table, i, PHASH(table, i+1));
}
}
#else
static inline void
resize_packed_table(st_table *table, st_index_t new_num_bins)
{
st_table_entry **new_bins = (st_table_entry **) malloc((new_num_bins) * sizeof(st_table_entry *));
memmove(new_bins, table->bins, sizeof(st_table_entry *) * table->num_entries);
memmove(new_bins + (new_num_bins - 2*table->num_entries),
table->bins + (table->num_bins - 2*table->num_entries),
sizeof(st_table_entry *) * table->num_entries * 2);
free((table)->bins);
(table)->bins = new_bins;
(table)->num_bins = (new_num_bins);
}
static inline void
remove_packed_entry(st_table *table, st_index_t i)
{
table->num_entries--;
if (i < table->num_entries) {
st_index_t mv = table->num_entries - i, upto = table->num_bins - 2*table->num_entries;
memmove(table->bins + i, table->bins + i + 1, sizeof(st_table_entry *) * mv);
memmove(table->bins + upto, table->bins + upto - 2,
sizeof(st_table_entry *) * mv * 2);
}
}
#endif
/*
* MINSIZE is the minimum size of a dictionary.
*/
......
Table of prime numbers 2^n+a, 2<=n<=30.
*/
static const unsigned int primes[] = {
8 + 3,
ST_DEFAULT_INIT_TABLE_SIZE,
16 + 3,
32 + 5,
64 + 3,
......
}
#endif
#define MAX_PACKED_NUMHASH (ST_DEFAULT_INIT_TABLE_SIZE/2)
st_table*
st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
{
......
}
#endif
size = new_size(size); /* round up to prime number */
tbl = alloc(st_table);
tbl->type = type;
tbl->num_entries = 0;
tbl->entries_packed = type == &type_numhash && size/2 <= MAX_PACKED_NUMHASH;
tbl->entries_packed = size <= MAX_PACKED_HASH;
size = new_size(size); /* round up to prime number */
tbl->num_bins = size;
tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*));
tbl->head = 0;
......
#define FOUND_ENTRY
#endif
#define FIND_ENTRY(table, ptr, hash_val, bin_pos) do {\
(bin_pos) = (hash_val)%(table)->num_bins;\
(ptr) = (table)->bins[(bin_pos)];\
FOUND_ENTRY;\
if (PTR_NOT_EQUAL((table), (ptr), (hash_val), key)) {\
COLLISION;\
while (PTR_NOT_EQUAL((table), (ptr)->next, (hash_val), key)) {\
(ptr) = (ptr)->next;\
}\
(ptr) = (ptr)->next;\
}\
} while (0)
static st_table_entry *
find_entry(st_table *table, st_data_t key, st_index_t hash_val)
{
st_index_t bin_pos = hash_val % table->num_bins;
register st_table_entry *ptr = table->bins[bin_pos];
FOUND_ENTRY;
if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {
COLLISION;
while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {
ptr = ptr->next;
}
ptr = ptr->next;
}
return ptr;
}
static inline st_index_t
find_packed_index(st_table *table, st_index_t hash_val, st_data_t key)
{
st_index_t i = 0;
for(;;) {
while (i < table->num_entries && PHASH(table, i) != hash_val) i++;
if (i == table->num_entries || EQUAL(table, key, PKEY(table, i)))
break;
i++;
}
return i;
}
#define collision_check 0
int
st_lookup(st_table *table, register st_data_t key, st_data_t *value)
{
st_index_t hash_val, bin_pos;
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
if (value !=0) *value = (st_data_t)table->bins[i*2+1];
return 1;
}
}
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->num_entries) {
if (value != 0) *value = PVAL(table, i);
return 1;
}
return 0;
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, hash_val);
if (ptr == 0) {
return 0;
......
int
st_get_key(st_table *table, register st_data_t key, st_data_t *result)
{
st_index_t hash_val, bin_pos;
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
if (result !=0) *result = (st_data_t)table->bins[i*2];
return 1;
}
}
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->num_entries) {
if (result != 0) *result = PKEY(table, i);
return 1;
}
return 0;
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, hash_val);
if (ptr == 0) {
return 0;
......
#undef collision_check
#define collision_check 1
#define MORE_PACKABLE_P(table) \
((st_index_t)((table)->num_entries+1) * 2 <= (table)->num_bins && \
(table)->num_entries+1 <= MAX_PACKED_NUMHASH)
#define ADD_DIRECT(table, key, value, hash_val, bin_pos)\
do {\
st_table_entry *entry;\
if ((table)->num_entries > ST_DEFAULT_MAX_DENSITY * (table)->num_bins) {\
rehash(table);\
(bin_pos) = (hash_val) % (table)->num_bins;\
}\
\
entry = alloc(st_table_entry);\
\
entry->hash = (hash_val);\
entry->key = (key);\
entry->record = (value);\
entry->next = (table)->bins[(bin_pos)];\
if ((table)->head != 0) {\
entry->fore = 0;\
(entry->back = (table)->tail)->fore = entry;\
(table)->tail = entry;\
}\
else {\
(table)->head = (table)->tail = entry;\
entry->fore = entry->back = 0;\
}\
(table)->bins[(bin_pos)] = entry;\
(table)->num_entries++;\
} while (0)
static void
add_direct(st_table * table, st_data_t key, st_data_t value,
st_index_t hash_val)
{
st_table_entry *entry;
register st_index_t bin_pos;
if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) {
rehash(table);
}
entry = alloc(st_table_entry);
entry->hash = hash_val;
entry->key = key;
entry->record = value;
entry->fore = 0;
if (table->head != 0) {
(entry->back = table->tail)->fore = entry;
table->tail = entry;
}
else {
table->head = table->tail = entry;
entry->back = 0;
}
bin_pos = hash_val % table->num_bins;
entry->next = table->bins[bin_pos];
table->bins[bin_pos] = entry;
table->num_entries++;
}
static void
unpack_entries(register st_table *table)
{
st_index_t i;
struct st_table_entry *packed_bins[MAX_PACKED_NUMHASH*2];
st_table tmp_table = *table;
memcpy(packed_bins, table->bins, sizeof(struct st_table_entry *) * table->num_entries*2);
table->bins = packed_bins;
tmp_table.entries_packed = 0;
tmp_table.num_entries = 0;
memset(tmp_table.bins, 0, sizeof(struct st_table_entry *) * tmp_table.num_bins);
st_table tmp_table = {table->type, 0, 0, 0, 0, 0, 0};
tmp_table.bins = (st_table_entry **)
calloc(ST_DEFAULT_INIT_TABLE_SIZE, sizeof(st_table_entry *));
tmp_table.num_bins = ST_DEFAULT_INIT_TABLE_SIZE;
for (i = 0; i < table->num_entries; i++) {
st_insert(&tmp_table, (st_data_t)packed_bins[i*2], (st_data_t)packed_bins[i*2+1]);
add_direct(&tmp_table, PKEY(table, i), PVAL(table, i), PHASH(table, i));
}
xfree(table->bins);
*table = tmp_table;
}
static int
add_packed_direct(st_table *table, st_data_t key, st_data_t value, st_index_t hash_val)
{
int res = 1;
if (table->num_entries < MAX_PACKED_HASH ) {
st_index_t i = table->num_entries;
if ( i+1 > table->num_bins/3 ) {
st_index_t new_num_bins = new_size(table->num_bins);
resize_packed_table(table, new_num_bins);
}
PKEY_SET(table, i, key);
PVAL_SET(table, i, value);
PHASH_SET(table, i, hash_val);
table->num_entries++;
}
else {
unpack_entries(table);
res = 0;
}
return res;
}
int
st_insert(register st_table *table, register st_data_t key, st_data_t value)
{
st_index_t hash_val, bin_pos;
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
table->bins[i*2+1] = (struct st_table_entry*)value;
return 1;
}
}
if (MORE_PACKABLE_P(table)) {
i = table->num_entries++;
table->bins[i*2] = (struct st_table_entry*)key;
table->bins[i*2+1] = (struct st_table_entry*)value;
return 0;
}
else {
unpack_entries(table);
}
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->num_entries) {
PVAL_SET(table, i, value);
return 1;
}
if (add_packed_direct(table, key, value, hash_val)) {
return 0;
}
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, hash_val);
if (ptr == 0) {
ADD_DIRECT(table, key, value, hash_val, bin_pos);
add_direct(table, key, value, hash_val);
return 0;
}
else {
......
st_insert2(register st_table *table, register st_data_t key, st_data_t value,
st_data_t (*func)(st_data_t))
{
st_index_t hash_val, bin_pos;
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
table->bins[i*2+1] = (struct st_table_entry*)value;
return 1;
}
}
if (MORE_PACKABLE_P(table)) {
i = table->num_entries++;
table->bins[i*2] = (struct st_table_entry*)key;
table->bins[i*2+1] = (struct st_table_entry*)value;
return 0;
}
else {
unpack_entries(table);
}
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->num_entries) {
PVAL_SET(table, i, value);
return 1;
}
if (add_packed_direct(table, key, value, hash_val)) {
return 0;
}
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, hash_val);
if (ptr == 0) {
key = (*func)(key);
ADD_DIRECT(table, key, value, hash_val, bin_pos);
add_direct(table, key, value, hash_val);
return 0;
}
else {
......
void
st_add_direct(st_table *table, st_data_t key, st_data_t value)
{
st_index_t hash_val, bin_pos;
st_index_t hash_val;
if (table->entries_packed) {
int i;
if (MORE_PACKABLE_P(table)) {
i = table->num_entries++;
table->bins[i*2] = (struct st_table_entry*)key;
table->bins[i*2+1] = (struct st_table_entry*)value;
return;
}
else {
unpack_entries(table);
}
hash_val = do_hash(key, table);
if (table->entries_packed ) {
if (add_packed_direct(table, key, value, hash_val)) {
return;
}
}
hash_val = do_hash(key, table);
bin_pos = hash_val % table->num_bins;
ADD_DIRECT(table, key, value, hash_val, bin_pos);
add_direct(table, key, value, hash_val);
}
static void
......
return new_table;
}
#define REMOVE_ENTRY(table, ptr) do \
{ \
if ((ptr)->fore == 0 && (ptr)->back == 0) { \
(table)->head = 0; \
(table)->tail = 0; \
} \
else { \
st_table_entry *fore = (ptr)->fore, *back = (ptr)->back; \
if (fore) fore->back = back; \
if (back) back->fore = fore; \
if ((ptr) == (table)->head) (table)->head = fore; \
if ((ptr) == (table)->tail) (table)->tail = back; \
} \
(table)->num_entries--; \
} while (0)
static inline void
remove_entry(st_table *table, st_table_entry *ptr)
{
if ((ptr)->fore == 0 && (ptr)->back == 0) {
(table)->head = 0;
(table)->tail = 0;
}
else {
st_table_entry *fore = (ptr)->fore, *back = (ptr)->back;
if (fore) fore->back = back;
if (back) back->fore = fore;
if ((ptr) == (table)->head) (table)->head = fore;
if ((ptr) == (table)->tail) (table)->tail = back;
}
(table)->num_entries--;
}
int
st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
......
st_table_entry **prev;
register st_table_entry *ptr;
hash_val = do_hash(*key, table);
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == *key) {
if (value != 0) *value = (st_data_t)table->bins[i*2+1];
table->num_entries--;
memmove(&table->bins[i*2], &table->bins[(i+1)*2],
sizeof(struct st_table_entry*) * 2*(table->num_entries-i));
return 1;
}
st_index_t i = find_packed_index(table, hash_val, *key);
if (i < table->num_entries) {
if (value != 0) *value = PVAL(table, i);
remove_packed_entry(table, i);
return 1;
}
if (value != 0) *value = 0;
return 0;
}
hash_val = do_hash_bin(*key, table);
for (prev = &table->bins[hash_val]; (ptr = *prev) != 0; prev = &ptr->next) {
for (prev = &table->bins[hash_val % table->num_bins]; (ptr = *prev) != 0; prev = &ptr->next) {
if (EQUAL(table, *key, ptr->key)) {
*prev = ptr->next;
REMOVE_ENTRY(table, ptr);
remove_entry(table, ptr);
if (value != 0) *value = ptr->record;
*key = ptr->key;
free(ptr);
......
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(*key, table);
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == *key) {
if (value != 0) *value = (st_data_t)table->bins[i*2+1];
table->bins[i*2] = (void *)never;
return 1;
}
st_index_t i = find_packed_index(table, hash_val, *key);
if (i < table->num_entries) {
if (value != 0) *value = PVAL(table, i);
PKEY_SET(table, i, never);
PHASH_SET(table, i, 0);
return 1;
}
if (value != 0) *value = 0;
return 0;
}
hash_val = do_hash_bin(*key, table);
ptr = table->bins[hash_val];
ptr = table->bins[hash_val % table->num_bins];
for (; ptr != 0; ptr = ptr->next) {
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
REMOVE_ENTRY(table, ptr);
remove_entry(table, ptr);
*key = ptr->key;
if (value != 0) *value = ptr->record;
ptr->key = ptr->record = never;
......
if (table->entries_packed) {
st_index_t i = 0, j = 0;
while ((st_data_t)table->bins[i*2] != never) {
while (PKEY(table, i) != never) {
if (i++ == table->num_entries) return;
}
for (j = i; ++i < table->num_entries;) {
if ((st_data_t)table->bins[i*2] == never) continue;
table->bins[j*2] = table->bins[i*2];
table->bins[j*2+1] = table->bins[i*2+1];
if (PKEY(table, i) == never) continue;
PKEY_SET(table, j, PKEY(table, i));
PVAL_SET(table, j, PVAL(table, i));
PHASH_SET(table, j, PHASH(table, i));
j++;
}
table->num_entries = j;
......
if (table->entries_packed) {
for (i = 0; i < table->num_entries; i++) {
st_index_t j;
st_index_t hash;
st_data_t key, val;
key = (st_data_t)table->bins[i*2];
val = (st_data_t)table->bins[i*2+1];
key = PKEY(table, i);
val = PVAL(table, i);
hash = PHASH(table,i);
retval = (*func)(key, val, arg);
if (!table->entries_packed) goto unpacked;
switch (retval) {
case ST_CHECK: /* check if hash is modified during iteration */
for (j = 0; j < table->num_entries; j++) {
if ((st_data_t)table->bins[j*2] == key)
break;
}
if (j == table->num_entries) {
/* work around uncomforming befaviour of hash */
if (PKEY(table, i) == Qundef && PHASH(table, i) == 0)
break;
else if (i < table->num_entries && EQUAL(table, key, PKEY(table, i)))
break;
if (find_packed_index(table, hash, key) == table->num_entries) {
/* call func with error notice */
retval = (*func)(0, 0, arg, 1);
return 1;
......
case ST_STOP:
return 0;
case ST_DELETE:
table->num_entries--;
memmove(&table->bins[i*2], &table->bins[(i+1)*2],
sizeof(struct st_table_entry*) * 2*(table->num_entries-i));
remove_packed_entry(table, i);
i--;
break;
}
......
if (ptr == tmp) {
tmp = ptr->fore;
*last = ptr->next;
REMOVE_ENTRY(table, ptr);
remove_entry(table, ptr);
free(ptr);
if (ptr == tmp) return 0;
ptr = tmp;
......
if (ptr == tmp) {
tmp = ptr->back;
*last = ptr->next;
REMOVE_ENTRY(table, ptr);
remove_entry(table, ptr);
free(ptr);
ptr = tmp;
break;
(3-3/6)