redis源码分析(adlist)
来源:互联网 发布:androidargb调色板源码 编辑:程序博客网 时间:2024/05/17 08:27
typedef struct listNode { struct listNode *prev; struct listNode *next; void *value;} listNode;
首先定义了一个节点,包含前驱和后继以及对应的value
typedef struct listIter { listNode *next; int direction;} listIter;list的迭代器,next指针和迭代方向
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;链表内容 head和tail指针分别指向收尾,另外三个函数指针代表在拷贝,删除,对比的时候,如果有函数可调用就调用当前次函数的逻辑,不然就调用自身的逻辑
/* 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. */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;}/* 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); zfree(current); current = next; } zfree(list);}创建和删除链表没什么说的
/* 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;
//长度为0的,增加一个那么头尾指针都指向这个节点
//先让node的前驱指向null,node的后继指向头节点,然后让头节点的前驱指向node,最后在把头节点指向新的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++; return list;}在头和尾部ADD道理一致
list *listInsertNode(list *list, listNode *old_node, void *value, int after) { listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value;
//在old_node前还是后insert 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;}
0 0
- redis源码分析(adlist)
- redis源码分析-adlist(链表)
- Redis源码分析(adlist)
- 【redis源码分析】双向链表---adlist
- redis源码阅读(2)---- adlist分析
- redis源码分析(9)redis源码链表学习总结 adlist.h adlist.c
- Redis源码分析(二)——链表adlist
- redis源码分析之数据结构(一)链表adlist.c
- Redis源码学习(1):adlist
- Redis源码-数据结构之Adlist双端链表
- redis源码系列-数据结构(adlist/ziplist/dict)
- Redis源码阅读笔记—adlist
- Redis adlist
- 唯快不破:redis源码剖析02-adlist双向链表结构
- redis之adlist
- (redis)adlist.h/adlist.c理解
- redis源码解读之双向链表————adlist.h文件
- Redis之2-双端链表(adlist.c,adlist.h)
- 关于遍历Android手机中应用的问题
- rk代码repo管理问题
- SpringMVC的拦截器
- Linux命令:ulimit
- java编程基础笔记2
- redis源码分析(adlist)
- Yocto Project 快速入门指南
- leetcode_middle_90_264. Ugly Number II
- 小程序已死?我们拭目以待吧
- 深度学习:过拟合
- Jmeter的安装
- UVA
- 计算机图形学:虚拟服装设计
- 打鼹鼠-SSL 1506