Example #1
0
int main()
{
    int arr[] = {3, 6, 9, 2, 11, 1, 4}, i;
    skiplist list;
    skiplist_init(&list);
 
    printf("Insert:--------------------\n");
    for (i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) {
        skiplist_insert(&list, arr[i], arr[i]);
    }
    skiplist_dump(&list);
 
    printf("Search:--------------------\n");
    int keys[] = {3, 4, 7, 10, 111};
 
    for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
        snode *x = skiplist_search(&list, keys[i]);
        if (x) {
            printf("key = %d, value = %d\n", keys[i], x->value);
        } else {
            printf("key = %d, not fuound\n", keys[i]);
        }
    }
 
    printf("Search:--------------------\n");
    skiplist_delete(&list, 3);
    skiplist_delete(&list, 9);
    skiplist_dump(&list);
 
    return 0;
}
static void test_delete()
{
    int i;
    int64_t start_time;
    int64_t end_time;
    void *value;

    start_time = get_current_time_ms();
    for (i=0; i<COUNT; i++) {
        assert(skiplist_delete(&sl, numbers + i) == 0);
    }
    assert(instance_count == 0);

    end_time = get_current_time_ms();
    printf("delete time used: %"PRId64" ms\n", end_time - start_time);

    start_time = get_current_time_ms();
    for (i=0; i<COUNT; i++) {
        value = skiplist_find(&sl, numbers + i);
        assert(value == NULL);
    }
    end_time = get_current_time_ms();
    printf("find after delete time used: %"PRId64" ms\n", end_time - start_time);

    i = 0;
    skiplist_iterator(&sl, &iterator);
    while ((value=skiplist_next(&iterator)) != NULL) {
        i++;
    }
    assert(i==0);
}
Example #3
0
/*从跳表中删除具有最小键值的表项*/
void *skiplist_delete_min(skiplist_t *l){
	skiplist_node_t *cur=l->head->forward[0];
	if(cur)
		return skiplist_delete(l,cur->item);
	else
		return NULL;
}
Example #4
0
/* skiplist_delete_free SKIPLIST FREEFUNC
 * Iterate over every element of SKIPLIST, calling FREEFUNC with the value of
 * each, and then delete SKIPLIST itself. If FREEFUNC is NULL, use free(3). */
