// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zsetConvert
void zsetConvert(robj *zobj, int encoding) {
    zset *zs;
    zskiplistNode *node, *next;
    robj *ele;
    double score;

    if (zobj->encoding == encoding) return;

    // 从 ZIPLIST 编码转换为 SKIPLIST 编码
    if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *zl = zobj->ptr;
        unsigned char *eptr, *sptr;
        unsigned char *vstr;
        unsigned int vlen;
        long long vlong;

        if (encoding != REDIS_ENCODING_SKIPLIST)
            redisPanic("Unknown target encoding");

        // 创建有序集合结构
        zs = zmalloc(sizeof(*zs));
        // 字典
        zs->dict = dictCreate(&zsetDictType,NULL);
        // 跳跃表
        zs->zsl = zslCreate();

        // 有序集合在 ziplist 中的排列：
        //
        // | member-1 | score-1 | member-2 | score-2 | ... |
        //
        // 指向 ziplist 中的首个节点（保存着元素成员）
        eptr = ziplistIndex(zl,0);
        redisAssertWithInfo(NULL,zobj,eptr != NULL);
        // 指向 ziplist 中的第二个节点（保存着元素分值）
        sptr = ziplistNext(zl,eptr);
        redisAssertWithInfo(NULL,zobj,sptr != NULL);

        // 遍历所有 ziplist 节点，并将元素的成员和分值添加到有序集合中
        while (eptr != NULL) {
            
            // 取出分值
            score = zzlGetScore(sptr);

            // 取出成员
            redisAssertWithInfo(NULL,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
            if (vstr == NULL)
                ele = createStringObjectFromLongLong(vlong);
            else
                ele = createStringObject((char*)vstr,vlen);

            /* Has incremented refcount since it was just created. */
            // 将成员和分值分别关联到跳跃表和字典中
            node = zslInsert(zs->zsl,score,ele);
            redisAssertWithInfo(NULL,zobj,dictAdd(zs->dict,ele,&node->score) == DICT_OK);
            incrRefCount(ele); /* Added to dictionary. */

            // 移动指针，指向下个元素
            zzlNext(zl,&eptr,&sptr);
        }

        // 释放原来的 ziplist
        zfree(zobj->ptr);

        // 更新对象的值，以及编码方式
        zobj->ptr = zs;
        zobj->encoding = REDIS_ENCODING_SKIPLIST;

    // 从 SKIPLIST 转换为 ZIPLIST 编码
    } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {

        // 新的 ziplist
        unsigned char *zl = ziplistNew();

        if (encoding != REDIS_ENCODING_ZIPLIST)
            redisPanic("Unknown target encoding");

        /* Approach similar to zslFree(), since we want to free the skiplist at
         * the same time as creating the ziplist. */
        // 指向跳跃表
        zs = zobj->ptr;

        // 先释放字典，因为只需要跳跃表就可以遍历整个有序集合了
        dictRelease(zs->dict);

        // 指向跳跃表首个节点
        node = zs->zsl->header->level[0].forward;

        // 释放跳跃表表头
        zfree(zs->zsl->header);
        zfree(zs->zsl);

        // 遍历跳跃表，取出里面的元素，并将它们添加到 ziplist
        while (node) {

            // 取出解码后的值对象
            ele = getDecodedObject(node->obj);

            // 添加元素到 ziplist
            zl = zzlInsertAt(zl,NULL,ele,node->score);
            decrRefCount(ele);

            // 沿着跳跃表的第 0 层前进
            next = node->level[0].forward;
            zslFreeNode(node);
            node = next;
        }

        // 释放跳跃表
        zfree(zs);

        // 更新对象的值，以及对象的编码方式
        zobj->ptr = zl;
        zobj->encoding = REDIS_ENCODING_ZIPLIST;
    } else {
        redisPanic("Unknown sorted set encoding");
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl (%rdi), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl %esi, %eax
je 0x3be65
movl %esi, %ebp
movq %rdi, %r13
cmpl $0x7, %eax
je 0x3bdb5
cmpl $0x5, %eax
jne 0x3beb7
cmpl $0x7, %ebp
jne 0x3becc
movq %r13, (%rsp)
movq 0x8(%r13), %r15
pushq $0x10
popq %rdi
callq 0x2828d
movq %rax, %r14
leaq 0x8abe2(%rip), %rdi     # 0xc68a0
xorl %esi, %esi
callq 0x216f0
movq %rax, (%r14)
callq 0x3a4ba
movq %rax, 0x8(%r14)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2abfb
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3befb
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0x2ae0d
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x3bf17
leaq 0x28(%rsp), %rbp
leaq 0x18(%rsp), %rbx
leaq 0x10(%rsp), %r12
testq %r13, %r13
je 0x3be48
movq 0x10(%rsp), %rdi
callq 0x3b09e
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movq %rbp, %rcx
callq 0x2aea9
testl %eax, %eax
je 0x3be74
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3bd5f
movl 0xc(%rsp), %esi
callq 0x2ea81
jmp 0x3bd69
movq 0x28(%rsp), %rdi
callq 0x2ea90
movq %rax, %r13
movq 0x8(%r14), %rdi
movsd 0x20(%rsp), %xmm0
movq %rax, %rsi
callq 0x3a577
movq (%r14), %rdi
leaq 0x8(%rax), %rdx
movq %r13, %rsi
callq 0x21956
testl %eax, %eax
jne 0x3be90
movq %r13, %rdi
callq 0x2eacc
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3b25c
movq 0x18(%rsp), %r13
jmp 0x3bd14
callq 0x2a868
cmpl $0x5, %ebp
jne 0x3bee1
movq %rax, %rbx
movq 0x8(%r13), %r14
movq (%r14), %rdi
callq 0x21e31
movq 0x8(%r14), %rax
movq (%rax), %rdi
movq 0x18(%rdi), %r15
callq 0x283cd
movq 0x8(%r14), %rdi
callq 0x283cd
testq %r15, %r15
je 0x3be2a
movq (%r15), %rdi
callq 0x2f087
movq %rax, %r12
movsd 0x8(%r15), %xmm0
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x3b8fc
movq %rax, %rbx
movq %r12, %rdi
callq 0x2ee1f
movq 0x18(%r15), %r12
movq %r15, %rdi
callq 0x3a510
movq %r12, %r15
jmp 0x3bdeb
movq %r14, %rdi
callq 0x283cd
movq %rbx, 0x8(%r13)
movl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
andl (%r13), %eax
orl $0x50, %eax
movl %eax, (%r13)
jmp 0x3be65
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x283cd
movq %r14, 0x8(%rbx)
movl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
andl (%rbx), %eax
orl $0x70, %eax
movl %eax, (%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5337f(%rip), %rdx     # 0x8f1fa
leaq 0x53341(%rip), %rcx     # 0x8f1c3
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x667, %r8d            # imm = 0x667
jmp 0x3beaa
leaq 0x53448(%rip), %rdx     # 0x8f2df
leaq 0x53325(%rip), %rcx     # 0x8f1c3
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x670, %r8d            # imm = 0x670
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0
leaq 0x514e9(%rip), %rdi     # 0x8d3a7
leaq 0x532fe(%rip), %rsi     # 0x8f1c3
movl $0x6ad, %edx            # imm = 0x6AD
jmp 0x3bef4
leaq 0x533f2(%rip), %rdi     # 0x8f2c5
leaq 0x532e9(%rip), %rsi     # 0x8f1c3
movl $0x64c, %edx            # imm = 0x64C
jmp 0x3bef4
leaq 0x533dd(%rip), %rdi     # 0x8f2c5
leaq 0x532d4(%rip), %rsi     # 0x8f1c3
movl $0x685, %edx            # imm = 0x685
callq 0x490e8
jmp 0x3beaf
leaq 0x5334a(%rip), %rdx     # 0x8f24c
leaq 0x532ba(%rip), %rcx     # 0x8f1c3
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x65b, %r8d            # imm = 0x65B
jmp 0x3beaa
leaq 0x535eb(%rip), %rdx     # 0x8f509
leaq 0x5329e(%rip), %rcx     # 0x8f1c3
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x65e, %r8d            # imm = 0x65E
jmp 0x3beaa