Redis的列表对象底层所使用的数据结构其中之一就是list。

list

Redis的list是一个双端链表,其由3部分构成:链表节点、链表迭代器、链表。这一设计思想和STL的list是一样的,STL的list也是由这三部分组成。需要特别说明的是Redis用C语言实现了list的迭代器,比较巧妙,下面就来分析list源码。

list节点

节点的值为void*类型,从而可以保存不同类型的值,甚至是另一种类型的对象。

  1. // 双端链表的节点
  2. typedef struct listNode {
  3. struct listNode *prev; // 指向上一个节点
  4. struct listNode *next; // 指向下一个节点
  5. void *value; // 指向节点的值, void*类型,使得节点可以保存不同类型的值
  6. } listNode;

list迭代器

c语言实现c++中的迭代器;双端链表的迭代器,方便了遍历链表的操作;根据direction,可设置为前向/反向迭代器

  1. typedef struct listIter {
  2. listNode *next; // 指向迭代器方向上下一个链表结点
  3. int direction; // AL_START_HEAD=0:从头部往尾部方向移动;AL_START_TAIL=1:往尾部往头部方向移动
  4. } listIter;

其中direction的取值有:

  1. /* Directions for iterators */
  2. // 迭代器方向的宏定义
  3. #define AL_START_HEAD 0
  4. #define AL_START_TAIL 1

list

与一般设计类似,list中有指向头尾节点的指针,以及链表节点数量的计数。不同的是,由于链表节点为void*类型,被设计为可以存储不同类型的数据,甚至是另一种类型的对象,所以添加了与节点相关的3个函数,作用分别是复制、释放、比较节点的值。

  1. // 双端链表
  2. typedef struct list {
  3. listNode *head; // 指向链表头节点
  4. listNode *tail; // 指向链表尾节点
  5. void *(*dup)(void *ptr); // 复制链表节点所保存的值
  6. void (*free)(void *ptr); // 释放链表节点所保存的值
  7. int (*match)(void *ptr, void *key); // 节点值比较函数
  8. unsigned long len; // 链表的节点数目
  9. } list;

list的操作函数

Redis用宏定义实现了一些复杂度为O(1)的链表操作,以提高list操作的效率。

  1. /* Functions implemented as macros */
  2. // 通过宏来实现一些O(1)时间复杂度的函数
  3. #define listLength(l) ((l)->len)
  4. #define listFirst(l) ((l)->head)
  5. #define listLast(l) ((l)->tail)
  6. #define listPrevNode(n) ((n)->prev)
  7. #define listNextNode(n) ((n)->next)
  8. #define listNodeValue(n) ((n)->value)
  9.  
  10. #define listSetDupMethod(l,m) ((l)->dup = (m))
  11. #define listSetFreeMethod(l,m) ((l)->free = (m))
  12. #define listSetMatchMethod(l,m) ((l)->match = (m))
  13.  
  14. #define listGetDupMethod(l) ((l)->dup)
  15. #define listGetFree(l) ((l)->free)
  16. #define listGetMatchMethod(l) ((l)->match)

list的源码比较好理解,本人对其已经做了详细的注释,就不仔细介绍了,下面附上源码及注释。list相关的文件有两个:adlist.h, adlist.c

