A trivial change makes the new implementation O(log(N)) instead of O(log(N))+O(M) when there are M repeated scores!

This commit is contained in:
antirez 2009-10-26 20:47:23 +01:00
parent 2b59cfdfe9
commit 9d60e6e466

15
redis.c
View File

@ -3855,7 +3855,10 @@ static void zslInsert(zskiplist *zsl, double score, robj *obj) {
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
while (x->forward[i] && x->forward[i]->score < score)
while (x->forward[i] &&
(x->forward[i]->score < score ||
(x->forward[i]->score == score &&
compareStringObjects(x->forward[i]->obj,obj) < 0)))
x = x->forward[i];
update[i] = x;
}
@ -3888,14 +3891,16 @@ static int zslDelete(zskiplist *zsl, double score, robj *obj) {
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
while (x->forward[i] && x->forward[i]->score < score)
while (x->forward[i] &&
(x->forward[i]->score < score ||
(x->forward[i]->score == score &&
compareStringObjects(x->forward[i]->obj,obj) < 0)))
x = x->forward[i];
update[i] = x;
}
/* We may have multiple elements with the same score, what we need
* is to find the element with both the right score and object. */
x = x->forward[0];
while(x->score == score) {
if (compareStringObjects(x->obj,obj) == 0) {
for (i = 0; i < zsl->level; i++) {
if (update[i]->forward[i] != x) break;
@ -3913,9 +3918,7 @@ static int zslDelete(zskiplist *zsl, double score, robj *obj) {
zsl->length--;
return 1;
} else {
x = x->forward[0];
if (!x) return 0; /* end of the list reached, not found */
}
return 0; /* not found */
}
return 0; /* not found */
}