当前位置: 首页 > news >正文

专做宠物的网站网站域名中请勿使用二级目录形式

专做宠物的网站,网站域名中请勿使用二级目录形式,网站开发需要的技术人员有什么软件,sem运营有出路吗Redis源码剖析—链表结构1. redis中的链表在redis中链表的应用非常广泛#xff0c;例如列表键的底层实现之一就是链表。而且#xff0c;在redis中的链表结构被实现成为双向链表#xff0c;因此#xff0c;在头部和尾部进行的操作就会非常快。通过列表键的命令感受一下双向链…Redis源码剖析—链表结构1. redis中的链表在redis中链表的应用非常广泛例如列表键的底层实现之一就是链表。而且在redis中的链表结构被实现成为双向链表因此在头部和尾部进行的操作就会非常快。通过列表键的命令感受一下双向链表127.0.0.1:6379 LPUSH list a b c //依次在链表头部插入a、b、c(integer) 3127.0.0.1:6379 RPUSH list d e f //依次在链表尾部插入d、e、f(integer) 6127.0.0.1:6379 LRANGE list 0 -1 //查看list的值1) c2) b3) a4) d5) e6) f2. 链表的实现2.1 链表节点的实现每个链表节点由adlist.h/listNode来表示typedef struct listNode { struct listNode *prev; //前驱节点如果是list的头结点则prev指向NULL struct listNode *next;//后继节点如果是list尾部结点则next指向NULL void *value; //万能指针能够存放任何信息} listNode;listNode结构通过prev和next指针就组成了双向链表。刚才通过列表键生成的双向链表如下图使用双向链表的好处prev和next指针获取某个节点的前驱节点和后继节点复杂度为O(1)。2.2 表头的实现redis还提供了一个表头用于存放上面双向链表的信息它由adlist.h/list结构表示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;利用list表头管理链表信息的好处head和tail指针对于链表的头结点和尾结点操作的复杂度为O(1)。len 链表长度计数器获取链表中节点数量的复杂度为O(1)。dup、free和match指针实现多态链表节点listNode使用万能指针void *保存节点的值而表头list使用dup、free和match指针来针对链表中存放的不同对象从而实现不同的方法3. 链表结构源码剖析3.1 adlist.h文件针对list结构和listNode结构的赋值和查询操作使用宏进行封装而且一下操作的复杂度均为O(1)。#define listLength(l) ((l)-len) //返回链表l节点数量#define listFirst(l) ((l)-head) //返回链表l的头结点地址#define listLast(l) ((l)-tail) //返回链表l的尾结点地址#define listPrevNode(n) ((n)-prev) //返回节点n的前驱节点地址#define listNextNode(n) ((n)-next) //返回节点n的后继节点地址#define listNodeValue(n) ((n)-value) //返回节点n的节点值#define listSetDupMethod(l,m) ((l)-dup (m)) //设置链表l的复制函数为m方法#define listSetFreeMethod(l,m) ((l)-free (m)) //设置链表l的释放函数为m方法#define listSetMatchMethod(l,m) ((l)-match (m)) //设置链表l的比较函数为m方法#define listGetDupMethod(l) ((l)-dup) //返回链表l的赋值函数#define listGetFree(l) ((l)-free) //返回链表l的释放函数#define listGetMatchMethod(l) ((l)-match) //返回链表l的比较函数链表操作的函数原型(Prototypes)list *listCreate(void); //创建一个表头void listRelease(list *list); //释放list表头和链表list *listAddNodeHead(list *list, void *value); //将value添加到list链表的头部list *listAddNodeTail(list *list, void *value); //将value添加到list链表的尾部list *listInsertNode(list *list, listNode *old_node, void *value, int after);//在list中根据after在old_node节点前后插入值为value的节点。void listDelNode(list *list, listNode *node); //从list删除node节点listIter *listGetIterator(list *list, int direction); //为list创建一个迭代器iteratorlistNode *listNext(listIter *iter); //返回迭代器iter指向的当前节点并更新iter void listReleaseIterator(listIter *iter); //释放iter迭代器list *listDup(list *orig); //拷贝表头为orig的链表并返回listNode *listSearchKey(list *list, void *key); //在list中查找value为key的节点并返回listNode *listIndex(list *list, long index); //返回下标为index的节点地址void listRewind(list *list, listIter *li); //将迭代器li重置为list的头结点并且设置为正向迭代void listRewindTail(list *list, listIter *li); //将迭代器li重置为list的尾结点并且设置为反向迭代void listRotate(list *list); //将尾节点插到头结点3.2 链表迭代器在adlist.h文件中使用C语言实现了迭代器源码如下typedef struct listIter { listNode *next; //迭代器当前指向的节点(名字叫next有点迷惑) int direction; //迭代方向可以取以下两个值AL_START_HEAD和AL_START_TAIL} listIter#define AL_START_HEAD 0 //正向迭代从表头向表尾进行迭代#define AL_START_TAIL 1 //反向迭代从表尾到表头进行迭代在listDup函数中就使用了迭代器listDup函数的定义如下//listDup的功能是拷贝一份链表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; //迭代整个orig的链表重点关注此部分。 iter listGetIterator(orig, AL_START_HEAD);//为orig定义一个迭代器并设置迭代方向在c中例如是 vector::interator it; while((node listNext(iter)) ! NULL) { //迭代器根据迭代方向不停迭代相当于it void *value; //复制节点值到新节点 if (copy-dup) { //如果定义了list结构中的dup指针则使用该方法拷贝节点值。 value copy-dup(node-value); if (value NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } else value node-value; //获得当前node的value值 if (listAddNodeTail(copy, value) NULL) { //将node节点尾插到copy表头的链表中 listRelease(copy); listReleaseIterator(iter); return NULL; } } listReleaseIterator(iter); //自行释放迭代器 return copy; //返回拷贝副本迭代器的好处提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。将指针操作进行了统一封装代码可读性增强。3.3 adlist.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; //返回表头}/* Free the whole list. * * This function cant fail. */void listRelease(list *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); //如果设置了list结构的释放函数则调用该函数释放节点值 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) //将value添加到list链表的头部{ listNode *node; if ((node zmalloc(sizeof(*node))) NULL) //为新节点分配空间 return NULL; node-value value; //设置node的value值 if (list-len 0) { //将node头插到空链表 list-head list-tail node; node-prev node-next NULL; } else { //将node头插到非空链表 node-prev NULL; node-next list-head; list-head-prev node; list-head node; } list-len; //链表元素计数器加1 return list;}/* 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) //将value添加到list链表的尾部{ listNode *node; if ((node zmalloc(sizeof(*node))) NULL) //为新节点分配空间 return NULL; node-value value; //设置node的value值 if (list-len 0) { //将node尾插到空链表 list-head list-tail node; node-prev node-next NULL; } else { //将node头插到非空链表 node-prev list-tail; node-next NULL; list-tail-next node; list-tail node; } list-len; //更新链表节点计数器 return list;}list *listInsertNode(list *list, listNode *old_node, void *value, int after) //在list中根据after在old_node节点前后插入值为value的节点。{ listNode *node; if ((node zmalloc(sizeof(*node))) NULL) //为新节点分配空间 return NULL; node-value value; //设置node的value值 if (after) { //after 非零则将节点插入到old_node的后面 node-prev old_node; node-next old_node-next; if (list-tail old_node) { //目标节点如果是链表的尾节点更新list的tail指针 list-tail node; } } else { //after 为零则将节点插入到old_node的前面 node-next old_node; node-prev old_node-prev; if (list-head old_node) { //如果节点如果是链表的头节点更新list的head指针 list-head node; } } if (node-prev ! NULL) { //如果有则更新node的前驱节点的指针 node-prev-next node; } if (node-next ! NULL) { //如果有则更新node的后继节点的指针 node-next-prev node; } list-len; //更新链表节点计数器 return list;}/* Remove the specified node from the specified list. * Its up to the caller to free the private value of the node. * * This function cant fail. */void listDelNode(list *list, listNode *node) //从list删除node节点{ if (node-prev) //更新node的前驱节点的指针 node-prev-next node-next; else list-head node-next; if (node-next) //更新node的后继节点的指针 node-next-prev node-prev; else list-tail node-prev; if (list-free) list-free(node-value); //如果设置了list结构的释放函数则调用该函数释放节点值 zfree(node); //释放节点 list-len--; //更新链表节点计数器}/* Returns a list iterator iter. After the initialization every * call to listNext() will return the next element of the list. * * This function cant fail. */listIter *listGetIterator(list *list, int direction) //为list创建一个迭代器iterator{ 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) { //释放iter迭代器 zfree(iter);}/* Create an iterator in the list private iterator structure */void listRewind(list *list, listIter *li) { //将迭代器li重置为list的头结点并且设置为正向迭代 li-next list-head; //设置迭代指针的起始位置 li-direction AL_START_HEAD; //设置迭代方向从头到尾}void listRewindTail(list *list, listIter *li) { //将迭代器li重置为list的尾结点并且设置为反向迭代 li-next list-tail; //设置迭代指针的起始位置 li-direction AL_START_TAIL; //设置迭代方向从尾到头}/* Return the next element of an iterator. * Its 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 patter * is: * * iter listGetIterator(list,); * while ((node listNext(iter)) ! NULL) { * doSomethingWith(listNodeValue(node)); * } * * */listNode *listNext(listIter *iter) //返回迭代器iter指向的当前节点并更新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; //返回备份的当前节点地址}/* 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) //拷贝表头为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; //迭代整个orig的链表 iter listGetIterator(orig, AL_START_HEAD); //为orig定义一个迭代器并设置迭代方向 while((node listNext(iter)) ! NULL) { //迭代器根据迭代方向不停迭代 void *value; //复制节点值到新节点 if (copy-dup) { value copy-dup(node-value); //如果定义了list结构中的dup指针则使用该方法拷贝节点值。 if (value NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } else value node-value; //获得当前node的value值 if (listAddNodeTail(copy, value) NULL) { //将node节点尾插到copy表头的链表中 listRelease(copy); listReleaseIterator(iter); return NULL; } } listReleaseIterator(iter); //自行释放迭代器 return copy; //返回拷贝副本}/* 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) //在list中查找value为key的节点并返回{ listIter *iter; listNode *node; iter listGetIterator(list, AL_START_HEAD); //创建迭代器 while((node listNext(iter)) ! NULL) { //迭代整个链表 if (list-match) { //如果设置list结构中的match方法则用该方法比较 if (list-match(node-value, key)) { listReleaseIterator(iter); //如果找到释放迭代器返回node地址 return node; } } else { if (key node-value) { listReleaseIterator(iter); return node; } } } listReleaseIterator(iter); //释放迭代器 return NULL;}/* Return the element at the specified zero-based index * where 0 is the head, 1 is the element next to head * and so on. Negative integers are used in order to count * from the tail, -1 is the last element, -2 the penultimate * and so on. If the index is out of range NULL is returned. */listNode *listIndex(list *list, long index) { //返回下标为index的节点地址 listNode *n; if (index 0) { index (-index)-1; //如果下标为负数从链表尾部开始 n list-tail; while(index-- n) n n-prev; } else { n list-head; //如果下标为正数从链表头部开始 while(index-- n) n n-next; } return n;}/* Rotate the list removing the tail node and inserting it to the head. */void listRotate(list *list) { //将尾节点插到头结点 listNode *tail list-tail; if (listLength(list) 1) return; //只有一个节点或空链表直接返回 /* Detach current tail */ list-tail tail-prev; //取出尾节点更新list的tail指针 list-tail-next NULL; /* Move it as head */ list-head-prev tail; //将节点插到表头更新list的head指针 tail-prev NULL; tail-next list-head; list-head tail;}
http://www.sadfv.cn/news/55302/

