* st.c (hash_pos): extract mapping hash values to hash bins index.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44634 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-01-18 05:47:38 +00:00
Родитель 9d2f5125ce
Коммит 254add068a
1 изменённых файлов: 21 добавлений и 20 удалений

41
st.c
Просмотреть файл

@ -85,7 +85,8 @@ static void rehash(st_table *);
#define EQUAL(table,x,y) ((x)==(y) || (*(table)->type->compare)((x),(y)) == 0) #define EQUAL(table,x,y) ((x)==(y) || (*(table)->type->compare)((x),(y)) == 0)
#define do_hash(key,table) (st_index_t)(*(table)->type->hash)((key)) #define do_hash(key,table) (st_index_t)(*(table)->type->hash)((key))
#define do_hash_bin(key,table) (do_hash((key), (table))%(table)->num_bins) #define hash_pos(h,n) ((h) % (n))
#define do_hash_bin(key,table) hash_pos(do_hash((key), (table)), (table)->num_bins)
/* preparation for possible allocation improvements */ /* preparation for possible allocation improvements */
#define st_alloc_entry() (st_table_entry *)malloc(sizeof(st_table_entry)) #define st_alloc_entry() (st_table_entry *)malloc(sizeof(st_table_entry))
@ -375,7 +376,7 @@ count_collision(const struct st_hash_type *type)
#endif #endif
#define FIND_ENTRY(table, ptr, hash_val, bin_pos) \ #define FIND_ENTRY(table, ptr, hash_val, bin_pos) \
((ptr) = find_entry((table), key, (hash_val), ((bin_pos) = (hash_val)%(table)->num_bins))) ((ptr) = find_entry((table), key, (hash_val), ((bin_pos) = hash_pos(hash_val, (table)->num_bins))))
static st_table_entry * static st_table_entry *
find_entry(st_table *table, st_data_t key, st_index_t hash_val, st_index_t bin_pos) find_entry(st_table *table, st_data_t key, st_index_t hash_val, st_index_t bin_pos)
@ -422,7 +423,7 @@ st_lookup(st_table *table, register st_data_t key, st_data_t *value)
return 0; return 0;
} }
ptr = find_entry(table, key, hash_val, hash_val % table->num_bins); ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins));
if (ptr == 0) { if (ptr == 0) {
return 0; return 0;
@ -450,7 +451,7 @@ st_get_key(st_table *table, register st_data_t key, st_data_t *result)
return 0; return 0;
} }
ptr = find_entry(table, key, hash_val, hash_val % table->num_bins); ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins));
if (ptr == 0) { if (ptr == 0) {
return 0; return 0;
@ -486,7 +487,7 @@ add_direct(st_table *table, st_data_t key, st_data_t value,
register st_table_entry *entry; register st_table_entry *entry;
if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) { if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) {
rehash(table); rehash(table);
bin_pos = hash_val % table->num_bins; bin_pos = hash_pos(hash_val, table->num_bins);
} }
entry = new_entry(table, key, value, hash_val, bin_pos); entry = new_entry(table, key, value, hash_val, bin_pos);
@ -527,7 +528,7 @@ unpack_entries(register st_table *table)
st_data_t val = packed_bins[i].val; st_data_t val = packed_bins[i].val;
st_index_t hash = packed_bins[i].hash; st_index_t hash = packed_bins[i].hash;
entry = new_entry(&tmp_table, key, val, hash, entry = new_entry(&tmp_table, key, val, hash,
hash % ST_DEFAULT_INIT_TABLE_SIZE); hash_pos(hash, ST_DEFAULT_INIT_TABLE_SIZE));
*chain = entry; *chain = entry;
entry->back = preventry; entry->back = preventry;
preventry = entry; preventry = entry;
@ -550,7 +551,7 @@ add_packed_direct(st_table *table, st_data_t key, st_data_t value, st_index_t ha
} }
else { else {
unpack_entries(table); unpack_entries(table);
add_direct(table, key, value, hash_val, hash_val % table->num_bins); add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
} }
} }
@ -631,7 +632,7 @@ st_add_direct(st_table *table, st_data_t key, st_data_t value)
return; return;
} }
add_direct(table, key, value, hash_val, hash_val % table->num_bins); add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
} }
static void static void
@ -647,7 +648,7 @@ rehash(register st_table *table)
if ((ptr = table->head) != 0) { if ((ptr = table->head) != 0) {
do { do {
hash_val = ptr->hash % new_num_bins; hash_val = hash_pos(ptr->hash, new_num_bins);
ptr->next = new_bins[hash_val]; ptr->next = new_bins[hash_val];
new_bins[hash_val] = ptr; new_bins[hash_val] = ptr;
} while ((ptr = ptr->fore) != 0); } while ((ptr = ptr->fore) != 0);
@ -690,7 +691,7 @@ st_copy(st_table *old_table)
return 0; return 0;
} }
*entry = *ptr; *entry = *ptr;
hash_val = entry->hash % num_bins; hash_val = hash_pos(entry->hash, num_bins);
entry->next = new_table->bins[hash_val]; entry->next = new_table->bins[hash_val];
new_table->bins[hash_val] = entry; new_table->bins[hash_val] = entry;
entry->back = prev; entry->back = prev;
@ -741,7 +742,7 @@ st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
return 0; return 0;
} }
prev = &table->bins[hash_val % table->num_bins]; prev = &table->bins[hash_pos(hash_val, table->num_bins)];
for (;(ptr = *prev) != 0; prev = &ptr->next) { for (;(ptr = *prev) != 0; prev = &ptr->next) {
if (EQUAL(table, *key, ptr->key)) { if (EQUAL(table, *key, ptr->key)) {
*prev = ptr->next; *prev = ptr->next;
@ -777,7 +778,7 @@ st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *val
return 0; return 0;
} }
ptr = table->bins[hash_val % table->num_bins]; ptr = table->bins[hash_pos(hash_val, table->num_bins)];
for (; ptr != 0; ptr = ptr->next) { for (; ptr != 0; ptr = ptr->next) {
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
@ -811,7 +812,7 @@ st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
return 1; return 1;
} }
prev = &table->bins[table->head->hash % table->num_bins]; prev = &table->bins[hash_pos(table->head->hash, table->num_bins)];
while ((ptr = *prev) != table->head) prev = &ptr->next; while ((ptr = *prev) != table->head) prev = &ptr->next;
*prev = ptr->next; *prev = ptr->next;
if (value != 0) *value = ptr->record; if (value != 0) *value = ptr->record;
@ -910,7 +911,7 @@ st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data
switch (retval) { switch (retval) {
case ST_CONTINUE: case ST_CONTINUE:
if (!existing) { if (!existing) {
add_direct(table, key, value, hash_val, hash_val % table->num_bins); add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
break; break;
} }
ptr->record = value; ptr->record = value;
@ -986,7 +987,7 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
do { do {
if (ptr->key == never) if (ptr->key == never)
goto unpacked_continue; goto unpacked_continue;
i = ptr->hash % table->num_bins; i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0); retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked: unpacked:
switch (retval) { switch (retval) {
@ -1007,7 +1008,7 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
case ST_STOP: case ST_STOP:
return 0; return 0;
case ST_DELETE: case ST_DELETE:
last = &table->bins[ptr->hash % table->num_bins]; last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) { for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) { if (ptr == tmp) {
tmp = ptr->fore; tmp = ptr->fore;
@ -1063,7 +1064,7 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
if (ptr != 0) { if (ptr != 0) {
do { do {
i = ptr->hash % table->num_bins; i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0); retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked: unpacked:
switch (retval) { switch (retval) {
@ -1074,7 +1075,7 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
case ST_STOP: case ST_STOP:
return 0; return 0;
case ST_DELETE: case ST_DELETE:
last = &table->bins[ptr->hash % table->num_bins]; last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) { for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) { if (ptr == tmp) {
tmp = ptr->fore; tmp = ptr->fore;
@ -1218,7 +1219,7 @@ st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
retval = (*func)(ptr->key, ptr->record, arg, 0); retval = (*func)(ptr->key, ptr->record, arg, 0);
switch (retval) { switch (retval) {
case ST_CHECK: /* check if hash is modified during iteration */ case ST_CHECK: /* check if hash is modified during iteration */
i = ptr->hash % table->num_bins; i = hash_pos(ptr->hash, table->num_bins);
for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) { for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
if (!tmp) { if (!tmp) {
/* call func with error notice */ /* call func with error notice */
@ -1233,7 +1234,7 @@ st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
case ST_STOP: case ST_STOP:
return 0; return 0;
case ST_DELETE: case ST_DELETE:
last = &table->bins[ptr->hash % table->num_bins]; last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) { for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) { if (ptr == tmp) {
tmp = ptr->back; tmp = ptr->back;