Example #1
0
static int _dict_expand_ifneed(dict *d)
{
    if(d->ht.size == 0)
        dict_expand(d,DICT_HT_INITIAL_SIZE);
    if(d->ht.used >= d->ht.size)
        dict_expand(d,d->ht.used * 2);

    return DICT_OK;
}
Example #2
0
File: ast.c Project: d11/MCECTL
int dict_append(struct dict **dict, struct dict *d2) {
    if (d2 == NULL)
        return 0;

    if (*dict == NULL) {
        *dict = d2;
        return 0;
    }

    struct dict *d1 = *dict;
    for (int i2 = 0; i2 < d2->used; i2++) {
        struct dict_node *n2 = d2->nodes[i2];
        int i1 = dict_pos(d1, n2->key);
        if (i1 < 0) {
            i1 = - i1 - 1;
            if (d1->size == d1->used) {
                if (dict_expand(d1) < 0)
                    return -1;
            }
            memmove(d1->nodes + i1 + 1, d1->nodes + i1,
                    sizeof(*d1->nodes) * (d1->used - i1));
            d1->nodes[i1] = n2;
            d1->used += 1;
        } else {
            struct dict_node *n1 = d1->nodes[i1];
            list_tail_cons(n1->entry, n1->mark, n2->entry);
            FREE(n2->key);
            FREE(n2);
        }
    }
    FREE(d2->nodes);
    FREE(d2);
    return 0;
}
Example #3
0
int main() {
	size_t i = 0, m, max=100000;
	char buf[128];
	struct timeval beg, end;
	long sec ;
	dict *d = dict_create(&opts);
	for(m = 0; m < 3; m++) {
		for(i = 0; i < max; i++) {
			memset(buf, 0, sizeof(buf));
			snprintf(buf, sizeof(buf), ";;%lu;;", i);
			dict_add(d, buf, buf);
		}
		printf("add %d, %d %llu\n", DICT_USED(d), DICT_CAP(d),used_mem());
		for(i = 0; i < max; i++) {
			dict_entry *entry;
			memset(buf, 0, sizeof(buf));
			snprintf(buf, sizeof(buf), ";;%lu;;", i);
			entry = dict_find(d, buf);
			if(strcmp((char*)entry->value, (char*)buf) != 0)
				abort();
		}
		printf("find %d, %d %llu\n", DICT_USED(d), DICT_CAP(d),used_mem());
		for(i = 0; i < max; i++) {
			memset(buf, 0, sizeof(buf));
			snprintf(buf, sizeof(buf), ";;%lu;;", i);
			dict_replace(d, buf, buf);
		}
		printf("prelace %d, %d %llu\n", DICT_USED(d), DICT_CAP(d),used_mem());
		for(i = 0; i < max; i++) {
			memset(buf, 0, sizeof(buf));
			snprintf(buf, sizeof(buf), ";;%lu;;", i);
			dict_del(d, buf);
		}
		printf("delete %d, %d %llu\n", DICT_USED(d), DICT_CAP(d),used_mem());
	}
	dict_expand(d, max*2);
	printf("%d, %d %llu\n", DICT_USED(d), DICT_CAP(d),used_mem());
	printf("%d %llu\n", DICT_CAP(d),used_mem());
	dict_destroy(d);
	printf("%llu\n", used_mem());


	d = dict_create(&opts);

	gettimeofday(&beg, NULL);
	for(i = 0; i < 1000000; i++) {
		memset(buf, 0, sizeof(buf));
		snprintf(buf, sizeof(buf), "%lu", i);
		dict_add(d, buf, buf);
	}
	gettimeofday(&end, NULL);
	sec = (end.tv_sec - beg.tv_sec)*1000;
	sec += (end.tv_usec - beg.tv_usec)/1000;
	printf("add msec %ld, mm: %llu\n", sec, used_mem());

	gettimeofday(&beg, NULL);
	dict_iterator *iter = dict_get_iterator(d);
	dict_entry *entry = NULL;
	i = 0;
	while((entry = dict_iterator_next(iter)) != NULL){
		i++;
	}
	dict_iterator_destroy(iter);
	gettimeofday(&end, NULL);
	sec = (end.tv_sec - beg.tv_sec)*1000;
	sec += (end.tv_usec - beg.tv_usec)/1000;
	printf("iterator msec %ld, times: %lu mm: %llu\n", sec, i, used_mem());

	gettimeofday(&beg, NULL);
	for(i = 0; i < 1000000; i++) {
		memset(buf, 0, sizeof(buf));
		snprintf(buf, sizeof(buf), "%lu", i);
		dict_del(d, buf);
	}
	gettimeofday(&end, NULL);
	sec = (end.tv_sec - beg.tv_sec)*1000;
	sec += (end.tv_usec - beg.tv_usec)/1000;
	printf("remove msec %ld, mm: %llu\n", sec, used_mem());

	dict_destroy(d);
	printf("%llu\r\n", used_mem());


	return 0;
}