// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/t_set.c
// Function: srandmemberWithCountCommand
void srandmemberWithCountCommand(redisClient *c) {
    long l;
    unsigned long count, size;

    // 默认在集合中不包含重复元素
    int uniq = 1;

    robj *set, *ele;
    int64_t llele;
    int encoding;

    dict *d;

    // 取出 l 参数
    if (getLongFromObjectOrReply(c,c->argv[2],&l,NULL) != REDIS_OK) return;
    if (l >= 0) {
        // l 为正数，表示返回元素各不相同
        count = (unsigned) l;
    } else {
        /* A negative count means: return the same elements multiple times
         * (i.e. don't remove the extracted element after every extraction). */
        // 如果 l 为负数，那么表示返回的结果中可以有重复元素
        count = -l;
        uniq = 0;
    }

    // 取出集合对象
    if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk))
        == NULL || checkType(c,set,REDIS_SET)) return;
    // 取出集合基数
    size = setTypeSize(set);

    /* If count is zero, serve it ASAP to avoid special cases later. */
    // count 为 0 ，直接返回
    if (count == 0) {
        addReply(c,shared.emptymultibulk);
        return;
    }

    /* CASE 1: The count was negative, so the extraction method is just:
     * "return N random elements" sampling the whole set every time.
     * This case is trivial and can be served without auxiliary data
     * structures. 
     *
     * 情形 1：count 为负数，结果集可以带有重复元素
     * 直接从集合中取出并返回 N 个随机元素就可以了
     *
     * 这种情形不需要额外的结构来保存结果集
     */
    if (!uniq) {
        addReplyMultiBulkLen(c,count);

        while(count--) {
            // 取出随机元素
            encoding = setTypeRandomElement(set,&ele,&llele);
            if (encoding == REDIS_ENCODING_INTSET) {
                addReplyBulkLongLong(c,llele);
            } else {
                addReplyBulk(c,ele);
            }
        }

        return;
    }

    /* CASE 2:
     * The number of requested elements is greater than the number of
     * elements inside the set: simply return the whole set. 
     *
     * 如果 count 比集合的基数要大，那么直接返回整个集合
     */
    if (count >= size) {
        sunionDiffGenericCommand(c,c->argv+1,1,NULL,REDIS_OP_UNION);
        return;
    }

    /* For CASE 3 and CASE 4 we need an auxiliary dictionary. */
    // 对于情形 3 和情形 4 ，需要一个额外的字典
    d = dictCreate(&setDictType,NULL);

    /* CASE 3:
     * 
     * 情形 3：
     *
     * The number of elements inside the set is not greater than
     * SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.
     *
     * count 参数乘以 SRANDMEMBER_SUB_STRATEGY_MUL 的积比集合的基数要大。
     *
     * In this case we create a set from scratch with all the elements, and
     * subtract random elements to reach the requested number of elements.
     *
     * 在这种情况下，程序创建一个集合的副本，
     * 并从集合中删除元素，直到集合的基数等于 count 参数指定的数量为止。
     *
     * This is done because if the number of requsted elements is just
     * a bit less than the number of elements in the set, the natural approach
     * used into CASE 3 is highly inefficient. 
     *
     * 使用这种做法的原因是，当 count 的数量接近于集合的基数时，
     * 从集合中随机取出 count 个参数的方法是非常低效的。
     */
    if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) {
        setTypeIterator *si;

        /* Add all the elements into the temporary dictionary. */
        // 遍历集合，将所有元素添加到临时字典中
        si = setTypeInitIterator(set);
        while((encoding = setTypeNext(si,&ele,&llele)) != -1) {
            int retval = DICT_ERR;

            // 为元素创建对象，并添加到字典中
            if (encoding == REDIS_ENCODING_INTSET) {
                retval = dictAdd(d,createStringObjectFromLongLong(llele),NULL);
            } else {
                retval = dictAdd(d,dupStringObject(ele),NULL);
            }
            redisAssert(retval == DICT_OK);
            /* 上面的代码可以重构为
            
            robj *elem_obj;
            if (encoding == REDIS_ENCODING_INTSET) {
                elem_obj = createStringObjectFromLongLong(...)
            } else if () {
                ...
            } else if () {
                ...
            }

            redisAssert(dictAdd(d, elem_obj) == DICT_OK)

            */
        }
        setTypeReleaseIterator(si);
        redisAssert(dictSize(d) == size);

        /* Remove random elements to reach the right count. */
        // 随机删除元素，直到集合基数等于 count 参数的值
        while(size > count) {
            dictEntry *de;

            // 取出并删除随机元素
            de = dictGetRandomKey(d);
            dictDelete(d,dictGetKey(de));

            size--;
        }
    }
    
    /* CASE 4: We have a big set compared to the requested number of elements.
     *
     * 情形 4 ： count 参数要比集合基数小很多。
     *
     * In this case we can simply get random elements from the set and add
     * to the temporary set, trying to eventually get enough unique elements
     * to reach the specified count. 
     *
     * 在这种情况下，我们可以直接从集合中随机地取出元素，
     * 并将它添加到结果集合中，直到结果集的基数等于 count 为止。
     */
    else {
        unsigned long added = 0;

        while(added < count) {

            // 随机地从目标集合中取出元素
            encoding = setTypeRandomElement(set,&ele,&llele);

            // 将元素转换为对象
            if (encoding == REDIS_ENCODING_INTSET) {
                ele = createStringObjectFromLongLong(llele);
            } else {
                ele = dupStringObject(ele);
            }

            /* Try to add the object to the dictionary. If it already exists
             * free it, otherwise increment the number of objects we have
             * in the result dictionary. */
            // 尝试将元素添加到字典中
            // dictAdd 只有在元素不存在于字典时，才会返回 1
            // 如果如果结果集已经有同样的元素，那么程序会执行 else 部分
            // 只有元素不存在于结果集时，添加才会成功
            if (dictAdd(d,ele,NULL) == DICT_OK)
                added++;
            else
                decrRefCount(ele);
        }
    }

    /* CASE 3 & 4: send the result to the user. */
    {
        // 情形 3 和 4 ：将结果集回复给客户端
        dictIterator *di;
        dictEntry *de;

        addReplyMultiBulkLen(c,count);
        // 遍历结果集元素
        di = dictGetIterator(d);
        while((de = dictNext(di)) != NULL)
            // 回复
            addReplyBulk(c,dictGetKey(de));
        dictReleaseIterator(di);
        dictRelease(d);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x10(%rax), %rsi
leaq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b960
cmpl $0x0, %eax
je 0x4be0d
jmp 0x4c15e
cmpq $0x0, -0x10(%rbp)
jl 0x4be20
movq -0x10(%rbp), %rax
movl %eax, %eax
movq %rax, -0x18(%rbp)
jmp 0x4be31
xorl %eax, %eax
subq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax), %rsi
leaq 0xc8308(%rip), %rax     # 0x114150
movq 0x68(%rax), %rdx
callq 0x3c0c0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x4be72
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x2, %edx
callq 0x3abb0
cmpl $0x0, %eax
je 0x4be77
jmp 0x4c15e
movq -0x30(%rbp), %rdi
callq 0x4b4b0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x4bea4
movq -0x8(%rbp), %rdi
leaq 0xc82ba(%rip), %rax     # 0x114150
movq 0x68(%rax), %rsi
callq 0x35a70
jmp 0x4c15e
cmpl $0x0, -0x24(%rbp)
jne 0x4bf09
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x364f0
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x18(%rbp)
cmpq $0x0, %rax
je 0x4bf04
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x4b400
movl %eax, -0x44(%rbp)
cmpl $0x6, -0x44(%rbp)
jne 0x4bef5
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x366c0
jmp 0x4bf02
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x36620
jmp 0x4beb7
jmp 0x4c15e
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x4bf39
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rsi
addq $0x8, %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x4c170
jmp 0x4c15e
leaq 0xc6940(%rip), %rdi     # 0x112880
xorl %eax, %eax
movl %eax, %esi
callq 0x231a0
movq %rax, -0x50(%rbp)
imulq $0x3, -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0x4c094
movq -0x30(%rbp), %rdi
callq 0x4b1d0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x4b2c0
movl %eax, -0x44(%rbp)
cmpl $-0x1, %eax
je 0x4c018
movl $0x1, -0x5c(%rbp)
cmpl $0x6, -0x44(%rbp)
jne 0x4bfbf
movq -0x50(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3a330
movq -0x88(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x236d0
movl %eax, -0x5c(%rbp)
jmp 0x4bfe9
movq -0x50(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rdi
callq 0x3a4f0
movq -0x90(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x236d0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x4bff1
jmp 0x4c013
leaq 0x9692a(%rip), %rdi     # 0xe2922
leaq 0x971b6(%rip), %rsi     # 0xe31b5
movl $0x314, %edx            # imm = 0x314
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
jmp 0x4bf69
movq -0x58(%rbp), %rdi
callq 0x4b280
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
addq 0x48(%rcx), %rax
cmpq -0x20(%rbp), %rax
jne 0x4c039
jmp 0x4c05b
leaq 0x97220(%rip), %rdi     # 0xe3260
leaq 0x9716e(%rip), %rsi     # 0xe31b5
movl $0x325, %edx            # imm = 0x325
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
jmp 0x4c05d
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x4c092
movq -0x50(%rbp), %rdi
callq 0x24470
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rdi
movq -0x68(%rbp), %rax
movq (%rax), %rsi
callq 0x23c30
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x4c05d
jmp 0x4c10d
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x4c10b
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x4b400
movl %eax, -0x44(%rbp)
cmpl $0x6, -0x44(%rbp)
jne 0x4c0cf
movq -0x40(%rbp), %rdi
callq 0x3a330
movq %rax, -0x38(%rbp)
jmp 0x4c0dc
movq -0x38(%rbp), %rdi
callq 0x3a4f0
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x236d0
cmpl $0x0, %eax
jne 0x4c100
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x4c109
movq -0x38(%rbp), %rdi
callq 0x3aaa0
jmp 0x4c09c
jmp 0x4c10d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x364f0
movq -0x50(%rbp), %rdi
callq 0x241d0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x24270
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x4c14c
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rax
movq (%rax), %rsi
callq 0x36620
jmp 0x4c127
movq -0x78(%rbp), %rdi
callq 0x243d0
movq -0x50(%rbp), %rdi
callq 0x24000
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)