TDengine/include/util/trbtree.h

79 lines
2.3 KiB
C
Raw Normal View History

2022-08-24 09:29:28 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-24 14:34:11 +00:00
#ifndef _TD_UTIL_RBTREE_H_
#define _TD_UTIL_RBTREE_H_
2022-08-24 09:29:28 +00:00
#include "os.h"
2022-08-24 14:34:11 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2022-08-24 09:29:28 +00:00
typedef struct SRBTree SRBTree;
typedef struct SRBTreeNode SRBTreeNode;
typedef struct SRBTreeIter SRBTreeIter;
2022-10-14 05:34:25 +00:00
typedef int32_t (*tRBTreeCmprFn)(const SRBTreeNode *, const SRBTreeNode *);
2022-08-24 09:29:28 +00:00
2022-08-24 11:01:27 +00:00
// SRBTree =============================================
2022-08-25 15:07:59 +00:00
#define tRBTreeMin(T) ((T)->min == ((T)->NIL) ? NULL : (T)->min)
#define tRBTreeMax(T) ((T)->max == ((T)->NIL) ? NULL : (T)->max)
2022-08-24 09:29:28 +00:00
2022-08-25 15:07:59 +00:00
void tRBTreeCreate(SRBTree *pTree, tRBTreeCmprFn cmprFn);
2023-02-08 09:46:11 +00:00
void tRBTreeClear(SRBTree *pTree);
2022-08-25 15:07:59 +00:00
SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *z);
void tRBTreeDrop(SRBTree *pTree, SRBTreeNode *z);
2022-08-24 09:29:28 +00:00
SRBTreeNode *tRBTreeDropByKey(SRBTree *pTree, void *pKey);
2023-02-08 09:46:11 +00:00
SRBTreeNode *tRBTreeDropMin(SRBTree *pTree);
SRBTreeNode *tRBTreeDropMax(SRBTree *pTree);
2023-05-18 07:06:21 +00:00
SRBTreeNode *tRBTreeGet(const SRBTree *pTree, const SRBTreeNode *pKeyNode);
2022-08-24 09:29:28 +00:00
2022-08-24 11:01:27 +00:00
// SRBTreeIter =============================================
2022-08-25 02:43:13 +00:00
#define tRBTreeIterCreate(tree, ascend) \
2022-08-25 09:04:42 +00:00
(SRBTreeIter) { .asc = (ascend), .pTree = (tree), .pNode = (ascend) ? (tree)->min : (tree)->max }
2022-08-24 09:29:28 +00:00
SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter);
2022-08-24 11:01:27 +00:00
// STRUCT =============================================
2022-08-25 08:21:13 +00:00
typedef enum { RED, BLACK } ECOLOR;
2022-08-24 09:29:28 +00:00
struct SRBTreeNode {
2022-08-25 08:21:13 +00:00
ECOLOR color;
2022-08-24 09:29:28 +00:00
SRBTreeNode *parent;
SRBTreeNode *left;
SRBTreeNode *right;
};
struct SRBTree {
tRBTreeCmprFn cmprFn;
2022-08-26 14:23:03 +00:00
int64_t n;
2022-08-25 09:04:42 +00:00
SRBTreeNode *root;
SRBTreeNode *min;
SRBTreeNode *max;
2022-08-25 15:07:59 +00:00
SRBTreeNode *NIL;
SRBTreeNode NILNODE;
2022-08-24 09:29:28 +00:00
};
struct SRBTreeIter {
2023-05-16 01:36:32 +00:00
int8_t asc;
const SRBTree *pTree;
SRBTreeNode *pNode;
2022-08-24 09:29:28 +00:00
};
2022-08-24 14:34:11 +00:00
#ifdef __cplusplus
}
#endif
#endif /*_TD_UTIL_RBTREE_H_*/