在上一篇中,我們瞭解了redis的基礎數據結構SDS,在redis中,基本上所有的數據都是以SDS的形式存儲的,無論是long還是int。
在有了基礎數據類型後,我們就要了解它是怎樣在redis中使用的,今天我們來分析redis鏈表的實現。
在學習的過程中基本是複習對鏈表的操作,只是有一些實現方法和理念解決了運行速度和適用環境的問題。
redis的鏈表具有以下特點:
1、雙端:獲取某個節點的前後節點的複雜度都是O(1);
2、無環:表頭的prev和表尾的next都指向NULL,對鏈表的訪問以NULL終結;
3、攜帶表頭和表尾指針:通過head和tail獲取的複雜度爲O(1);
4、帶鏈表長度計數器:使用len獲取節點數數量複雜度O(1);
5、多態:使用void*指向數據,通過dup,free,match設置節點的函數,可以保存各種不同類型的值。
上代碼:
.h文件中定義了鏈表的子節點,迭代器,和鏈表結構體。
/* adlist.h - A generic doubly linked list implementation */
#ifndef __ADLIST_H__
#define __ADLIST_H__
/* Node, List, and Iterator are the only data structures used currently. */
//鏈表的子節點,是一個雙向鏈表,void*可以指向任何結構
typedef struct listNode {
struct listNode *prev;
struct listNode *next;
void *value;
} listNode;
//該結構體用作實現迭代器
typedef struct listIter {
listNode *next;
int direction; //表示迭代器的方向
} listIter;
//主要的鏈表結構,記錄長度,頭尾,函數指針
typedef struct list {
listNode *head;
listNode *tail;
void *(*dup)(void *ptr);//複製函數指針
void (*free)(void *ptr);//釋放函數指針
int (*match)(void *ptr, void *key);//比較函數指針
unsigned long len;//長度
} list;
//這些宏定義的訪函數提高了程序的運行速度
/* Functions implemented as macros */
#define listLength(l) ((l)->len)
#define listFirst(l) ((l)->head)
#define listLast(l) ((l)->tail)
#define listPrevNode(n) ((n)->prev)
#define listNextNode(n) ((n)->next)
#define listNodeValue(n) ((n)->value)
#define listSetDupMethod(l,m) ((l)->dup = (m))
#define listSetFreeMethod(l,m) ((l)->free = (m))
#define listSetMatchMethod(l,m) ((l)->match = (m))
#define listGetDupMethod(l) ((l)->dup)
#define listGetFree(l) ((l)->free)
#define listGetMatchMethod(l) ((l)->match)
/* Prototypes */
list *listCreate(void);//創建鏈表
void listRelease(list *list);//釋放鏈表
list *listAddNodeHead(list *list, void *value);//頭插
list *listAddNodeTail(list *list, void *value);//尾插
list *listInsertNode(list *list, listNode *old_node, void *value, int after);//位置插
void listDelNode(list *list, listNode *node);//刪除某節點
listIter *listGetIterator(list *list, int direction);//獲取迭代器
listNode *listNext(listIter *iter);//獲取迭代器下一節點
void listReleaseIterator(listIter *iter);//釋放迭代器
list *listDup(list *orig);//複製鏈表
listNode *listSearchKey(list *list, void *key);//搜索
listNode *listIndex(list *list, long index);//下標索引
void listRewind(list *list, listIter *li);//重置迭代器指向頭
void listRewindTail(list *list, listIter *li);//重置迭代器指向尾節點
void listRotate(list *list);//鏈表旋轉
//這裏定義了迭代器的兩個方向,這裏我奇怪既然要效率爲什麼不直接定義兩種迭代器類型,這樣的話就不用在迭代器中判斷方向了
/* Directions for iterators */
#define AL_START_HEAD 0
#define AL_START_TAIL 1
#endif /* __ADLIST_H__ */
下面是.c文件關於鏈表的操作:
// adlist.c - A generic doubly linked list implementation
#include <stdlib.h>
#include "adlist.h"
#include "zmalloc.h"
//在堆中創建一個鏈表節點,並將各個數據成員初始化,放回地址
list *listCreate(void)
{
struct list *list;
if ((list = zmalloc(sizeof(*list))) == NULL)
return NULL;
list->head = list->tail = NULL;
list->len = 0;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
return list;
}
//釋放每個節點,最後釋放鏈表節點
void listRelease(list *list)
{
unsigned long len;
listNode *current, *next;
current = list->head;
len = list->len;
while(len--) {
next = current->next;//鏈表釋放:一定要先記錄下一個節點地址再釋放該節點
if (list->free) list->free(current->value);
zfree(current);
current = next;
}
zfree(list);
}
//頭插法,如果鏈表沒有節點,那麼頭尾都指向該節點
list *listAddNodeHead(list *list, void *value)
{
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->len++;
return list;
}
//尾插,如果長度爲0,那麼頭尾都指向該節點
list *listAddNodeTail(list *list, void *value)
{
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len++;
return list;
}
//在指定節點之前或者之後插入數據,after表示前後true爲齊納
list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (after) {
node->prev = old_node;
node->next = old_node->next;
if (list->tail == old_node) {
list->tail = node;
}
} else {
node->next = old_node;
node->prev = old_node->prev;
if (list->head == old_node) {
list->head = node;
}
}
if (node->prev != NULL) {
node->prev->next = node;//將老節點的next指向新節點
}
if (node->next != NULL) {
node->next->prev = node;//將老節點的下一個節點的prev指向新節點
}
list->len++;
return list;
}
//刪除節點,頭尾幾點是特殊情況,其他可以直接讓前一節點的next指向後一節點,後一節點的prev指向前一節點,然後釋放該節點
void listDelNode(list *list, listNode *node)
{
if (node->prev)
node->prev->next = node->next;
else
list->head = node->next;
if (node->next)
node->next->prev = node->prev;
else
list->tail = node->prev;
if (list->free) list->free(node->value);
zfree(node);
list->len--;
}
//獲取鏈表的迭代器,要指明是正向還是逆向的
listIter *listGetIterator(list *list, int direction)
{
listIter *iter;
if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
if (direction == AL_START_HEAD)
iter->next = list->head;
else
iter->next = list->tail;
iter->direction = direction;
return iter;
}
void listReleaseIterator(listIter *iter) {
zfree(iter);
}
//重置迭代器的位置
void listRewind(list *list, listIter *li) {
li->next = list->head;
li->direction = AL_START_HEAD;//0
}
void listRewindTail(list *list, listIter *li) {
li->next = list->tail;
li->direction = AL_START_TAIL;//1
}
//讓迭代器指向下一個節點
listNode *listNext(listIter *iter)
{
listNode *current = iter->next;
if (current != NULL) {
if (iter->direction == AL_START_HEAD)
iter->next = current->next;
else
iter->next = current->prev;
}
return current;
}
//複製鏈表,創建一個新鏈表,將鏈表的所有成員複製過來,然後利用迭代器和複製函數指針指向的複製函數複製每個節點
list *listDup(list *orig)
{
list *copy;
listIter iter;
listNode *node;
if ((copy = listCreate()) == NULL)
return NULL;
copy->dup = orig->dup;
copy->free = orig->free;
copy->match = orig->match;
listRewind(orig, &iter);
while((node = listNext(&iter)) != NULL) {
void *value;
if (copy->dup) {
value = copy->dup(node->value);
if (value == NULL) {
listRelease(copy);
return NULL;
}
} else
value = node->value;//如果沒有複製函數那麼就直接賦值
//使用尾插將節點插入新鏈表
if (listAddNodeTail(copy, value) == NULL) {
listRelease(copy);
return NULL;
}
}
return copy;
}
//查找某一值的鏈表節點
listNode *listSearchKey(list *list, void *key)
{
listIter iter;
listNode *node;
//利用迭代器遍歷鏈表,比較值是否相同,返回節點
listRewind(list, &iter);
while((node = listNext(&iter)) != NULL) {
if (list->match) {
if (list->match(node->value, key)) {
return node;//利用比較函數進行比較
}
} else {
if (key == node->value) {
return node;//直接進行值比較
}
}
}
return NULL;
}
//返回第幾個節點(從頭開始數),如果是負值就從尾開始數
listNode *listIndex(list *list, long index) {
listNode *n;
if (index < 0) {
index = (-index)-1;
n = list->tail;
while(index-- && n) n = n->prev;
} else {
n = list->head;
while(index-- && n) n = n->next;
}
return n;
}
/* Rotate the list removing the tail node and inserting it to the head. */
//將尾節點變成頭結點
void listRotate(list *list) {
listNode *tail = list->tail;
if (listLength(list) <= 1) return;
//記錄尾節點,將尾節點的前一節點變成尾節點
/* Detach current tail */
list->tail = tail->prev;
list->tail->next = NULL;
/* Move it as head 類似頭插操作*/
list->head->prev = tail;
tail->prev = NULL;
tail->next = list->head;
list->head = tail;
}
因爲鏈表操作比較簡單且可以適用箇中數據類型,在以後的工作中可能會用到,所以將所有的鏈表操作都寫了進來。