skiplist bugfixed

pull/5/head
yuqing 2015-12-25 16:06:43 +08:00
parent 74fe4c8889
commit 0bb237190d
4 changed files with 91 additions and 34 deletions

View File

@ -50,7 +50,7 @@ else
OFF_BITS=32 OFF_BITS=32
fi fi
DEBUG_FLAG=1 DEBUG_FLAG=0
CFLAGS='-Wall -D_FILE_OFFSET_BITS=64' CFLAGS='-Wall -D_FILE_OFFSET_BITS=64'
if [ "$DEBUG_FLAG" = "1" ]; then if [ "$DEBUG_FLAG" = "1" ]; then

View File

@ -90,11 +90,11 @@ void skiplist_destroy(Skiplist *sl)
sl->mblocks = NULL; sl->mblocks = NULL;
} }
static inline int skiplist_get_previous_level_index(Skiplist *sl) static inline int skiplist_get_level_index(Skiplist *sl)
{ {
int i; int i;
for (i=0; i<sl->level_count; i++) { for (i=0; i<sl->top_level_index; i++) {
if (rand() < RAND_MAX / 2) { if (rand() < RAND_MAX / 2) {
break; break;
} }
@ -111,7 +111,7 @@ int skiplist_insert(Skiplist *sl, void *data)
SkiplistNode *previous; SkiplistNode *previous;
SkiplistNode *current; SkiplistNode *current;
level_index = skiplist_get_previous_level_index(sl); level_index = skiplist_get_level_index(sl);
node = (SkiplistNode *)fast_mblock_alloc_object(sl->mblocks + level_index); node = (SkiplistNode *)fast_mblock_alloc_object(sl->mblocks + level_index);
if (node == NULL) { if (node == NULL) {
return ENOMEM; return ENOMEM;
@ -211,4 +211,3 @@ void *skiplist_find(Skiplist *sl, void *data)
return (previous != NULL) ? previous->links[level_index]->data : NULL; return (previous != NULL) ? previous->links[level_index]->data : NULL;
} }

View File

@ -1,6 +1,6 @@
.SUFFIXES: .c .o .SUFFIXES: .c .o
COMPILE = $(CC) -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG COMPILE = $(CC) -O2 -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG
INC_PATH = -I/usr/include/fastcommon INC_PATH = -I/usr/include/fastcommon
LIB_PATH = -lfastcommon LIB_PATH = -lfastcommon

View File

@ -7,55 +7,47 @@
#include <inttypes.h> #include <inttypes.h>
#include <sys/time.h> #include <sys/time.h>
#include "skiplist.h" #include "skiplist.h"
#include "logger.h"
#include "shared_func.h"
#define COUNT 1000000
#define LAST_INDEX (COUNT - 1)
static int *numbers;
static Skiplist sl;
static SkiplistIterator iterator;
static int compare_func(const void *p1, const void *p2) static int compare_func(const void *p1, const void *p2)
{ {
return *((int *)p1) - *((int *)p2); return *((int *)p1) - *((int *)p2);
} }
int main(int argc, char *argv[]) static int test_insert()
{ {
#define COUNT 1280
#define LAST_INDEX (COUNT - 1)
Skiplist sl;
SkiplistIterator iterator;
int *numbers;
int i; int i;
int tmp;
int index;
int result; int result;
void *value; void *value;
int64_t start_time;
int64_t end_time;
numbers = (int *)malloc(sizeof(int) * COUNT); start_time = get_current_time_ms();
srand(time(NULL));
for (i=0; i<COUNT; i++) {
numbers[i] = i + 1;
}
for (i=0; i<COUNT; i++) {
index = LAST_INDEX * (int64_t)rand() / (int64_t)RAND_MAX;
tmp = numbers[index];
numbers[index] = numbers[LAST_INDEX - index];
numbers[LAST_INDEX - index] = tmp;
}
result = skiplist_init_ex(&sl, 12, compare_func, 128);
if (result != 0) {
return result;
}
for (i=0; i<COUNT; i++) { for (i=0; i<COUNT; i++) {
if ((result=skiplist_insert(&sl, numbers + i)) != 0) { if ((result=skiplist_insert(&sl, numbers + i)) != 0) {
return result; return result;
} }
} }
end_time = get_current_time_ms();
printf("insert time used: %"PRId64" ms\n", end_time - start_time);
start_time = get_current_time_ms();
for (i=1; i<=COUNT; i++) { for (i=1; i<=COUNT; i++) {
value = skiplist_find(&sl, &i); value = skiplist_find(&sl, &i);
assert(value != NULL && *((int *)value) == i); assert(value != NULL && *((int *)value) == i);
} }
end_time = get_current_time_ms();
printf("find time used: %"PRId64" ms\n", end_time - start_time);
start_time = get_current_time_ms();
i = 0; i = 0;
skiplist_iterator(&sl, &iterator); skiplist_iterator(&sl, &iterator);
while ((value=skiplist_next(&iterator)) != NULL) { while ((value=skiplist_next(&iterator)) != NULL) {
@ -63,6 +55,72 @@ int main(int argc, char *argv[])
assert(i == *((int *)value)); assert(i == *((int *)value));
} }
assert(i==COUNT); assert(i==COUNT);
end_time = get_current_time_ms();
printf("iterator time used: %"PRId64" ms\n", end_time - start_time);
return 0;
}
int main(int argc, char *argv[])
{
int i;
int tmp;
int index1;
int index2;
int result;
int64_t start_time;
int64_t end_time;
void *value;
log_init();
numbers = (int *)malloc(sizeof(int) * COUNT);
srand(time(NULL));
for (i=0; i<COUNT; i++) {
numbers[i] = i + 1;
}
for (i=0; i<COUNT; i++) {
index1 = LAST_INDEX * (int64_t)rand() / (int64_t)RAND_MAX;
index2 = LAST_INDEX * (int64_t)rand() / (int64_t)RAND_MAX;
if (index1 == index2) {
continue;
}
tmp = numbers[index1];
numbers[index1] = numbers[index2];
numbers[index2] = tmp;
}
result = skiplist_init_ex(&sl, 12, compare_func, 128);
if (result != 0) {
return result;
}
test_insert();
start_time = get_current_time_ms();
for (i=1; i<=COUNT; i++) {
assert(skiplist_delete(&sl, &i) == 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=1; i<=COUNT; i++) {
value = skiplist_find(&sl, &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);
test_insert();
skiplist_destroy(&sl); skiplist_destroy(&sl);
printf("pass OK\n"); printf("pass OK\n");