adlist.h

  1. #ifndef __ADLIST_H__
  2. #define __ADLIST_H__
  3.  
  4. /* Node, List, and Iterator are the only data structures used currently. */
  5.  
  6. // redis的链表为双端链表
  7. // 节点的值为void*类型,从而可以保存不同类型的值
  8. // 结合dup,free,match函数实现链表的多态
  9.  
  10. // 双端链表的节点
  11. typedef struct listNode {
  12. struct listNode *prev; // 指向上一个节点
  13. struct listNode *next; // 指向下一个节点
  14. void *value; // 指向节点的值, void*类型,使得节点可以保存不同类型的值
  15. } listNode;
  16.  
  17. // c语言实现c++中的迭代器!!!
  18. // 双端链表的迭代器,方便了遍历链表的操作
  19. // 根据direction,可设置为前向/反向迭代器
  20. typedef struct listIter {
  21. listNode *next; // 指向迭代器方向上下一个链表结点
  22. int direction; // AL_START_HEAD=0:从头部往尾部方向移动;AL_START_TAIL=1:往尾部往头部方向移动
  23. } listIter;
  24.  
  25. // 双端链表
  26. typedef struct list {
  27. listNode *head; // 指向链表头节点
  28. listNode *tail; // 指向链表尾节点
  29. void *(*dup)(void *ptr); // 复制链表节点所保存的值
  30. void (*free)(void *ptr); // 释放链表节点所保存的值
  31. int (*match)(void *ptr, void *key); // 节点值比较函数
  32. unsigned long len; // 链表的节点数目
  33. } list;
  34.  
  35. /* Functions implemented as macros */
  36. // 通过宏来实现一些O(1)时间复杂度的函数
  37. #define listLength(l) ((l)->len)
  38. #define listFirst(l) ((l)->head)
  39. #define listLast(l) ((l)->tail)
  40. #define listPrevNode(n) ((n)->prev)
  41. #define listNextNode(n) ((n)->next)
  42. #define listNodeValue(n) ((n)->value)
  43.  
  44. #define listSetDupMethod(l,m) ((l)->dup = (m))
  45. #define listSetFreeMethod(l,m) ((l)->free = (m))
  46. #define listSetMatchMethod(l,m) ((l)->match = (m))
  47.  
  48. #define listGetDupMethod(l) ((l)->dup)
  49. #define listGetFree(l) ((l)->free)
  50. #define listGetMatchMethod(l) ((l)->match)
  51.  
  52. /* Prototypes */
  53. // list数据结构相关的函数
  54. // 具体含义见adlist.c
  55. list *listCreate(void);
  56. void listRelease(list *list);
  57. list *listAddNodeHead(list *list, void *value);
  58. list *listAddNodeTail(list *list, void *value);
  59. list *listInsertNode(list *list, listNode *old_node, void *value, int after);
  60. void listDelNode(list *list, listNode *node);
  61. listIter *listGetIterator(list *list, int direction);
  62. listNode *listNext(listIter *iter);
  63. void listReleaseIterator(listIter *iter);
  64. list *listDup(list *orig);
  65. listNode *listSearchKey(list *list, void *key);
  66. listNode *listIndex(list *list, long index);
  67. void listRewind(list *list, listIter *li);
  68. void listRewindTail(list *list, listIter *li);
  69. void listRotate(list *list);
  70.  
  71. /* Directions for iterators */
  72. // 迭代器方向的宏定义
  73. #define AL_START_HEAD 0
  74. #define AL_START_TAIL 1
  75.  
  76. #endif /* __ADLIST_H__ */

