redis鏈表

redis鏈表

  • 作用:實現(xiàn)list命令
  • 作為redis定時事件的實現(xiàn)方式
  • 服務(wù)器保存客戶端列表等

數(shù)據(jù)結(jié)構(gòu)

  • 雙向非循環(huán)鏈表
// 鏈表節(jié)點
typedef struct listNode {
    struct listNode *prev; // 前驅(qū)
    struct listNode *next; // 后繼
    void *value;  // 值
} listNode;

// 鏈表迭代器
typedef struct listIter {
    listNode *next; // 下一個節(jié)點
    int direction; // 迭代方向
} listIter;

// 迭代方向
#define AL_START_HEAD 0
#define AL_START_TAIL 1


// 鏈表定義
typedef struct list {
    listNode *head; // 鏈表頭指針
    listNode *tail; // 鏈表尾指針
    void *(*dup)(void *ptr); // 節(jié)點值的復(fù)制函數(shù)
    void (*free)(void *ptr); // 節(jié)點值的釋放函數(shù)
    int (*match)(void *ptr, void *key); // 節(jié)點值的匹配函數(shù) 
    unsigned long len; // 鏈表長度
} list;

相關(guān)宏定義

/* Functions implemented as macros */
#define listLength(l) ((l)->len) // 獲取鏈表長度
#define listFirst(l) ((l)->head) // 獲取鏈表頭部節(jié)點
#define listLast(l) ((l)->tail) // 獲取鏈表尾部節(jié)點
#define listPrevNode(n) ((n)->prev) // 獲取某個節(jié)點的前驅(qū)節(jié)點
#define listNextNode(n) ((n)->next) // 獲取某個節(jié)點的后繼節(jié)點
#define listNodeValue(n) ((n)->value) // 獲取某個節(jié)點的值

#define listSetDupMethod(l,m) ((l)->dup = (m))  // 設(shè)置節(jié)點復(fù)制函數(shù)
#define listSetFreeMethod(l,m) ((l)->free = (m)) // 設(shè)置節(jié)點釋放函數(shù)
#define listSetMatchMethod(l,m) ((l)->match = (m))// 設(shè)置節(jié)點匹配函數(shù)

#define listGetDupMethod(l) ((l)->dup) // 獲取節(jié)點復(fù)制函數(shù)
#define listGetFree(l) ((l)->free) // 獲取節(jié)點釋放函數(shù)
#define listGetMatchMethod(l) ((l)->match) // 獲取節(jié)點匹配函數(shù)

功能函數(shù)實現(xiàn)

  • 函數(shù)原型
/* 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);
  • 具體實現(xiàn)
/* Create a new list. The created list can be freed with
 * AlFreeList(), but private value of every node need to be freed
 * by the user before to call AlFreeList().
 *
 * On error, NULL is returned. Otherwise the pointer to the new list. */
 
// 創(chuàng)建鏈表
list *listCreate(void)
{
    struct list *list;
    // 分配內(nèi)存
    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;
}

/* Free the whole list.
 *
 * This function can't fail. */
// 釋放鏈表
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); // 如果有值的釋放函數(shù)調(diào)用
        zfree(current); // 釋放內(nèi)存
        current = next;
    }
    zfree(list); // 釋放整個鏈表管理節(jié)點
}

/* Add a new node to the list, to head, containing the specified 'value'
 * pointer as value.
 *
 * On error, NULL is returned and no operation is performed (i.e. the
 * list remains unaltered).
 * On success the 'list' pointer you pass to the function is returned. */
// 頭部增加節(jié)點
list *listAddNodeHead(list *list, void *value)
{
    listNode *node;
    // 為節(jié)點分配內(nèi)存
    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 {// 已經(jīng)存在頭節(jié)點
        node->prev = NULL;
        node->next = list->head;
        list->head->prev = node;
        list->head = node;
    }
    list->len++; // 增加長度
    return list;
}

/* Add a new node to the list, to tail, containing the specified 'value'
 * pointer as value.
 *
 * On error, NULL is returned and no operation is performed (i.e. the
 * list remains unaltered).
 * On success the 'list' pointer you pass to the function is returned. */
// 在鏈表尾部添加節(jié)點
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;
}

// 在某個節(jié)點前(后)插入節(jié)點
list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
    listNode *node;
    // 創(chuàng)建插入節(jié)點
    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
    node->value = value;
    if (after) {// 節(jié)點之后插入
        node->prev = old_node;
        node->next = old_node->next;
        if (list->tail == old_node) { // 在尾部節(jié)點之后插入節(jié)點
            list->tail = node;
        }
    } else {// 節(jié)點之前插入
        node->next = old_node;
        node->prev = old_node->prev;
        if (list->head == old_node) {// 在頭部部節(jié)點之前插入節(jié)點
            list->head = node;
        }
    }
    if (node->prev != NULL) { // 插入節(jié)點后,非頭節(jié)點
        node->prev->next = node;
    }
    if (node->next != NULL) { // 插入節(jié)點后,非尾部節(jié)點
        node->next->prev = node;
    }
    list->len++;
    return list;
}

/* Remove the specified node from the specified list.
 * It's up to the caller to free the private value of the node.
 *
 * This function can't fail. */
 
// 刪除某個節(jié)點
void listDelNode(list *list, listNode *node)
{
    if (node->prev) // 待刪除節(jié)點有前驅(qū)節(jié)點
        node->prev->next = node->next;
    else // 刪除頭節(jié)點
        list->head = node->next;
    if (node->next)// 待刪除節(jié)點有后繼節(jié)點
        node->next->prev = node->prev;
    else // 刪除尾節(jié)點
        list->tail = node->prev;
    if (list->free) list->free(node->value); // 釋放值函數(shù)
    zfree(node); 
    list->len--; // 鏈表長度減1
}