void skiplist_delete_free(skiplist S, void (*freefunc)(void*)) {
    skiplist_iterator itr;
    if (!freefunc) freefunc = xfree;
    for (itr = skiplist_itr_first(S); itr; itr = skiplist_itr_next(S, itr))
        freefunc(skiplist_itr_value(S, itr));
    skiplist_delete(S);
}
Example #5
0
int main(int argc, char *argv[]) {
  srand(clock());
  SkipList_t *ptrSl = skiplist_alloc(10,1);
  int64_t i;
  for (i = 1; i <= 10; i++) {
    skiplist_insert(ptrSl, i, (void*)(i*i));
    SlKey_t slKey;
    int64_t cValue;
    if (skiplist_index(ptrSl, (i+1)/2-1, &slKey, (void**)(&cValue))) {
      assert(slKey == (i+1)/2);
    } else {
      assert(0);
    }
  }
  for (i = 0; i < 10; i++) {
    SlKey_t slKey;
    int64_t cValue;
    if (skiplist_index(ptrSl, i, &slKey, (void**)(&cValue))) {
      assert(slKey == i+1);
    } else {
      assert(0);
    }
  }
  int32_t ixRm = 5;
  assert(skiplist_delete(ptrSl, ixRm));
  for (i = 1; i <= 10; i++) {
    int64_t cValue = (int64_t)skiplist_find(ptrSl, i);
    if (i != ixRm) {
      assert(cValue == i*i);
    } else {
      assert(cValue == 0);
    }
  }
  for (i = 1; i <= 10; i+=2) {
    int32_t fSuccess = skiplist_delete(ptrSl,i);
    if (i != ixRm) {
      assert(fSuccess);
    } else {
      assert(!fSuccess);
    }
  }
  skiplist_free(ptrSl);
  return 1;
}
Example #6
0
int
main(void)
{
        int i;

	int *key = malloc(N * sizeof(int));
	if (key == NULL) {
		exit(-1);
        }

        struct skiplist *list = skiplist_new();
        if (list == NULL) {
                exit(-1);
        }

        printf("Test start!\n");
        printf("Add %d nodes...\n", N);

        /* Insert test */
        srandom(time(NULL));
        for (i = 0; i < N; i++) {
                int value = key[i] = (int)random();
                skiplist_add(list, key[i], value);
        }
#ifdef SKIPLIST_DEBUG
        skiplist_dump(list);
#endif

        /* Search test */
        printf("Now search each node...\n");
        for (i = 0; i < N; i++) {
                int value = skiplist_search(list, key[i]);
                if (value != -1) {
#ifdef SKIPLIST_DEBUG
                        printf("key:0x%08x value:0x%08x\n", key[i], value);
#endif
                } else {
                        printf("Not found:0x%08x\n", key[i]);
                }
        }

        /* Delete test */
        printf("Now remove all nodes...\n");
        for (i = 0; i < N; i++) {
                skiplist_remove(list, key[i]);
        }
#ifdef SKIPLIST_DEBUG
        skiplist_dump(list);
#endif

        printf("End of Test.\n");
        skiplist_delete(list);

        return 0;  
}
Example #7
0
int main(int argc, char **argv)
{
    skiplist *list = skiplist_new(compare_word);
    assert(list != NULL);
    

    FILE *f = fopen("../_test/wordlist.txt", "r");
    assert(f != NULL);

    char *key;
    int i = 0;
    long long start, stop;

    start = get_ustime_sec();
    while (!feof(f)) {
        i++;
        key = (char *)malloc(sizeof(char) * 30);
        assert(key != NULL);

        fscanf(f, "%s\n", key);
        skiplist_insert(list, key, &value);
    }
    stop = get_ustime_sec();
    fclose(f);
    
    printf("put %d word in skiplist cost time %lldμs\n", i, stop-start);
    
    /* 查询 */
    char test[30] = "comically";
    int *t = skiplist_search(list, test);
    if (t) {
        printf("%s in wordlist and value = %d\n", test, *t);
    } else {
        printf("%s not in wordlist\n", test);
    }


    skiplist_delete(list, test);

    t = skiplist_search(list, test);
    if (t) {
        printf("%s in wordlist\n", test);
    } else {
        printf("%s not in wordlist\n", test);
    }

    /**
     * 在删除 Hashmap 前,需要自己管理 Key, Value 指向的内存
     * 这里并没有释放 Key 内存
     */
    skiplist_free(list);
    return 0;
}
int
main(void)
{
        int i;
        struct timeval start, end;

        int *key = (int *)malloc(N * sizeof(int));
        if (key == NULL) {
                exit(-1);
        }

        struct skiplist *list = skiplist_new();
        if (list == NULL) {
                exit(-1);
        }

        printf("Test start!\n");
        printf("Add %d nodes...\n", N);

        /* Insert test */
        srandom(time(NULL));
        gettimeofday(&start, NULL);
        for (i = 0; i < N; i++) {
                int value = key[i] = (int)random();
                skiplist_insert(list, key[i], value);
        }
        gettimeofday(&end, NULL);
        printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000);
#ifdef SKIPLIST_DEBUG
        skiplist_dump(list);
#endif

        /* Search test 1 */
        printf("Now search each node by key...\n");
        gettimeofday(&start, NULL);
        for (i = 0; i < N; i++) {
                struct skipnode *node = skiplist_search_by_key(list, key[i]);
                if (node != NULL) {
#ifdef SKIPLIST_DEBUG
                        printf("key:0x%08x value:0x%08x\n", node->key, node->value);
#endif
                } else {
                        printf("Not found:0x%08x\n", key[i]);
                }
#ifdef SKIPLIST_DEBUG
                printf("key rank:%d\n", skiplist_key_rank(list, key[i]));
#else
                //skiplist_key_rank(list, key[i]);
#endif
        }
        gettimeofday(&end, NULL);
        printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000);

        /* Search test 2 */
        printf("Now search each node by rank...\n");
        gettimeofday(&start, NULL);
        for (i = 0; i < N; i++) {
                struct skipnode *node = skiplist_search_by_rank(list, i + 1);
                if (node != NULL) {
#ifdef SKIPLIST_DEBUG
                        printf("rank:%d value:0x%08x\n", i + 1, node->value);
#endif
                } else {
                        printf("Not found:%d\n", i + 1);
                }
        }
        gettimeofday(&end, NULL);
        printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000);

        /* Delete test */
        printf("Now remove all nodes...\n");
        gettimeofday(&start, NULL);
        for (i = 0; i < N; i++) {
                skiplist_remove(list, key[i]);
        }
        gettimeofday(&end, NULL);
        printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000);
#ifdef SKIPLIST_DEBUG
        skiplist_dump(list);
#endif

        printf("End of Test.\n");
        skiplist_delete(list);

        return 0;
}