// File: /workspace/github_c_makefile_star2_O3/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 0x3ed36
movl %esi, %ebp
movq %rdi, %r13
cmpl $0x7, %eax
je 0x3ec9a
cmpl $0x5, %eax
jne 0x3ed8a
cmpl $0x7, %ebp
jne 0x3ed9f
movq %r13, (%rsp)
movq 0x8(%r13), %r15
movl $0x10, %edi
callq 0x299b5
movq %rax, %r14
leaq 0x8ad1a(%rip), %rdi     # 0xc98a0
xorl %esi, %esi
callq 0x2217b
movq %rax, (%r14)
callq 0x3d036
movq %rax, 0x8(%r14)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2c28b
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3edce
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0x2c558
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x3edea
leaq 0x28(%rsp), %rbp
leaq 0x18(%rsp), %rbx
leaq 0x10(%rsp), %r12
movq 0x10(%rsp), %rdi
callq 0x3ded9
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movq %rbp, %rcx
callq 0x2c5f6
testl %eax, %eax
je 0x3ed45
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3ec1e
movl 0xc(%rsp), %esi
callq 0x3068f
jmp 0x3ec28
movq 0x28(%rsp), %rdi
callq 0x3069e
movq %rax, %r13
movq 0x8(%r14), %rdi
movsd 0x20(%rsp), %xmm0
movq %rax, %rsi
callq 0x3d10a
movq (%r14), %rdi
leaq 0x8(%rax), %rdx
movq %r13, %rsi
callq 0x2249c
testl %eax, %eax
jne 0x3ed61
movq %r13, %rdi
callq 0x306f3
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3e09f
movq 0x18(%rsp), %r13
testq %r13, %r13
jne 0x3ebdc
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x29af7
movq %r14, 0x8(%rbx)
movl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
andl (%rbx), %eax
orl $0x70, %eax
movl %eax, (%rbx)
jmp 0x3ed36
callq 0x2be40
cmpl $0x5, %ebp
jne 0x3edb4
movq %rax, %rbx
movq 0x8(%r13), %r14
movq (%r14), %rdi
callq 0x229a9
movq 0x8(%r14), %rax
movq (%rax), %rdi
movq 0x18(%rdi), %r15
callq 0x29af7
movq 0x8(%r14), %rdi
callq 0x29af7
testq %r15, %r15
je 0x3ed1a
movq (%r15), %rdi
callq 0x30d93
movq %rax, %r12
movsd 0x8(%r15), %xmm0
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x3e795
movq %rax, %rbx
movq %r12, %rdi
callq 0x30b0c
movq (%r15), %rdi
movq 0x18(%r15), %r12
callq 0x30b0c
movq %r15, %rdi
callq 0x29af7
movq %r12, %r15
testq %r12, %r12
jne 0x3ecd5
movq %r14, %rdi
callq 0x29af7
movq %rbx, 0x8(%r13)
movl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
andl (%r13), %eax
orl $0x50, %eax
movl %eax, (%r13)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5a4ea(%rip), %rdx     # 0x99236
leaq 0x5a4ac(%rip), %rcx     # 0x991ff
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x667, %r8d            # imm = 0x667
jmp 0x3ed7b
leaq 0x5a5b3(%rip), %rdx     # 0x9931b
leaq 0x5a490(%rip), %rcx     # 0x991ff
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x670, %r8d            # imm = 0x670
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0
leaq 0x58622(%rip), %rdi     # 0x973b3
leaq 0x5a467(%rip), %rsi     # 0x991ff
movl $0x6ad, %edx            # imm = 0x6AD
jmp 0x3edc7
leaq 0x5a55b(%rip), %rdi     # 0x99301
leaq 0x5a452(%rip), %rsi     # 0x991ff
movl $0x64c, %edx            # imm = 0x64C
jmp 0x3edc7
leaq 0x5a546(%rip), %rdi     # 0x99301
leaq 0x5a43d(%rip), %rsi     # 0x991ff
movl $0x685, %edx            # imm = 0x685
callq 0x4cae0
jmp 0x3ed80
leaq 0x5a4b3(%rip), %rdx     # 0x99288
leaq 0x5a423(%rip), %rcx     # 0x991ff
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x65b, %r8d            # imm = 0x65B
jmp 0x3ed7b
leaq 0x5a754(%rip), %rdx     # 0x99545
leaq 0x5a407(%rip), %rcx     # 0x991ff
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x65e, %r8d            # imm = 0x65E
jmp 0x3ed7b