相关文章:

  • 杭州高端网站建设排名郑州大型网站建设电话
  • 教做香肠的网站设计制作一个网站
  • 网站建设中的英文外贸网站开发多少钱
  • 网站项目实施方案网站反链接
  • 在哪个网站上做简历win 7怎么卸载wordpress
  • vs网站毕业设计怎么做总部基地网站建设公司
  • 陕西企业电脑网站制作wordpress主题不能用
  • 成交功能网站网站备案表上面的开办单位写什么
  • 业务网站风格模板wordpress主题添加
  • 河北网站建设seo优化营销制作设计购买建立网站费怎么做会计凭证
  • 站长工具ip地址网页毕业设计说明书
  • 德阳网站网站建设网站公司架构
  • 网站开发人力成本烟台网站制作公司
  • 易联网站制作全是图片的网站怎么做seo
  • 做任务的网站源码横店影视城网站建设
  • 山西制作网站东莞公司网站怎么做
  • 主机怎么做网站服务器外汇做单记录做单专业网站有哪些
  • 网站制作费用明细男女做羞羞的视频网站
  • 跨境电商网站如何查看网站开发单位
  • 建设网站费用主要包括哪些广州seo公司排名
  • 婺源网站建制作网站建设教程纯正苏州久远网络
  • 为校园网站建设提供seo上海公司
  • 深圳网站平台建设网上销售平台
  • 网页设计和网站设计黑龙江省建设厅的网站
  • 网站备案怎样提交到管局网站建设超速云免费
  • 全国送花网站承接网站建设广告语
  • 怎么做自己的简历网站wordpress 推荐返利
  • wordpress代码创建子站点专业地推团队
  • 成都网站建设scjsc888营销型网站有那些网站
  • 打渔网站建设天津市建设厅注册中心网站