mirror of
https://github.com/fluencelabs/redis
synced 2025-03-19 17:10:50 +00:00
move logic for obvious corner cases to intsetSearch
This commit is contained in:
parent
144b0094c3
commit
3ab98cef4e
25
intset.c
25
intset.c
@ -68,6 +68,16 @@ static uint8_t intsetSearch(intset *is, int64_t value, uint32_t *pos) {
|
|||||||
if (is->length == 0) {
|
if (is->length == 0) {
|
||||||
if (pos) *pos = 0;
|
if (pos) *pos = 0;
|
||||||
return 0;
|
return 0;
|
||||||
|
} else {
|
||||||
|
/* Check for the case where we know we cannot find the value,
|
||||||
|
* but do know the insert position. */
|
||||||
|
if (value > INTSET_GET(is,is->length-1)) {
|
||||||
|
if (pos) *pos = is->length;
|
||||||
|
return 0;
|
||||||
|
} else if (value < INTSET_GET(is,0)) {
|
||||||
|
if (pos) *pos = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while(max >= min) {
|
while(max >= min) {
|
||||||
@ -124,22 +134,13 @@ intset *intsetAdd(intset *is, int64_t value, uint8_t *success) {
|
|||||||
is = intsetUpgrade(is,valenc,1,offset);
|
is = intsetUpgrade(is,valenc,1,offset);
|
||||||
pos = (value < 0) ? 0 : is->length;
|
pos = (value < 0) ? 0 : is->length;
|
||||||
} else {
|
} else {
|
||||||
if (is->length == 0) {
|
/* Abort if the value is already present in the set.
|
||||||
pos = 0;
|
* This call will populate "pos" with the right position to insert
|
||||||
} else {
|
* the value when it cannot be found. */
|
||||||
/* Check for the case where we know the insert position */
|
|
||||||
if (value > INTSET_GET(is,is->length-1)) {
|
|
||||||
pos = is->length;
|
|
||||||
} else if (value < INTSET_GET(is,0)) {
|
|
||||||
pos = 0;
|
|
||||||
} else {
|
|
||||||
/* Abort if the value is already present in the set */
|
|
||||||
if (intsetSearch(is,value,&pos)) {
|
if (intsetSearch(is,value,&pos)) {
|
||||||
if (success) *success = 0;
|
if (success) *success = 0;
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is = intsetResize(is,is->length+1);
|
is = intsetResize(is,is->length+1);
|
||||||
if (pos < is->length) intsetMoveTail(is,pos,pos+1);
|
if (pos < is->length) intsetMoveTail(is,pos,pos+1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user