2015-08-12 11:43:55 +03:00
|
|
|
/* This file is included by symbol.c */
|
|
|
|
|
|
|
|
#include "id_table.h"
|
|
|
|
|
|
|
|
#ifndef ID_TABLE_DEBUG
|
|
|
|
#define ID_TABLE_DEBUG 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ID_TABLE_DEBUG == 0
|
2020-04-17 23:40:57 +03:00
|
|
|
#undef NDEBUG
|
2015-08-12 11:43:55 +03:00
|
|
|
#define NDEBUG
|
|
|
|
#endif
|
2016-01-22 11:33:55 +03:00
|
|
|
#include "ruby_assert.h"
|
2015-08-12 11:43:55 +03:00
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
typedef rb_id_serial_t id_key_t;
|
2015-08-12 11:43:55 +03:00
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
static inline ID
|
|
|
|
key2id(id_key_t key)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2017-01-25 06:03:52 +03:00
|
|
|
return rb_id_serial_to_id(key);
|
2015-08-12 11:43:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
static inline id_key_t
|
|
|
|
id2key(ID id)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2017-01-25 06:03:52 +03:00
|
|
|
return rb_id_to_serial(id);
|
2015-08-12 11:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* simple open addressing with quadratic probing.
|
|
|
|
uses mark-bit on collisions - need extra 1 bit,
|
|
|
|
ID is strictly 3 bits larger than rb_id_serial_t */
|
|
|
|
|
|
|
|
typedef struct rb_id_item {
|
|
|
|
id_key_t key;
|
|
|
|
#if SIZEOF_VALUE == 8
|
|
|
|
int collision;
|
|
|
|
#endif
|
|
|
|
VALUE val;
|
|
|
|
} item_t;
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
struct rb_id_table {
|
2015-08-12 11:43:55 +03:00
|
|
|
int capa;
|
|
|
|
int num;
|
|
|
|
int used;
|
|
|
|
item_t *items;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if SIZEOF_VALUE == 8
|
|
|
|
#define ITEM_GET_KEY(tbl, i) ((tbl)->items[i].key)
|
|
|
|
#define ITEM_KEY_ISSET(tbl, i) ((tbl)->items[i].key)
|
|
|
|
#define ITEM_COLLIDED(tbl, i) ((tbl)->items[i].collision)
|
|
|
|
#define ITEM_SET_COLLIDED(tbl, i) ((tbl)->items[i].collision = 1)
|
|
|
|
static inline void
|
2017-01-25 06:03:52 +03:00
|
|
|
ITEM_SET_KEY(struct rb_id_table *tbl, int i, id_key_t key)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
tbl->items[i].key = key;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define ITEM_GET_KEY(tbl, i) ((tbl)->items[i].key >> 1)
|
|
|
|
#define ITEM_KEY_ISSET(tbl, i) ((tbl)->items[i].key > 1)
|
|
|
|
#define ITEM_COLLIDED(tbl, i) ((tbl)->items[i].key & 1)
|
|
|
|
#define ITEM_SET_COLLIDED(tbl, i) ((tbl)->items[i].key |= 1)
|
|
|
|
static inline void
|
2017-01-25 06:03:52 +03:00
|
|
|
ITEM_SET_KEY(struct rb_id_table *tbl, int i, id_key_t key)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
tbl->items[i].key = (key << 1) | ITEM_COLLIDED(tbl, i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline int
|
2015-09-27 03:10:47 +03:00
|
|
|
round_capa(int capa)
|
|
|
|
{
|
2015-08-12 11:43:55 +03:00
|
|
|
/* minsize is 4 */
|
|
|
|
capa >>= 2;
|
|
|
|
capa |= capa >> 1;
|
|
|
|
capa |= capa >> 2;
|
|
|
|
capa |= capa >> 4;
|
|
|
|
capa |= capa >> 8;
|
|
|
|
capa |= capa >> 16;
|
|
|
|
return (capa + 1) << 2;
|
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
static struct rb_id_table *
|
|
|
|
rb_id_table_init(struct rb_id_table *tbl, int capa)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2017-01-25 06:03:52 +03:00
|
|
|
MEMZERO(tbl, struct rb_id_table, 1);
|
2015-08-12 11:43:55 +03:00
|
|
|
if (capa > 0) {
|
|
|
|
capa = round_capa(capa);
|
|
|
|
tbl->capa = (int)capa;
|
|
|
|
tbl->items = ZALLOC_N(item_t, capa);
|
|
|
|
}
|
|
|
|
return tbl;
|
|
|
|
}
|
|
|
|
|
2023-03-07 08:34:31 +03:00
|
|
|
struct rb_id_table *
|
2017-01-25 06:03:52 +03:00
|
|
|
rb_id_table_create(size_t capa)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2017-01-25 06:03:52 +03:00
|
|
|
struct rb_id_table *tbl = ALLOC(struct rb_id_table);
|
|
|
|
return rb_id_table_init(tbl, (int)capa);
|
2015-08-12 11:43:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
void
|
|
|
|
rb_id_table_free(struct rb_id_table *tbl)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
xfree(tbl->items);
|
|
|
|
xfree(tbl);
|
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
void
|
|
|
|
rb_id_table_clear(struct rb_id_table *tbl)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
tbl->num = 0;
|
|
|
|
tbl->used = 0;
|
|
|
|
MEMZERO(tbl->items, item_t, tbl->capa);
|
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
size_t
|
|
|
|
rb_id_table_size(const struct rb_id_table *tbl)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
return (size_t)tbl->num;
|
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
size_t
|
|
|
|
rb_id_table_memsize(const struct rb_id_table *tbl)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2017-01-25 06:03:52 +03:00
|
|
|
return sizeof(item_t) * tbl->capa + sizeof(struct rb_id_table);
|
2015-08-12 11:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-01-25 06:03:52 +03:00
|
|
|
hash_table_index(struct rb_id_table* tbl, id_key_t key)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
if (tbl->capa > 0) {
|
|
|
|
int mask = tbl->capa - 1;
|
|
|
|
int ix = key & mask;
|
|
|
|
int d = 1;
|
|
|
|
while (key != ITEM_GET_KEY(tbl, ix)) {
|
|
|
|
if (!ITEM_COLLIDED(tbl, ix))
|
|
|
|
return -1;
|
|
|
|
ix = (ix + d) & mask;
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
return ix;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-25 06:03:52 +03:00
|
|
|
hash_table_raw_insert(struct rb_id_table *tbl, id_key_t key, VALUE val)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
int mask = tbl->capa - 1;
|
|
|
|
int ix = key & mask;
|
|
|
|
int d = 1;
|
2024-02-08 17:43:39 +03:00
|
|
|
RUBY_ASSERT(key != 0);
|
2015-08-12 11:43:55 +03:00
|
|
|
while (ITEM_KEY_ISSET(tbl, ix)) {
|
|
|
|
ITEM_SET_COLLIDED(tbl, ix);
|
|
|
|
ix = (ix + d) & mask;
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
tbl->num++;
|
|
|
|
if (!ITEM_COLLIDED(tbl, ix)) {
|
|
|
|
tbl->used++;
|
|
|
|
}
|
|
|
|
ITEM_SET_KEY(tbl, ix, key);
|
|
|
|
tbl->items[ix].val = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-01-25 06:03:52 +03:00
|
|
|
hash_delete_index(struct rb_id_table *tbl, int ix)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
if (ix >= 0) {
|
|
|
|
if (!ITEM_COLLIDED(tbl, ix)) {
|
|
|
|
tbl->used--;
|
|
|
|
}
|
|
|
|
tbl->num--;
|
|
|
|
ITEM_SET_KEY(tbl, ix, 0);
|
|
|
|
tbl->items[ix].val = 0;
|
|
|
|
return TRUE;
|
2017-05-10 03:39:26 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-08-12 11:43:55 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-25 06:03:52 +03:00
|
|
|
hash_table_extend(struct rb_id_table* tbl)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
if (tbl->used + (tbl->used >> 1) >= tbl->capa) {
|
|
|
|
int new_cap = round_capa(tbl->num + (tbl->num >> 1));
|
|
|
|
int i;
|
|
|
|
item_t* old;
|
2017-01-25 06:03:52 +03:00
|
|
|
struct rb_id_table tmp_tbl = {0, 0, 0};
|
2016-08-14 16:35:42 +03:00
|
|
|
if (new_cap < tbl->capa) {
|
|
|
|
new_cap = round_capa(tbl->used + (tbl->used >> 1));
|
|
|
|
}
|
2015-08-26 03:26:02 +03:00
|
|
|
tmp_tbl.capa = new_cap;
|
2015-08-12 11:43:55 +03:00
|
|
|
tmp_tbl.items = ZALLOC_N(item_t, new_cap);
|
|
|
|
for (i = 0; i < tbl->capa; i++) {
|
|
|
|
id_key_t key = ITEM_GET_KEY(tbl, i);
|
|
|
|
if (key != 0) {
|
|
|
|
hash_table_raw_insert(&tmp_tbl, key, tbl->items[i].val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
old = tbl->items;
|
|
|
|
*tbl = tmp_tbl;
|
|
|
|
xfree(old);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ID_TABLE_DEBUG && 0
|
|
|
|
static void
|
2017-01-25 06:03:52 +03:00
|
|
|
hash_table_show(struct rb_id_table *tbl)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
const id_key_t *keys = tbl->keys;
|
|
|
|
const int capa = tbl->capa;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stderr, "tbl: %p (capa: %d, num: %d, used: %d)\n", tbl, tbl->capa, tbl->num, tbl->used);
|
|
|
|
for (i=0; i<capa; i++) {
|
|
|
|
if (ITEM_KEY_ISSET(tbl, i)) {
|
|
|
|
fprintf(stderr, " -> [%d] %s %d\n", i, rb_id2name(key2id(keys[i])), (int)keys[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-07 08:34:31 +03:00
|
|
|
int
|
2017-01-25 06:03:52 +03:00
|
|
|
rb_id_table_lookup(struct rb_id_table *tbl, ID id, VALUE *valp)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
id_key_t key = id2key(id);
|
|
|
|
int index = hash_table_index(tbl, key);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
2020-01-08 10:14:01 +03:00
|
|
|
*valp = tbl->items[index].val;
|
2015-08-12 11:43:55 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-01-25 06:03:52 +03:00
|
|
|
rb_id_table_insert_key(struct rb_id_table *tbl, const id_key_t key, const VALUE val)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
const int index = hash_table_index(tbl, key);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
tbl->items[index].val = val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hash_table_extend(tbl);
|
|
|
|
hash_table_raw_insert(tbl, key, val);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-03-07 08:34:31 +03:00
|
|
|
int
|
2017-01-25 06:03:52 +03:00
|
|
|
rb_id_table_insert(struct rb_id_table *tbl, ID id, VALUE val)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2017-01-25 06:03:52 +03:00
|
|
|
return rb_id_table_insert_key(tbl, id2key(id), val);
|
2015-08-12 11:43:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
int
|
|
|
|
rb_id_table_delete(struct rb_id_table *tbl, ID id)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
|
|
|
const id_key_t key = id2key(id);
|
|
|
|
int index = hash_table_index(tbl, key);
|
|
|
|
return hash_delete_index(tbl, index);
|
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
void
|
|
|
|
rb_id_table_foreach(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, void *data)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2019-10-03 06:26:41 +03:00
|
|
|
int i, capa = tbl->capa;
|
2019-09-25 07:00:56 +03:00
|
|
|
|
2019-10-03 06:26:41 +03:00
|
|
|
for (i=0; i<capa; i++) {
|
|
|
|
if (ITEM_KEY_ISSET(tbl, i)) {
|
|
|
|
const id_key_t key = ITEM_GET_KEY(tbl, i);
|
|
|
|
enum rb_id_table_iterator_result ret = (*func)(key2id(key), tbl->items[i].val, data);
|
2024-02-08 17:43:39 +03:00
|
|
|
RUBY_ASSERT(key != 0);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2019-10-03 06:26:41 +03:00
|
|
|
if (ret == ID_TABLE_DELETE)
|
|
|
|
hash_delete_index(tbl, i);
|
|
|
|
else if (ret == ID_TABLE_STOP)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-08-12 11:43:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 06:03:52 +03:00
|
|
|
void
|
|
|
|
rb_id_table_foreach_values(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, void *data)
|
2015-08-12 11:43:55 +03:00
|
|
|
{
|
2019-10-03 06:26:41 +03:00
|
|
|
int i, capa = tbl->capa;
|
2015-08-12 11:43:55 +03:00
|
|
|
|
2019-10-03 06:26:41 +03:00
|
|
|
for (i=0; i<capa; i++) {
|
|
|
|
if (ITEM_KEY_ISSET(tbl, i)) {
|
|
|
|
enum rb_id_table_iterator_result ret = (*func)(tbl->items[i].val, data);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2019-10-03 06:26:41 +03:00
|
|
|
if (ret == ID_TABLE_DELETE)
|
|
|
|
hash_delete_index(tbl, i);
|
|
|
|
else if (ret == ID_TABLE_STOP)
|
|
|
|
return;
|
|
|
|
}
|
2015-08-12 11:43:55 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 00:11:24 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
rb_id_table_foreach_values_with_replace(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, rb_id_table_update_value_callback_func_t *replace, void *data)
|
|
|
|
{
|
|
|
|
int i, capa = tbl->capa;
|
|
|
|
|
|
|
|
for (i = 0; i < capa; i++) {
|
|
|
|
if (ITEM_KEY_ISSET(tbl, i)) {
|
|
|
|
enum rb_id_table_iterator_result ret = (*func)(tbl->items[i].val, data);
|
|
|
|
|
|
|
|
if (ret == ID_TABLE_REPLACE) {
|
|
|
|
VALUE val = tbl->items[i].val;
|
|
|
|
ret = (*replace)(&val, data, TRUE);
|
|
|
|
tbl->items[i].val = val;
|
|
|
|
}
|
2022-01-26 00:16:37 +03:00
|
|
|
|
|
|
|
if (ret == ID_TABLE_STOP)
|
2022-01-26 00:11:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|