编辑
2023-11-16
Redis源码阅读
00
请注意,本文编写于 541 天前,最后修改于 541 天前,其中某些信息可能已经过时。

目录

创建list并初始化
删所有的节点
释放整个链表
头插法插入
尾插法插入
往中间插入节点
删除节点
迭代器相关
复制链表
搜索值
把一个链表加入另一个链表
链接头尾

前几天读了redis双链表的实现,一个标准的双链表,但是也实现了存储任意类型,用void*,真的是奇淫技巧,可能是我见识少,感觉也能实现内核链表那种感觉,今天看来记录一下。

首先是结构定义

c
/* Node, List, and Iterator are the only data structures used currently. */ 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;

然后是一些简单的操作函数,用宏来实现

c
/* 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 listGetFreeMethod(l) ((l)->free) #define listGetMatchMethod(l) ((l)->match)

创建list并初始化

c
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; }

删所有的节点

删除所有节点,初始化长度为0

c
/* Remove all the elements from the list without destroying the list itself. */ void listEmpty(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; } list->head = list->tail = NULL; list->len = 0; }

释放整个链表

c
/* Free the whole list. * * This function can't fail. */ void listRelease(list *list) { listEmpty(list); zfree(list); }

头插法插入

c
/* 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. */ list *listAddNodeHead(list *list, void *value) { listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value; listLinkNodeHead(list, node); return list; } /* * Add a node that has already been allocated to the head of list */ void listLinkNodeHead(list* list, listNode *node) { 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++; }

尾插法插入

c
/* 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. */ list *listAddNodeTail(list *list, void *value) { listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value; listLinkNodeTail(list, node); return list; } /* * Add a node that has already been allocated to the tail of list */ void listLinkNodeTail(list *list, listNode *node) { 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++; }

往中间插入节点

c
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; }

删除节点

/* Remove the specified node from the specified list. * The node is freed. If free callback is provided the value is freed as well. * * This function can't fail. */ void listDelNode(list *list, listNode *node) { listUnlinkNode(list, node); if (list->free) list->free(node->value); zfree(node); } /* * Remove the specified node from the list without freeing it. */ void listUnlinkNode(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; node->next = NULL; node->prev = NULL; list->len--; }

迭代器相关

c
/* 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 */ void listReleaseIterator(listIter *iter) { zfree(iter); } /* Create an iterator in the list private iterator structure */ void listRewind(list *list, listIter *li) { li->next = list->head; li->direction = AL_START_HEAD; } 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 * pattern 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; }

复制链表

c
/* 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. */ 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) { /* Free value if dup succeed but listAddNodeTail failed. */ if (copy->free) copy->free(value); listRelease(copy); return NULL; } } return copy; }

搜索值

c
/* 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; 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; }

把一个链表加入另一个链表

被加入的链表清空

c
/* Add all the elements of the list 'o' at the end of the * list 'l'. The list 'other' remains empty but otherwise valid. */ void listJoin(list *l, list *o) { if (o->len == 0) return; o->head->prev = l->tail; if (l->tail) l->tail->next = o->head; else l->head = o->head; l->tail = o->tail; l->len += o->len; /* Setup other as an empty list. */ o->head = o->tail = NULL; o->len = 0; } /* Initializes the node's value and sets its pointers * so that it is initially not a member of any list. */ void listInitNode(listNode *node, void *value) { node->prev = NULL; node->next = NULL; node->value = value; }

链接头尾

c
/* Rotate the list removing the tail node and inserting it to the head. */ void listRotateTailToHead(list *list) { if (listLength(list) <= 1) return; /* Detach current tail */ listNode *tail = list->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; } /* Rotate the list removing the head node and inserting it to the tail. */ void listRotateHeadToTail(list *list) { if (listLength(list) <= 1) return; listNode *head = list->head; /* Detach current head */ list->head = head->next; list->head->prev = NULL; /* Move it as tail */ list->tail->next = head; head->next = NULL; head->prev = list->tail; list->tail = head; }

差不多,就是一个很正常的双向链表,但是也很多奇淫技巧。

本文作者:yowayimono

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!