MCPcopy Create free account
hub / github.com/F-Stack/f-stack / zslGetRank

Function zslGetRank

app/redis-6.2.6/src/t_zset.c:475–496  ·  view source on GitHub ↗

Find the rank for an element by both score and key. * Returns 0 when the element cannot be found, rank otherwise. * Note that the rank is 1-based due to the span of zsl->header to the * first element. */

Source from the content-addressed store, hash-verified

473 * Note that the rank is 1-based due to the span of zsl->header to the
474 * first element. */
475unsigned long zslGetRank(zskiplist *zsl, double score, sds ele) {
476 zskiplistNode *x;
477 unsigned long rank = 0;
478 int i;
479
480 x = zsl->header;
481 for (i = zsl->level-1; i >= 0; i--) {
482 while (x->level[i].forward &&
483 (x->level[i].forward->score < score ||
484 (x->level[i].forward->score == score &&
485 sdscmp(x->level[i].forward->ele,ele) <= 0))) {
486 rank += x->level[i].span;
487 x = x->level[i].forward;
488 }
489
490 /* x might be equal to zsl->header, so test if obj is non-NULL */
491 if (x->ele && sdscmp(x->ele,ele) == 0) {
492 return rank;
493 }
494 }
495 return 0;
496}
497
498/* Finds an element by its rank. The rank argument needs to be 1-based. */
499zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) {

Callers 3

zsetRankFunction · 0.85
zcountCommandFunction · 0.85
zlexcountCommandFunction · 0.85

Calls 1

sdscmpFunction · 0.85

Tested by

no test coverage detected