コード例 #1
0
ファイル: unit_tests.c プロジェクト: cmdrclueless/libdict
void test_basic_splay_tree()
{
    test_basic(sp_dict_new(dict_str_cmp, NULL), keys1, NKEYS1,
	       closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
    test_basic(sp_dict_new(dict_str_cmp, NULL), keys2, NKEYS2,
	       closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
}
コード例 #2
0
ファイル: unit_tests.c プロジェクト: sorc1/libdict
void test_basic_splay_tree()
{
    test_basic(sp_dict_new(dict_str_cmp, NULL), keys1, NKEYS1,
	       cl_infos, N_CL_INFOS);
    test_basic(sp_dict_new(dict_str_cmp, NULL), keys2, NKEYS2,
	       cl_infos, N_CL_INFOS);
}
コード例 #3
0
ファイル: benchmark.c プロジェクト: cmdrclueless/libdict
dict *
create_dictionary(char type, const char **container_name)
{
    dict_compare_func cmp_func = my_strcmp;
    dict_hash_func hash_func = str_hash;

    switch (type) {
	case 'h':
	    *container_name = "hb";
	    return hb_dict_new(cmp_func, key_str_free);

	case 'p':
	    *container_name = "pr";
	    return pr_dict_new(cmp_func, key_str_free);

	case 'r':
	    *container_name = "rb";
	    return rb_dict_new(cmp_func, key_str_free);

	case 't':
	    *container_name = "tr";
	    return tr_dict_new(cmp_func, NULL, key_str_free);

	case 's':
	    *container_name = "sp";
	    return sp_dict_new(cmp_func, key_str_free);

	case 'S':
	    *container_name = "sk";
	    return skiplist_dict_new(cmp_func, key_str_free, 12);

	case 'w':
	    *container_name = "wb";
	    return wb_dict_new(cmp_func, key_str_free);

	case 'H':
	    *container_name = "ht";
	    return hashtable_dict_new(cmp_func, hash_func, key_str_free, HASHTABLE_SIZE);

	case '2':
	    *container_name = "h2";
	    return hashtable2_dict_new(cmp_func, hash_func, key_str_free, HASHTABLE_SIZE);

	default:
	    quit("type must be one of h, p, r, t, s, w or H");
    }
}
コード例 #4
0
ファイル: demo.c プロジェクト: weixu8/libdict
int
main(int argc, char **argv)
{
    char buf[512], *p, *ptr, *ptr2;
    int rv;
    dict *dct;

    if (argc != 2)
	quit("usage: %s [type]", appname);

    srand((unsigned)time(NULL));

    dict_malloc_func = xmalloc;

    ++argv;
    switch (argv[0][0]) {
	case 'h':
	    dct = hb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'p':
	    dct = pr_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'r':
	    dct = rb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 't':
	    dct = tr_dict_new((dict_compare_func)strcmp, NULL, key_val_free);
	    break;
	case 's':
	    dct = sp_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'w':
	    dct = wb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'H':
	    dct = hashtable_dict_new((dict_compare_func)strcmp,
				     dict_str_hash,
				     key_val_free, HSIZE);
	    break;
	default:
	    quit("type must be one of h, p, r, t, s, w, or H");
    }

    if (!dct)
	quit("can't create container");

    for (;;) {
	printf("> ");
	fflush(stdout);
	if (fgets(buf, sizeof(buf), stdin) == NULL)
	    break;
	if ((p = strchr(buf, '\n')) != NULL)
	    *p = 0;
	for (p = buf; isspace(*p); p++)
	    /* void */;
	strcpy(buf, p);
	ptr2 = (ptr = strtok(buf, " ") ? strtok(NULL, " ") : NULL) ?
	    strtok(NULL, " ") : NULL;
	if (*buf == 0)
	    continue;
	if (strcmp(buf, "insert") == 0) {
	    if (!ptr2) {
		printf("usage: insert <key> <data>\n");
		continue;
	    }
	    void **datum_location;
	    if (dict_insert(dct, xstrdup(ptr), &datum_location)) {
		*datum_location = xstrdup(ptr2);
		printf("inserted '%s': '%s'\n",
		       ptr, *datum_location);
	    } else {
		printf("key '%s' already in dict: '%s'\n",
		       ptr, *datum_location);
	    }
	} else if (strcmp(buf, "search") == 0) {
	    if (ptr2) {
		printf("usage: search <key>\n");
		continue;
	    }
	    ptr2 = dict_search(dct, ptr);
	    if (ptr2)
		printf("found '%s': '%s'\n", ptr, ptr2);
	    else
		printf("key '%s' not in dict!\n", ptr);
	} else if (strcmp(buf, "remove") == 0) {
	    if (!ptr || ptr2) {
		printf("usage: remove <key>\n");
		continue;
	    }
	    rv = dict_remove(dct, ptr);
	    if (rv == 0)
		printf("removed '%s' from dict\n", ptr);
	    else
		printf("key '%s' not in dict!\n", ptr);
	} else if (strcmp(buf, "show") == 0) {
	    if (ptr) {
		printf("usage: show\n");
		continue;
	    }
	    dict_itor *itor = dict_itor_new(dct);
	    dict_itor_first(itor);
	    for (; dict_itor_valid(itor); dict_itor_next(itor))
		printf("'%s': '%s'\n",
		       (char *)dict_itor_key(itor),
		       (char *)dict_itor_data(itor));
	    dict_itor_free(itor);
	} else if (strcmp(buf, "reverse") == 0) {
	    if (ptr) {
		printf("usage: reverse\n");
		continue;
	    }
	    dict_itor *itor = dict_itor_new(dct);
	    dict_itor_last(itor);
	    for (; dict_itor_valid(itor); dict_itor_prev(itor))
		printf("'%s': '%s'\n",
		       (char *)dict_itor_key(itor),
		       (char *)dict_itor_data(itor));
	    dict_itor_free(itor);
	} else if (strcmp(buf, "clear") == 0) {
	    if (ptr) {
		printf("usage: clear\n");
		continue;
	    }
	    dict_clear(dct);
	} else if (strcmp(buf, "count") == 0) {
	    if (ptr) {
		printf("usage: count\n");
		continue;
	    }
	    printf("count = %zu\n", dict_count(dct));
	} else if (strcmp(buf, "quit") == 0) {
	    break;
	} else {
	    printf("Usage summary:\n");
	    printf("  insert <key> <data>\n");
	    printf("  search <key>\n");
	    printf("  remove <key>\n");
	    printf("  clear\n");
	    printf("  count\n");
	    printf("  show\n");
	    printf("  reverse\n");
	    printf("  quit\n");
	}
    }

    dict_free(dct);

    exit(0);
}
コード例 #5
0
ファイル: unit_tests.c プロジェクト: weixu8/libdict
void test_basic_splay_tree()
{
    test_basic(sp_dict_new(dict_str_cmp, NULL), keys1, NKEYS1);
    test_basic(sp_dict_new(dict_str_cmp, NULL), keys2, NKEYS2);
}