提交 a01ea0be 编写于 作者: H Hongze Cheng

more code

上级 0bb11b08
...@@ -13,17 +13,24 @@ ...@@ -13,17 +13,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _TD_UTIL_RBTREE_H_
#define _TD_UTIL_RBTREE_H_
#include "os.h" #include "os.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct SRBTree SRBTree; typedef struct SRBTree SRBTree;
typedef struct SRBTreeNode SRBTreeNode; typedef struct SRBTreeNode SRBTreeNode;
typedef struct SRBTreeIter SRBTreeIter; typedef struct SRBTreeIter SRBTreeIter;
typedef int32_t (*tRBTreeCmprFn)(void *, void *); typedef int32_t (*tRBTreeCmprFn)(const void *, const void *);
// SRBTree ============================================= // SRBTree =============================================
#define tRBTreeCreate(compare) \ #define tRBTreeCreate(compare) \
(SRBTree) { .cmprFn = (compare), .root = NULL, .minNode = NULL, .maxNode = NULL } (SRBTree) { .cmprFn = (compare), .rootNode = NULL, .minNode = NULL, .maxNode = NULL }
SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew); SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew);
void tRBTreeDrop(SRBTree *pTree, SRBTreeNode *pNode); void tRBTreeDrop(SRBTree *pTree, SRBTreeNode *pNode);
...@@ -56,3 +63,9 @@ struct SRBTreeIter { ...@@ -56,3 +63,9 @@ struct SRBTreeIter {
SRBTree *pTree; SRBTree *pTree;
SRBTreeNode *pNode; SRBTreeNode *pNode;
}; };
#ifdef __cplusplus
}
#endif
#endif /*_TD_UTIL_RBTREE_H_*/
\ No newline at end of file
...@@ -256,7 +256,7 @@ SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter) { ...@@ -256,7 +256,7 @@ SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter) {
if (pIter->pNode->right) { if (pIter->pNode->right) {
pIter->pNode = pIter->pNode->right; pIter->pNode = pIter->pNode->right;
while (pIter->pNode->left) { while (pIter->pNode->left) {
pIter->pNode->left; pIter->pNode = pIter->pNode->left;
} }
} else { } else {
while (true) { while (true) {
......
...@@ -76,3 +76,11 @@ add_test( ...@@ -76,3 +76,11 @@ add_test(
NAME taosbsearchTest NAME taosbsearchTest
COMMAND taosbsearchTest COMMAND taosbsearchTest
) )
# trbtreeTest
add_executable(rbtreeTest "trbtreeTest.cpp")
target_link_libraries(rbtreeTest os util gtest_main)
add_test(
NAME rbtreeTest
COMMAND rbtreeTest
)
\ No newline at end of file
#include <gtest/gtest.h>
#include <stdio.h>
#include <stdlib.h>
#include "trbtree.h"
static int32_t tCmprInteger(const void *p1, const void *p2) {
if (*(int *)p1 < *(int *)p2) {
return -1;
} else if (*(int *)p1 > *(int *)p2) {
return 1;
}
return 0;
}
TEST(trbtreeTest, rbtree_test1) {
SRBTree rt = tRBTreeCreate(tCmprInteger);
int a[] = {1, 3, 4, 2, 7, 5, 8};
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
SRBTreeNode *pNode = (SRBTreeNode *)taosMemoryMalloc(sizeof(*pNode) + sizeof(int));
*(int *)pNode->payload = a[i];
tRBTreePut(&rt, pNode);
}
SRBTreeIter rti = tRBTreeIterCreate(&rt);
SRBTreeNode *pNode = tRBTreeIterNext(&rti);
int la = 0;
while (pNode) {
GTEST_ASSERT_GT(*(int *)pNode->payload, la);
la = *(int *)pNode->payload;
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.
先完成此消息的编辑!
想要评论请 注册