Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
08253bf4
Commit
08253bf4
authored
May 21, 2010
by
Pieter Noordhuis
Browse files
code to iterate over a ziplist
parent
11ac6ff6
Changes
1
Hide whitespace changes
Inline
Side-by-side
ziplist.c
View file @
08253bf4
...
...
@@ -107,6 +107,30 @@ unsigned char *ziplistPop(unsigned char *zl, sds *value, int where) {
return
zl
;
}
/* Returns an offset to use for iterating with ziplistNext. */
unsigned
char
*
ziplistIndex
(
unsigned
char
*
zl
,
unsigned
int
index
)
{
unsigned
char
*
p
=
zl
+
ZIPLIST_HEADER_SIZE
;
unsigned
int
i
=
0
;
for
(;
i
<
index
;
i
++
)
{
if
(
*
p
==
ZIP_END
)
break
;
p
+=
zipRawEntryLength
(
p
);
}
return
p
;
}
/* Store entry at current position in sds *value and return pointer
* to the next entry. */
unsigned
char
*
ziplistNext
(
unsigned
char
*
p
,
sds
*
value
)
{
if
(
*
p
==
ZIP_END
)
return
NULL
;
if
(
value
)
{
unsigned
int
len
;
len
=
zipDecodeLength
(
p
);
*
value
=
sdsnewlen
(
p
+
zipEncodeLength
(
NULL
,
len
),
len
);
}
p
+=
zipRawEntryLength
(
p
);
return
p
;
}
void
ziplistRepr
(
unsigned
char
*
zl
)
{
unsigned
char
*
p
;
unsigned
int
l
;
...
...
@@ -125,16 +149,20 @@ void ziplistRepr(unsigned char *zl) {
}
#ifdef ZIPLIST_TEST_MAIN
int
main
(
int
argc
,
char
**
argv
)
{
unsigned
char
*
zl
;
sds
s
;
zl
=
ziplistNew
();
unsigned
char
*
createList
()
{
unsigned
char
*
zl
=
ziplistNew
();
zl
=
ziplistPush
(
zl
,
(
unsigned
char
*
)
"foo"
,
3
,
ZIPLIST_TAIL
);
ziplistRepr
(
zl
);
zl
=
ziplistPush
(
zl
,
(
unsigned
char
*
)
"quux"
,
4
,
ZIPLIST_TAIL
);
ziplistRepr
(
zl
);
zl
=
ziplistPush
(
zl
,
(
unsigned
char
*
)
"hello"
,
5
,
ZIPLIST_HEAD
);
return
zl
;
}
int
main
(
int
argc
,
char
**
argv
)
{
unsigned
char
*
zl
,
*
p
;
sds
s
;
zl
=
createList
();
ziplistRepr
(
zl
);
zl
=
ziplistPop
(
zl
,
&
s
,
ZIPLIST_TAIL
);
...
...
@@ -145,6 +173,47 @@ int main(int argc, char **argv) {
printf
(
"Pop head: %s (length %ld)
\n
"
,
s
,
sdslen
(
s
));
ziplistRepr
(
zl
);
printf
(
"Iterate list from 0 to end:
\n
"
);
{
zl
=
createList
();
p
=
ziplistIndex
(
zl
,
0
);
while
((
p
=
ziplistNext
(
p
,
&
s
))
!=
NULL
)
{
printf
(
"Entry: %s (length %ld)
\n
"
,
s
,
sdslen
(
s
));
}
printf
(
"
\n
"
);
}
printf
(
"Iterate list from 1 to end:
\n
"
);
{
zl
=
createList
();
p
=
ziplistIndex
(
zl
,
1
);
while
((
p
=
ziplistNext
(
p
,
&
s
))
!=
NULL
)
{
printf
(
"Entry: %s (length %ld)
\n
"
,
s
,
sdslen
(
s
));
}
printf
(
"
\n
"
);
}
printf
(
"Iterate list from 2 to end:
\n
"
);
{
zl
=
createList
();
p
=
ziplistIndex
(
zl
,
2
);
while
((
p
=
ziplistNext
(
p
,
&
s
))
!=
NULL
)
{
printf
(
"Entry: %s (length %ld)
\n
"
,
s
,
sdslen
(
s
));
}
printf
(
"
\n
"
);
}
printf
(
"Iterate starting out of range:
\n
"
);
{
zl
=
createList
();
p
=
ziplistIndex
(
zl
,
3
);
if
(
ziplistNext
(
p
,
&
s
)
==
NULL
)
{
printf
(
"No entry
\n
"
);
}
else
{
printf
(
"ERROR
\n
"
);
}
}
return
0
;
}
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment