42 DG_CLASS_ALLOCATOR(allocator)
61 inline void SetColor(REDBLACK_COLOR color);
62 REDBLACK_COLOR GetColor()
const;
63 dgUnsigned32 IsInTree()
const;
64 inline void SetInTreeFlag(dgUnsigned32 flag);
82 dgUnsigned32 m_color : 1;
83 dgUnsigned32 m_inTree : 1;
87 template<
class OBJECT,
class KEY>
115 dgRedBackNode::m_left = node;
119 dgRedBackNode::m_right = node;
123 dgRedBackNode::m_parent = node;
127 const KEY &GetKey()
const {
135 const OBJECT &GetInfo()
const {
142 friend class dgTree<OBJECT, KEY>;
158 m_ptr = m_tree->Minimum();
162 m_ptr = m_tree->Maximum();
169 operator dgInt32()
const {
170 return m_ptr != NULL;
174 NEWTON_ASSERT(m_ptr);
175 m_ptr = m_ptr->Next();
178 void operator++ (
int) {
179 NEWTON_ASSERT(m_ptr);
180 m_ptr = m_ptr->Next();
184 NEWTON_ASSERT(m_ptr);
185 m_ptr = m_ptr->Prev();
188 void operator-- (
int) {
189 NEWTON_ASSERT(m_ptr);
190 m_ptr = m_ptr->Prev();
193 OBJECT &operator* ()
const {
203 return tmp ? tmp->GetKey() : KEY(0);
217 DG_CLASS_ALLOCATOR(allocator)
227 operator dgInt32()
const;
228 dgInt32 GetCount()
const;
239 dgTreeNode *GetNodeFromInfo(OBJECT &info)
const;
241 dgTreeNode *Insert(
const OBJECT &element, KEY key,
bool &elementWasInTree);
242 dgTreeNode *Insert(
const OBJECT &element, KEY key);
245 dgTreeNode *Replace(OBJECT &element, KEY key);
246 dgTreeNode *ReplaceKey(KEY oldKey, KEY newKey);
249 void Remove(KEY key);
254 void SwapInfo(
dgTree &tree);
256 bool SanityCheck()
const;
266 dgInt32 CompareKeys(
const KEY &key0,
const KEY &key1)
const;
267 bool SanityCheck(
dgTreeNode *
const ptr, dgInt32 height)
const;
273 inline dgRedBackNode::dgRedBackNode(
dgRedBackNode *
const parent) {
277 inline void dgRedBackNode::Initdata(
dgRedBackNode *
const parent) {
285 inline void dgRedBackNode::SetColor(dgRedBackNode::REDBLACK_COLOR color) {
291 inline dgRedBackNode::REDBLACK_COLOR dgRedBackNode::GetColor()
const {
292 return REDBLACK_COLOR(m_color);
296 inline void dgRedBackNode::SetInTreeFlag(dgUnsigned32 flag) {
300 inline dgUnsigned32 dgRedBackNode::IsInTree()
const {
314 template<
class OBJECT,
class KEY>
318 m_allocator = allocator;
322 template<
class OBJECT,
class KEY>
327 template<
class OBJECT,
class KEY>
332 template<
class OBJECT,
class KEY>
334 if ((m_count == 0) && (m_allocator == NULL)) {
335 m_allocator = allocator;
340 template<
class OBJECT,
class KEY>
342 return m_head != NULL;
345 template<
class OBJECT,
class KEY>
350 template<
class OBJECT,
class KEY>
352 return m_head ? (
dgTreeNode *)m_head->Minimum() : NULL;
355 template<
class OBJECT,
class KEY>
357 return m_head ? (
dgTreeNode *)m_head->Maximum() : NULL;
360 template<
class OBJECT,
class KEY>
365 template<
class OBJECT,
class KEY>
367 if (m_head == NULL) {
372 while (ptr != NULL) {
373 if (key < ptr->m_key) {
374 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == -1) ;
375 ptr = ptr->GetLeft();
376 }
else if (key > ptr->m_key) {
377 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == 1) ;
378 ptr = ptr->GetRight();
380 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == 0) ;
387 template<
class OBJECT,
class KEY>
390 dgInt64 offset = ((
char *) &node->m_info) - ((
char *) node);
393 NEWTON_ASSERT(retnode->IsInTree());
394 NEWTON_ASSERT(&retnode->GetInfo() == &info);
395 return (retnode->IsInTree()) ? retnode : NULL;
399 template<
class OBJECT,
class KEY>
401 if (m_head == NULL) {
408 while (ptr != NULL) {
409 if (key < ptr->m_key) {
411 ptr = ptr->GetLeft();
413 ptr = ptr->GetRight();
417 #ifdef __ENABLE_SANITY_CHECK 420 for (iter.Begin(); iter.GetNode() != prev; iter ++) {
421 KEY key1 = iter.GetKey();
422 NEWTON_ASSERT(key1 <= key);
424 for (; iter.GetNode(); iter ++) {
425 KEY key1 = iter.GetKey();
426 NEWTON_ASSERT(key1 > key);
434 template<
class OBJECT,
class KEY>
436 if (m_head == NULL) {
443 while (ptr != NULL) {
444 if (key == ptr->m_key) {
447 if (key < ptr->m_key) {
449 ptr = ptr->GetLeft();
451 ptr = ptr->GetRight();
455 #ifdef __ENABLE_SANITY_CHECK 458 for (iter.Begin(); iter.GetNode() != prev; iter ++) {
459 KEY key1 = iter.GetKey();
460 NEWTON_ASSERT(key1 <= key);
462 for (; iter.GetNode(); iter ++) {
463 KEY key1 = iter.GetKey();
464 NEWTON_ASSERT(key1 >= key);
472 template<
class OBJECT,
class KEY>
474 if (m_head == NULL) {
481 while (ptr != NULL) {
482 if (key == ptr->m_key) {
486 if (key < ptr->m_key) {
487 ptr = ptr->GetLeft();
490 ptr = ptr->GetRight();
495 #ifdef __ENABLE_SANITY_CHECK 498 for (iter.End(); iter.GetNode() != prev; iter --) {
499 KEY key1 = iter.GetKey();
500 NEWTON_ASSERT(key1 >= key);
502 for (; iter.GetNode(); iter --) {
503 KEY key1 = iter.GetKey();
504 NEWTON_ASSERT(key1 < key);
512 template<
class OBJECT,
class KEY>
517 elementWasInTree =
false;
518 while (ptr != NULL) {
521 if (key < ptr->m_key) {
522 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == -1) ;
524 ptr = ptr->GetLeft();
525 }
else if (key > ptr->m_key) {
526 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == 1) ;
528 ptr = ptr->GetRight();
530 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == 0) ;
531 elementWasInTree =
true;
537 NEWTON_ASSERT(m_allocator);
538 ptr =
new (m_allocator)
dgTreeNode(element, key, parent);
543 parent->m_left = ptr;
545 parent->m_right = ptr;
555 template<
class OBJECT,
class KEY>
559 dgTreeNode *
const node = Insert(element, key, foundState);
566 template<
class OBJECT,
class KEY>
571 while (ptr != NULL) {
582 if (key < ptr->m_key) {
583 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == -1) ;
585 ptr = ptr->GetLeft();
586 }
else if (key > ptr->m_key) {
587 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == 1) ;
589 ptr = ptr->GetRight();
591 NEWTON_ASSERT(CompareKeys(ptr->m_key, key) == 0) ;
600 ptr->Initdata(parent);
606 parent->m_left = ptr;
608 parent->m_right = ptr;
619 template<
class OBJECT,
class KEY>
624 while (ptr != NULL) {
628 val = CompareKeys(ptr->m_key, key);
630 ptr->m_info = element;
634 ptr = ptr->GetLeft();
636 ptr = ptr->GetRight();
640 NEWTON_ASSERT(m_allocator);
641 ptr =
new (m_allocator)
dgTreeNode(element, key, parent);
646 parent->m_left = ptr;
648 parent->m_right = ptr;
659 template<
class OBJECT,
class KEY>
667 template<
class OBJECT,
class KEY>
670 return node ? ReplaceKey(node, newKey) : NULL;
673 template<
class OBJECT,
class KEY>
679 NEWTON_ASSERT(!Find(node->GetKey()));
683 template<
class OBJECT,
class KEY>
690 template<
class OBJECT,
class KEY>
702 template<
class OBJECT,
class KEY>
711 template<
class OBJECT,
class KEY>
713 return SanityCheck(m_head, 0);
717 template<
class OBJECT,
class KEY>
723 if (!ptr->IsInTree()) {
728 if (CompareKeys(ptr->m_key, ptr->GetLeft()->m_key) > 0) {
734 if (CompareKeys(ptr->m_key, ptr->GetRight()->m_key) < 0) {
739 if (ptr->GetColor() == dgTreeNode::BLACK) {
741 }
else if (!((!ptr->m_left || (ptr->m_left->GetColor() == dgTreeNode::BLACK)) &&
742 (!ptr->m_right || (ptr->m_right->GetColor() == dgTreeNode::BLACK)))) {
746 if (!ptr->m_left && !ptr->m_right) {
748 for (
dgTreeNode *x = ptr; x; x = x->GetParent()) {
749 if (x->GetColor() == dgTreeNode::BLACK) {
758 if (ptr->m_left && !SanityCheck(ptr->GetLeft(), height)) {
762 if (ptr->m_right && !SanityCheck(ptr->GetRight(), height)) {
769 template<
class OBJECT,
class KEY>
780 template<
class OBJECT,
class KEY>
782 Swap(m_head, tree.m_head);
783 Swap(m_count, tree.m_count);
Definition: dgMemory.h:80