add uniq_skiplist.[hc]

pull/37/head
YuQing 2020-01-19 22:40:46 +08:00
parent 916ad1f9e0
commit 91e0124ae5
6 changed files with 537 additions and 9 deletions

View File

@ -1,6 +1,7 @@
Version 1.44 2020-01-19 Version 1.44 2020-01-19
* add test file src/tests/test_pthread_lock.c * add test file src/tests/test_pthread_lock.c
* add uniq_skiplist.[hc]
Version 1.43 2019-12-25 Version 1.43 2019-12-25
* replace function call system to getExecResult, * replace function call system to getExecResult,

View File

@ -14,8 +14,8 @@ FAST_SHARED_OBJS = hash.lo chain.lo shared_func.lo ini_file_reader.lo \
fast_buffer.lo multi_skiplist.lo flat_skiplist.lo \ fast_buffer.lo multi_skiplist.lo flat_skiplist.lo \
system_info.lo fast_blocked_queue.lo id_generator.lo \ system_info.lo fast_blocked_queue.lo id_generator.lo \
char_converter.lo char_convert_loader.lo common_blocked_queue.lo \ char_converter.lo char_convert_loader.lo common_blocked_queue.lo \
multi_socket_client.lo skiplist_set.lo json_parser.lo \ multi_socket_client.lo skiplist_set.lo uniq_skiplist.lo \
buffered_file_writer.lo json_parser.lo buffered_file_writer.lo
FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
logger.o sockopt.o base64.o sched_thread.o \ logger.o sockopt.o base64.o sched_thread.o \
@ -26,8 +26,8 @@ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
fast_buffer.o multi_skiplist.o flat_skiplist.o \ fast_buffer.o multi_skiplist.o flat_skiplist.o \
system_info.o fast_blocked_queue.o id_generator.o \ system_info.o fast_blocked_queue.o id_generator.o \
char_converter.o char_convert_loader.o common_blocked_queue.o \ char_converter.o char_convert_loader.o common_blocked_queue.o \
multi_socket_client.o skiplist_set.o json_parser.o \ multi_socket_client.o skiplist_set.o uniq_skiplist.o \
buffered_file_writer.o json_parser.o buffered_file_writer.o
HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
shared_func.h pthread_func.h ini_file_reader.h _os_define.h \ shared_func.h pthread_func.h ini_file_reader.h _os_define.h \
@ -39,8 +39,8 @@ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
skiplist_common.h system_info.h fast_blocked_queue.h \ skiplist_common.h system_info.h fast_blocked_queue.h \
php7_ext_wrapper.h id_generator.h char_converter.h \ php7_ext_wrapper.h id_generator.h char_converter.h \
char_convert_loader.h common_blocked_queue.h \ char_convert_loader.h common_blocked_queue.h \
multi_socket_client.h skiplist_set.h fc_list.h \ multi_socket_client.h skiplist_set.h uniq_skiplist.h \
json_parser.h buffered_file_writer.h fc_list.h json_parser.h buffered_file_writer.h
ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS) ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS)

View File

@ -15,6 +15,7 @@
#include <string.h> #include <string.h>
#include "common_define.h" #include "common_define.h"
#define SKIPLIST_MAX_LEVEL_COUNT 30
#define SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE 64 #define SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE 64
typedef int (*skiplist_compare_func)(const void *p1, const void *p2); typedef int (*skiplist_compare_func)(const void *p1, const void *p2);

View File

@ -35,10 +35,10 @@ int skiplist_set_init_ex(SkiplistSet *sl, const int level_count,
return EINVAL; return EINVAL;
} }
if (level_count > 30) { if (level_count > SKIPLIST_MAX_LEVEL_COUNT) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"level count: %d is too large", "level count: %d is too large exceeds %d",
__LINE__, level_count); __LINE__, level_count, SKIPLIST_MAX_LEVEL_COUNT);
return E2BIG; return E2BIG;
} }

417
src/uniq_skiplist.c Normal file
View File

@ -0,0 +1,417 @@
/**
* Copyright (C) 2015 Happy Fish / YuQing
*
* libfastcommon may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
**/
//uniq_skiplist.c
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "logger.h"
#include "uniq_skiplist.h"
int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
const int max_level_count, skiplist_compare_func compare_func,
skiplist_free_func free_func, const int alloc_skiplist_once,
const int min_alloc_elements_once)
{
int bytes;
int element_size;
int i;
int alloc_elements_once;
int result;
if (max_level_count <= 0) {
logError("file: "__FILE__", line: %d, "
"invalid max level count: %d",
__LINE__, max_level_count);
return EINVAL;
}
if (max_level_count > SKIPLIST_MAX_LEVEL_COUNT) {
logError("file: "__FILE__", line: %d, "
"max level count: %d is too large, exceeds %d",
__LINE__, max_level_count, SKIPLIST_MAX_LEVEL_COUNT);
return E2BIG;
}
bytes = sizeof(struct fast_mblock_man) * max_level_count;
factory->node_allocators = (struct fast_mblock_man *)malloc(bytes);
if (factory->node_allocators == NULL) {
logError("file: "__FILE__", line: %d, "
"malloc %d bytes fail, errno: %d, error info: %s",
__LINE__, bytes, errno, STRERROR(errno));
return errno != 0 ? errno : ENOMEM;
}
memset(factory->node_allocators, 0, bytes);
alloc_elements_once = min_alloc_elements_once;
if (alloc_elements_once <= 0) {
alloc_elements_once = SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE;
}
else if (alloc_elements_once > 1024) {
alloc_elements_once = 1024;
}
for (i=max_level_count-1; i>=0; i--) {
element_size = sizeof(UniqSkiplistNode) +
sizeof(UniqSkiplistNode *) * (i + 1);
if ((result=fast_mblock_init_ex(factory->node_allocators + i,
element_size, alloc_elements_once, NULL, false)) != 0)
{
return result;
}
if (i % 2 == 0 && alloc_elements_once < 64 * 1024) {
alloc_elements_once *= 2;
}
}
if ((result=fast_mblock_init_ex(&factory->skiplist_allocators,
sizeof(UniqSkiplist), alloc_skiplist_once > 0 ?
alloc_skiplist_once : 16 * 1024, NULL, false)) != 0)
{
return result;
}
factory->max_level_count = max_level_count;
factory->compare_func = compare_func;
factory->free_func = free_func;
srand(time(NULL));
return 0;
}
void uniq_skiplist_destroy(UniqSkiplistFactory *factory)
{
int i;
if (factory->node_allocators == NULL) {
return;
}
fast_mblock_destroy(&factory->skiplist_allocators);
for (i=0; i<factory->max_level_count; i++) {
fast_mblock_destroy(factory->node_allocators + i);
}
free(factory->node_allocators);
factory->node_allocators = NULL;
}
UniqSkiplist *uniq_skiplist_new(UniqSkiplistFactory *factory,
const int level_count)
{
UniqSkiplist *sl;
struct fast_mblock_man *top_mblock;
int i;
if (level_count <= 0) {
logError("file: "__FILE__", line: %d, "
"invalid level count: %d",
__LINE__, level_count);
errno = EINVAL;
return NULL;
}
if (level_count > factory->max_level_count) {
logError("file: "__FILE__", line: %d, "
"level count: %d is too large, "
"exceeds max level count: %d",
__LINE__, level_count, factory->max_level_count);
errno = E2BIG;
return NULL;
}
sl = (UniqSkiplist *)fast_mblock_alloc_object(
&factory->skiplist_allocators);
sl->element_count = 0;
sl->factory = factory;
sl->top_level_index = level_count - 1;
top_mblock = sl->factory->node_allocators + sl->top_level_index;
sl->top = (UniqSkiplistNode *)fast_mblock_alloc_object(top_mblock);
if (sl->top == NULL) {
errno = ENOMEM;
return NULL;
}
memset(sl->top, 0, top_mblock->info.element_size);
sl->tail = (UniqSkiplistNode *)fast_mblock_alloc_object(
sl->factory->node_allocators + 0);
if (sl->tail == NULL) {
errno = ENOMEM;
return NULL;
}
memset(sl->tail, 0, sl->factory->node_allocators[0].info.element_size);
for (i=0; i<level_count; i++) {
sl->top->links[i] = sl->tail;
}
return sl;
}
void uniq_skiplist_free(UniqSkiplist *sl)
{
UniqSkiplistNode *node;
UniqSkiplistNode *deleted;
if (sl->element_count == 0)
{
return;
}
node = sl->top->links[0];
if (sl->factory->free_func != NULL) {
while (node != sl->tail) {
deleted = node;
node = node->links[0];
sl->factory->free_func(deleted->data);
fast_mblock_free_object(sl->factory->node_allocators +
deleted->level_index, deleted);
}
} else {
while (node != sl->tail) {
deleted = node;
node = node->links[0];
fast_mblock_free_object(sl->factory->node_allocators +
deleted->level_index, deleted);
}
}
sl->element_count = 0;
}
static inline int uniq_skiplist_get_level_index(UniqSkiplist *sl)
{
int i;
for (i=0; i<sl->top_level_index; i++) {
if (rand() < RAND_MAX / 2) {
break;
}
}
return i;
}
int uniq_skiplist_insert(UniqSkiplist *sl, void *data)
{
int i;
int level_index;
int cmp;
UniqSkiplistNode *node;
UniqSkiplistNode *previous;
UniqSkiplistNode *tmp_previous[SKIPLIST_MAX_LEVEL_COUNT];
level_index = uniq_skiplist_get_level_index(sl);
previous = sl->top;
for (i=sl->top_level_index; i>level_index; i--) {
while (previous->links[i] != sl->tail) {
cmp = sl->factory->compare_func(data, previous->links[i]->data);
if (cmp < 0) {
break;
}
else if (cmp == 0) {
return EEXIST;
}
previous = previous->links[i];
}
}
while (i >= 0) {
while (previous->links[i] != sl->tail) {
cmp = sl->factory->compare_func(data, previous->links[i]->data);
if (cmp < 0) {
break;
}
else if (cmp == 0) {
return EEXIST;
}
previous = previous->links[i];
}
tmp_previous[i] = previous;
i--;
}
node = (UniqSkiplistNode *)fast_mblock_alloc_object(
sl->factory->node_allocators + level_index);
if (node == NULL) {
return ENOMEM;
}
node->level_index = level_index;
node->data = data;
//thread safe for one write with many read model
for (i=0; i<=level_index; i++) {
node->links[i] = tmp_previous[i]->links[i];
tmp_previous[i]->links[i] = node;
}
return 0;
}
static UniqSkiplistNode *uniq_skiplist_get_equal_previous(UniqSkiplist *sl,
void *data, int *level_index)
{
int i;
int cmp;
UniqSkiplistNode *previous;
previous = sl->top;
for (i=sl->top_level_index; i>=0; i--) {
while (previous->links[i] != sl->tail) {
cmp = sl->factory->compare_func(data, previous->links[i]->data);
if (cmp < 0) {
break;
}
else if (cmp == 0) {
*level_index = i;
return previous;
}
previous = previous->links[i];
}
}
return NULL;
}
static UniqSkiplistNode *uniq_skiplist_get_first_larger_or_equal(
UniqSkiplist *sl, void *data)
{
int i;
int cmp;
UniqSkiplistNode *previous;
previous = sl->top;
for (i=sl->top_level_index; i>=0; i--) {
while (previous->links[i] != sl->tail) {
cmp = sl->factory->compare_func(data, previous->links[i]->data);
if (cmp < 0) {
break;
}
else if (cmp == 0) {
return previous->links[i];
}
previous = previous->links[i];
}
}
return previous->links[0];
}
static UniqSkiplistNode *uniq_skiplist_get_first_larger(
UniqSkiplist *sl, void *data)
{
int i;
int cmp;
UniqSkiplistNode *previous;
previous = sl->top;
for (i=sl->top_level_index; i>=0; i--) {
while (previous->links[i] != sl->tail) {
cmp = sl->factory->compare_func(data, previous->links[i]->data);
if (cmp < 0) {
break;
}
else if (cmp == 0) {
return previous->links[i]->links[0];
}
previous = previous->links[i];
}
}
return previous->links[0];
}
int uniq_skiplist_delete(UniqSkiplist *sl, void *data)
{
int i;
int level_index;
UniqSkiplistNode *previous;
UniqSkiplistNode *deleted;
previous = uniq_skiplist_get_equal_previous(sl, data, &level_index);
if (previous == NULL) {
return ENOENT;
}
deleted = previous->links[level_index];
for (i=level_index; i>=0; i--) {
while (previous->links[i] != sl->tail &&
previous->links[i] != deleted)
{
previous = previous->links[i];
}
assert(previous->links[i] == deleted);
previous->links[i] = previous->links[i]->links[i];
}
if (sl->factory->free_func != NULL) {
sl->factory->free_func(deleted->data);
}
fast_mblock_free_object(sl->factory->node_allocators + level_index, deleted);
return 0;
}
void *uniq_skiplist_find(UniqSkiplist *sl, void *data)
{
int level_index;
UniqSkiplistNode *previous;
previous = uniq_skiplist_get_equal_previous(sl, data, &level_index);
return (previous != NULL) ? previous->links[level_index]->data : NULL;
}
int uniq_skiplist_find_all(UniqSkiplist *sl, void *data,
UniqSkiplistIterator *iterator)
{
int level_index;
UniqSkiplistNode *previous;
previous = uniq_skiplist_get_equal_previous(sl, data, &level_index);
if (previous == NULL) {
iterator->tail = sl->tail;
iterator->current = sl->tail;
return ENOENT;
}
iterator->current = previous->links[level_index];
iterator->tail = iterator->current->links[0];
return 0;
}
int uniq_skiplist_find_range(UniqSkiplist *sl, void *start_data,
void *end_data, UniqSkiplistIterator *iterator)
{
if (sl->factory->compare_func(start_data, end_data) > 0) {
iterator->current = sl->tail;
iterator->tail = sl->tail;
return EINVAL;
}
iterator->current = uniq_skiplist_get_first_larger_or_equal(sl, start_data);
if (iterator->current == sl->tail) {
iterator->tail = sl->tail;
return ENOENT;
}
iterator->tail = uniq_skiplist_get_first_larger(sl, end_data);
return iterator->current != iterator->tail ? 0 : ENOENT;
}