adlist.c

  1. /* adlist.c - A generic doubly linked list implementation
  2. */
  3.  
  4. #include <stdlib.h>
  5. #include "adlist.h"
  6. #include "zmalloc.h"
  7.  
  8. /* Create a new list. The created list can be freed with
  9. * AlFreeList(), but private value of every node need to be freed
  10. * by the user before to call AlFreeList().
  11. *
  12. * On error, NULL is returned. Otherwise the pointer to the new list. */
  13.  
  14. // 创建一个链表
  15. // 返回值:list/NULL
  16. list *listCreate(void)
  17. {
  18. struct list *list;
  19.  
  20. if ((list = zmalloc(sizeof(*list))) == NULL) // 为链表分配内存
  21. return NULL;
  22. // 初始化链表结构体的成员
  23. list->head = list->tail = NULL;
  24. list->len = ;
  25. list->dup = NULL;
  26. list->free = NULL;
  27. list->match = NULL;
  28. return list; // 返回为新链表分配的内存的起始地址
  29. }
  30.  
  31. /* Free the whole list.
  32. *
  33. * This function can't fail. */
  34.  
  35. // 释放链表及链表节点
  36. void listRelease(list *list)
  37. {
  38. unsigned long len;
  39. listNode *current, *next;
  40.  
  41. current = list->head;
  42. len = list->len;
  43. while(len--) {
  44. next = current->next;
  45. if (list->free) list->free(current->value); // 释放链表节点的值
  46. zfree(current); // 释放链表节点
  47. current = next;
  48. }
  49. zfree(list); // 释放链表
  50. }
  51.  
  52. /* Add a new node to the list, to head, containing the specified 'value'
  53. * pointer as value.
  54. *
  55. * On error, NULL is returned and no operation is performed (i.e. the
  56. * list remains unaltered).
  57. * On success the 'list' pointer you pass to the function is returned. */
  58.  
  59. // 从双端链表的头部插入新节点
  60. // 返回值:list/NULL
  61. list *listAddNodeHead(list *list, void *value)
  62. {
  63. listNode *node;
  64.  
  65. if ((node = zmalloc(sizeof(*node))) == NULL)
  66. return NULL;
  67. node->value = value;
  68. if (list->len == ) { // 原链表为一空链表
  69. list->head = list->tail = node;
  70. node->prev = node->next = NULL;
  71. } else {
  72. // 插入到双端链表的头结点之前
  73. node->prev = NULL;
  74. node->next = list->head;
  75. list->head->prev = node;
  76. list->head = node;
  77. }
  78. list->len++;
  79. return list;
  80. }
  81.  
  82. /* Add a new node to the list, to tail, containing the specified 'value'
  83. * pointer as value.
  84. *
  85. * On error, NULL is returned and no operation is performed (i.e. the
  86. * list remains unaltered).
  87. * On success the 'list' pointer you pass to the function is returned. */
  88.  
  89. // 从双端链表的尾部插入新节点
  90. // 返回值:list/NULL
  91. list *listAddNodeTail(list *list, void *value)
  92. {
  93. listNode *node;
  94.  
  95. if ((node = zmalloc(sizeof(*node))) == NULL)
  96. return NULL;
  97. node->value = value;
  98. if (list->len == ) {
  99. list->head = list->tail = node;
  100. node->prev = node->next = NULL;
  101. } else {
  102. node->prev = list->tail;
  103. node->next = NULL;
  104. list->tail->next = node;
  105. list->tail = node;
  106. }
  107. list->len++;
  108. return list;
  109. }
  110.  
  111. // 在链表list的节点old_node的前或后插入新节点
  112. // after为0,则在old_node之前插入;否则,在old_node之后插入
  113. // 返回值:list/NULL
  114. list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
  115. listNode *node;
  116.  
  117. if ((node = zmalloc(sizeof(*node))) == NULL)
  118. return NULL;
  119. node->value = value;
  120. if (after) { // old_node之后插入
  121. node->prev = old_node;
  122. node->next = old_node->next;
  123. if (list->tail == old_node) {
  124. list->tail = node;
  125. }
  126. } else { // old_node之前插入
  127. node->next = old_node;
  128. node->prev = old_node->prev;
  129. if (list->head == old_node) {
  130. list->head = node;
  131. }
  132. }
  133. if (node->prev != NULL) {
  134. node->prev->next = node;
  135. }
  136. if (node->next != NULL) {
  137. node->next->prev = node;
  138. }
  139. list->len++;
  140. return list;
  141. }
  142.  
  143. /* Remove the specified node from the specified list.
  144. * It's up to the caller to free the private value of the node.
  145. *
  146. * This function can't fail. */
  147.  
  148. // 删除链表list中节点node
  149. void listDelNode(list *list, listNode *node)
  150. {
  151. if (node->prev)
  152. node->prev->next = node->next;
  153. else
  154. list->head = node->next;
  155. if (node->next)
  156. node->next->prev = node->prev;
  157. else
  158. list->tail = node->prev;
  159. if (list->free) list->free(node->value);
  160. zfree(node);
  161. list->len--;
  162. }
  163.  
  164. /* Returns a list iterator 'iter'. After the initialization every
  165. * call to listNext() will return the next element of the list.
  166. *
  167. * This function can't fail. */
  168.  
  169. // 返回链表的迭代器
  170. // 返回值:list/NULL
  171. listIter *listGetIterator(list *list, int direction)
  172. {
  173. listIter *iter;
  174.  
  175. if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
  176. if (direction == AL_START_HEAD)
  177. iter->next = list->head; // 设置为前向迭代器
  178. else
  179. iter->next = list->tail; // 设置为反向迭代器
  180. iter->direction = direction;
  181. return iter;
  182. }
  183.  
  184. /* Release the iterator memory */
  185.  
  186. // 释放迭代器的内存
  187. void listReleaseIterator(listIter *iter) {
  188. zfree(iter);
  189. }
  190.  
  191. /* Create an iterator in the list private iterator structure */
  192.  
  193. // 回绕迭代器到链表头部
  194. void listRewind(list *list, listIter *li) {
  195. li->next = list->head;
  196. li->direction = AL_START_HEAD;
  197. }
  198.  
  199. // 回绕迭代器到链表尾部
  200. void listRewindTail(list *list, listIter *li) {
  201. li->next = list->tail;
  202. li->direction = AL_START_TAIL;
  203. }
  204.  
  205. /* Return the next element of an iterator.
  206. * It's valid to remove the currently returned element using
  207. * listDelNode(), but not to remove other elements.
  208. *
  209. * The function returns a pointer to the next element of the list,
  210. * or NULL if there are no more elements, so the classical usage patter
  211. * is:
  212. *
  213. * iter = listGetIterator(list,<direction>);
  214. * while ((node = listNext(iter)) != NULL) {
  215. * doSomethingWith(listNodeValue(node));
  216. * }
  217. *
  218. * */
  219.  
  220. // 返回迭代器所指向的元素,并将迭代器往其方向上移动一步
  221. // 返回值:指向当前节点的指针/NULL
  222. listNode *listNext(listIter *iter)
  223. {
  224. listNode *current = iter->next;
  225.  
  226. if (current != NULL) {
  227. if (iter->direction == AL_START_HEAD)
  228. iter->next = current->next;
  229. else
  230. iter->next = current->prev;
  231. }
  232. return current;
  233. }
  234.  
  235. /* Duplicate the whole list. On out of memory NULL is returned.
  236. * On success a copy of the original list is returned.
  237. *
  238. * The 'Dup' method set with listSetDupMethod() function is used
  239. * to copy the node value. Otherwise the same pointer value of
  240. * the original node is used as value of the copied node.
  241. *
  242. * The original list both on success or error is never modified. */
  243.  
  244. // 复制输入链表
  245. // list*/NULL
  246. list *listDup(list *orig)
  247. {
  248. list *copy;
  249. listIter iter;
  250. listNode *node;
  251.  
  252. if ((copy = listCreate()) == NULL) // 创建新链表
  253. return NULL;
  254. copy->dup = orig->dup;
  255. copy->free = orig->free;
  256. copy->match = orig->match;
  257. listRewind(orig, &iter); // 回绕迭代器到链表头部
  258. while((node = listNext(&iter)) != NULL) { // 遍历原链表,顺序取出节点
  259. void *value;
  260.  
  261. if (copy->dup) {
  262. value = copy->dup(node->value); // 通过list.dup函数复制节点值
  263. if (value == NULL) {
  264. listRelease(copy); // 出错释放链表
  265. return NULL;
  266. }
  267. } else
  268. value = node->value;
  269. if (listAddNodeTail(copy, value) == NULL) { // 从新链表尾部插入值
  270. listRelease(copy); // 出错释放链表
  271. return NULL;
  272. }
  273. }
  274. return copy;
  275. }
  276.  
  277. /* Search the list for a node matching a given key.
  278. * The match is performed using the 'match' method
  279. * set with listSetMatchMethod(). If no 'match' method
  280. * is set, the 'value' pointer of every node is directly
  281. * compared with the 'key' pointer.
  282. *
  283. * On success the first matching node pointer is returned
  284. * (search starts from head). If no matching node exists
  285. * NULL is returned. */
  286.  
  287. // 返回链表中节点值与key相匹配的节点
  288. // listNode*/NULL
  289. listNode *listSearchKey(list *list, void *key)
  290. {
  291. listIter iter;
  292. listNode *node;
  293.  
  294. listRewind(list, &iter);
  295. while((node = listNext(&iter)) != NULL) {
  296. if (list->match) {
  297. if (list->match(node->value, key)) { // 调用list.match函数对节点值进行比较
  298. return node;
  299. }
  300. } else {
  301. if (key == node->value) {
  302. return node;
  303. }
  304. }
  305. }
  306. return NULL;
  307. }
  308.  
  309. /* Return the element at the specified zero-based index
  310. * where 0 is the head, 1 is the element next to head
  311. * and so on. Negative integers are used in order to count
  312. * from the tail, -1 is the last element, -2 the penultimate
  313. * and so on. If the index is out of range NULL is returned. */
  314.  
  315. // 返回给定索引位置的节点
  316. // index=0,返回头结点
  317. // index < 0,则从尾部开始返回,index = -1,返回尾部节点
  318. listNode *listIndex(list *list, long index) {
  319. listNode *n;
  320.  
  321. if (index < ) {
  322. index = (-index)-;
  323. n = list->tail;
  324. while(index-- && n) n = n->prev;
  325. } else {
  326. n = list->head;
  327. while(index-- && n) n = n->next;
  328. }
  329. return n;
  330. }
  331.  
  332. /* Rotate the list removing the tail node and inserting it to the head. */
  333.  
  334. // 将尾部节点弹出,插入到链表头节点之前,成为新的表头节点
  335. void listRotate(list *list) {
  336. listNode *tail = list->tail;
  337.  
  338. if (listLength(list) <= ) return;
  339.  
  340. /* Detach current tail */
  341. list->tail = tail->prev;
  342. list->tail->next = NULL;
  343. /* Move it as head */
  344. list->head->prev = tail;
  345. tail->prev = NULL;
  346. tail->next = list->head;
  347. list->head = tail;
  348. }

