redis6.0.5之基树rax理解测试

今天对这个基树单步跟踪理解了下,下面是用到的文件列表
*******************************************************
D:\mysourcecode\mytestcode\rax>tree /F
卷 新加卷 的文件夹 PATH 列表
卷序列号为 BA81-13D2
D:.
    atomicvar.h
    rax.c
    rax.h
    rax_malloc.h
    TestRax
    TestRax.c
    zmalloc.c
    zmalloc.h
没有子文件夹
*******************************************************
就TestRax.c是自己写的测试主函数
为了更好理解基树的结构,写了个深度优先打印所有节点元素,时候发现源代码中已经有这个功能,
不过自己写下对理解确实有很大帮助
int DFSRecursion(raxNode *hh,int level)
{
    raxNode **headfirst = raxNodeFirstChildPtr(hh);  //获取首地址
    if(hh->iscompr == 1){  // 只有一个子指针
            //printf("%d",*headfirst);
            printf("level%d c ",level);
            for(int j=0;j<hh->size; j++)
                printf("%c",hh->data[j]);
            printf("\n");
            DFSRecursion(*headfirst,level+1);  //继续递归搜索子节点
    }else{
            printf("size %d  :  ",hh->size);
            if(!hh->size) printf("leaf node\n");
            for(int i=0; i < hh->size; i++) //遍历每个字节点
            {
                printf("level%d: nc %c\n",level, hh->data[i]);
                DFSRecursion(*headfirst,level+1);  //对子节点做递归搜索
                (headfirst)++; 指向下一个子节点指针
            }
    }
}
******************************************************************
。。。。
    rax *clients_index;
    clients_index= raxNew();
    unsigned char *s = "abcd";
    printf( "strlen(s) = %d \n", strlen(s) );
    int retInt = raxInsert(clients_index, s, strlen(s),NULL,NULL);
    printf( "retInt= %d \n", retInt );
    unsigned char *s1 = "abg";
    raxInsert(clients_index, s1, strlen(s1),NULL,NULL);
    unsigned char *s2 = "ab";
    raxInsert(clients_index, s2, strlen(s2),NULL,NULL);
    unsigned char *s21 = "ak1";
    raxInsert(clients_index, s21, strlen(s21),NULL,NULL);
    unsigned char *s3 = "ABCdef";
    raxInsert(clients_index, s3, strlen(s3),NULL,NULL);
    unsigned char *s31 = "ABf";
    raxInsert(clients_index, s31, strlen(s31),NULL,NULL);
    unsigned char *s32 = "ABcd";
    raxInsert(clients_index, s32, strlen(s32),NULL,NULL);
    unsigned char *s4 = "abcdef";
    raxInsert(clients_index, s4, strlen(s4),NULL,NULL);
    printf("node=%d,elem=%d\n",clients_index->numnodes, clients_index->numele);
    raxNode *hh = clients_index->head;
    
    raxRecursiveShow(0,0,hh);  源码漂亮的格式
    printf("\n");
    printf("*****************\n");
            DFSRecursion(clients_index->head,1); 自己简单的展示
    printf("*****************\n");
。。。
*****************************************************************
为了gdb,编译如下,加了-g
gcc -g  -lm zmalloc.c  rax.c  TestRax.c   -o  TestRax

 

posted on 2021-01-13 19:43  子虚乌有  阅读(284)  评论(0)    收藏  举报