109
src/uniq_skiplist.h Normal file
View File

@ -0,0 +1,109 @@
/**
* Copyright (C) 2015 Happy Fish / YuQing
*
* libfastcommon may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
**/
//uniq_skiplist.h
#ifndef _UNIQ_SKIPLIST_H
#define _UNIQ_SKIPLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common_define.h"
#include "skiplist_common.h"
#include "fast_mblock.h"
typedef struct uniq_skiplist_node
{
void *data;
int level_index;
struct uniq_skiplist_node *links[0];
} UniqSkiplistNode;
typedef struct uniq_skiplist_factory
{
int max_level_count;
skiplist_compare_func compare_func;
skiplist_free_func free_func;
struct fast_mblock_man skiplist_allocators;
struct fast_mblock_man *node_allocators;
} UniqSkiplistFactory;
typedef struct uniq_skiplist
{
UniqSkiplistFactory *factory;
int top_level_index;
int element_count;
UniqSkiplistNode *top; //the top node
UniqSkiplistNode *tail; //the tail node for interator
} UniqSkiplist;
typedef struct uniq_skiplist_iterator {
UniqSkiplistNode *current;
UniqSkiplistNode *tail;
} UniqSkiplistIterator;
#ifdef __cplusplus
extern "C" {
#endif
#define uniq_skiplist_init(factory, max_level_count, compare_func, free_func) \
uniq_skiplist_init_ex(factory, max_level_count, \
compare_func, free_func, 64 * 1024, \
SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE)
int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
const int max_level_count, skiplist_compare_func compare_func,
skiplist_free_func free_func, const int alloc_skiplist_once,
const int min_alloc_elements_once);
void uniq_skiplist_destroy(UniqSkiplistFactory *factory);
UniqSkiplist *uniq_skiplist_new(UniqSkiplistFactory *factory,
const int level_count);
void uniq_skiplist_free(UniqSkiplist *sl);
int uniq_skiplist_insert(UniqSkiplist *sl, void *data);
int uniq_skiplist_delete(UniqSkiplist *sl, void *data);
void *uniq_skiplist_find(UniqSkiplist *sl, void *data);
int uniq_skiplist_find_all(UniqSkiplist *sl, void *data,
UniqSkiplistIterator *iterator);
int uniq_skiplist_find_range(UniqSkiplist *sl, void *start_data,
void *end_data, UniqSkiplistIterator *iterator);
static inline void uniq_skiplist_iterator(UniqSkiplist *sl, UniqSkiplistIterator *iterator)
{
iterator->current = sl->top->links[0];
iterator->tail = sl->tail;
}
static inline void *uniq_skiplist_next(UniqSkiplistIterator *iterator)
{
void *data;
if (iterator->current == iterator->tail) {
return NULL;
}
data = iterator->current->data;
iterator->current = iterator->current->links[0];
return data;
}
static inline bool uniq_skiplist_empty(UniqSkiplist *sl)
{
return sl->top->links[0] == sl->tail;
}
#ifdef __cplusplus
}
#endif
#endif