(全文完)

附:Redis系列:http://www.cnblogs.com/zxiner/p/7197415.html

Redis—数据结构之list的更多相关文章

  1. Redis 数据结构使用场景

    转自http://get.ftqq.com/523.get 一.redis 数据结构使用场景 原来看过 redisbook 这本书,对 redis 的基本功能都已经熟悉了,从上周开始看 redis 的 ...

  2. Redis数据结构

    Redis数据结构 Redis数据结构详解(一)   前言 Redis和Memcached最大的区别,Redis 除啦支持数据持久化之外,还支持更多的数据类型而不仅仅是简单key-value结构的数据 ...

  3. Redis数据结构底层知识总结

    Redis数据结构底层总结 本篇文章是基于作者黄建宏写的书Redis设计与实现而做的笔记 数据结构与对象 Redis中数据结构的底层实现包括以下对象: 对象 解释 简单动态字符串 字符串的底层实现 链 ...

  4. Redis 数据结构与内存管理策略(上)

    Redis 数据结构与内存管理策略(上) 标签: Redis Redis数据结构 Redis内存管理策略 Redis数据类型 Redis类型映射 Redis 数据类型特点与使用场景 String.Li ...

  5. Redis 数据结构与内存管理策略(下)

    Redis 数据结构与内存管理策略(下) 标签: Redis Redis数据结构 Redis内存管理策略 Redis数据类型 Redis类型映射 Redis 数据类型特点与使用场景 String.Li ...

  6. Redis数据结构之intset

    本文及后续文章,Redis版本均是v3.2.8 上篇文章<Redis数据结构之robj>,我们说到redis object数据结构,其有5中数据类型:OBJ_STRING,OBJ_LIST ...

  7. Redis数据结构之robj

    本文及后续文章,Redis版本均是v3.2.8 我们知道一个database内的这个映射关系是用一个dict来维护的.dict的key固定用一种数据结构来表达,这这数据结构就是动态字符串sds.而va ...

  8. Redis 数据结构之dict(2)

    本文及后续文章,Redis版本均是v3.2.8 上篇文章<Redis 数据结构之dict>,我们对dict的结构有了大致的印象.此篇文章对dict是如何维护数据结构的做个详细的理解. 老规 ...

  9. Redis 数据结构之dict

    上篇文章<Redis数据结构概述>中,了解了常用数据结构.我们知道Redis以高效的方式实现了多种数据结构,因此把Redis看做为数据结构服务器也未尝不可.研究Redis的数据结构和正确. ...

  10. Redis数据结构以及应用场景

    1. Redis数据结构以及应用场景 1.1. Memcache VS Redis 1.1.1. 选Memcache理由 系统业务以KV的缓存为主,数据量.并发业务量大,memcache较为合适 me ...

