redis 之 adlist
redis 之 adlist
老汉-憨憨 发表于5个月前
redis 之 adlist
  • 发表于 5个月前
  • 阅读 7
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 图片来源: http://redissrc.readthedocs.io/en/latest/_images/graphviz-caa48c97a30851a5d976fd0b496a0da80caf4b05.png

digraph adlist {

    rankdir=LR;

    node [shape=record, style = filled, fillcolor = "#95BBE3"];

    edge [style = bold];

    list_node_1 [label = "<head>listNode |{<prev> prev| value|<next> next}", ];
    list_node_2 [label = "<head>listNode |{<prev> prev| value|<next> next}"];
    list_node_3 [label = "<head>listNode |{<prev> prev| value|<next> next}"];

    list_node_1:next -> list_node_2:head;
    list_node_2:next -> list_node_3:head;

    list_node_2:prev -> list_node_1:head;
    list_node_3:prev -> list_node_2:head;

    node [width=1.5, style = filled, fillcolor = "#A8E270"];
    list [label = "list |<head> head|<tail> tail|<dup> dup|<free> free|<match> match|<len> len"];

    list:tail -> list_node_3:head;
    list:head -> list_node_1:head;
}

这里主要是测试和学习使用了 adlist 的相关函数, 具体原理后面补充:

1.  拷贝 :

adlist.c  和 adlist.h  到一个单独的文件夹, 自己创建一个 main.c 的文件

# 我的目录文件结构
adlist  adlist.c  adlist.h  main.c

#adlist 是编译后生成的可执行文件
#adlist.c 是 redis 源码里面的 adlist.c 做了少许修改
#adlist.h 是 redis 源码里面的 adlist.h 没有做任何修改
#main.c 为测试代码程序

adlist.c 修改部分如下:

#include <stdlib.h>
#include "adlist.h"
//#include "zmalloc.h"  // 注释调该头文件

// 定义了下面的两个宏
#define zmalloc malloc
#define zfree free

这里不贴 adlist.c 和 adlist.h 文件的内容了, 具体可以从 redis 里面的源码获得。

2. 具体测试源码如下:

#include <stdio.h>
#include <stdlib.h>
#include "adlist.h"

char str[] = "ABCDEFGHIJKLMOPQRSTUVWXYZ";

int main(int argc, char *argv[])
{
    int alphLen = sizeof(str)/sizeof(char);
    list *lst = listCreate();
    if (lst == NULL) {
        printf("listCreate() error!\n");
        return -1;
    }
    
    int i = 0;
    for (i = 0; i < alphLen; i++) {
       lst = listAddNodeHead(lst, &str[i]);
    }

    listIter *item = listGetIterator(lst, AL_START_HEAD);
    listNode *node = NULL;
    while ((node = listNext(item)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");

    
    listRewindTail(lst, item);
    while ((node = listNext(item)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    
    listReleaseIterator(item);
    item = NULL;

    printf("------------------------------------------------\n");
    list *lstdup = listDup(lst);

    listIter *itemdup = listGetIterator(lstdup, AL_START_TAIL);
    listNode *nodedup = NULL;
    while ((nodedup = listNext(itemdup)) != NULL) {
        char *p = listNodeValue(nodedup);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    
    listRewind(lstdup, itemdup);
    while ((nodedup = listNext(itemdup)) != NULL) {
        char *p = listNodeValue(nodedup);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    listReleaseIterator(itemdup); 
    itemdup = NULL;

    printf("------------------------------------------------\n");
    for (i = 0; i < alphLen; i++)  {
        listNode *indexN = listIndex(lst, (long)(i));
        char *p = listNodeValue(indexN);
        printf("%d -> %c\n", i, (char)(*p));
    }

    printf("------------------------------------------------\n");
    listRotate(lst);
    listNode *index0 = listIndex(lst, (long)(0));
    char *p = listNodeValue(index0);
    printf("%d -> %c\n", 0, (char)(*p));

    printf("------------------------------------------------\n");
    listDelNode(lst, index0); 
    listIter it;
    listRewindTail(lst, &it);
    while ((node = listNext(&it)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    printf("------------------------------------------------\n");
    
    listNode *index3 = listIndex(lst, (long)(3));
    lst = listInsertNode(lst, index3, &str[0], 1);
    listRewind(lst, &it);
    while ((node = listNext(&it)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    printf("------------------------------------------------\n");
    
    
    listRelease(lst);
    listRelease(lstdup);
    return 0;
}


3. 编译:

gcc main.c adlist.c -o adlist

4. 输出:

./adlist 
 Z Y X W V U T S R Q P O M L K J I H G F E D C B A 
A B C D E F G H I J K L M O P Q R S T U V W X Y Z  
------------------------------------------------
A B C D E F G H I J K L M O P Q R S T U V W X Y Z  
 Z Y X W V U T S R Q P O M L K J I H G F E D C B A 
------------------------------------------------
0 -> 
1 -> Z
2 -> Y
3 -> X
4 -> W
5 -> V
6 -> U
7 -> T
8 -> S
9 -> R
10 -> Q
11 -> P
12 -> O
13 -> M
14 -> L
15 -> K
16 -> J
17 -> I
18 -> H
19 -> G
20 -> F
21 -> E
22 -> D
23 -> C
24 -> B
25 -> A
------------------------------------------------
0 -> A
------------------------------------------------
B C D E F G H I J K L M O P Q R S T U V W X Y Z  
------------------------------------------------
 Z Y X A W V U T S R Q P O M L K J I H G F E D C B 
------------------------------------------------

 

标签: redis adlist
共有 人打赏支持
粉丝 18
博文 263
码字总数 60618
×
老汉-憨憨
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: