trbtree.h 2.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16 17 18
#ifndef _TD_UTIL_RBTREE_H_
#define _TD_UTIL_RBTREE_H_

H
Hongze Cheng 已提交
19 20
#include "os.h"

H
Hongze Cheng 已提交
21 22 23 24
#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
25 26 27 28
typedef struct SRBTree     SRBTree;
typedef struct SRBTreeNode SRBTreeNode;
typedef struct SRBTreeIter SRBTreeIter;

H
Hongze Cheng 已提交
29
typedef int32_t (*tRBTreeCmprFn)(const SRBTreeNode *, const SRBTreeNode *);
H
Hongze Cheng 已提交
30

H
Hongze Cheng 已提交
31
// SRBTree =============================================
H
Hongze Cheng 已提交
32 33
#define tRBTreeMin(T) ((T)->min == ((T)->NIL) ? NULL : (T)->min)
#define tRBTreeMax(T) ((T)->max == ((T)->NIL) ? NULL : (T)->max)
H
Hongze Cheng 已提交
34

H
Hongze Cheng 已提交
35 36 37
void         tRBTreeCreate(SRBTree *pTree, tRBTreeCmprFn cmprFn);
SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *z);
void         tRBTreeDrop(SRBTree *pTree, SRBTreeNode *z);
H
Hongze Cheng 已提交
38
SRBTreeNode *tRBTreeDropByKey(SRBTree *pTree, void *pKey);
H
Hongze Cheng 已提交
39
SRBTreeNode *tRBTreeGet(SRBTree *pTree, const SRBTreeNode *pKeyNode);
H
Hongze Cheng 已提交
40

H
Hongze Cheng 已提交
41
// SRBTreeIter =============================================
H
Hongze Cheng 已提交
42
#define tRBTreeIterCreate(tree, ascend) \
H
Hongze Cheng 已提交
43
  (SRBTreeIter) { .asc = (ascend), .pTree = (tree), .pNode = (ascend) ? (tree)->min : (tree)->max }
H
Hongze Cheng 已提交
44 45 46

SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter);

H
Hongze Cheng 已提交
47
// STRUCT =============================================
H
Hongze Cheng 已提交
48
typedef enum { RED, BLACK } ECOLOR;
H
Hongze Cheng 已提交
49
struct SRBTreeNode {
H
Hongze Cheng 已提交
50
  ECOLOR       color;
H
Hongze Cheng 已提交
51 52 53 54 55 56 57
  SRBTreeNode *parent;
  SRBTreeNode *left;
  SRBTreeNode *right;
};

struct SRBTree {
  tRBTreeCmprFn cmprFn;
H
Hongze Cheng 已提交
58
  int64_t       n;
H
Hongze Cheng 已提交
59 60 61
  SRBTreeNode  *root;
  SRBTreeNode  *min;
  SRBTreeNode  *max;
H
Hongze Cheng 已提交
62 63
  SRBTreeNode  *NIL;
  SRBTreeNode   NILNODE;
H
Hongze Cheng 已提交
64 65 66
};

struct SRBTreeIter {
H
Hongze Cheng 已提交
67
  int8_t       asc;
H
Hongze Cheng 已提交
68 69
  SRBTree     *pTree;
  SRBTreeNode *pNode;
H
Hongze Cheng 已提交
70
};
H
Hongze Cheng 已提交
71 72 73 74 75 76

#ifdef __cplusplus
}
#endif

#endif /*_TD_UTIL_RBTREE_H_*/