1 /* adlist.h - A generic doubly linked list implementation 2 * 3 * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * * Neither the name of Redis nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without 16 * specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef __ADLIST_H__ 32 #define __ADLIST_H__ 33 34 /* Node, List, and Iterator are the only data structures used currently. */ 35 36 typedef struct listNode { 37 /* 前驱节点 */ 38 struct listNode *prev; 39 40 /* 后继节点 */ 41 struct listNode *next; 42 43 /* 值 */ 44 void *value; 45 } listNode;/* 双链表节点 */ 46 47 typedef struct listIter { 48 /* 下一节点 */ 49 listNode *next; 50 51 /* 迭代方向 */ 52 int direction; 53 } listIter;/* 迭代器 */ 54 55 typedef struct list { 56 /* 表头指针 */ 57 listNode *head; 58 59 /* 表尾指针 */ 60 listNode *tail; 61 62 /* 复制函数 */ 63 void *(*dup)(void *ptr); 64 65 /* 释放函数 */ 66 void (*free)(void *ptr); 67 68 /* 对比函数 */ 69 int (*match)(void *ptr, void *key); 70 71 /* 节点数量 */ 72 unsigned long len; 73 } list;/* 双链表 */ 74 75 /* Functions implemented as macros */ 76 #define listLength(l) ((l)->len) 77 #define listFirst(l) ((l)->head) 78 #define listLast(l) ((l)->tail) 79 #define listPrevNode(n) ((n)->prev) 80 #define listNextNode(n) ((n)->next) 81 #define listNodeValue(n) ((n)->value) 82 83 #define listSetDupMethod(l,m) ((l)->dup = (m)) 84 #define listSetFreeMethod(l,m) ((l)->free = (m)) 85 #define listSetMatchMethod(l,m) ((l)->match = (m)) 86 87 #define listGetDupMethod(l) ((l)->dup) 88 #define listGetFree(l) ((l)->free) 89 #define listGetMatchMethod(l) ((l)->match) 90 91 /* Prototypes */ 92 93 /* 创建一个新链表 */ 94 list *listCreate(void); 95 96 /* 释放一个新链表,以及该链表包含的节点 */ 97 void listRelease(list *list); 98 99 /* 将一个包含给定值的节点添加到链表的表头 */ 100 list *listAddNodeHead(list *list, void *value); 101 102 /* 将一个包含给定值的节点添加到链表的表尾 */ 103 list *listAddNodeTail(list *list, void *value); 104 105 /* 将一个包含给定值的节点添加到某个节点的之前或之后 */ 106 list *listInsertNode(list *list, listNode *old_node, void *value, int after); 107 108 /* 删除给定节点 */ 109 void listDelNode(list *list, listNode *node); 110 111 112 /* 创建一个列表迭代器 */ 113 listIter *listGetIterator(list *list, int direction); 114 115 /* 取出迭代器当前指向的节点 */ 116 listNode *listNext(listIter *iter); 117 118 /* 释放迭代器 */ 119 void listReleaseIterator(listIter *iter); 120 121 /* 创建一个给定链表的副本 */ 122 list *listDup(list *orig); 123 124 /* 在链表中查找和给定key匹配的节点 */ 125 listNode *listSearchKey(list *list, void *key); 126 127 /* 给据给定索引,返回列表中相应的节点 */ 128 listNode *listIndex(list *list, long index); 129 130 /* 将迭代器的指针指向表头 */ 131 void listRewind(list *list, listIter *li); 132 133 /* 将迭代器的指针指向表尾 */ 134 void listRewindTail(list *list, listIter *li); 135 136 /* 取出链表的表尾节点,将它插入到表头 */ 137 void listRotate(list *list); 138 139 /* Directions for iterators */ 140 #define AL_START_HEAD 0 141 #define AL_START_TAIL 1 142 143 #endif /* __ADLIST_H__ */
Redis源码笔记_adlist.h,布布扣,bubuko.com
原文:http://www.cnblogs.com/Justfun/p/3636714.html