| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398 |
- /****************************************************************************************
-
- Copyright (C) 2015 Autodesk, Inc.
- All rights reserved.
-
- Use of this software is subject to the terms of the Autodesk license agreement
- provided at the time of installation or download, or which otherwise accompanies
- this software in either electronic or hard copy form.
-
- ****************************************************************************************/
- //! \file fbxredblacktree.h
- #ifndef _FBXSDK_CORE_BASE_REDBLACKTREE_H_
- #define _FBXSDK_CORE_BASE_REDBLACKTREE_H_
- #include <fbxsdk/fbxsdk_def.h>
- #include <fbxsdk/core/base/fbxcontainerallocators.h>
- #include <fbxsdk/core/base/fbxpair.h>
- #include <fbxsdk/fbxsdk_nsbegin.h>
- /*****************************************************************************************************************************
- ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
- *****************************************************************************************************************************/
- #ifndef DOXYGEN_SHOULD_SKIP_THIS
- template <typename RecordType> class FbxRedBlackConstIterator;
- template <typename RecordType> class FbxRedBlackIterator
- {
- public:
- FbxRedBlackIterator() : mRecord(0) {}
- FbxRedBlackIterator(RecordType* pRecord) : mRecord(pRecord) {}
- FbxRedBlackIterator(const FbxRedBlackIterator<RecordType>& pV) : mRecord(pV.mRecord) {}
- FbxRedBlackIterator& operator++()
- {
- FBX_ASSERT( mRecord != NULL );
- mRecord = mRecord->Successor();
- return *this;
- }
- const FbxRedBlackIterator operator++(int)
- {
- FbxRedBlackIterator t(*this);
- operator++();
- return t;
- }
- FbxRedBlackIterator& operator+=(int pCount)
- {
- FBX_ASSERT( mRecord != NULL );
- for( int i = 0; i < pCount; ++i )
- {
- if( !mRecord ) break;
- mRecord = mRecord->Successor();
- }
- return *this;
- }
- FbxRedBlackIterator& operator--()
- {
- FBX_ASSERT( mRecord );
- mRecord = mRecord->Predecessor();
- return *this;
- }
- const FbxRedBlackIterator operator--(int)
- {
- FbxRedBlackIterator t(*this);
- operator--();
- return t;
- }
- FbxRedBlackIterator& operator-=(int pCount)
- {
- FBX_ASSERT( mRecord != NULL );
- for( int i = 0; i < pCount; ++i )
- {
- if( !mRecord ) break;
- mRecord = mRecord->Predecessor();
- }
- return *this;
- }
- const RecordType& operator*() const
- {
- FBX_ASSERT( mRecord );
- return *mRecord;
- }
- RecordType& operator*()
- {
- FBX_ASSERT( mRecord );
- return *mRecord;
- }
- const RecordType* operator->() const
- {
- FBX_ASSERT( mRecord );
- return mRecord;
- }
- RecordType* operator->()
- {
- FBX_ASSERT( mRecord );
- return mRecord;
- }
- inline bool operator==(const FbxRedBlackIterator& pOther) const
- {
- return mRecord == pOther.mRecord;
- }
- inline bool operator !=(const FbxRedBlackIterator& pOther) const
- {
- return mRecord != pOther.mRecord;
- }
- protected:
- RecordType* mRecord;
- friend class FbxRedBlackConstIterator<RecordType>;
- };
- template <typename RecordType> class FbxRedBlackConstIterator
- {
- public:
- FbxRedBlackConstIterator() : mRecord(0) {}
- FbxRedBlackConstIterator(const RecordType* pRecord) : mRecord(pRecord) {}
- FbxRedBlackConstIterator(const FbxRedBlackIterator<RecordType>& pV) : mRecord(pV.mRecord) {}
- FbxRedBlackConstIterator(const FbxRedBlackConstIterator<RecordType>& pV) : mRecord(pV.mRecord) {}
- FbxRedBlackConstIterator & operator++()
- {
- FBX_ASSERT( mRecord != NULL );
- mRecord = mRecord->Successor();
- return *this;
- }
- const FbxRedBlackConstIterator operator++(int)
- {
- FbxRedBlackConstIterator t(*this);
- operator++();
- return t;
- }
- FbxRedBlackConstIterator& operator+=(int pCount)
- {
- FBX_ASSERT( mRecord != NULL );
- for( int i = 0; i < pCount; ++i )
- {
- if( !mRecord ) break;
- mRecord = mRecord->Successor();
- }
- return *this;
- }
- FbxRedBlackConstIterator & operator--()
- {
- FBX_ASSERT( mRecord );
- mRecord = mRecord->Predecessor();
- return *this;
- }
- const FbxRedBlackConstIterator operator--(int)
- {
- FbxRedBlackConstIterator t(*this);
- operator--();
- return t;
- }
- FbxRedBlackConstIterator& operator-=(int pCount)
- {
- FBX_ASSERT( mRecord != NULL );
- for( int i = 0; i < pCount; ++i )
- {
- if( !mRecord ) break;
- mRecord = mRecord->Predecessor();
- }
- return *this;
- }
- const RecordType& operator*() const
- {
- FBX_ASSERT( mRecord );
- return *mRecord;
- }
- const RecordType& operator*()
- {
- FBX_ASSERT( mRecord );
- return *mRecord;
- }
- const RecordType* operator->() const
- {
- FBX_ASSERT( mRecord );
- return mRecord;
- }
- const RecordType* operator->()
- {
- FBX_ASSERT( mRecord );
- return mRecord;
- }
- inline bool operator==(const FbxRedBlackConstIterator& pOther) const
- {
- return mRecord == pOther.mRecord;
- }
- inline bool operator !=(const FbxRedBlackConstIterator& pOther) const
- {
- return mRecord != pOther.mRecord;
- }
- protected:
- const RecordType* mRecord;
- friend class FbxRedBlackIterator<RecordType>;
- };
- //! Implements an efficient ordered data storage.
- template <typename Type, typename Compare, typename Allocator> class FbxRedBlackTree
- {
- public:
- typedef Type DataType;
- typedef typename Type::KeyType KeyType;
- typedef typename Type::ConstKeyType ConstKeyType;
- typedef typename Type::ValueType ValueType;
- typedef typename Type::ConstValueType ConstValueType;
- typedef Allocator AllocatorType;
- /**
- This class represents a node in the tree. It contains the key,
- the value, and internal tree management data.
- */
- class RecordType
- {
- public:
- inline ConstKeyType& GetKey() const { return mData.GetKey(); }
- inline ConstValueType& GetValue() const { return mData.GetValue(); }
- inline ValueType& GetValue() { return mData.GetValue(); }
- inline const RecordType* Minimum() const
- {
- const RecordType* lParent = 0;
- const RecordType* lNode = this;
- while (lNode != 0)
- {
- lParent = lNode;
- lNode = lNode->mLeftChild;
- }
- return lParent;
- }
- inline RecordType* Minimum()
- {
- RecordType* lParent = 0;
- RecordType* lNode = this;
- while (lNode != 0)
- {
- lParent = lNode;
- lNode = lNode->mLeftChild;
- }
- return lParent;
- }
- inline const RecordType* Maximum() const
- {
- const RecordType* lParent = 0;
- const RecordType* lNode = this;
- while (lNode != 0)
- {
- lParent = lNode;
- lNode = lNode->mRightChild;
- }
- return lParent;
- }
- inline RecordType* Maximum()
- {
- RecordType* lParent = 0;
- RecordType* lNode = this;
- while (lNode != 0)
- {
- lParent = lNode;
- lNode = lNode->mRightChild;
- }
- return lParent;
- }
- inline const RecordType* Predecessor() const
- {
- if (mLeftChild)
- {
- return mLeftChild->Maximum();
- }
- else
- {
- const RecordType* lParent = mParent;
- const RecordType* lNode = this;
- while (lParent && lParent->mLefttChild == lNode)
- {
- lNode = lParent;
- lParent = lParent->mParent;
- }
- return lParent;
- }
- }
- inline RecordType* Predecessor()
- {
- if (mLeftChild)
- {
- return mLeftChild->Maximum();
- }
- else
- {
- RecordType* lParent = mParent;
- RecordType* lNode = this;
- while (lParent && lParent->mLeftChild == lNode)
- {
- lNode = lParent;
- lParent = lParent->mParent;
- }
- return lParent;
- }
- }
- inline const RecordType* Successor() const
- {
- if (mRightChild)
- {
- return mRightChild->Minimum();
- }
- else
- {
- const RecordType* lParent = mParent;
- const RecordType* lNode = this;
- while (lParent && lParent->mRightChild == lNode)
- {
- lNode = lParent;
- lParent = lParent->mParent;
- }
- return lParent;
- }
- }
- inline RecordType* Successor()
- {
- if (mRightChild)
- {
- return mRightChild->Minimum();
- }
- else
- {
- RecordType* lParent = mParent;
- RecordType* lNode = this;
- while (lParent && lParent->mRightChild == lNode)
- {
- lNode = lParent;
- lParent = lParent->mParent;
- }
- return lParent;
- }
- }
- inline const int GetBlackDepth() { return mBlackDepth; }
- private:
- enum ETreeType {eRed, eBlack};
- inline RecordType(const DataType& pData)
- : mData(pData)
- , mParent(0)
- , mLeftChild(0)
- , mRightChild(0)
- , mColor(eRed)
- , mBlackDepth(0)
- {
- }
- inline RecordType(const RecordType& pRecordType)
- : mData(pRecordType.mData)
- , mParent(0)
- , mLeftChild(0)
- , mRightChild(0)
- , mColor(pRecordType.mColor)
- , mBlackDepth(pRecordType.mBlackDepth)
- {
- }
- DataType mData;
- friend class FbxRedBlackTree;
- RecordType* mParent;
- RecordType* mLeftChild;
- RecordType* mRightChild;
- unsigned int mColor:2;
- unsigned int mBlackDepth:30;
- };
- public:
- typedef FbxRedBlackConstIterator<RecordType> ConstIteratorType;
- typedef FbxRedBlackIterator<RecordType> IteratorType;
- inline FbxRedBlackTree() : mRoot(0), mSize(0), mAllocator(sizeof(RecordType)) {}
- inline FbxRedBlackTree(const FbxRedBlackTree& pTree) : mRoot(0), mSize(0), mAllocator(sizeof(RecordType)) { operator=(pTree); }
- inline ~FbxRedBlackTree() { Clear(); }
- /** Deep copy pTree in this.
- * \param pTree The tree to copy in this tree. */
- inline FbxRedBlackTree& operator=(const FbxRedBlackTree& pTree)
- {
- if( this != &pTree )
- {
- Clear();
- mAllocator = pTree.mAllocator;
- if( pTree.mRoot )
- {
- void* lBuffer = mAllocator.AllocateRecords();
- mRoot = new(lBuffer) RecordType(*(pTree.mRoot)); //in-place new won't allocate memory, so it is safe
- mRoot->mLeftChild = DuplicateSubTree(pTree.mRoot->mLeftChild);
- mRoot->mRightChild = DuplicateSubTree(pTree.mRoot->mRightChild);
- if (mRoot->mLeftChild)
- {
- mRoot->mLeftChild->mParent = mRoot;
- }
- if (mRoot->mRightChild)
- {
- mRoot->mRightChild->mParent = mRoot;
- }
- }
- else
- {
- FBX_ASSERT( pTree.mSize == 0 );
- FBX_ASSERT( mRoot == 0 );
- }
- mSize = pTree.mSize;
- }
- return *this;
- }
- inline bool operator==(const FbxRedBlackTree& pTree) const
- {
- // Check a few quick shortcuts
- if( this == &pTree )
- return true;
- if( GetSize() != pTree.GetSize() )
- return false;
- // Iterator through all nodes; if we reach the end of both iterators at the same
- // time then we have two iterators that match.
- ConstIteratorType End;
- ConstIteratorType Iter1(Minimum());
- ConstIteratorType Iter2(pTree.Minimum());
- while( (Iter1 != End) && (Iter2 != End) &&
- (Iter1->GetKey() == Iter2->GetKey()) &&
- (Iter1->GetValue() == Iter2->GetValue()) )
- {
- ++Iter1;
- ++Iter2;
- }
- return Iter1 == End && Iter2 == End;
- }
- /** Ask Allocator to reserve space to hold pRecordCount elements.
- * \param pRecordCount
- */
- inline void Reserve(unsigned int pRecordCount)
- {
- mAllocator.Reserve(pRecordCount);
- }
- /** Get the number of elements in the tree. Takes O(1) time.
- * \return The number of elements in the tree.
- */
- inline int GetSize() const { return mSize; }
- inline bool Empty() const { return mSize == 0; }
- /** Insert a new element in the tree. Takes O(log n) time.
- * \param pData The element to insert.
- * \return If pData.GetKey() is already present in the tree, returns the
- * existing record and false; else returns the new record and true.
- */
- inline FbxPair<RecordType*, bool> Insert(const DataType& pData)
- {
- Compare lCompareKeys;
- bool lResult = false;
- RecordType* lParent = 0;
- RecordType* lNode = mRoot;
- while (lNode != 0)
- {
- const KeyType& lNodeKey = lNode->GetKey();
- const KeyType& lDataKey = pData.GetKey();
- if (lCompareKeys(lNodeKey, lDataKey) < 0)
- {
- lParent = lNode;
- lNode = lNode->mRightChild;
- }
- else if (lCompareKeys(lNodeKey, lDataKey) > 0)
- {
- lParent = lNode;
- lNode = lNode->mLeftChild;
- }
- else
- {
- break;
- }
- }
- if (lNode == 0)
- {
- void* lBuffer = mAllocator.AllocateRecords();
- lNode = new(lBuffer) RecordType(pData); //in-place new won't allocate memory, so it is safe
- mSize++;
- FBX_ASSERT(lNode == lBuffer);
- if (lParent)
- {
- if (lCompareKeys(lParent->GetKey(), pData.GetKey()) < 0)
- {
- FBX_ASSERT(lParent->mRightChild == 0);
- lParent->mRightChild = lNode;
- lNode->mParent = lParent;
- }
- else
- {
- FBX_ASSERT(lParent->mLeftChild == 0);
- lParent->mLeftChild = lNode;
- lNode->mParent = lParent;
- }
- }
- else
- {
- mRoot = lNode;
- }
- // Fix red black tree property
- FixNodesAfterInsertion(lNode);
- lResult = true;
- }
- return FbxPair<RecordType*, bool>(lNode, lResult);
- }
- /** Remove an element identified by a key from the tree. Takes O(log n) time.
- * \param pKey The key identifying the element to remove.
- */
- inline bool Remove(const KeyType& pKey)
- {
- Compare lCompareKeys;
- bool lResult = false;
- RecordType* lNode = mRoot;
- while (lNode != 0)
- {
- if (lCompareKeys(lNode->GetKey(), pKey) < 0)
- {
- lNode = lNode->mRightChild;
- }
- else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
- {
- lNode = lNode->mLeftChild;
- }
- else
- {
- break;
- }
- }
- if (lNode)
- {
- RemoveNode(lNode);
- mSize--;
- lNode->~RecordType();
- mAllocator.FreeMemory(lNode);
- lResult = true;
- }
- return lResult;
- }
- /** Remove all elements from the tree. Takes O(n) time. Recursive.
- */
- inline void Clear()
- {
- if (mRoot)
- {
- ClearSubTree(mRoot->mLeftChild);
- ClearSubTree(mRoot->mRightChild);
- mRoot->~RecordType();
- mAllocator.FreeMemory(mRoot);
- mRoot = 0;
- mSize = 0;
- }
- }
- /** Find the smallest element in the tree.
- * Takes O(log n) time.
- */
- inline const RecordType* Minimum() const
- {
- if (0 != mRoot)
- {
- return mRoot->Minimum();
- }
- else
- {
- return 0;
- }
- }
- /** Find the smallest element in the tree.
- * Takes O(log n) time.
- */
- inline RecordType* Minimum()
- {
- if (0 != mRoot)
- {
- return mRoot->Minimum();
- }
- else
- {
- return 0;
- }
- }
- /** Find the largest element in the tree.
- * Takes O(log n) time.
- */
- inline const RecordType* Maximum() const
- {
- if (0 != mRoot)
- {
- return mRoot->Maximum();
- }
- else
- {
- return 0;
- }
- }
- /** Find the largest element in the tree.
- * Takes O(log n) time.
- */
- inline RecordType* Maximum()
- {
- if (0 != mRoot)
- {
- return mRoot->Maximum();
- }
- else
- {
- return 0;
- }
- }
- /** Find the key-value pair with key pKey.
- * Takes O(log n) time.
- * \param pKey The key to look for.
- */
- inline const RecordType* Find(const KeyType& pKey) const
- {
- Compare lCompareKeys;
- const RecordType* lNode = mRoot;
- while (lNode != 0)
- {
- if (lCompareKeys(lNode->GetKey(), pKey) < 0)
- {
- lNode = lNode->mRightChild;
- }
- else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
- {
- lNode = lNode->mLeftChild;
- }
- else
- {
- break;
- }
- }
- return lNode;
- }
- /** Find the key-value pair with key pKey.
- * Takes O(log n) time.
- * \param pKey The key to look for.
- */
- inline RecordType* Find(const KeyType& pKey)
- {
- Compare lCompareKeys;
- RecordType* lNode = mRoot;
- while (lNode != 0)
- {
- if (lCompareKeys(lNode->GetKey(), pKey) < 0)
- {
- lNode = lNode->mRightChild;
- }
- else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
- {
- lNode = lNode->mLeftChild;
- }
- else
- {
- break;
- }
- }
- return lNode;
- }
- /** Find the key-value pair with the smallest key greater than pKey.
- * Takes O(log n) time.
- * \param pKey The key to look for.
- */
- inline const RecordType* UpperBound(const KeyType& pKey) const
- {
- Compare lCompareKeys;
- const RecordType* lNode = mRoot;
- const RecordType* lCandidate = 0;
- while (lNode != 0)
- {
- if (lCompareKeys(lNode->GetKey(), pKey) <= 0)
- {
- lNode = lNode->mRightChild;
- }
- else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
- {
- lCandidate = lNode;
- lNode = lNode->mLeftChild;
- }
- }
-
- return lCandidate;
- }
- /** Find the key-value pair with the smallest key greater than pKey.
- * Takes O(log n) time.
- * \param pKey The key to look for.
- */
- inline RecordType* UpperBound(const KeyType& pKey)
- {
- Compare lCompareKeys;
- RecordType* lNode = mRoot;
- RecordType* lCandidate = 0;
- while (lNode != 0)
- {
- if (lCompareKeys(lNode->GetKey(), pKey) <= 0)
- {
- lNode = lNode->mRightChild;
- }
- else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
- {
- lCandidate = lNode;
- lNode = lNode->mLeftChild;
- }
- }
-
- return lCandidate;
- }
- protected:
- RecordType* mRoot;
- int mSize;
- AllocatorType mAllocator;
- inline RecordType* DuplicateSubTree(const RecordType* pNode)
- {
- RecordType* lNewSubTree = 0;
- if (pNode)
- {
- void* lBuffer = mAllocator.AllocateRecords();
- lNewSubTree = new(lBuffer) RecordType(*pNode); //in-place new won't allocate memory, so it is safe
- lNewSubTree->mLeftChild = DuplicateSubTree(pNode->mLeftChild);
- lNewSubTree->mRightChild = DuplicateSubTree(pNode->mRightChild);
- if (lNewSubTree->mLeftChild)
- {
- lNewSubTree->mLeftChild->mParent = lNewSubTree;
- }
- if (lNewSubTree->mRightChild)
- {
- lNewSubTree->mRightChild->mParent = lNewSubTree;
- }
- }
- return lNewSubTree;
- }
- inline void FixNodesAfterInsertion(RecordType* pNode)
- {
- RecordType* lNode = pNode;
- bool lDone = false;
- while (!lDone)
- {
- lDone = true;
- if (lNode->mParent == 0)
- {
- lNode->mColor = RecordType::eBlack;
- }
- else if (lNode->mParent->mColor == RecordType::eRed)
- {
- RecordType* lUncle = 0;
- if (lNode->mParent == lNode->mParent->mParent->mLeftChild)
- {
- lUncle = lNode->mParent->mParent->mRightChild;
- }
- else if (lNode->mParent == lNode->mParent->mParent->mRightChild)
- {
- lUncle = lNode->mParent->mParent->mLeftChild;
- }
- // since lNode->mParent is red, lNode->mParent->mParent exists
- if (lUncle && lUncle->mColor == RecordType::eRed)
- {
- lNode->mParent->mColor = RecordType::eBlack;
- lUncle->mColor = RecordType::eBlack;
- lNode->mParent->mParent->mColor = RecordType::eRed;
- lNode = lNode->mParent->mParent;
- lDone = false;
- }
- else
- {
- if ((lNode == lNode->mParent->mRightChild) &&
- (lNode->mParent == lNode->mParent->mParent->mLeftChild))
- {
- LeftRotate(lNode->mParent);
- lNode = lNode->mLeftChild;
- }
- else if ((lNode == lNode->mParent->mLeftChild) &&
- (lNode->mParent == lNode->mParent->mParent->mRightChild))
- {
- RightRotate(lNode->mParent);
- lNode = lNode->mRightChild;
- }
- lNode->mParent->mColor = RecordType::eBlack;
- lNode->mParent->mParent->mColor = RecordType::eRed;
- if ((lNode == lNode->mParent->mLeftChild) &&
- (lNode->mParent == lNode->mParent->mParent->mLeftChild))
- {
- RightRotate(lNode->mParent->mParent);
- }
- else
- {
- LeftRotate(lNode->mParent->mParent);
- }
- }
- }
- }
- mRoot->mColor = RecordType::eBlack;
- }
- inline void LeftRotate(RecordType* pNode)
- {
- FBX_ASSERT_RETURN(pNode);
- RecordType* lNode = pNode->mRightChild;
- FBX_ASSERT_RETURN(lNode);
- #ifdef _DEBUG
- RecordType* A = pNode->mLeftChild;
- RecordType* B = lNode->mLeftChild;
- RecordType* C = lNode->mRightChild;
- RecordType* Z = pNode->mParent;
- #endif
- pNode->mRightChild = lNode->mLeftChild;
- if (pNode->mRightChild)
- {
- pNode->mRightChild->mParent = pNode;
- }
- lNode->mParent = pNode->mParent;
- if (pNode->mParent == 0)
- {
- FBX_ASSERT(mRoot == pNode);
- mRoot = lNode;
- }
- else if (pNode == pNode->mParent->mLeftChild)
- {
- pNode->mParent->mLeftChild = lNode;
- }
- else
- {
- pNode->mParent->mRightChild = lNode;
- }
- pNode->mParent = lNode;
- lNode->mLeftChild = pNode;
- FBX_ASSERT(pNode->mLeftChild == A);
- FBX_ASSERT(pNode->mRightChild == B);
- FBX_ASSERT(pNode->mParent == lNode);
- FBX_ASSERT(lNode->mLeftChild == pNode);
- FBX_ASSERT(lNode->mRightChild == C);
- FBX_ASSERT(lNode->mParent == Z);
- FBX_ASSERT(A == 0 || A->mParent == pNode);
- FBX_ASSERT(B == 0 || B->mParent == pNode);
- FBX_ASSERT(C == 0 || C->mParent == lNode);
- FBX_ASSERT(Z == 0 || Z->mLeftChild == lNode || Z->mRightChild == lNode);
- }
- inline void RightRotate(RecordType* pNode)
- {
- RecordType* lNode = pNode->mLeftChild;
- #ifdef _DEBUG
- RecordType* A = lNode->mLeftChild;
- RecordType* B = lNode->mRightChild;
- RecordType* C = pNode->mRightChild;
- RecordType* Z = pNode->mParent;
- #endif
- pNode->mLeftChild = lNode->mRightChild;
- if (pNode->mLeftChild)
- {
- pNode->mLeftChild->mParent = pNode;
- }
- lNode->mParent = pNode->mParent;
- if (pNode->mParent == 0)
- {
- FBX_ASSERT(mRoot == pNode);
- mRoot = lNode;
- }
- else if (pNode == pNode->mParent->mRightChild)
- {
- pNode->mParent->mRightChild = lNode;
- }
- else
- {
- pNode->mParent->mLeftChild = lNode;
- }
- pNode->mParent = lNode;
- lNode->mRightChild = pNode;
- FBX_ASSERT(lNode->mLeftChild == A);
- FBX_ASSERT(lNode->mRightChild == pNode);
- FBX_ASSERT(lNode->mParent == Z);
- FBX_ASSERT(pNode->mLeftChild == B);
- FBX_ASSERT(pNode->mRightChild == C);
- FBX_ASSERT(pNode->mParent == lNode);
- FBX_ASSERT(A == 0 || A->mParent == lNode);
- FBX_ASSERT(B == 0 || B->mParent == pNode);
- FBX_ASSERT(C == 0 || C->mParent == pNode);
- FBX_ASSERT(Z == 0 || Z->mLeftChild == lNode || Z->mRightChild == lNode);
- }
- inline void RemoveNode(RecordType* pNode)
- {
- if (pNode->mLeftChild == 0)
- {
- if (pNode->mRightChild == 0)
- {
- if (pNode->mParent)
- {
- if (pNode->mParent->mLeftChild == pNode)
- {
- pNode->mParent->mLeftChild = 0;
- }
- else if (pNode->mParent->mRightChild == pNode)
- {
- pNode->mParent->mRightChild = 0;
- }
- else
- {
- FBX_ASSERT_NOW("Node not found in FbxRedBlackTree");
- }
- }
- else
- {
- FBX_ASSERT(mRoot == pNode);
- mRoot = 0;
- }
- if (pNode->mColor == RecordType::eBlack)
- {
- FixNodesAfterRemoval(pNode->mParent, 0);
- }
- }
- else
- {
- if (pNode->mParent)
- {
- if (pNode->mParent->mLeftChild == pNode)
- {
- pNode->mParent->mLeftChild = pNode->mRightChild;
- pNode->mRightChild->mParent = pNode->mParent;
- }
- else if (pNode->mParent->mRightChild == pNode)
- {
- pNode->mParent->mRightChild = pNode->mRightChild;
- pNode->mRightChild->mParent = pNode->mParent;
- }
- else
- {
- FBX_ASSERT_NOW("Node not found in FbxRedBlackTree");
- }
- }
- else
- {
- FBX_ASSERT(mRoot == pNode);
- mRoot = pNode->mRightChild;
- pNode->mRightChild->mParent = 0;
- }
- if (pNode->mColor == RecordType::eBlack)
- {
- FixNodesAfterRemoval(pNode->mRightChild->mParent, pNode->mRightChild);
- }
- }
- }
- else
- {
- if (pNode->mRightChild == 0)
- {
- if (pNode->mParent)
- {
- if (pNode->mParent->mLeftChild == pNode)
- {
- pNode->mParent->mLeftChild = pNode->mLeftChild;
- pNode->mLeftChild->mParent = pNode->mParent;
- }
- else if (pNode->mParent->mRightChild == pNode)
- {
- pNode->mParent->mRightChild = pNode->mLeftChild;
- pNode->mLeftChild->mParent = pNode->mParent;
- }
- else
- {
- FBX_ASSERT_NOW("Node not found in FbxRedBlackTree");
- }
- }
- else
- {
- FBX_ASSERT(mRoot == pNode);
- mRoot = pNode->mLeftChild;
- pNode->mLeftChild->mParent = 0;
- }
- if (pNode->mColor == RecordType::eBlack)
- {
- FixNodesAfterRemoval(pNode->mLeftChild->mParent, pNode->mLeftChild);
- }
- }
- else
- {
- RecordType* lMinRightNode = pNode->mRightChild->Minimum();
- RemoveNode(lMinRightNode);
- lMinRightNode->mColor = pNode->mColor;
- ReplaceNode(pNode, lMinRightNode);
- }
- }
- pNode->mParent = 0;
- pNode->mLeftChild = 0;
- pNode->mRightChild = 0;
- }
- inline void ReplaceNode(RecordType* pNodeToReplace, RecordType* pReplacement)
- {
- pReplacement->mParent = pNodeToReplace->mParent;
- if (pNodeToReplace->mParent)
- {
- if (pNodeToReplace->mParent->mLeftChild == pNodeToReplace)
- {
- pNodeToReplace->mParent->mLeftChild = pReplacement;
- }
- else if (pNodeToReplace->mParent->mRightChild == pNodeToReplace)
- {
- pNodeToReplace->mParent->mRightChild = pReplacement;
- }
- }
- else
- {
- FBX_ASSERT(mRoot == pNodeToReplace);
- mRoot = pReplacement;
- }
- pReplacement->mLeftChild = pNodeToReplace->mLeftChild;
- if (pReplacement->mLeftChild)
- {
- pReplacement->mLeftChild->mParent = pReplacement;
- }
- pReplacement->mRightChild = pNodeToReplace->mRightChild;
- if (pReplacement->mRightChild)
- {
- pReplacement->mRightChild->mParent = pReplacement;
- }
- }
- inline RecordType* Sibling(const RecordType* pParent, const RecordType* pNode) const
- {
- if (pParent)
- {
- if (pParent->mLeftChild == pNode)
- {
- return pParent->mRightChild;
- }
- else if (pParent->mRightChild == pNode)
- {
- return pParent->mLeftChild;
- }
- }
- return 0;
- }
- inline bool IsBlack(const RecordType* pNode)
- {
- return ((pNode == 0) || (pNode->mColor == RecordType::eBlack));
- }
- inline void FixNodesAfterRemoval(RecordType* pParent, RecordType* pNode)
- {
- RecordType* lParent = pParent;
- RecordType* lNode = pNode;
- bool lDone = false;
- while (!lDone)
- {
- lDone = true;
- if (!IsBlack(lNode))
- {
- lNode->mColor = RecordType::eBlack;
- }
- else if (lParent != NULL)
- {
- RecordType* lSibling = Sibling(lParent, lNode);
- if (!IsBlack(lSibling))
- {
- lParent->mColor = RecordType::eRed;
- lSibling->mColor = RecordType::eBlack;
- if (lNode == lParent->mLeftChild)
- {
- LeftRotate(lParent);
- }
- else
- {
- RightRotate(lParent);
- }
- // update sibling: it may have change after rotation
- // parent was not affected by this rotation
- lSibling = Sibling(lParent, lNode);
- }
- /* check this for null sibling */
- if (lSibling &&
- IsBlack(lParent) &&
- IsBlack(lSibling) &&
- IsBlack(lSibling->mLeftChild) &&
- IsBlack(lSibling->mRightChild))
- {
- lSibling->mColor = RecordType::eRed;
- lNode = lParent;
- lParent = lParent->mParent;
- lDone = false;
- }
- else
- {
- if (!IsBlack(lParent) &&
- IsBlack(lSibling) &&
- ((lSibling == 0) || IsBlack(lSibling->mLeftChild)) &&
- ((lSibling == 0) || IsBlack(lSibling->mRightChild)))
- {
- if (lSibling)
- {
- lSibling->mColor = RecordType::eRed;
- }
- lParent->mColor = RecordType::eBlack;
- }
- else if( lSibling != 0 )
- {
- if ((lNode == lParent->mLeftChild) &&
- IsBlack(lSibling) &&
- !IsBlack(lSibling->mLeftChild) &&
- IsBlack(lSibling->mRightChild))
- {
- lSibling->mColor = RecordType::eRed;
- lSibling->mLeftChild->mColor = RecordType::eBlack;
- RightRotate(lSibling);
- }
- else if ((lNode == lParent->mRightChild) &&
- IsBlack(lSibling) &&
- IsBlack(lSibling->mLeftChild) &&
- !IsBlack(lSibling->mRightChild))
- {
- lSibling->mColor = RecordType::eRed;
- lSibling->mRightChild->mColor = RecordType::eBlack;
- LeftRotate(lSibling);
- }
- // update sibling: it may have change after rotation
- lSibling = Sibling(lParent, lNode);
- FBX_ASSERT(lSibling != 0 && lParent != 0); // lSibling is now
- // the former red
- // child of the
- // former sibling
- if( lSibling != 0 && lParent != 0 )
- {
- lSibling->mColor = lParent->mColor;
- lParent->mColor = RecordType::eBlack;
- if (lNode == lParent->mLeftChild)
- {
- if (lSibling->mRightChild)
- {
- lSibling->mRightChild->mColor = RecordType::eBlack;
- }
- LeftRotate(lParent);
- }
- else
- {
- if (lSibling->mLeftChild)
- {
- lSibling->mLeftChild->mColor = RecordType::eBlack;
- }
- RightRotate(lParent);
- }
- }
- }
- }
- }
- }
- if (mRoot)
- {
- mRoot->mColor = RecordType::eBlack;
- }
- }
- inline void ClearSubTree(RecordType* pNode)
- {
- if (pNode)
- {
- ClearSubTree(pNode->mLeftChild);
- ClearSubTree(pNode->mRightChild);
- pNode->~RecordType();
- mAllocator.FreeMemory(pNode);
- }
- }
- inline int GetSubTreeSize(RecordType* pNode) const
- {
- if (pNode)
- {
- return GetSubTreeSize(pNode->mLeftChild) + GetSubTreeSize(pNode->mRightChild) + 1;
- }
- else
- {
- return 0;
- }
- }
- #if 0
- inline void IsSane()
- {
- FBX_ASSERT((mRoot == 0) || (mRoot->mColor == RecordType::eBlack));
- FBX_ASSERT(((mRoot == 0) && (mSize == 0)) || (mRoot != 0) && (mSize != 0));
- IsSubTreeSane(mRoot);
- ComputeBlackDepth(mRoot, 0);
- RecordType* lNode = mRoot;
- unsigned int lLeafBlackDepth = 0;
- while (lNode)
- {
- if (lNode->mLeftChild == 0)
- {
- lLeafBlackDepth = lNode->mBlackDepth + ((lNode->mColor == RecordType::eBlack) ? 1 : 0);
- }
- lNode = lNode->mLeftChild;
- }
- CheckLeavesBlackDepth(mRoot, lLeafBlackDepth);
- }
- inline void IsSubTreeSane(const RecordType* pNode) const
- {
- Compare lCompareKeys;
- if (pNode)
- {
- FBX_ASSERT(pNode != pNode->mParent);
- FBX_ASSERT(pNode != pNode->mLeftChild);
- FBX_ASSERT(pNode != pNode->mRightChild);
- // Check for two consecutive red nodes
- FBX_ASSERT((pNode->mColor == RecordType::eBlack) ||
- (pNode->mLeftChild == NULL) ||
- (pNode->mLeftChild->mColor == RecordType::eBlack));
- FBX_ASSERT((pNode->mColor == RecordType::eBlack) ||
- (pNode->mRightChild == NULL) ||
- (pNode->mRightChild->mColor == RecordType::eBlack));
- // Check key ordering
- FBX_ASSERT((pNode->mLeftChild == 0 ||
- lCompareKeys(pNode->GetKey(), pNode->mLeftChild->GetKey()) > 0));
- FBX_ASSERT((pNode->mRightChild == 0 ||
- lCompareKeys(pNode->GetKey(), pNode->mRightChild->GetKey()) < 0));
- IsSubTreeSane(pNode->mLeftChild);
- IsSubTreeSane(pNode->mRightChild);
- }
- }
- inline void ComputeBlackDepth(RecordType* pNode, unsigned int pDepth)
- {
- if( pNode )
- {
- pNode->mBlackDepth = pDepth;
- if( pNode->mColor == RecordType::eBlack )
- {
- pDepth++;
- }
- ComputeBlackDepth(pNode->mLeftChild, pDepth);
- ComputeBlackDepth(pNode->mRightChild, pDepth);
- }
- }
- inline void CheckLeavesBlackDepth(RecordType* pNode, unsigned int pBlackDepth)
- {
- if( pNode )
- {
- if( pNode->mLeftChild == 0 || pNode->mRightChild == 0 )
- {
- FBX_ASSERT((pNode->mBlackDepth + ((pNode->mColor == RecordType::eBlack) ? 1 : 0)) == pBlackDepth);
- }
- CheckLeavesBlackDepth(pNode->mLeftChild, pBlackDepth);
- CheckLeavesBlackDepth(pNode->mRightChild, pBlackDepth);
- }
- }
- #endif
- };
- #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
- #include <fbxsdk/fbxsdk_nsend.h>
- #endif /*_FBXSDK_CORE_BASE_REDBLACKTREE_H_ */
|