int
main(void)
{
    SXE_HASH    * hash;
    unsigned      i;
    unsigned      id;
    unsigned      bucket;
    int           counter[MAX_BUCKET_INDEX];

    plan_tests(2);
    sxe_log_set_level(SXE_LOG_LEVEL_DEBUG);

    memset(counter, 0, MAX_BUCKET_INDEX * sizeof(int));
    hash = sxe_hash_new("test-hash", HASH_SIZE);

    for (i = 0; i < HASH_SIZE; i++)
    {
        char in_buf[5];
        char out_buf[41];

        snprintf(in_buf, 5, "%d", i);
        sha1sum( in_buf, 4, out_buf);
        id = sxe_hash_set(hash, out_buf, SXE_HASH_SHA1_AS_HEX_LENGTH, 1U);
        bucket = sxe_pool_index_to_state(hash, id);
        SXEA11(bucket < MAX_BUCKET_INDEX, "Bucket index %u is out of range", bucket);
        counter[bucket]++;

        if (counter[bucket] > MAX_ALLOWED_PER_BUCKET_INDEX) {
            diag("Count at bucket index %u is greater than %u", counter[bucket], MAX_ALLOWED_PER_BUCKET_INDEX);
            break;
        }
    }

    is(i, HASH_SIZE, "%u items SHA1 hashed and no bucket has more that %u entries", i, MAX_ALLOWED_PER_BUCKET_INDEX);

    memset(counter, 0, MAX_BUCKET_INDEX * sizeof(int));
    hash = sxe_hash_new_plus("lookup3", HASH_SIZE, sizeof(SXE_HASH), 0, 8, SXE_HASH_OPTION_LOOKUP3_HASH);

    for (i = 0; i < HASH_SIZE; i++)
    {
        id = sxe_hash_take(hash);
        snprintf((char *)&hash[id], 9, "%08x", i);
        sxe_hash_add(hash, id);

        bucket = sxe_pool_index_to_state(hash, id);
        SXEA11(bucket < MAX_BUCKET_INDEX, "Bucket index %u is out of range", bucket);
        counter[bucket]++;

        if (counter[bucket] > MAX_ALLOWED_PER_BUCKET_INDEX + 1) {
            diag("Count at bucket index %u is greater than %u", counter[bucket], MAX_ALLOWED_PER_BUCKET_INDEX + 1);
            break;
        }
    }

    is(i, HASH_SIZE, "%u items lookup3 hashed and no bucket has more that %u entries", i, MAX_ALLOWED_PER_BUCKET_INDEX + 1);
    return exit_status();
}
Exemplo n.º 2
0
void
sxe_hash_give(void * array, unsigned id)
{
    SXE_HASH * hash  = SXE_HASH_ARRAY_TO_IMPL(array);

    SXEE6("sxe_hash_give(hash=%s,id=%u)", sxe_pool_get_name(array), id);
    sxe_pool_set_indexed_element_state(hash->pool, id, sxe_pool_index_to_state(array, id), SXE_HASH_UNUSED_BUCKET);
    SXER6("return");
}
Exemplo n.º 3
0
/*
 * Walk all the populated elements in the hash
 */
void
sxe_hash_walk(void * array, void (*cb)(unsigned))
{
    SXE_HASH * hash  = SXE_HASH_ARRAY_TO_IMPL(array);
    unsigned   id;
    unsigned   state;

    SXEE6("sxe_hash_walk(hash=%s)", sxe_pool_get_name(array));
    for (id = 0; id < hash->count; id++) {
        state = sxe_pool_index_to_state(hash->pool, id);
        if ((state != SXE_HASH_UNUSED_BUCKET   ) &&
            (state != SXE_HASH_NEW_BUCKET      ) &&
            (state != SXE_HASH_BUCKETS_RESERVED)  )
        {
            (*cb)(id);
        }
    }
    SXER6("return");
}