/* Returns a list iterator 'iter'. After the initialization every
 * call to listNext() will return the next element of the list.
 *
 * This function can't fail. */
// 獲取鏈表某個方向上的迭代器
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;
}

/* Release the iterator memory */
// 釋放迭代器內(nèi)存
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}

/* Create an iterator in the list private iterator structure */
// 關(guān)聯(lián)迭代器和鏈表,從頭部開始迭代
void listRewind(list *list, listIter *li) {
    li->next = list->head;
    li->direction = AL_START_HEAD;
}

// 關(guān)聯(lián)迭代器和鏈表,從尾部開始迭代
void listRewindTail(list *list, listIter *li) {
    li->next = list->tail;
    li->direction = AL_START_TAIL;
}

/* Return the next element of an iterator.
 * It's valid to remove the currently returned element using
 * listDelNode(), but not to remove other elements.
 *
 * The function returns a pointer to the next element of the list,
 * or NULL if there are no more elements, so the classical usage patter
 * is:
 *
 * iter = listGetIterator(list,<direction>);
 * while ((node = listNext(iter)) != NULL) {
 *     doSomethingWith(listNodeValue(node));
 * }
 *
 * */
// 獲取迭代器的下一個元素
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;
}

/* Duplicate the whole list. On out of memory NULL is returned.
 * On success a copy of the original list is returned.
 *
 * The 'Dup' method set with listSetDupMethod() function is used
 * to copy the node value. Otherwise the same pointer value of
 * the original node is used as value of the copied node.
 *
 * The original list both on success or error is never modified. */
// 復(fù)制鏈表
list *listDup(list *orig)
{
    list *copy;
    listIter *iter;
    listNode *node;

    if ((copy = listCreate()) == NULL)
        return NULL;
    // 函數(shù)復(fù)制
    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) { // 復(fù)制節(jié)點值失敗
                listRelease(copy);
                listReleaseIterator(iter);
                return NULL;
            }
        } else // 沒有復(fù)制函數(shù),那么復(fù)制后的鏈表指向復(fù)制前的鏈表
            value = node->value;
        if (listAddNodeTail(copy, value) == NULL) { // 添加節(jié)點到尾部
            listRelease(copy);
            listReleaseIterator(iter);
            return NULL;
        }
    }
    listReleaseIterator(iter);
    return copy;
}

/* Search the list for a node matching a given key.
 * The match is performed using the 'match' method
 * set with listSetMatchMethod(). If no 'match' method
 * is set, the 'value' pointer of every node is directly
 * compared with the 'key' pointer.
 *
 * On success the first matching node pointer is returned
 * (search starts from head). If no matching node exists
 * NULL is returned. */
// 查找鏈表
listNode *listSearchKey(list *list, void *key)
{
    listIter *iter;
    listNode *node;

    iter = listGetIterator(list, AL_START_HEAD);
    while((node = listNext(iter)) != NULL) {
        if (list->match) {// 值匹配函數(shù)
            if (list->match(node->value, key)) {
                listReleaseIterator(iter);
                return node;
            }
        } else { // 直接使用==
            if (key == node->value) {
                listReleaseIterator(iter);
                return node;
            }
        }
    }
    listReleaseIterator(iter);
    return NULL;
}

/* Return the element at the specified zero-based index
 * where 0 is the head, 1 is the element next to head
 * and so on. Negative integers are used in order to count
 * from the tail, -1 is the last element, -2 the penultimate
 * and so on. If the index is out of range NULL is returned. */
// 獲取索引上的鏈表節(jié)點
listNode *listIndex(list *list, long index) {
    listNode *n;

    if (index < 0) { // 索引為負數(shù),從尾部開始查找
        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. */
// 旋轉(zhuǎn)鏈表,把尾部節(jié)點刪除,插入到頭部
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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 鏈表的實現(xiàn)方式有很多種,常見的主要有三個,單向鏈表、雙向鏈表、循環(huán)鏈表。 1、單鏈表 結(jié)構(gòu):第一個部分保存或者顯示...
    多多的大白閱讀 946評論 0 0
  • 鏈表作為一種常用的數(shù)據(jù)結(jié)構(gòu),提供了高效的節(jié)點重排能力,以及順序性節(jié)點訪問方式。并且可以通過增刪來靈活的調(diào)整鏈表的長...
    binge1024閱讀 809評論 0 0
  • 鏈表提供了高效的節(jié)點重排能力,以及順序性的節(jié)點訪問方式,并且可以通過增刪節(jié)點來靈魂的調(diào)整鏈表長度。 鏈表和鏈表節(jié)點...
    我要嘗鮮閱讀 402評論 0 1
  • 鏈表結(jié)構(gòu)是 Redis 中一個常用的結(jié)構(gòu),它可以存儲多個字符串,而且它是有序的,能夠存儲 2 的 32 次方減 1...
    祐吢房_2c9a閱讀 208評論 0 0
  • ?鏈表:具有高效節(jié)點重排能力,順序性節(jié)點訪問,通過增刪節(jié)點靈活調(diào)整長度。C語言中沒有內(nèi)置鏈表,Redis構(gòu)建了自身...
    i孤獨行者閱讀 131評論 0 0

友情鏈接更多精彩內(nèi)容