Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
c3998092
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c3998092
编写于
8月 25, 2022
作者:
H
Hongze Cheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
more code
上级
b8a70bc5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
66 addition
and
63 deletion
+66
-63
include/util/trbtree.h
include/util/trbtree.h
+3
-3
source/util/src/trbtree.c
source/util/src/trbtree.c
+61
-59
source/util/test/trbtreeTest.cpp
source/util/test/trbtreeTest.cpp
+2
-1
未找到文件。
include/util/trbtree.h
浏览文件 @
c3998092
...
...
@@ -38,8 +38,8 @@ SRBTreeNode *tRBTreeDropByKey(SRBTree *pTree, void *pKey);
SRBTreeNode
*
tRBTreeGet
(
SRBTree
*
pTree
,
void
*
pKey
);
// SRBTreeIter =============================================
#define tRBTreeIterCreate(tree,
de
scend) \
(SRBTreeIter) { .
des = (descend), .pTree = (tree), .pNode = (descend) ? (tree)->maxNode : (tree)->min
Node }
#define tRBTreeIterCreate(tree,
a
scend) \
(SRBTreeIter) { .
asc = (ascend), .pTree = (tree), .pNode = (ascend) ? (tree)->minNode : (tree)->max
Node }
SRBTreeNode
*
tRBTreeIterNext
(
SRBTreeIter
*
pIter
);
...
...
@@ -60,7 +60,7 @@ struct SRBTree {
};
struct
SRBTreeIter
{
int8_t
des
;
int8_t
asc
;
SRBTree
*
pTree
;
SRBTreeNode
*
pNode
;
};
...
...
source/util/src/trbtree.c
浏览文件 @
c3998092
...
...
@@ -60,6 +60,55 @@ static void tRBTreeRotateRight(SRBTree *pTree, SRBTreeNode *pNode) {
pNode
->
parent
=
left
;
}
static
SRBTreeNode
*
tRBTreeSuccessor
(
SRBTreeNode
*
pNode
)
{
if
(
pNode
->
right
)
{
pNode
=
pNode
->
right
;
while
(
pNode
->
left
)
{
pNode
=
pNode
->
left
;
}
}
else
{
while
(
true
)
{
if
(
pNode
->
parent
)
{
if
(
pNode
==
pNode
->
parent
->
left
)
{
pNode
=
pNode
->
parent
;
break
;
}
else
{
pNode
=
pNode
->
parent
;
}
}
else
{
pNode
=
NULL
;
break
;
}
}
}
return
pNode
;
}
static
SRBTreeNode
*
tRBTreePredecessor
(
SRBTreeNode
*
pNode
)
{
if
(
pNode
->
left
)
{
pNode
=
pNode
->
left
;
while
(
pNode
->
right
)
{
pNode
=
pNode
->
right
;
}
}
else
{
while
(
true
)
{
if
(
pNode
->
parent
)
{
if
(
pNode
==
pNode
->
parent
->
right
)
{
pNode
=
pNode
->
parent
;
break
;
}
else
{
pNode
=
pNode
->
parent
;
}
}
else
{
pNode
=
NULL
;
break
;
}
}
}
return
NULL
;
}
SRBTreeNode
*
tRBTreePut
(
SRBTree
*
pTree
,
SRBTreeNode
*
pNew
)
{
pNew
->
left
=
NULL
;
pNew
->
right
=
NULL
;
...
...
@@ -153,8 +202,12 @@ SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew) {
void
tRBTreeDrop
(
SRBTree
*
pTree
,
SRBTreeNode
*
pNode
)
{
// update min/max node
if
(
pTree
->
minNode
==
pNode
)
pTree
->
minNode
=
pNode
->
parent
;
if
(
pTree
->
maxNode
==
pNode
)
pTree
->
maxNode
=
pNode
->
parent
;
if
(
pTree
->
minNode
==
pNode
)
{
pTree
->
minNode
=
tRBTreeSuccessor
(
pTree
->
minNode
);
}
if
(
pTree
->
maxNode
==
pNode
)
{
pTree
->
maxNode
=
tRBTreePredecessor
(
pTree
->
maxNode
);
}
// drop impl
if
(
pNode
->
left
==
NULL
)
{
...
...
@@ -208,19 +261,7 @@ void tRBTreeDrop(SRBTree *pTree, SRBTreeNode *pNode) {
}
SRBTreeNode
*
tRBTreeDropByKey
(
SRBTree
*
pTree
,
void
*
pKey
)
{
SRBTreeNode
*
pNode
=
pTree
->
rootNode
;
while
(
pNode
)
{
int32_t
c
=
pTree
->
cmprFn
(
pKey
,
pNode
->
payload
);
if
(
c
<
0
)
{
pNode
=
pNode
->
left
;
}
else
if
(
c
>
0
)
{
pNode
=
pNode
->
right
;
}
else
{
break
;
}
}
SRBTreeNode
*
pNode
=
tRBTreeGet
(
pTree
,
pKey
);
if
(
pNode
)
{
tRBTreeDrop
(
pTree
,
pNode
);
...
...
@@ -250,53 +291,14 @@ SRBTreeNode *tRBTreeGet(SRBTree *pTree, void *pKey) {
// SRBTreeIter ================================================
SRBTreeNode
*
tRBTreeIterNext
(
SRBTreeIter
*
pIter
)
{
SRBTreeNode
*
pNode
=
pIter
->
pNode
;
SRBTree
*
pTree
=
pIter
->
pTree
;
if
(
pIter
->
pNode
)
{
if
(
pIter
->
des
)
{
// descend
if
(
pIter
->
pNode
->
left
)
{
pIter
->
pNode
=
pIter
->
pNode
->
left
;
while
(
pIter
->
pNode
->
right
)
{
pIter
->
pNode
=
pIter
->
pNode
->
right
;
}
}
else
{
while
(
true
)
{
if
(
pIter
->
pNode
->
parent
)
{
if
(
pIter
->
pNode
==
pIter
->
pNode
->
parent
->
right
)
{
pIter
->
pNode
=
pIter
->
pNode
->
parent
;
break
;
}
else
{
pIter
->
pNode
=
pIter
->
pNode
->
parent
;
}
}
else
{
pIter
->
pNode
=
NULL
;
break
;
}
}
}
}
else
{
if
(
pIter
->
asc
)
{
// ascend
if
(
pIter
->
pNode
->
right
)
{
pIter
->
pNode
=
pIter
->
pNode
->
right
;
while
(
pIter
->
pNode
->
left
)
{
pIter
->
pNode
=
pIter
->
pNode
->
left
;
}
}
else
{
while
(
true
)
{
if
(
pIter
->
pNode
->
parent
)
{
if
(
pIter
->
pNode
==
pIter
->
pNode
->
parent
->
left
)
{
pIter
->
pNode
=
pIter
->
pNode
->
parent
;
break
;
}
else
{
pIter
->
pNode
=
pIter
->
pNode
->
parent
;
}
}
else
{
pIter
->
pNode
=
NULL
;
break
;
}
}
}
pIter
->
pNode
=
tRBTreeSuccessor
(
pIter
->
pNode
);
}
else
{
// descend
pIter
->
pNode
=
tRBTreePredecessor
(
pIter
->
pNode
);
}
}
...
...
source/util/test/trbtreeTest.cpp
浏览文件 @
c3998092
...
...
@@ -25,12 +25,13 @@ TEST(trbtreeTest, rbtree_test1) {
tRBTreePut
(
&
rt
,
pNode
);
}
SRBTreeIter
rti
=
tRBTreeIterCreate
(
&
rt
,
0
);
SRBTreeIter
rti
=
tRBTreeIterCreate
(
&
rt
,
1
);
SRBTreeNode
*
pNode
=
tRBTreeIterNext
(
&
rti
);
int
la
=
0
;
while
(
pNode
)
{
GTEST_ASSERT_GT
(
*
(
int
*
)
pNode
->
payload
,
la
);
la
=
*
(
int
*
)
pNode
->
payload
;
// printf("%d\n", la);
pNode
=
tRBTreeIterNext
(
&
rti
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录