Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
6435c767
R
redis
项目概览
xindoo
/
redis
通知
2
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6435c767
编写于
5月 29, 2010
作者:
P
Pieter Noordhuis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
function to insert an element at an arbitrary position in the list
上级
0c0d0564
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
46 addition
and
33 deletion
+46
-33
ziplist.c
ziplist.c
+46
-33
未找到文件。
ziplist.c
浏览文件 @
6435c767
...
...
@@ -286,66 +286,79 @@ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, int n
return
zl
;
}
unsigned
char
*
ziplistPush
(
unsigned
char
*
zl
,
unsigned
char
*
entry
,
unsigned
int
elen
,
int
where
)
{
unsigned
int
curlen
=
ZIPLIST_BYTES
(
zl
),
reqlen
,
prevlen
;
unsigned
char
*
p
,
*
curtail
;
/* Insert item at "p". */
static
unsigned
char
*
__ziplistInsert
(
unsigned
char
*
zl
,
unsigned
char
*
p
,
unsigned
char
*
s
,
unsigned
int
slen
)
{
unsigned
int
curlen
=
ZIPLIST_BYTES
(
zl
),
reqlen
,
prevlen
=
0
;
unsigned
int
offset
,
nextdiff
=
0
;
unsigned
char
*
tail
;
char
encoding
=
ZIP_ENC_RAW
;
long
long
value
;
zlentry
entry
;
/* We need to store the length of the current tail when the list
* is non-empty and we push at the tail. */
curtail
=
zl
+
ZIPLIST_TAIL_OFFSET
(
zl
);
if
(
where
==
ZIPLIST_TAIL
&&
curtail
[
0
]
!=
ZIP_END
)
{
prevlen
=
zipRawEntryLength
(
curtail
);
/* Find out prevlen for the entry that is inserted. */
if
(
p
[
0
]
!=
ZIP_END
)
{
entry
=
zipEntry
(
p
);
prevlen
=
entry
.
prevrawlen
;
}
else
{
prevlen
=
0
;
tail
=
ziplistTail
(
zl
);
if
(
tail
[
0
]
!=
ZIP_END
)
{
prevlen
=
zipRawEntryLength
(
tail
);
}
}
/* See if the entry can be encoded */
if
(
zipTryEncoding
(
entry
,
&
value
,
&
encoding
))
{
if
(
zipTryEncoding
(
s
,
&
value
,
&
encoding
))
{
reqlen
=
zipEncodingSize
(
encoding
);
}
else
{
reqlen
=
e
len
;
reqlen
=
s
len
;
}
/* We need space for both the length of the previous entry and
* the length of the payload. */
reqlen
+=
zipEncodeLength
(
NULL
,
ZIP_ENC_RAW
,
prevlen
);
reqlen
+=
zipEncodeLength
(
NULL
,
encoding
,
elen
);
/* Resize the ziplist and move if needed */
zl
=
ziplistResize
(
zl
,
curlen
+
reqlen
);
if
(
where
==
ZIPLIST_HEAD
)
{
p
=
zl
+
ZIPLIST_HEADER_SIZE
;
if
(
*
p
!=
ZIP_END
)
{
/* Subtract one because of the ZIP_END bytes */
memmove
(
p
+
reqlen
,
p
,
curlen
-
ZIPLIST_HEADER_SIZE
-
1
);
}
reqlen
+=
zipEncodeLength
(
NULL
,
encoding
,
slen
);
/* When the insert position is not equal to the tail, we need to
* make sure that the next entry can hold this entry's length in
* its prevlen field. */
nextdiff
=
p
[
0
]
!=
ZIP_END
?
zipPrevLenByteDiff
(
p
,
reqlen
)
:
0
;
/* Store offset because a realloc may change the address of zl. */
offset
=
p
-
zl
;
zl
=
ziplistResize
(
zl
,
curlen
+
reqlen
+
nextdiff
);
p
=
zl
+
offset
;
/* Apply memory move when necessary and update tail offset. */
if
(
p
[
0
]
!=
ZIP_END
)
{
/* Subtract one because of the ZIP_END bytes */
memmove
(
p
+
reqlen
,
p
-
nextdiff
,
curlen
-
offset
-
1
+
nextdiff
);
/* Encode this entry's raw length in the next entry. */
zipEncodeLength
(
p
+
reqlen
,
ZIP_ENC_RAW
,
reqlen
);
/* Update offset for tail */
ZIPLIST_TAIL_OFFSET
(
zl
)
+=
reqlen
+
nextdiff
;
}
else
{
p
=
zl
+
curlen
-
1
;
}
/* Update tail offset if this is not the first element */
if
(
curtail
[
0
]
!=
ZIP_END
)
{
if
(
where
==
ZIPLIST_HEAD
)
{
ZIPLIST_TAIL_OFFSET
(
zl
)
+=
reqlen
;
}
else
{
ZIPLIST_TAIL_OFFSET
(
zl
)
+=
prevlen
;
}
/* This element will be the new tail. */
ZIPLIST_TAIL_OFFSET
(
zl
)
=
p
-
zl
;
}
/* Write the entry */
p
+=
zipEncodeLength
(
p
,
ZIP_ENC_RAW
,
prevlen
);
p
+=
zipEncodeLength
(
p
,
encoding
,
e
len
);
p
+=
zipEncodeLength
(
p
,
encoding
,
s
len
);
if
(
encoding
!=
ZIP_ENC_RAW
)
{
zipSaveInteger
(
p
,
value
,
encoding
);
}
else
{
memcpy
(
p
,
entry
,
e
len
);
memcpy
(
p
,
s
,
s
len
);
}
ZIPLIST_INCR_LENGTH
(
zl
,
1
);
return
zl
;
}
unsigned
char
*
ziplistPush
(
unsigned
char
*
zl
,
unsigned
char
*
s
,
unsigned
int
slen
,
int
where
)
{
unsigned
char
*
p
;
p
=
(
where
==
ZIPLIST_HEAD
)
?
ziplistHead
(
zl
)
:
(
zl
+
ZIPLIST_BYTES
(
zl
)
-
1
);
return
__ziplistInsert
(
zl
,
p
,
s
,
slen
);
}
unsigned
char
*
ziplistPop
(
unsigned
char
*
zl
,
sds
*
target
,
int
where
)
{
zlentry
entry
;
unsigned
char
*
p
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录