redis 数据结构之跳跃表
程序员文章站
2024-02-28 21:44:52
...
数据结构
跳跃表
typedef struct zskiplist {
struct zskiplistNode *header,*tail;
unsigned long length;
int level;
} zskiplist;
length 节点数量
level 表的最大层
跳跃表节点
typedef struct zskiplistNode {
sds ele;
double score;
struct zskiplistNode *backword;
struct zskiplistLevel {
struct zskiplistNode *forward;
unsigned int span;
} level[];
} zskiplistNode;
score 分值
forward 前进指针
span 间隔的节点数量,当前节点和下一个节点的距离,初始span为0,level[0]的span都为1
demo
源码API分析
创建skiplist
/* Create a new skiplist. */
zskiplist *zslCreate(void) {
int j;
zskiplist *zsl;
zsl = zmalloc(sizeof(*zsl));
zsl->level = 1;
zsl->length = 0;
zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);
for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
zsl->header->level[j].forward = NULL;
zsl->header->level[j].span = 0;
}
zsl->header->backward = NULL;
zsl->tail = NULL;
return zsl;
}
头节点的层是ZSKIPLIST_MAXLEVEL 32
zslFree 释放给定跳跃表
/* Free the specified skiplist node. The referenced SDS string representation
* of the element is freed too, unless node->ele is set to NULL before calling
* this function. */
void zslFreeNode(zskiplistNode *node) {
sdsfree(node->ele);
zfree(node);
}
/* Free a whole skiplist. */
void zslFree(zskiplist *zsl) {
zskiplistNode *node = zsl->header->level[0].forward, *next;//逗号表达式 声明了一个 zskiplistNode * 变量
zfree(zsl->header);
while(node) {
next = node->level[0].forward;
zslFreeNode(node);
node = next;
}
zfree(zsl);
}
从头节点依次释放,时间复杂度O(N)
zslCreateNode创建skiplistNode
/* Create a skiplist node with the specified number of levels.
* The SDS string 'ele' is referenced by the node after the call. */
zskiplistNode *zslCreateNode(int level, double score, sds ele) {
zskiplistNode *zn =
zmalloc(sizeof(*zn)+level*sizeof(struct zskiplistLevel));
zn->score = score;
zn->ele = ele;
return zn;
}
生成随机level
int zslRandomLevel(void) {
int level = 1;
while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
level += 1;
return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;
}
ZSKIPLIST_P的值是0.25,random()生成的数,转换成32位表示,高三位只有001、000满足,也就是说,k层的概率是k+1层概率的4倍
查找member的rank(ZRANK)
/* Find the rank for an element by both score and key.
* Returns 0 when the element cannot be found, rank otherwise.
* Note that the rank is 1-based due to the span of zsl->header to the
* first element. */
unsigned long zslGetRank(zskiplist *zsl, double score, sds ele) {
zskiplistNode *x;
unsigned long rank = 0;
int i;
//起始位置是从header开始
x = zsl->header;
//从跳跃表的level-1的位置开始,向下遍历
for (i = zsl->level-1; i >= 0; i--) {
//当前进指针不为null && 要查找的节点值在forward节点的前面(也就是说还需要往前查找)
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
(x->level[i].forward->score == score &&
sdscmp(x->level[i].forward->ele,ele) <= 0))) {
//rank加上这一层的span
rank += x->level[i].span;
//x前移
x = x->level[i].forward;
}
/* x might be equal to zsl->header, so test if obj is non-NULL */
if (x->ele && sdscmp(x->ele,ele) == 0) {
return rank;
}
}
return 0;
}
查找的过程类似树的查找,从K层开始查找,查找的score大于当前的score,指针前移,小于就从指针指向的位置的K-1层开始查找
插入新的Node
/* Insert a new node in the skiplist. Assumes the element does not already
* exist (up to the caller to enforce that). The skiplist takes ownership
* of the passed SDS string 'ele'. */
zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele) {
//update记录的是插入节点的每一层的上一个节点
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
unsigned int rank[ZSKIPLIST_MAXLEVEL];
int i, level;
serverAssert(!isnan(score));
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
/* store rank that is crossed to reach the insert position */
rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
(x->level[i].forward->score == score &&
sdscmp(x->level[i].forward->ele,ele) < 0)))
{
rank[i] += x->level[i].span;//rank[i]记录的是i层到从表头到update[i]的距离
x = x->level[i].forward;
}
//x为待插入节点的每一层的最后一个位置
update[i] = x;
}
/* we assume the element is not already inside, since we allow duplicated
* scores, reinserting the same element should never happen since the
* caller of zslInsert() should test in the hash table if the element is
* already inside or not. */
//新节点的level长度random是随机数
level = zslRandomLevel();
//如果random 大于 skiplist的最大层max,更新头节点的max到random层中的span,更新skiplist的最大层
if (level > zsl->level) {
for (i = zsl->level; i < level; i++) {
rank[i] = 0;
update[i] = zsl->header;
update[i]->level[i].span = zsl->length;//表头i层的span等于length
}
zsl->level = level;
}
x = zslCreateNode(level,score,ele);
for (i = 0; i < level; i++) {
//第i层 节点插入update[i]的后面
x->level[i].forward = update[i]->level[i].forward;
update[i]->level[i].forward = x;//i层x的前后节点都初始化
/* update span covered by update[i] as x is inserted here */
// update[i]->level[i].span i层x上一个节点到x下一个节点的距离
// rank[i] i层上一个节点到表头的距离
// rank[0]+1 是x到表头的距离
x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]);
update[i]->level[i].span = (rank[0] - rank[i]) + 1;
}
/* increment span for untouched levels */
//从random到max的这些层span进行更新
for (i = level; i < zsl->level; i++) {
update[i]->level[i].span++;
}
x->backward = (update[0] == zsl->header) ? NULL : update[0];
if (x->level[0].forward)
x->level[0].forward->backward = x;
else
zsl->tail = x;
zsl->length++;
return x;
}
时间复杂度 平均O(logN),最差O(N) ps(i+1层是i层数的1/2,0层的平均耗时为N/2)
zslDelete 删除,和插入类似,忽略。
注意
跳跃表允许重复的score值
查找的时候需要检查score值和member,查找的时候是从跳跃表的最大层在头节点开始查找,依次往下次查找,查找都是用的forward
头节点的最大层是32,跳跃表的最大层为节点的最大层
每层保存的span可以用来快速计算最后的rank,跳跃表的查找性能很高