+/* Delete (element,score) pair from ziplist. Use local copy of eptr because we
+ * don't want to modify the one given as argument. */
+int zzlDelete(robj *zobj, unsigned char *eptr) {
+ unsigned char *zl = zobj->ptr;
+ unsigned char *p = eptr;
+
+ /* TODO: add function to ziplist API to delete N elements from offset. */
+ zl = ziplistDelete(zl,&p);
+ zl = ziplistDelete(zl,&p);
+ zobj->ptr = zl;
+ return REDIS_OK;
+}
+
+int zzlInsertAt(robj *zobj, robj *ele, double score, unsigned char *eptr) {
+ unsigned char *zl = zobj->ptr;
+ unsigned char *sptr;
+ char scorebuf[128];
+ int scorelen;
+ int offset;
+
+ redisAssert(ele->encoding == REDIS_ENCODING_RAW);
+ scorelen = d2string(scorebuf,sizeof(scorebuf),score);
+ if (eptr == NULL) {
+ zl = ziplistPush(zl,ele->ptr,sdslen(ele->ptr),ZIPLIST_TAIL);
+ zl = ziplistPush(zl,(unsigned char*)scorebuf,scorelen,ZIPLIST_TAIL);
+ } else {
+ /* Keep offset relative to zl, as it might be re-allocated. */
+ offset = eptr-zl;
+ zl = ziplistInsert(zl,eptr,ele->ptr,sdslen(ele->ptr));
+ eptr = zl+offset;
+
+ /* Insert score after the element. */
+ redisAssert((sptr = ziplistNext(zl,eptr)) != NULL);
+ zl = ziplistInsert(zl,sptr,(unsigned char*)scorebuf,scorelen);
+ }
+
+ zobj->ptr = zl;
+ return REDIS_OK;
+}
+
+/* Insert (element,score) pair in ziplist. This function assumes the element is
+ * not yet present in the list. */
+int zzlInsert(robj *zobj, robj *ele, double score) {
+ unsigned char *zl = zobj->ptr;
+ unsigned char *eptr = ziplistIndex(zl,0), *sptr;
+ double s;
+
+ ele = getDecodedObject(ele);
+ while (eptr != NULL) {
+ sptr = ziplistNext(zl,eptr);
+ redisAssert(sptr != NULL);
+ s = zzlGetScore(sptr);
+
+ if (s > score) {
+ /* First element with score larger than score for element to be
+ * inserted. This means we should take its spot in the list to
+ * maintain ordering. */
+ zzlInsertAt(zobj,ele,score,eptr);
+ break;
+ } else if (s == score) {
+ /* Ensure lexicographical ordering for elements. */
+ if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) > 0) {
+ zzlInsertAt(zobj,ele,score,eptr);
+ break;
+ }
+ }
+
+ /* Move to next element. */
+ eptr = ziplistNext(zl,sptr);
+ }
+
+ /* Push on tail of list when it was not yet inserted. */
+ if (eptr == NULL)
+ zzlInsertAt(zobj,ele,score,NULL);
+
+ decrRefCount(ele);
+ return REDIS_OK;
+}
+
+unsigned long zzlDeleteRangeByScore(robj *zobj, zrangespec range) {
+ unsigned char *zl = zobj->ptr;
+ unsigned char *eptr, *sptr;
+ double score;
+ unsigned long deleted = 0;
+
+ eptr = zzlFirstInRange(zobj,range);
+ if (eptr == NULL) return deleted;
+
+
+ /* When the tail of the ziplist is deleted, eptr will point to the sentinel
+ * byte and ziplistNext will return NULL. */
+ while ((sptr = ziplistNext(zl,eptr)) != NULL) {
+ score = zzlGetScore(sptr);
+ if (zslValueLteMax(score,&range)) {
+ /* Delete both the element and the score. */
+ zl = ziplistDelete(zl,&eptr);
+ zl = ziplistDelete(zl,&eptr);
+ deleted++;