change iteration code to avoid allocating a new sds for each traversed entry

This commit is contained in:
Pieter Noordhuis 2010-05-21 13:29:14 +02:00
parent 08253bf42b
commit 335d16bc0f

View File

@ -120,12 +120,11 @@ unsigned char *ziplistIndex(unsigned char *zl, unsigned int index) {
/* Store entry at current position in sds *value and return pointer /* Store entry at current position in sds *value and return pointer
* to the next entry. */ * to the next entry. */
unsigned char *ziplistNext(unsigned char *p, sds *value) { unsigned char *ziplistNext(unsigned char *p, unsigned char **entry, unsigned int *elen) {
if (*p == ZIP_END) return NULL; if (*p == ZIP_END) return NULL;
if (value) { if (entry) {
unsigned int len; *elen = zipDecodeLength(p);
len = zipDecodeLength(p); *entry = p+ZIP_LEN_BYTES(*elen);
*value = sdsnewlen(p+zipEncodeLength(NULL,len),len);
} }
p += zipRawEntryLength(p); p += zipRawEntryLength(p);
return p; return p;
@ -159,7 +158,8 @@ unsigned char *createList() {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
unsigned char *zl, *p; unsigned char *zl, *p, *entry;
unsigned int elen;
sds s; sds s;
zl = createList(); zl = createList();
@ -177,8 +177,10 @@ int main(int argc, char **argv) {
{ {
zl = createList(); zl = createList();
p = ziplistIndex(zl, 0); p = ziplistIndex(zl, 0);
while ((p = ziplistNext(p, &s)) != NULL) { while ((p = ziplistNext(p, &entry, &elen)) != NULL) {
printf("Entry: %s (length %ld)\n", s, sdslen(s)); printf("Entry: ");
fwrite(entry,elen,1,stdout);
printf(" (length %d)\n", elen);
} }
printf("\n"); printf("\n");
} }
@ -187,8 +189,10 @@ int main(int argc, char **argv) {
{ {
zl = createList(); zl = createList();
p = ziplistIndex(zl, 1); p = ziplistIndex(zl, 1);
while ((p = ziplistNext(p, &s)) != NULL) { while ((p = ziplistNext(p, &entry, &elen)) != NULL) {
printf("Entry: %s (length %ld)\n", s, sdslen(s)); printf("Entry: ");
fwrite(entry,elen,1,stdout);
printf(" (length %d)\n", elen);
} }
printf("\n"); printf("\n");
} }
@ -197,8 +201,10 @@ int main(int argc, char **argv) {
{ {
zl = createList(); zl = createList();
p = ziplistIndex(zl, 2); p = ziplistIndex(zl, 2);
while ((p = ziplistNext(p, &s)) != NULL) { while ((p = ziplistNext(p, &entry, &elen)) != NULL) {
printf("Entry: %s (length %ld)\n", s, sdslen(s)); printf("Entry: ");
fwrite(entry,elen,1,stdout);
printf(" (length %d)\n", elen);
} }
printf("\n"); printf("\n");
} }
@ -207,7 +213,7 @@ int main(int argc, char **argv) {
{ {
zl = createList(); zl = createList();
p = ziplistIndex(zl, 3); p = ziplistIndex(zl, 3);
if (ziplistNext(p, &s) == NULL) { if (ziplistNext(p, &entry, &elen) == NULL) {
printf("No entry\n"); printf("No entry\n");
} else { } else {
printf("ERROR\n"); printf("ERROR\n");