Introduction
Quickly access nodes by maintaining multiple pointers to other nodes in each node
Search speed: average O(log N), worst O(N)
Support sequential operations
Use skip list as one of the underlying implementations of ordered collection keys
nature
- Each layer is an ordered linked list, and the linked list at the bottom (L1) contains all elements
- If an element appears in the linked list at level n, it also appears at level n+1
- The search first searches from the upper layer. When the current element is larger than the target, it searches to the next level (that is, looking for more dense data)
- Overall, look from the top left to the bottom right
achieve
level: the number of levels of the node with the largest number of levels in the table
length: number of nodes
Skip list node
typedef struct zskiplistNode
{
struct zskiplistNode * backward;//后退指针
double score;//分值
robj * obj;//成员对象
struct zskiplistLevel //层
{
struct zskiplistNode * forward; //前进指针
unsigned int span;//跨度
}
}
Floor
The level array can contain multiple elements, and each element contains a pointer to other nodes to speed up access to other nodes
Feel the power law, randomly generate a value of [1,32] as the size of the level array, that is, the height of the layer
Forward pointer
Each layer has a forward pointer pointing to the end of the table (level[i].forward)
span
Record the distance between two nodes
The span of all forward pointers to NULL is 0
The span is actually used to calculate the ranking: in the process of finding a node, the span of all layers visited along the way is accumulated, and the result obtained is the ranking of the target node in the jump table (that is, the node is the first node)
Back pointer
You can first access the tail node of the table through the tail pointer of the jump table, and then access the penultimate node through the back pointer... until you encounter the back pointer pointing to NULL
Points and membership
- Score: All nodes in the jump table are sorted from small to large
- Member object: pointer to SDS string object
- The member object in the jump list must be unique, but the score can be the same
- Nodes with the same score are sorted in lexicographic order of member variables
Skip list
typedef struct zskiplist
{
struct zskiplistNode * header,*tial;
unsigned long length;//表中节点的数量
int level;//表中层数最大的节点的层数
} zskiplist;
It enables O(1) to access the head and end nodes of the table, access the length of the jump table, and obtain the number of layers of the node with the largest number of layers in the table
t level;//The number of layers of the node with the largest number of layers in the table
) zskiplist;
使得能O(1)访问表头表尾节点,访问跳跃表长度,获取表中层数最大的节点的层数