use 1-based rank across zsl*Rank functions consistently

This commit is contained in:
Pieter Noordhuis 2010-03-04 17:48:31 +01:00
parent 9212eafd5d
commit 2424490f9b

10
redis.c
View File

@ -5055,16 +5055,12 @@ static unsigned long zslDeleteRange(zskiplist *zsl, double min, double max, dict
} }
/* Delete all the elements with rank between start and end from the skiplist. /* Delete all the elements with rank between start and end from the skiplist.
* Start and end are inclusive. */ * Start and end are inclusive. Note that start and end need to be 1-based */
static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) { static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x; zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
unsigned long traversed = 0, removed = 0; unsigned long traversed = 0, removed = 0;
int i; int i;
/* start and end are given 0-based, but zsl uses 1-based
* ranks internally */
start++; end++;
x = zsl->header; x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) { for (i = zsl->level-1; i >= 0; i--) {
while (x->forward[i] && (traversed + (i > 0 ? x->span[i-1] : 1)) < start) { while (x->forward[i] && (traversed + (i > 0 ? x->span[i-1] : 1)) < start) {
@ -5360,7 +5356,9 @@ static void zremrangebyrankCommand(redisClient *c) {
} }
if (end >= llen) end = llen-1; if (end >= llen) end = llen-1;
deleted = zslDeleteRangeByRank(zs->zsl,start,end,zs->dict); /* increment start and end because zsl*Rank functions
* use 1-based rank */
deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
if (htNeedsResize(zs->dict)) dictResize(zs->dict); if (htNeedsResize(zs->dict)) dictResize(zs->dict);
server.dirty += deleted; server.dirty += deleted;
addReplyLong(c, deleted); addReplyLong(c, deleted);