随机推荐

  1. C++ STL 常用排序算法

    C++ STL 常用排序算法 merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. 例如: vecIntA,vecIntB,vecIntC是用ve ...

  2. 【BZOJ5334】数学计算(线段树)

    [BZOJ5334]数学计算(线段树) 题面 BZOJ 洛谷 题解 简单的线段树模板题??? 咕咕咕. #include<iostream> #include<cstdio> ...

  3. LaTex Font Size 字体大小

    目录 命令 效果图 命令 LaTex中字体大小由以下命令控制: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LA ...

  4. phpredis pipeline

    通过pipeline方式将client端命令一起发出,redis server会处理完多条命令后,将结果一起打包返回client,从而节省大量的网络延迟开销.

  5. java 面试题 -- 线程 按序 交替

    编写一个程序,开启 3 个线程,这三个线程的 ID 分别为A.B.C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示.如:ABCABCABC…… 依次递归? packag ...

  6. 2017实习【Java研发】面经

    标签: 实习 面经 Java研发 阿里.腾讯.华为 找2017暑假实习,经历过被腾讯拒绝的无奈,也有拿到阿里和华为offer的喜悦,找实习过程也有一段时间了,在此把之前的面试知识点和经历做个小总结,以 ...

  7. centos6.5搭建LVS+Keepalived

    1.配置LVS负载调度器 (1)为eth0配置IP地址,为eth0:0配置VIP地址. vi /etc/sysconfig/network-scripts/ifcfg-eth0 …… DEVICE=e ...

  8. bzoj 刷题计划~_~

    bzoj 2818 两个互质的数的gcd=1,所以他们同时乘一个素数那么他们的gcd=这个素数,所以枚举素数p找n/p以内有多少对互质数,用欧拉函数. bzoj 2809 可并堆,对于每一个子树显然是 ...

  9. Java入门:Java下载与安装方法

    本文适合刚入门的Java编程的初学者阅读. JDK有两种下载方法,一个是官网下载,另一个是第三方网站下载.官网速度也许有点慢,慢的话可以考虑去第三方网站下载. 一.官网下载 1. 访问地址:http: ...

  10. Python模拟登录cnblogs

    Python利用requests.Session对象模拟浏览器登录cnblogs request.Session对行可以跨请求的保持cookie,非常方便的用于模拟登录. cnblogs登录页面分析: ...