コード例 #1
0
ファイル: dynamic_hash_table.c プロジェクト: bioparr/biosal
void core_dynamic_hash_table_set_current_size_estimate(struct core_dynamic_hash_table *table,
                double value)
{
    uint64_t current_size;
    double threshold;
    uint64_t size_estimate;
    uint64_t next_size;
    uint64_t required;
    int avoided_resizing_operations;
    uint64_t current_buckets;

    current_size = core_dynamic_hash_table_size(table);
    threshold = table->resize_load_threshold;
    current_buckets = core_hash_table_buckets(table->current);

    size_estimate = current_size * (1 / value);

    next_size = 2;
    required = size_estimate / (threshold - 0.01);

    while (next_size < required) {
        next_size *= 2;
    }

    printf("ESTIMATE estimate %f current_size %" PRIu64 "/%" PRIu64 " size_estimate %" PRIu64 " threshold %f required %" PRIu64 " next_size %" PRIu64 "\n",
                    value, current_size, current_buckets,
                    size_estimate, threshold, required, next_size);

    if (next_size > table->resize_next_size) {
        avoided_resizing_operations = 0;

        while (current_buckets < next_size) {
            current_buckets *= 2;
            ++avoided_resizing_operations;
        }

        avoided_resizing_operations -= 1;
        printf("OPTIMIZATION resize_next_size... old value %" PRIu64 " new value %" PRIu64 " (avoided resizing operations: %d)\n",
                    table->resize_next_size, next_size, avoided_resizing_operations);

        table->resize_next_size = next_size;
    }
}
コード例 #2
0
ファイル: map.c プロジェクト: gkthiruvathukal/biosal
uint64_t core_map_size(struct core_map *self)
{
    return core_dynamic_hash_table_size(&self->table);
}
コード例 #3
0
int main(int argc, char **argv)
{
    BEGIN_TESTS();

    {
        struct core_dynamic_hash_table table;
        uint64_t i;
        uint64_t elements;

        elements = 900;
        core_dynamic_hash_table_init(&table, 1140, sizeof(int), 0);

        for (i = 0; i < elements; i++) {
            /*
            printf("insert %d\n", i);

            printf("before actual %d expected %d\n", (int)core_dynamic_hash_table_size(&table), i);
            */
            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i);

            core_dynamic_hash_table_add(&table, &i);

            /*
            printf("after1 actual %d expected %d\n", (int)core_dynamic_hash_table_size(&table), i + 1);
            */
            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i + 1);
            /*
            printf("after2 actual %d expected %d\n", (int)core_dynamic_hash_table_size(&table), i + 1);
            */
            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i + 1);
            /*
            printf("after3 actual %d expected %d\n", (int)core_dynamic_hash_table_size(&table), i + 1);
            */
            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i + 1);
            /*
            printf("after4 actual %d expected %d\n", (int)core_dynamic_hash_table_size(&table), i + 1);
            */
            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i + 1);
            /*
            printf("after5 actual %d expected %d\n", (int)core_dynamic_hash_table_size(&table), i + 1);
            */
            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i + 1);
            /*
            printf("after6 actual %d expected %d\n", (int)core_dynamic_hash_table_size(&table), i + 1);
            */
        }

        core_dynamic_hash_table_destroy(&table);
    }

    {
        struct core_dynamic_hash_table table;
        struct core_dynamic_hash_table_iterator iterator;
        int key_size;
        int value_size;
        uint64_t buckets;
        uint64_t i;
        int j;
        uint64_t inserted;
        int key;
        int *key_bucket;
        int *value_bucket;
        int found;

        key_size = sizeof(int);
        value_size = sizeof(int);
        buckets = 4;
        inserted = 0;

        core_dynamic_hash_table_init(&table, buckets, key_size, value_size);

        for (i = 0; i < buckets; i++) {
            key = inserted;
            value_bucket = core_dynamic_hash_table_add(&table, &key);
            inserted++;

            TEST_POINTER_NOT_EQUALS(value_bucket, NULL);

            *value_bucket = i;

            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), inserted);

            value_bucket = core_dynamic_hash_table_get(&table, &key);

            TEST_POINTER_NOT_EQUALS(value_bucket, NULL);
        }

        key = inserted;
        value_bucket = core_dynamic_hash_table_add(&table, &key);
        inserted++;

        for (j = 0; j < 1000; j++) {
            for (i = 0; i < buckets; i++) {
                key = inserted;
                value_bucket = core_dynamic_hash_table_add(&table, &key);
                inserted++;

                TEST_POINTER_NOT_EQUALS(value_bucket, NULL);

                *value_bucket = i;

                /*
                printf("DEBUG actual %d expected %d\n",
                                (int)core_dynamic_hash_table_size(&table), inserted);
                                */

                TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), inserted);

                value_bucket = core_dynamic_hash_table_get(&table, &key);

                TEST_POINTER_NOT_EQUALS(value_bucket, NULL);
            }
        }

        key = 9999;
        value_bucket = core_dynamic_hash_table_add(&table, &key);
        *value_bucket = 8888;

        core_dynamic_hash_table_iterator_init(&iterator, &table);

        i = 0;
        found = 0;

        while (core_dynamic_hash_table_iterator_has_next(&iterator)) {
            core_dynamic_hash_table_iterator_next(&iterator, (void **)&key_bucket,
                                                  (void **)&value_bucket);

            if (*key_bucket == 9999 && *value_bucket == 8888) {
                found = 1;
            }
            i++;
        }

        TEST_UINT64_T_EQUALS(i, core_dynamic_hash_table_size(&table));
        TEST_INT_EQUALS(found, 1);

        core_dynamic_hash_table_iterator_destroy(&iterator);

        core_dynamic_hash_table_destroy(&table);
    }

    {
        struct core_dynamic_hash_table table;
        int key;
        int *value;

        /*
        printf("-------------------\n");
        printf("DEBUG TEST-alpha-89\n");
        */

        core_dynamic_hash_table_init(&table, 8, sizeof(int), sizeof(int));

        for (key = 0; key < 1000000; key++) {

            /*
                        printf("DEBUG key %d\n", key);
                        */
            core_dynamic_hash_table_add(&table, &key);

            value = (int *)core_dynamic_hash_table_get(&table, &key);

            TEST_POINTER_NOT_EQUALS(value, NULL);

            *value = key;
        }

        core_dynamic_hash_table_destroy(&table);
    }

    {
        struct core_dynamic_hash_table table;
        uint64_t i;

        core_dynamic_hash_table_init(&table, 2, sizeof(int), sizeof(int));

        for (i = 0; i < 999; i++) {

            core_dynamic_hash_table_add(&table, &i);

            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i + 1);
        }

        core_dynamic_hash_table_destroy(&table);
    }

    {
        struct core_dynamic_hash_table table;
        uint64_t i;
        void *bucket;

        core_dynamic_hash_table_init(&table, 2, sizeof(int), 128);

        for (i = 0; i < 999; i++) {

            core_dynamic_hash_table_add(&table, &i);

            TEST_UINT64_T_EQUALS(core_dynamic_hash_table_size(&table), i + 1);
        }

        for (i = 0; i < 999; i++) {
            bucket = core_dynamic_hash_table_get(&table, &i);

            TEST_POINTER_NOT_EQUALS(bucket, NULL);
        }

        core_dynamic_hash_table_destroy(&table);

    }

    END_TESTS();

    return 0;
}