Click
Loading...
Searching...
No Matches
rbt.hh
1/*
2 * rbt.hh - Set of methods and structures used to create and manage red black trees
3 *
4 * This file only declares and documents the methods that can be used publicly to manage RBTs
5 * For helper methods used internally, please refer to rbt.cc
6 *
7 * Romain Gaillard - Adapted from the Red Black Tree implementation of Emin Martinian.
8 */
9
10#ifndef MIDDLEBOX_RBT_HH
11#define MIDDLEBOX_RBT_HH
12
13
14#include <clicknet/tcp.h>
15#include "memorypool.hh"
16
17CLICK_DECLS
18
19class RBTManager;
20
30typedef struct rb_red_blk_node {
31 void* key;
32 void* info;
33 int red; /* if red=0 then the node is black */
34 struct rb_red_blk_node* left;
35 struct rb_red_blk_node* right;
36 struct rb_red_blk_node* parent;
38
42typedef struct rb_red_blk_tree {
43 RBTManager* manager; // Pointer to the RBTManager used to compare keys and allocate memory
44 /* A sentinel is used for root and for nil. These sentinels are */
45 /* created when RBTreeCreate is called. root->left should always */
46 /* point to the node which is the root of the tree. nil points to a */
47 /* node which should always be black but has aribtrary children and */
48 /* parent and no key or info. The point of using these sentinels is so */
49 /* that the root and nil nodes do not require special cases in the code */
50 rb_red_blk_node* root;
51 rb_red_blk_node* nil;
53
59rb_red_blk_tree* RBTreeCreate(RBTManager* manager);
60
68rb_red_blk_node * RBTreeInsert(rb_red_blk_tree* tree, void* key, void* info);
69
74void RBTreePrint(rb_red_blk_tree* tree);
75
81void RBDelete(rb_red_blk_tree* tree, rb_red_blk_node* node);
82
87void RBTreeDestroy(rb_red_blk_tree* tree);
88
95rb_red_blk_node* TreePredecessor(rb_red_blk_tree* tree, rb_red_blk_node* node);
96
104
111rb_red_blk_node* RBExactQuery(rb_red_blk_tree* tree, void* key);
112
123rb_red_blk_node* RBFindElementGreatestBelow(rb_red_blk_tree* tree, void* key);
124
134rb_red_blk_node* RBMin(rb_red_blk_tree* tree);
135
145rb_red_blk_node* RBMax(rb_red_blk_tree* tree);
146
158void RBPrune(rb_red_blk_tree* tree, void* key);
159
160#define BS_TREE_POOL_SIZE 10
161#define BS_POOL_SIZE 5000
162#define BS_PRUNE_THRESHOLD 10
163
164
173{
174public:
175 RBTManager() : poolNodes(BS_POOL_SIZE),
176 poolKeys(BS_POOL_SIZE),
177 poolInfos(BS_POOL_SIZE),
178 poolTrees(BS_TREE_POOL_SIZE)
179 {
180
181 }
182
183 inline int compareKeys(const void* first, const void* second)
184 {
185 if(SEQ_GT(*(uint32_t*)first, *(uint32_t*)second))
186 return 1;
187 if(SEQ_LT(*(uint32_t*)first, *(uint32_t*)second))
188 return -1;
189
190 return 0;
191 }
192
193 void printKey(const void* first)
194 {
195 click_chatter("%u", *(uint32_t*)first);
196 }
197
198 void printInfo(void* first)
199 {
200 click_chatter("%d", *(int*)first);
201 }
202
203 rb_red_blk_node* allocateNode(void)
204 {
205 return poolNodes.getMemory();
206 }
207
208 rb_red_blk_tree* allocateTree(void)
209 {
210 return poolTrees.getMemory();
211 }
212
213 void freeNode(rb_red_blk_node* node)
214 {
215 poolNodes.releaseMemory(node);
216 }
217
218 void freeKey(void* key)
219 {
220 poolKeys.releaseMemory((uint32_t*)key);
221 }
222
223 void freeInfo(void* info)
224 {
225 poolInfos.releaseMemory((int*)info);
226 }
227
228 void freeTree(rb_red_blk_tree* tree)
229 {
230 poolTrees.releaseMemory(tree);
231 }
232
233 uint32_t* allocateKey(void)
234 {
235 return poolKeys.getMemory();
236 }
237
238 int* allocateInfo(void)
239 {
240 return poolInfos.getMemory();
241 }
242
243private:
246 MemoryPool<uint32_t> poolKeys;
247 MemoryPool<int> poolInfos;
248};
249
250
251CLICK_ENDDECLS
252
253#endif
Template for memory pools.
Definition memorypool.hh:20
void releaseMemory(T *p)
Release memory and put it back in the pool.
Definition memorypool.hh:94
T * getMemory()
Obtain memory from the memory pool.
Definition memorypool.hh:76
RBTManager that uses memory pools to allocate memory and compares the keys as sequence numbers.
Definition rbt.hh:173
Definition rbt.hh:30
Definition rbt.hh:42