skiplist bugfixed
parent
74fe4c8889
commit
0bb237190d
2
make.sh
2
make.sh
|
|
@ -50,7 +50,7 @@ else
|
|||
OFF_BITS=32
|
||||
fi
|
||||
|
||||
DEBUG_FLAG=1
|
||||
DEBUG_FLAG=0
|
||||
|
||||
CFLAGS='-Wall -D_FILE_OFFSET_BITS=64'
|
||||
if [ "$DEBUG_FLAG" = "1" ]; then
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ int skiplist_init_ex(Skiplist *sl, const int level_count,
|
|||
|
||||
for (i=0; i<level_count; i++) {
|
||||
element_size = sizeof(SkiplistNode) + sizeof(SkiplistNode *) * (i + 1);
|
||||
if ((result=fast_mblock_init_ex(sl->mblocks + i,
|
||||
if ((result=fast_mblock_init_ex(sl->mblocks + i,
|
||||
element_size, alloc_elements_once, NULL, false)) != 0)
|
||||
{
|
||||
return result;
|
||||
|
|
@ -90,11 +90,11 @@ void skiplist_destroy(Skiplist *sl)
|
|||
sl->mblocks = NULL;
|
||||
}
|
||||
|
||||
static inline int skiplist_get_previous_level_index(Skiplist *sl)
|
||||
static inline int skiplist_get_level_index(Skiplist *sl)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0; i<sl->level_count; i++) {
|
||||
for (i=0; i<sl->top_level_index; i++) {
|
||||
if (rand() < RAND_MAX / 2) {
|
||||
break;
|
||||
}
|
||||
|
|
@ -111,7 +111,7 @@ int skiplist_insert(Skiplist *sl, void *data)
|
|||
SkiplistNode *previous;
|
||||
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);
|
||||
if (node == NULL) {
|
||||
return ENOMEM;
|
||||
|
|
@ -211,4 +211,3 @@ void *skiplist_find(Skiplist *sl, void *data)
|
|||
return (previous != NULL) ? previous->links[level_index]->data : NULL;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
.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
|
||||
LIB_PATH = -lfastcommon
|
||||
|
||||
|
|
|
|||
|
|
@ -7,55 +7,47 @@
|
|||
#include <inttypes.h>
|
||||
#include <sys/time.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)
|
||||
{
|
||||
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 tmp;
|
||||
int index;
|
||||
int result;
|
||||
void *value;
|
||||
int64_t start_time;
|
||||
int64_t end_time;
|
||||
|
||||
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++) {
|
||||
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;
|
||||
}
|
||||
|
||||
start_time = get_current_time_ms();
|
||||
for (i=0; i<COUNT; i++) {
|
||||
if ((result=skiplist_insert(&sl, numbers + i)) != 0) {
|
||||
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++) {
|
||||
value = skiplist_find(&sl, &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;
|
||||
skiplist_iterator(&sl, &iterator);
|
||||
while ((value=skiplist_next(&iterator)) != NULL) {
|
||||
|
|
@ -63,6 +55,72 @@ int main(int argc, char *argv[])
|
|||
assert(i == *((int *)value));
|
||||
}
|
||||
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);
|
||||
|
||||
printf("pass OK\n");
|
||||
|
|
|
|||
Loading…
Reference in New Issue