设为首页 收藏本站
查看: 532|回复: 0

[经验分享] redis下的adlist

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-22 09:52:42 | 显示全部楼层 |阅读模式
1 //adlist.h
2 #ifndef __ADLIST__H__
3 #define __ADLIST__H__
4
5 typedef struct listNode_ {
6   struct listNode_ *prev;
7   struct listNode_ *next;
8   void *value;
9 } listNode;
10
11 typedef struct listIter_ {
12   listNode *next;
13   int      direction;
14 }listIter;
15
16 typedef struct List_ {
17   listNode *head;
18   listNode *tail;
19   void *(*dup)(void *ptr);
20   void (*free)(void *ptr);
21   int (*match)(void *ptr, void *key);
22   unsigned long len;
23 }List;
24
25 class ListM {
26   public:
27     enum {
28       AL_START_HEAD = 0,
29       AL_START_TAIL = 1,
30       AL_START_ERR  
31     };  
32
33     static List *ListCreate(void);
34     static void ListRelease(List *list);
35     
36     static List *ListAddNodeHead(List *list, void *value);
37     static List *ListAddNodeTail(List *list, void *value);
38     static List *ListInsertNode(List *list,
39                                 listNode *old_node,
40                                 void *value,
41                                 int after);
42         
43     static void ListDelNode(List *list, listNode *node);
44
45     static listIter *ListGetIterator(List *list, int direction);
46     static listNode *ListNext(listIter *iter);
47     static void ListReleaseIterator(listIter *iter);
48
49     static List *ListDup(List *orig);
50     static listNode *ListSearchKey(List *list, void *key);
51     static listNode *ListIndex(List *list, long index);
52
53     static void ListRewind(List *list, listIter *li);
54     static void ListRewindTail(List *list, listIter *li);
55     static void ListRotate(List *list);
56
57     static void Traversal(List *list);
58 };
59
60 #endif //__ADLIST__H__
  
  



  1 //adlist.cpp
  2 #include
  3 #include
  4 #include "adlist.h"
  5 #include "zmalloc.h"
  6
  7 List* ListM::ListCreate(void) {
  8    List *list;
  9    if((list = (List *)zmalloc(sizeof(List))) == NULL)
10      return NULL;
11
12    list->head = list->tail = NULL;
13    list->len = 0;
14    list->dup = NULL;
15    list->free = NULL;
16    list->match = NULL;
17    return list;
18 }
19
20 void ListM::ListRelease(List *list) {
21   unsigned long len = 0;
22   listNode *current, *next;
23
24   current = list->head;
25   len = list->len;
26   while(len--) {
27     next = current->next;
28     if(list->free) list->free(current->value);
29     zfree(current);
30     current = next;
31   }
32   zfree(list);
33 }
34
35 List *ListM::ListAddNodeHead(List *list, void *value) {
36   
37   listNode *node;
38
39   if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
40     return NULL;
41   }
42   node->value = value;
43
44   if(list->len == 0) {
45     list->head = list->tail = node;
46     node->prev = node->next = NULL;
47   } else {
48     node->prev = NULL;
49     node->next = list->head;
50     list->head->prev = node;
51     list->head = node;
52   }
53   list->len++;
54
55   return list;
56 }
57
58 List *ListM::ListAddNodeTail(List *list, void *value) {
59
60   listNode *node;
61
62   if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
63     return NULL;
64   }
65
66   node->value = value;
67   if(list->len == 0) {
68     list->head = list->tail = node;
69     node->prev = node->next = NULL;
70   } else {
71     node->prev = list->tail;
72     node->next = NULL;
73     list->tail->next = node;
74     list->tail = node;
75   }
76   list->len++;
77
78   return list;
79 }
80
81 List *ListM::ListInsertNode(List *list, listNode *old_node, void *value,int after) {
82    listNode *node;
83
84    if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
85      return NULL;
86    }
87
88    node->value = value;
89    if(after) {
90     node->prev = old_node;
91     node->next = old_node->next;
92     if(list->tail == old_node) {
93       list->tail = node;
94     }
95    } else {
96     node->next = old_node;
97     node->prev = old_node->prev;
98     if(list->head == old_node) {
99       list->head = node;
100     }
101    }
102
103    if(node->prev != NULL) {
104     node->prev->next = node;
105    }
106
107    if(node->next != NULL) {
108     node->next->prev = node;
109    }
110    list->len++;
111
112    return list;
113 }
114
115
116 void ListM::ListDelNode(List *list, listNode *node) {
117   if(node->prev)
118     node->prev->next =  node->next;
119   else
120     list->head = node->next;
121
122   if(node->next)
123     node->next->prev = node->prev;
124   else
125     list->tail = node->prev;
126
127   if(list->free) list->free(node->value);
128   zfree(node);
129   list->len--;
130 }
131
132 listIter *ListM::ListGetIterator(List *list, int direction) {
133
134   listIter *iter;
135
136   if((iter = (listIter *)zmalloc(sizeof(*iter))) == NULL) return NULL;
137
138   if(direction == AL_START_HEAD)
139     iter->next = list->head;
140   else
141     iter->next = list->tail;
142   iter->direction = direction;
143
144   return iter;
145 }
146
147 listNode *ListM::ListNext(listIter *iter) {
148   listNode *current = iter->next;
149
150   if(current != NULL) {
151     if(iter->direction == AL_START_HEAD)
152       iter->next = current->next;
153     else
154       iter->next = current->prev;
155   }
156   return current;
157 }
158
159 void ListM::ListReleaseIterator(listIter *iter) {
160   zfree(iter);
161 }
162
163 listNode *ListM::ListSearchKey(List *list, void *key) {
164   listNode *node = NULL;
165   listIter *iter = NULL;
166
167   iter = ListGetIterator(list, AL_START_HEAD);
168   while((node = ListNext(iter)) !=  NULL) {
169     if(list->match) {
170       if(list->match(node->value, key)) {
171         ListReleaseIterator(iter);
172         return node;
173       }
174     } else {
175       if(key == node->value) {
176         ListReleaseIterator(iter);
177         return node;
178       }
179     }
180   }
181
182   ListReleaseIterator(iter);
183   return NULL;
184 }
185
186 listNode *ListM::ListIndex(List *list, long index) {
187   listNode *node = NULL;
188
189   if(index < 0 ) {
190     index = (-index) - 1;
191     node = list->tail;
192     while(index-- && node) node = node->prev;
193   } else {
194     node = list->head;
195     while(index-- && node) node = node->next;
196   }
197   return node;
198 }
199
200 void ListM::ListRewind(List *list, listIter *li) {
201   li->next = list->head;
202   li->direction = AL_START_HEAD;
203 }
204
205 void ListM::ListRewindTail(List *list, listIter *li) {
206   li->next = list->tail;
207   li->direction = AL_START_TAIL;
208 }
209
210 void ListM::ListRotate(List *list) {
211   listNode *tail = list->tail;
212
213   if(list->len tail = tail->prev;
216   list->tail->next = NULL;
217
218   list->head->prev = tail;
219   tail->prev = NULL;
220   tail->next = list->head;
221   list->head = tail;
222 }
223
224 void ListM::Traversal(List *list) {
225   if(list->head == list->tail ) return;
226
227   listNode *node = NULL;
228   listIter *iter = ListGetIterator(list, AL_START_HEAD);
229   std::cout

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-89353-1-1.html 上篇帖子: Redis指令手册中文版 下篇帖子: rails 使用redis-rb
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表