咱們知道,數據結構中,鏈表的最大好處就是能高效的實現動態增、刪、改,缺點就是遍歷訪問比較慢,所以,在Redis中,不少功能的底層實現都是基於鏈表的,由於Redis是基於C語言來寫的,因此只能本身實現本身的鏈表結構。對於一個常規的雙向鏈表節點,咱們一般使用下面的方式來定義: java
typedef struct Node{ void *value; struct Node *prev; struct Node *next; }Node;
Redis中,在adlist.h中也是這樣子定義的 node
typedef struct listNode { // 前置節點 struct listNode *prev; // 後置節點 struct listNode *next; // 節點的值 void *value; } listNode;
上面的node定義就是鏈表的每一個元素了。在Redis中,同時定義了一個雙向鏈表,並給這個鏈表定義了三個操做函數(方法): 數據結構
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;
同時,也定義了一個雙向鏈表的迭代器 函數
typedef struct listIter { // 當前迭代到的節點 listNode *next; // 迭代的方向,有從頭至尾,也有從尾到頭的方向 int direction; } listIter;
看到了上面的雙向節點,咱們可能會問在Redis中,他是採用有環的鏈表呢仍是無環鏈表?咱們之後的功能來看,由於要實現有環的鏈表只須要使用list的head的prev不爲NULL、tail的next不爲NULL便可實現。 spa
在adlist.h中,同時定義了一堆宏,用來實現簡單的函數功能 指針
// 返回給定鏈表所包含的節點數量 #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) // 將鏈表 l 的值複製函數設置爲 m #define listSetDupMethod(l,m) ((l)->dup = (m)) // 將鏈表 l 的值釋放函數設置爲 m #define listSetFreeMethod(l,m) ((l)->free = (m)) // 將鏈表的對比函數設置爲 m #define listSetMatchMethod(l,m) ((l)->match = (m)) // 返回給定鏈表的值複製函數 #define listGetDupMethod(l) ((l)->dup) // 返回給定鏈表的值釋放函數 #define listGetFree(l) ((l)->free) // 返回給定鏈表的值對比函數 #define listGetMatchMethod(l) ((l)->match)
上面定義了基本的獲取一個鏈表的常規屬性的函數(宏),根據咱們對一個數據結構的經常使用操做方式,下面就應該是定義建立、初始化、增長、刪除等的方法(函數)了。 code
//建立一個新列表 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; // 指向頭指針,我對於這裏的寫法,總會以爲有問題,加入list==NULL呢? current = list->head; // 遍歷整個鏈表 len = list->len; while(len--) { next = current->next; // 若是有設置值釋放函數,那麼調用它 if (list->free) list->free(current->value); // 釋放節點結構 zfree(current); current = next; } // 釋放鏈表結構 zfree(list); }
//用一個指定的value值建立一個node加入到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; }
//用一個指定的value來建立node,並加到list的末尾 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; }
//用指定的value來建立一個node,並根據傳入的after參數來判斷是要添加到指定的節點以前仍是以後 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; } // 更新新節點的後置指針 if (node->next != NULL) { node->next->prev = node; } // 更新鏈表節點數 list->len++; return list; }
//從list中刪除指定的node void listDelNode(list *list, listNode *node) { // 調整前置節點的指針,我在用java來編寫程序的時候,特別懼怕這種寫法,由於遇到空指針的時候,這裏必定會報錯,因此,下面的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--; }
//爲特定的list建立迭代器 listIter *listGetIterator(list *list, int direction) { // 爲迭代器分配內存 listIter *iter; if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL; // 根據迭代方向,設置迭代器的起始節點 if (direction == AL_START_HEAD)//這裏的AL_START_HEAD==0 iter->next = list->head; else iter->next = list->tail; // 記錄迭代方向 iter->direction = direction; return iter; }
//得到迭代器當前的節點,由於返回的都是指針,因此,是能夠改變的 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; }
//釋放迭代器 void listReleaseIterator(listIter *iter) { zfree(iter); }
//複製整個list,須要注意的是,若是原始的list的dup屬性爲空,那麼,先後兩個list會共享節點的指針,所以,要特別當心 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; // 迭代整個輸入鏈表 iter = listGetIterator(orig, AL_START_HEAD); while((node = listNext(iter)) != NULL) { void *value; // 複製節點值到新節點 if (copy->dup) { value = copy->dup(node->value); if (value == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } else value = node->value; // 將節點添加到鏈表 if (listAddNodeTail(copy, value) == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } // 釋放迭代器 listReleaseIterator(iter); // 返回副本 return copy; }
//在列表中尋找第一次出現指定key的節點,須要注意的是,若是在list中的節點的match函數爲空,那麼就直接比較value的指針,找到了第一個就返回,找不到就返回爲NULL listNode *listSearchKey(list *list, void *key) { listIter *iter; listNode *node; // 迭代整個鏈表 iter = listGetIterator(list, AL_START_HEAD); while((node = listNext(iter)) != NULL) { // 對比 if (list->match) { if (list->match(node->value, key)) { listReleaseIterator(iter); // 找到 return node; } } else { if (key == node->value) { listReleaseIterator(iter); // 找到 return node; } } } listReleaseIterator(iter); // 未找到 return NULL; }//查找list中指定索引的節點,節點能夠爲正數,也能夠是負數,當爲負數的時候,-1表明list的tail,依次類推,若是不爲負數,那麼,索引就是從0開始的 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; }
索引
內存
接下來的兩個函數,根據名字就能夠看出來,倒回,什麼意思呢?就是講list的迭代器的
//設置爲head->tail void listRewind(list *list, listIter *li) { li->next = list->head; li->direction = AL_START_HEAD; }
//設置爲tail->head void listRewindTail(list *list, listIter *li) { li->next = list->tail; li->direction = AL_START_TAIL; }
接下來的這個函數,總以爲名字和他實現的內容不匹配,這個函數要實現的是將鏈表的tail挪成list的head。 it
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; }好了,Redis的List基本就是這樣子啦