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
66ef8da0
Commit
66ef8da0
authored
Mar 04, 2010
by
antirez
Browse files
zipmap iteration code
parent
94e543b5
Changes
1
Show whitespace changes
Inline
Side-by-side
zipmap.c
View file @
66ef8da0
...
...
@@ -304,6 +304,37 @@ unsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int kle
return
zm
;
}
/* This function is used to iterate through all the zipmap elements.
* In the first call the first argument is the pointer to the zipmap + 1.
* In the next calls what zipmapNext returns is used as first argument.
* Example:
*
* unsigned char *i = my_zipmap+1;
* while((i = zipmapNext(i,&key,&klen,&value,&vlen)) != NULL) {
* printf("%d bytes key at $p\n", klen, key);
* printf("%d bytes value at $p\n", vlen, value);
* }
*/
unsigned
char
*
zipmapNext
(
unsigned
char
*
zm
,
unsigned
char
**
key
,
unsigned
int
*
klen
,
unsigned
char
**
value
,
unsigned
int
*
vlen
)
{
while
(
zm
[
0
]
==
ZIPMAP_EMPTY
)
zm
+=
zipmapDecodeLength
(
zm
+
1
);
if
(
zm
[
0
]
==
ZIPMAP_END
)
return
NULL
;
if
(
key
)
{
*
key
=
zm
;
*
klen
=
zipmapDecodeLength
(
zm
);
*
key
+=
(
*
klen
<
ZIPMAP_BIGLEN
)
?
1
:
sizeof
(
unsigned
int
);
}
zm
+=
zipmapRawKeyLength
(
zm
);
if
(
value
)
{
*
value
=
zm
+
1
;
*
vlen
=
zipmapDecodeLength
(
zm
);
*
value
+=
(
*
vlen
<
ZIPMAP_BIGLEN
)
?
1
:
sizeof
(
unsigned
int
);
}
zm
+=
zipmapRawValueLength
(
zm
);
return
zm
;
}
void
zipmapRepr
(
unsigned
char
*
p
)
{
unsigned
int
l
;
...
...
@@ -355,5 +386,15 @@ int main(void) {
zipmapRepr
(
zm
);
zm
=
zipmapDel
(
zm
,(
unsigned
char
*
)
"new"
,
3
,
NULL
);
zipmapRepr
(
zm
);
printf
(
"Iterate trought elements:
\n
"
);
{
unsigned
char
*
i
=
zm
+
1
;
unsigned
char
*
key
,
*
value
;
unsigned
int
klen
,
vlen
;
while
((
i
=
zipmapNext
(
i
,
&
key
,
&
klen
,
&
value
,
&
vlen
))
!=
NULL
)
{
printf
(
"%d:%.*s => %d:%.*s
\n
"
,
klen
,
klen
,
key
,
vlen
,
vlen
,
value
);
}
}
return
0
;
}
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