跳跃表可以方便实现有序存储,可以理解为链表的升级版。所以通过二分查找,可以实现平均O(logN),最坏O(N)的复杂度查找。据说大部分情况可以和平衡树相媲美,但是实现更为简单。
先学习跳跃表的原理,可以参考:跳跃表的原理及实现
总结几点就是:
1、层是为了方便实现二分查找设计的
2、理想情况下x层的元素数量是x+1层元素数量的2倍。0层保存了所有的节点,1层保存了0层奇数序的节点(也可以是偶数序),2层保存了1层奇数序的节点。理想情况下层数(不包含0层)和元素数量是2的指数关系,即为了最理想的实现二分查找,如果链表的层数是3(不包含0层),那么最理想的情况是支持元素数量为2^3=8个元素。
3、软件上一般使用随机数来确认新增节点的层数。
4、如果只有层0,那就是普通的有序链表。
redis中跳跃表的结构如下,加入了一个后向指针(指向表头的方向)。使用了柔性数组,层数可变。层数越多,表示跳跃的越精细,更有利于查找。没个节点的level可以不一样,redis使用的是随机数生成的层数,最大为64,可以支持2^64个节点。
/* ZSETs use a specialized version of Skiplists */
/* ZSETs use a specialized version of Skiplists */
typedef struct zskiplistNode {
sds ele; // 可以认为是真正要保存的数据
double score; // 数值 依赖该值来排序
struct zskiplistNode *backward; // 后向指针
struct zskiplistLevel {
struct zskiplistNode *forward; // 前向指针
unsigned long span; // 前向指针指向的节点和当前节点的跨度
} level[]; // 1个前向指针和跨度表示1层
} zskiplistNode;
下面是管理跳跃表的结构(非表头),表头的结构也是zskiplistNode,只不过只用到了level。
typedef struct zskiplist {
struct zskiplistNode *header, *tail; // 指向表头和表尾
unsigned long length; // 表中节点的数量,不包含表头
int level; // 表中元素层最多的层,不包含表头
redis跳跃表的源码在src\t_zset.c中。
跳跃表的创建即对以上的结构体赋初值。创造管理节点和表头节点,表头节点层数为最大值。
/* 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;
}
/* 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;
}
跳跃表难点是插入,插入分两步:
1、找到需要插入的位置,计算出插入节点在每层forward和span
2、生成新节点并插入,0层是所有节点必须有的层,跨度固定为1。
/* 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) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
unsigned int rank[ZSKIPLIST_MAXLEVEL];
int i, level;
// 确认score是否是数值
serverAssert(!isnan(score));
// 找到需要插入的位置,计算出插入节点在每层forward和span
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;
}