Exemplo n.º 1
0
void hattrie_iter_next(hattrie_iter_t* i)
{
    if (hattrie_iter_finished(i)) return;

    if (i->i != NULL && !hhash_iter_finished(i->i)) {
        hhash_iter_next(i->i);
    }
    else if (i->has_nil_key) {
        i->has_nil_key = false;
        i->nil_val = 0;
        hattrie_iter_nextnode(i);
    }

    while (((i->i == NULL || hhash_iter_finished(i->i)) && !i->has_nil_key) &&
           i->stack != NULL ) {

        free(i->i);
        i->i = NULL;
        hattrie_iter_nextnode(i);
    }

    if (i->i != NULL && hhash_iter_finished(i->i)) {
        free(i->i);
        i->i = NULL;
    }
}
Exemplo n.º 2
0
static inline void hhash_sorted_iter_next(hhash_iter_t* i)
{
	if (hhash_iter_finished(i)) {
		return;
	}
	++i->i;
}
Exemplo n.º 3
0
static int node_apply(node_ptr node, int (*f)(value_t*,void*), void* d)
{
    int result = TRIE_EOK;

    if (*node.flag & NODE_TYPE_TRIE) {
        size_t i;
        for (i = 0; i < NODE_CHILDS; ++i) {
            if (i > 0 && node.t->xs[i].t == node.t->xs[i - 1].t) {
                continue;
            }
            if (node.t->xs[i].t) {
                result = node_apply(node.t->xs[i], f, d);
            }
            if (result == TRIE_EOK && *node.flag & NODE_HAS_VAL) {
                result = f(&node.t->val, d);
            }
            if (result != TRIE_EOK) {
                break;
            }
        }
    }
    else {
        hhash_iter_t i;
        hhash_iter_begin(node.b, &i, false);
        while (!hhash_iter_finished(&i)) {
            result = f(hhash_iter_val(&i), d);
            if (result != TRIE_EOK) {
                break;
            }
            hhash_iter_next(&i);
        }
    }

    return result;
}
Exemplo n.º 4
0
static value_t *hhash_unsorted_iter_val(hhash_iter_t* i)
{
	if (hhash_iter_finished(i)) {
		return NULL;
	}

	return (value_t *)KEY_VAL(i->tbl->item[i->i].d);
}
Exemplo n.º 5
0
static void hhash_unsorted_iter_next(hhash_iter_t* i)
{
	if (hhash_iter_finished(i)) {
		return;
	}

	i->i = hhash_unsorted_seek_valid(i->tbl, i->i + 1);
}
Exemplo n.º 6
0
static value_t *hhash_sorted_iter_val(hhash_iter_t* i)
{
	if (hhash_iter_finished(i)) {
		return NULL;
	}

	return (value_t *)KEY_VAL(hhash_sorted_iter_item(i));
}
Exemplo n.º 7
0
static const char* hhash_unsorted_iter_key(hhash_iter_t* i, uint16_t* len)
{
	if (hhash_iter_finished(i)) {
		return NULL;
	}

	void *key = i->tbl->item[i->i].d;
	*len = key_readlen(key);
	return KEY_STR(key);
}
Exemplo n.º 8
0
static const char* hhash_sorted_iter_key(hhash_iter_t* i, uint16_t* len)
{
	if (hhash_iter_finished(i)) {
		return NULL;
	}

	void *key = hhash_sorted_iter_item(i);
	*len = key_readlen(key);
	return KEY_STR(key);
}
Exemplo n.º 9
0
void hhash_iter_begin(hhash_t* tbl, hhash_iter_t* i, bool sorted)
{
	memset(i, 0, sizeof(hhash_iter_t));
	i->tbl = tbl;
	if (sorted) {
		i->flags |= HH_SORTED;
		if (!hhash_iter_finished(i)) {
			assert(tbl->index);
		}
	} else {
		hhash_unsorted_iter_begin(i);
	}
}
Exemplo n.º 10
0
hattrie_iter_t* hattrie_iter_begin(const hattrie_t* T, bool sorted)
{
    hattrie_iter_t* i = NULL;
    if (T) {
        i = malloc(sizeof(hattrie_iter_t));
        i->T = T;
        i->sorted = sorted;
        i->i = NULL;
        i->keysize = 16;
        i->key = malloc(i->keysize * sizeof(char));
        i->level   = 0;
        i->has_nil_key = false;
        i->nil_val     = 0;

        i->stack = malloc(sizeof(hattrie_node_stack_t));
        i->stack->next   = NULL;
        i->stack->node   = T->root;
        i->stack->c      = '\0';
        i->stack->level  = 0;

        while (((i->i == NULL || hhash_iter_finished(i->i)) && !i->has_nil_key) &&
               i->stack != NULL ) {

            free(i->i);
            i->i = NULL;
            hattrie_iter_nextnode(i);
        }

        if (i->i != NULL && hhash_iter_finished(i->i)) {
             free(i->i);
             i->i = NULL;
        }
    }

    return i;
}
Exemplo n.º 11
0
static void hashnode_split_reinsert(hattrie_t *T, node_ptr parent, node_ptr src)
{
    value_t* u = NULL;
    const char* key = NULL;
    uint16_t len = 0;
    hhash_iter_t i;

    hhash_iter_begin(src.b, &i, false);
    while (!hhash_iter_finished(&i)) {
        key = hhash_iter_key(&i, &len);
        u   = hhash_iter_val(&i);

        *find_below(T, parent, key, len) = *u;

        hhash_iter_next(&i);
    }
    hhash_free(src.b);
}
Exemplo n.º 12
0
int hattrie_split_mid(node_ptr node, unsigned *left_m, unsigned *right_m)
{
    /* count the number of occourances of every leading character */
    unsigned int cs[NODE_CHILDS]; // occurance count for leading chars
    memset(cs, 0, NODE_CHILDS * sizeof(unsigned int));
    uint16_t len;
    const char* key;

    /*! \todo expensive, maybe some heuristics or precalc would be better */
    hhash_iter_t i;
    hhash_iter_begin(node.b, &i, false);
    while (!hhash_iter_finished(&i)) {
        key = hhash_iter_key(&i, &len);
        assert(len > 0);
        cs[(unsigned char) key[0]] += 1;
        hhash_iter_next(&i);
    }

    /* choose a split point */
    unsigned int all_m;
    unsigned char j = node.b->c0;
    all_m   = node.b->weight;
    *left_m  = cs[j];
    *right_m = all_m - *left_m;
    int d;

    while (j + 1 < node.b->c1) {
        d = abs((int) (*left_m + cs[j + 1]) - (int) (*right_m - cs[j + 1]));
        if (d <= abs((int) (*left_m) - (int) (*right_m)) && *left_m + cs[j + 1] < all_m) {
            j += 1;
            *left_m  += cs[j];
            *right_m -= cs[j];
        }
        else break;
    }

    return j;
}
Exemplo n.º 13
0
Arquivo: hhash.c Projeto: idtek/knot
int main(int argc, char *argv[])
{
	plan(11);

	/* Create memory pool context. */
	struct mempool *pool = mp_new(64 * 1024);
	knot_mm_t mm;
	mm.ctx = pool;
	mm.alloc = (knot_mm_alloc_t)mp_alloc;
	mm.free = NULL;

	/* Create hashtable */
	int ret = KNOT_EOK;
	uint16_t len = 0;
	const char *key = "mykey", *cur = NULL, *prev = NULL;
	value_t val = (void*)0xdeadbeef, *rval = NULL;
	hhash_iter_t it;
	hhash_t *tbl = hhash_create_mm(ELEM_COUNT, &mm);
	ok(tbl != NULL, "hhash: create");
	if (tbl == NULL) {
		return KNOT_ERROR; /* No point in testing further on. */
	}

	/* Generate random keys. */
	char *keys[ELEM_COUNT];
	unsigned nfilled = 0;
	for (unsigned i = 0; i < ELEM_COUNT; ++i) {
		keys[i] = test_randstr_mm(&mm);
	}

	/* Insert single element. */
	ret = hhash_insert(tbl, key, KEY_LEN(key), val);
	ok(ret == KNOT_EOK, "hhash: insert single element");

	/* Retrieve nonexistent element. */
	cur = "nokey";
	rval = hhash_find(tbl, cur, KEY_LEN(cur));
	ok(rval == NULL, "hhash: find non-existent element");

	/* Retrieve single element. */
	rval = hhash_find(tbl, key, KEY_LEN(key));
	ok(rval != NULL, "hhash: find existing element");

	/* Fill the table. */
	for (unsigned i = 0; i < ELEM_COUNT; ++i) {
		ret = hhash_insert(tbl, keys[i], KEY_LEN(keys[i]), keys[i]);
		if (ret != KNOT_EOK) {
			nfilled = i;
			break;
		}
	}

	/* Check all keys integrity. */
	unsigned nfound = 0;
	for (unsigned i = 0; i < nfilled; ++i) {
		rval = hhash_find(tbl, keys[i], KEY_LEN(keys[i]));
		if (!rval || memcmp(*rval, keys[i], KEY_LEN(keys[i])) != 0) {
			break; /* Mismatch */
		}
		++nfound;
	}
	is_int(nfilled, nfound, "hhash: found all inserted keys");

	/* Test keys order index. */
	hhash_build_index(tbl);
	hhash_iter_begin(tbl, &it, true);
	while (!hhash_iter_finished(&it)) {
		cur = hhash_iter_key(&it, &len);
		if (!str_check_sort(prev, cur)) {
			break;
		}
		prev = cur;
		int strl = strlen(cur);
		assert(strl + 1 == len);
		hhash_iter_next(&it);
	}
	ok(hhash_iter_finished(&it), "hhash: passed order index checks");

	/* Retrieve all keys. */
	nfound = 0;
	hhash_iter_begin(tbl, &it, false);
	while (!hhash_iter_finished(&it)) {
		cur = hhash_iter_key(&it, &len);
		if (hhash_find(tbl, cur, len) == NULL) {
			break;
		} else {
			++nfound;
		}
		hhash_iter_next(&it);
	}
	ok(hhash_iter_finished(&it), "hhash: found all iterated keys");
	is_int(tbl->weight, nfound, "hhash: all iterated keys found");

	/* Test find less or equal. */
	prev = "mykey0"; /* mykey should precede it */
	hhash_find_leq(tbl, prev, KEY_LEN(prev), &rval);
	ok(rval && *rval == val, "hhash: find less or equal");

	/* Delete key and retrieve it. */
	ret = hhash_del(tbl, key, KEY_LEN(key));
	ok(ret == KNOT_EOK, "hhash: remove key");
	rval = hhash_find(tbl, key, KEY_LEN(key));
	ok(rval == NULL, "hhash: find removed element");

	/* Free all memory. */
	mp_delete(mm.ctx);
	return KNOT_EOK;
}