zn->ele = ele;
return zn;
}
插入节点
插入节点的步骤如下:
1、寻找插入位置
2、调增跳跃表高度
3、插入节点
4、调整backward
看似不难,谁写谁知道。
/* 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;
//rank:记录当前层从header到update[i]所需步长,更新span的时候需要用到
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;
x = x->level[i].forward;
}
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 = zslRandomLevel();
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;
}
zsl->level = level;
}
x = zslCreateNode(level,score,ele);
for (i = 0; i < level; i++) {
x->level[i].forward = update[i]->level[i].forward;
update[i]->level[i].forward = x;
/* update span covered by update[i] as x is inserted here */
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 */
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;
}
我们用这个示例来看这个跳跃表的插入循环:
找这么张图不容易哈。
我们现在往里面插入 score=27,level=3 的数据。
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;
x = x->level[i].forward;
}
update[i] = x;
}
第一次进入for循环:
1)x为头,i=1;
2)rank[1] = 0;
3)进入while循环;
4)rank[1] = 1;x成为第一个节点(也就是update(1)那边)
5)由于第一个节点的L1层后继无人,所以被迫终止循环。update[i]=x;
第二次进入for循环(层级下降):
1)x为第一个节点,i=0;
2)rank[0]=rank[1]=1;
3)进入循环,这时已经下降一层了。
4)rank[0] = 2;
5)x继续往后,进入到第二个节点(也就是update[0]那边了)
6)由于插入score干不过后面那个,所以被迫退出循环,update[0]=x;
第三次进入for循环?nonono,莫得了、
接下来干嘛?调整最大高度。
level = zslRandomLevel();
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;
}
zsl->level = level;
}
rank用来记录头节点到update的span,不要忘了。
由于目标level=2,所以会进入分支。
1)rank[2]=0,因为update[2]是头结点。
2)update[2]是头结点。
3)先给level赋个值,后面会调整。
接下来干嘛?前戏都做完了,接下来插入节点。
x = zslCreateNode(level,score,ele);
for (i = 0; i < level; i++) {
x->level[i].forward = update[i]->level[i].forward;
update[i]->level[i].forward = x;
/* update span covered by update[i] as x is inserted here */
x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]);
update[i]->level[i].span = (rank[0] - rank[i]) + 1;
}
如果新插入的列太矮,那比较高的那一部分就不用去转接,只需要span+1即可。
/* increment span for untouched levels */
for (i = level; i < zsl->level; i++) {
update[i]->level[i].span++;
}
还剩最后一段,调整backword。
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;
真是庖丁解牛。我自己写的时候没有想这么清楚,就搞得有点乱。
查改就不说了吧,上面有查了,改就更不多说了吧,再看一下删。
删除节点
/* Internal function used by zslDelete, zslDeleteRangeByScore and
- zslDeleteRangeByRank. */
void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
int i;
for (i = 0; i < zsl->level; i++) {
if (update[i]->level[i].forward == x) {
update[i]->level[i].span += x->level[i].span - 1;
update[i]->level[i].forward = x->level[i].forward;
} else {
update[i]->level[i].span -= 1;
}
}
if (x->level[0].forward) {
x->level[0].forward->backward = x->backward;
} else {
zsl->tail = x->backward;
}
while(zsl->level > 1 && zsl->header->level[zsl->level-1].forward == NULL)
zsl->level–;
zsl->length–;
}
看完增,删就是一个缩减版的逆操作,应该不难理解吧。
删除整表
这里的英文解释挺详尽的了,代码也很清晰。从第0层开始,通过forward向后遍历,一个一个回收内存。节点都回收完了,再回收表结构。
/* Delete an element with matching score/element from the skiplist.
-
The function returns 1 if the node was found and deleted, otherwise
-
0 is returned.
-
If ‘node’ is NULL the deleted node is freed by zslFreeNode(), otherwise
-
it is not freed (but just unlinked) and *node is set to the node pointer,
-
so that it is possible for the caller to reuse the node (including the
-
referenced SDS string at node->ele). */
int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
int i;
x = zsl->header;
for (i = zsl->level-1; i >= 0; i–) {
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
(x->level[i].forward->score == score &&
分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
Spring Cloud实战
Spring Boot实战
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)
[ZSKIPLIST_MAXLEVEL], *x;
int i;
x = zsl->header;
for (i = zsl->level-1; i >= 0; i–) {
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
(x->level[i].forward->score == score &&
分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
[外链图片转存中…(img-3MnKKsQ5-1720022875739)]
Spring Cloud实战
[外链图片转存中…(img-ooeLXxQS-1720022875740)]
Spring Boot实战
[外链图片转存中…(img-aJ9P5cJH-1720022875741)]
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)