When skiplist meets list head, something interesting might happen...Code less than z_set in Redis.
begeekmyfriend / skiplist Goto Github PK
View Code? Open in Web Editor NEWskip list with rank, code less than z_set in redis
License: MIT License
skip list with rank, code less than z_set in redis
License: MIT License
Hi,
struct skiplist *list = skiplist_new();
skiplist_insert(list, 5, 10);
skiplist_insert(list, 7, 10);
skiplist_dump(list);
skiplist_remove(list, 5);
skiplist_dump(list);
The above code produces
Total 2 nodes:
level:2 key:0x00000007 value:0x0000000a
level:1 key:0x00000005 value:0x0000000a
level:1 key:0x00000007 value:0x0000000a
Total 2 nodes:
level:2 key:0x00000007 value:0x0000000a
level:1 key:0x00000005 value:0x0000000a
level:1 key:0x00000007 value:0x0000000a
Seems 5 is not removed from the list. Would you be able to check it out?
in order to use gettimeofday, you should include
<sys/time.h>
instead of
<time.h>
which must be included when you want to use timespec which is another tool for timing
timespec is highly recommended for you to get an accurate timing result
struct skiplist *list = skiplist_new();
skiplist_insert(list, 1, 1);
skiplist_insert(list, 2, 2);
skiplist_insert(list, 3, 3);
remove_in_rank(list,1,1);
//list->count becomes 0.
Things seems went well like this:
struct skiplist *list = skiplist_new();
skiplist_insert(list, 3, 1);
skiplist_insert(list, 2, 2);
skiplist_insert(list, 1, 3);
remove_in_rank(list,1,1);
//list->count becomes 2.
Hello.
I would be nice to know under what license this code is distributed.
node = malloc(sizeof(*node) + level * sizeof(struct sk_link));
the right hand returned is void *. many compliers may give you an error that void * can not be assigned to a variable of type skipnode*. so, please use this instead:
node = (struct skipnode *)malloc(sizeof(*node) + level * sizeof(struct sk_link));
likewise, you should
list = (struct skiplist *)malloc(sizeof(*list));
I srand six numbers, This is result of skiplist_search_by_key(), it is correct:
Now search each node by key...
key:0x0000047e value:0x0000047e
key rank:1
key:0x000078c0 value:0x000078c0
key rank:6
key:0x00007477 value:0x00007477
key rank:5
key:0x00002dec value:0x00002dec
key rank:3
key:0x0000381f value:0x0000381f
key rank:4
key:0x00001355 value:0x00001355
key rank:2
But skiplist_search_by_rank() seems not right. Maybe I will check code later.
Now search each node by rank...
rank:1 value:0x00002dec
rank:2 value:0x00001355
rank:3 value:0x00002dec
rank:4 value:0x00007477
rank:5 value:0x00007477
rank:6 value:0x000078c0
OS: windows10 x86
static int random_level(void)
{
int level = 1;
const double p = 0.25;
while ((random() & 0xffff) < 0xffff * p) {
level++;
}
return level > MAX_LEVEL ? MAX_LEVEL : level;
}
这种随机算法的用意是?虽然比纯粹 { return random() % MAX_LEVEL + 1; } 好,但不知道为什么
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.