40#define TREEIN_CHECK_ASSERT 0x02
41#define TREEIN_CHECK_AUTO 0x04
57#if TREEIN_CHECK & TREEIN_CHECK_ASSERT
58#define TREEIN_ASSERT(cond) configASSERT(cond)
60#define TREEIN_ASSERT(cond) if(!(cond)) { readUnlock(save); return false; }
91template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
93 N
const* me =
static_cast<N const*
>(
this);
94 unsigned save = readLock(
false);
96 Root* myroot = m_root;
97 if (m_parent ==
nullptr) {
98 TREEIN_ASSERT(
static_cast<Root*
>(m_root)->m_base == me);
100 Node* myparent = m_parent;
101 TREEIN_ASSERT(myparent->m_left == me || myparent->m_right == me);
102 TREEIN_ASSERT(myparent->m_root == m_root);
106 Node* myleft = m_left;
107 TREEIN_ASSERT(myleft->m_parent == me);
108 TREEIN_ASSERT(myroot->compare(*me, *m_left) <= 0);
109 TREEIN_ASSERT(myroot->compare(*m_left, *me) >= 0);
110 if(myleft->m_right) {
112 while(myleft->m_right) {
113 myleft = myleft->m_right;
115 N
const* rightmost =
static_cast<N const*
>(myleft);
116 TREEIN_ASSERT(myroot->compare(*me, *rightmost) <= 0);
117 TREEIN_ASSERT(myroot->compare(*rightmost, *me) >= 0);
122 Node* myright = m_right;
123 TREEIN_ASSERT(myright->m_parent == me);
124 TREEIN_ASSERT(myroot->compare(*me, *m_right) >= 0);
125 TREEIN_ASSERT(myroot->compare(*m_right, *me) <= 0);
126 if(myright->m_left) {
128 while(myright->m_left) {
129 myright = myright->m_left;
131 N
const* leftmost =
static_cast<N const*
>(myright);
132 TREEIN_ASSERT(myroot->compare(*me, *leftmost) >= 0);
133 TREEIN_ASSERT(myroot->compare(*leftmost, *me) <= 0);
138 TREEIN_ASSERT(m_parent ==
nullptr);
139 TREEIN_ASSERT(m_left ==
nullptr);
140 TREEIN_ASSERT(m_right ==
nullptr);
146template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
148 R
const* me =
static_cast<R const*
>(
this);
150 unsigned save = readLock(
false);
152 Node
const* node = m_base;
153 TREEIN_ASSERT(node->m_root == me);
154 TREEIN_ASSERT(node->m_parent ==
nullptr);
156 flag = node->check();
160 }
else if(node->m_left !=
nullptr) {
162 }
else if(node->m_right !=
nullptr) {
163 node = node->m_right;
165 while(node->m_parent) {
166 Node* parent = node->m_parent;
167 if(parent->m_left == node && parent->m_right){
168 node = parent->m_right;
193template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
197 unsigned save = readLock(
false);
202 while (mynode->m_left) {
203 node = mynode->m_left;
210 mynode =
const_cast<Node *
>(
this);
211 node =
static_cast<N*
>(mynode);
212 while (mynode->m_parent) {
213 N* myparent = mynode->m_parent;
215 if (mynode->m_left == node) {
228template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
232 unsigned save = readLock(
false);
237 while (mynode->m_right) {
238 node = mynode->m_right;
245 mynode =
const_cast<Node*
>(
this);
246 node =
static_cast<N*
>(mynode);
247 while (mynode->m_parent) {
248 N* myparent = mynode->m_parent;
250 if (mynode->m_right == node) {
260template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
262 unsigned save = readLock(
false);
263 if (m_base ==
nullptr)
return nullptr;
266 while (mynode->m_left) {
267 node = mynode->m_left;
275template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
277 if (m_base == 0)
return 0;
278 unsigned save = readLock();
281 while (mynode->m_right) {
282 node = mynode->m_right;
334template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
338#if TREEIN_CHECK & TREEIN_CHECK_AUTO
341 unsigned save = writeLock(
false);
343 Node* myparent = m_parent;
344 N* newLink =
nullptr;
345 if (m_left ==
nullptr) {
346 if (m_right ==
nullptr) {
353 if (m_right ==
nullptr) {
362 nl->m_right = m_right;
363 static_cast<Node*
>(m_right)->m_parent = newLink;
365 if (newLink != m_left){
369 static_cast<Node*
>(nl->m_parent)->m_right = nl->m_left;
371 static_cast<Node*
>(nl->m_left)->m_parent = nl->m_parent;
376 static_cast<Node*
>(m_left)->m_parent = newLink;
383 static_cast<Node*
>(newLink)->m_parent = m_parent;
387 if (myparent->m_left ==
static_cast<N*
>(
this)) {
388 myparent->m_left = newLink;
390 myparent->m_right = newLink;
393 static_cast<Root*
>(m_root)->m_base = newLink;
395#if TREEIN_CHECK & TREEIN_CHECK_AUTO
396 static_cast<Root*
>(m_root)->check();
402 root->writeUnlock(save);
404#if TREEIN_CHECK & TREEIN_CHECK_AUTO
416template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
419 unsigned save = writeLock(
false);
420 if (mynode.m_root ==
this) {
435template <
class R,
class N,
class K, ContainerThreadSafety s,
int n_>
437 if (node !=
nullptr) remove(*node);
449template<
class R,
class N,
class K, ContainerThreadSafety s,
int n>
452 if (mynode.m_root ==
this)
return;
453 if (mynode.m_root !=
nullptr) mynode.remove();
454 unsigned save = writeLock(
false);
456 N* nodebase = m_base;
458 Node* mynodebase = nodebase;
459 int cmp = compare(*nodebase, node);
461 if (mynodebase->m_left) {
462 nodebase = mynodebase->m_left;
464 mynodebase->m_left = &node;
465 mynode.m_parent = nodebase;
469 if (mynodebase->m_right) {
470 nodebase = mynodebase->m_right;
472 mynodebase->m_right = &node;
473 mynode.m_parent = nodebase;
481 mynode.m_parent =
nullptr;
483 mynode.setRoot(
static_cast<R*
>(
this));
484 mynode.m_left =
nullptr;
485 mynode.m_right =
nullptr;
488#if TREEIN_CHECK & TREEIN_CHECK_AUTO
500template<
class R,
class N,
class K, ContainerThreadSafety s,
int n_>
502 if (node !=
nullptr) add(*node);
512template<
class R,
class N,
class K, ContainerThreadSafety s,
int n>
515 static_cast<Root&
>(myroot).add(*
static_cast<N*
>(
this));
525template<
class R,
class N,
class K, ContainerThreadSafety s,
int n>
538template<
class R,
class N,
class K, ContainerThreadSafety s,
int n>
541 unsigned save = readLock(
false);
543 int cmp = compareKey(*node, key);
546 node =
static_cast<Node*
>(node)->m_left;
548 node =
static_cast<Node*
>(node)->m_right;
558template<
class R,
class N,
class K, ContainerThreadSafety s,
int n>
562 unsigned save = readLock(
false);
564 int cmp = compareKey(*cursor, key);
571 cursor =
static_cast<Node*
>(cursor)->m_left;
573 cursor =
static_cast<Node*
>(cursor)->m_right;
584template<
class R,
class N,
class K, ContainerThreadSafety s,
int n>
588 unsigned save = readLock(
false);
590 int cmp = compareKey(*cursor, key);
596 cursor =
static_cast<Node*
>(cursor)->m_left;
599 cursor =
static_cast<Node*
>(cursor)->m_right;
612template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
625template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
631 }
else if (node->m_right) {
632 node = node->m_right;
634 Node* parent = node->m_parent;
635 node->m_left =
nullptr;
636 node->m_right =
nullptr;
637 node->m_parent =
nullptr;
639 if (parent->m_left == node) {
640 parent->m_left =
nullptr;
642 if (parent->m_right == node) {
643 parent->m_right =
nullptr;
656template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
670template <
class R,
class N,
class K, ContainerThreadSafety s,
int n>
Intrusive Binary Tree (unbalanced).
Base Container Node Class.
Definition Container.h:255
bool check() const override
Definition TreeIn.hpp:182
virtual ~TreeInNode()
Destructor.
Definition TreeIn.hpp:671
N * prev() const
Return previous node in sort sequence.
Definition TreeIn.hpp:229
virtual void remove()
Remove node from whatever tree it is on, if it is on a tree.
Definition TreeIn.hpp:335
class TreeInNode< R, N, K, s, n > Node
Type of DListIInNode.
Definition TreeIn.h:280
N * next() const
Return node next in sort sequence.
Definition TreeIn.hpp:194
void addTo(R &root)
Add ourself to a tree.
Definition TreeIn.hpp:513
class TreeInRoot< R, N, K, s, n > Root
Type of TreeInRoot.
Definition TreeIn.h:279
TreeInNode()
Constructor.
Definition TreeIn.hpp:657
N * last() const
Definition TreeIn.hpp:276
void remove(N &node)
Remove Node from List.
Definition TreeIn.hpp:417
virtual ~TreeInRoot()
Destructor.
Definition TreeIn.hpp:626
N * find(K key) const
find a node
Definition TreeIn.hpp:539
class TreeInNode< R, N, K, s, n > Node
Type of DListIInNode.
Definition TreeIn.h:158
bool check() const override
Definition TreeIn.hpp:185
TreeInRoot()
Constructor.
Definition TreeIn.hpp:613
N * findMinus(K key) const
find a node, or the node that would be just lower than this node
Definition TreeIn.hpp:559
N * first() const
Definition TreeIn.hpp:261
N * findPlus(K key) const
find a node, r the node that would be just above this node.
Definition TreeIn.hpp:585
virtual void add(N &node)
Add node to our tree, note trees are sorted by the compare member function which needs to be implemen...
Definition TreeIn.hpp:450