fbxredblacktree.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /****************************************************************************************
  2. Copyright (C) 2015 Autodesk, Inc.
  3. All rights reserved.
  4. Use of this software is subject to the terms of the Autodesk license agreement
  5. provided at the time of installation or download, or which otherwise accompanies
  6. this software in either electronic or hard copy form.
  7. ****************************************************************************************/
  8. //! \file fbxredblacktree.h
  9. #ifndef _FBXSDK_CORE_BASE_REDBLACKTREE_H_
  10. #define _FBXSDK_CORE_BASE_REDBLACKTREE_H_
  11. #include <fbxsdk/fbxsdk_def.h>
  12. #include <fbxsdk/core/base/fbxcontainerallocators.h>
  13. #include <fbxsdk/core/base/fbxpair.h>
  14. #include <fbxsdk/fbxsdk_nsbegin.h>
  15. /*****************************************************************************************************************************
  16. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  17. *****************************************************************************************************************************/
  18. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  19. template <typename RecordType> class FbxRedBlackConstIterator;
  20. template <typename RecordType> class FbxRedBlackIterator
  21. {
  22. public:
  23. FbxRedBlackIterator() : mRecord(0) {}
  24. FbxRedBlackIterator(RecordType* pRecord) : mRecord(pRecord) {}
  25. FbxRedBlackIterator(const FbxRedBlackIterator<RecordType>& pV) : mRecord(pV.mRecord) {}
  26. FbxRedBlackIterator& operator++()
  27. {
  28. FBX_ASSERT( mRecord != NULL );
  29. mRecord = mRecord->Successor();
  30. return *this;
  31. }
  32. const FbxRedBlackIterator operator++(int)
  33. {
  34. FbxRedBlackIterator t(*this);
  35. operator++();
  36. return t;
  37. }
  38. FbxRedBlackIterator& operator+=(int pCount)
  39. {
  40. FBX_ASSERT( mRecord != NULL );
  41. for( int i = 0; i < pCount; ++i )
  42. {
  43. if( !mRecord ) break;
  44. mRecord = mRecord->Successor();
  45. }
  46. return *this;
  47. }
  48. FbxRedBlackIterator& operator--()
  49. {
  50. FBX_ASSERT( mRecord );
  51. mRecord = mRecord->Predecessor();
  52. return *this;
  53. }
  54. const FbxRedBlackIterator operator--(int)
  55. {
  56. FbxRedBlackIterator t(*this);
  57. operator--();
  58. return t;
  59. }
  60. FbxRedBlackIterator& operator-=(int pCount)
  61. {
  62. FBX_ASSERT( mRecord != NULL );
  63. for( int i = 0; i < pCount; ++i )
  64. {
  65. if( !mRecord ) break;
  66. mRecord = mRecord->Predecessor();
  67. }
  68. return *this;
  69. }
  70. const RecordType& operator*() const
  71. {
  72. FBX_ASSERT( mRecord );
  73. return *mRecord;
  74. }
  75. RecordType& operator*()
  76. {
  77. FBX_ASSERT( mRecord );
  78. return *mRecord;
  79. }
  80. const RecordType* operator->() const
  81. {
  82. FBX_ASSERT( mRecord );
  83. return mRecord;
  84. }
  85. RecordType* operator->()
  86. {
  87. FBX_ASSERT( mRecord );
  88. return mRecord;
  89. }
  90. inline bool operator==(const FbxRedBlackIterator& pOther) const
  91. {
  92. return mRecord == pOther.mRecord;
  93. }
  94. inline bool operator !=(const FbxRedBlackIterator& pOther) const
  95. {
  96. return mRecord != pOther.mRecord;
  97. }
  98. protected:
  99. RecordType* mRecord;
  100. friend class FbxRedBlackConstIterator<RecordType>;
  101. };
  102. template <typename RecordType> class FbxRedBlackConstIterator
  103. {
  104. public:
  105. FbxRedBlackConstIterator() : mRecord(0) {}
  106. FbxRedBlackConstIterator(const RecordType* pRecord) : mRecord(pRecord) {}
  107. FbxRedBlackConstIterator(const FbxRedBlackIterator<RecordType>& pV) : mRecord(pV.mRecord) {}
  108. FbxRedBlackConstIterator(const FbxRedBlackConstIterator<RecordType>& pV) : mRecord(pV.mRecord) {}
  109. FbxRedBlackConstIterator & operator++()
  110. {
  111. FBX_ASSERT( mRecord != NULL );
  112. mRecord = mRecord->Successor();
  113. return *this;
  114. }
  115. const FbxRedBlackConstIterator operator++(int)
  116. {
  117. FbxRedBlackConstIterator t(*this);
  118. operator++();
  119. return t;
  120. }
  121. FbxRedBlackConstIterator& operator+=(int pCount)
  122. {
  123. FBX_ASSERT( mRecord != NULL );
  124. for( int i = 0; i < pCount; ++i )
  125. {
  126. if( !mRecord ) break;
  127. mRecord = mRecord->Successor();
  128. }
  129. return *this;
  130. }
  131. FbxRedBlackConstIterator & operator--()
  132. {
  133. FBX_ASSERT( mRecord );
  134. mRecord = mRecord->Predecessor();
  135. return *this;
  136. }
  137. const FbxRedBlackConstIterator operator--(int)
  138. {
  139. FbxRedBlackConstIterator t(*this);
  140. operator--();
  141. return t;
  142. }
  143. FbxRedBlackConstIterator& operator-=(int pCount)
  144. {
  145. FBX_ASSERT( mRecord != NULL );
  146. for( int i = 0; i < pCount; ++i )
  147. {
  148. if( !mRecord ) break;
  149. mRecord = mRecord->Predecessor();
  150. }
  151. return *this;
  152. }
  153. const RecordType& operator*() const
  154. {
  155. FBX_ASSERT( mRecord );
  156. return *mRecord;
  157. }
  158. const RecordType& operator*()
  159. {
  160. FBX_ASSERT( mRecord );
  161. return *mRecord;
  162. }
  163. const RecordType* operator->() const
  164. {
  165. FBX_ASSERT( mRecord );
  166. return mRecord;
  167. }
  168. const RecordType* operator->()
  169. {
  170. FBX_ASSERT( mRecord );
  171. return mRecord;
  172. }
  173. inline bool operator==(const FbxRedBlackConstIterator& pOther) const
  174. {
  175. return mRecord == pOther.mRecord;
  176. }
  177. inline bool operator !=(const FbxRedBlackConstIterator& pOther) const
  178. {
  179. return mRecord != pOther.mRecord;
  180. }
  181. protected:
  182. const RecordType* mRecord;
  183. friend class FbxRedBlackIterator<RecordType>;
  184. };
  185. //! Implements an efficient ordered data storage.
  186. template <typename Type, typename Compare, typename Allocator> class FbxRedBlackTree
  187. {
  188. public:
  189. typedef Type DataType;
  190. typedef typename Type::KeyType KeyType;
  191. typedef typename Type::ConstKeyType ConstKeyType;
  192. typedef typename Type::ValueType ValueType;
  193. typedef typename Type::ConstValueType ConstValueType;
  194. typedef Allocator AllocatorType;
  195. /**
  196. This class represents a node in the tree. It contains the key,
  197. the value, and internal tree management data.
  198. */
  199. class RecordType
  200. {
  201. public:
  202. inline ConstKeyType& GetKey() const { return mData.GetKey(); }
  203. inline ConstValueType& GetValue() const { return mData.GetValue(); }
  204. inline ValueType& GetValue() { return mData.GetValue(); }
  205. inline const RecordType* Minimum() const
  206. {
  207. const RecordType* lParent = 0;
  208. const RecordType* lNode = this;
  209. while (lNode != 0)
  210. {
  211. lParent = lNode;
  212. lNode = lNode->mLeftChild;
  213. }
  214. return lParent;
  215. }
  216. inline RecordType* Minimum()
  217. {
  218. RecordType* lParent = 0;
  219. RecordType* lNode = this;
  220. while (lNode != 0)
  221. {
  222. lParent = lNode;
  223. lNode = lNode->mLeftChild;
  224. }
  225. return lParent;
  226. }
  227. inline const RecordType* Maximum() const
  228. {
  229. const RecordType* lParent = 0;
  230. const RecordType* lNode = this;
  231. while (lNode != 0)
  232. {
  233. lParent = lNode;
  234. lNode = lNode->mRightChild;
  235. }
  236. return lParent;
  237. }
  238. inline RecordType* Maximum()
  239. {
  240. RecordType* lParent = 0;
  241. RecordType* lNode = this;
  242. while (lNode != 0)
  243. {
  244. lParent = lNode;
  245. lNode = lNode->mRightChild;
  246. }
  247. return lParent;
  248. }
  249. inline const RecordType* Predecessor() const
  250. {
  251. if (mLeftChild)
  252. {
  253. return mLeftChild->Maximum();
  254. }
  255. else
  256. {
  257. const RecordType* lParent = mParent;
  258. const RecordType* lNode = this;
  259. while (lParent && lParent->mLefttChild == lNode)
  260. {
  261. lNode = lParent;
  262. lParent = lParent->mParent;
  263. }
  264. return lParent;
  265. }
  266. }
  267. inline RecordType* Predecessor()
  268. {
  269. if (mLeftChild)
  270. {
  271. return mLeftChild->Maximum();
  272. }
  273. else
  274. {
  275. RecordType* lParent = mParent;
  276. RecordType* lNode = this;
  277. while (lParent && lParent->mLeftChild == lNode)
  278. {
  279. lNode = lParent;
  280. lParent = lParent->mParent;
  281. }
  282. return lParent;
  283. }
  284. }
  285. inline const RecordType* Successor() const
  286. {
  287. if (mRightChild)
  288. {
  289. return mRightChild->Minimum();
  290. }
  291. else
  292. {
  293. const RecordType* lParent = mParent;
  294. const RecordType* lNode = this;
  295. while (lParent && lParent->mRightChild == lNode)
  296. {
  297. lNode = lParent;
  298. lParent = lParent->mParent;
  299. }
  300. return lParent;
  301. }
  302. }
  303. inline RecordType* Successor()
  304. {
  305. if (mRightChild)
  306. {
  307. return mRightChild->Minimum();
  308. }
  309. else
  310. {
  311. RecordType* lParent = mParent;
  312. RecordType* lNode = this;
  313. while (lParent && lParent->mRightChild == lNode)
  314. {
  315. lNode = lParent;
  316. lParent = lParent->mParent;
  317. }
  318. return lParent;
  319. }
  320. }
  321. inline const int GetBlackDepth() { return mBlackDepth; }
  322. private:
  323. enum ETreeType {eRed, eBlack};
  324. inline RecordType(const DataType& pData)
  325. : mData(pData)
  326. , mParent(0)
  327. , mLeftChild(0)
  328. , mRightChild(0)
  329. , mColor(eRed)
  330. , mBlackDepth(0)
  331. {
  332. }
  333. inline RecordType(const RecordType& pRecordType)
  334. : mData(pRecordType.mData)
  335. , mParent(0)
  336. , mLeftChild(0)
  337. , mRightChild(0)
  338. , mColor(pRecordType.mColor)
  339. , mBlackDepth(pRecordType.mBlackDepth)
  340. {
  341. }
  342. DataType mData;
  343. friend class FbxRedBlackTree;
  344. RecordType* mParent;
  345. RecordType* mLeftChild;
  346. RecordType* mRightChild;
  347. unsigned int mColor:2;
  348. unsigned int mBlackDepth:30;
  349. };
  350. public:
  351. typedef FbxRedBlackConstIterator<RecordType> ConstIteratorType;
  352. typedef FbxRedBlackIterator<RecordType> IteratorType;
  353. inline FbxRedBlackTree() : mRoot(0), mSize(0), mAllocator(sizeof(RecordType)) {}
  354. inline FbxRedBlackTree(const FbxRedBlackTree& pTree) : mRoot(0), mSize(0), mAllocator(sizeof(RecordType)) { operator=(pTree); }
  355. inline ~FbxRedBlackTree() { Clear(); }
  356. /** Deep copy pTree in this.
  357. * \param pTree The tree to copy in this tree. */
  358. inline FbxRedBlackTree& operator=(const FbxRedBlackTree& pTree)
  359. {
  360. if( this != &pTree )
  361. {
  362. Clear();
  363. mAllocator = pTree.mAllocator;
  364. if( pTree.mRoot )
  365. {
  366. void* lBuffer = mAllocator.AllocateRecords();
  367. mRoot = new(lBuffer) RecordType(*(pTree.mRoot)); //in-place new won't allocate memory, so it is safe
  368. mRoot->mLeftChild = DuplicateSubTree(pTree.mRoot->mLeftChild);
  369. mRoot->mRightChild = DuplicateSubTree(pTree.mRoot->mRightChild);
  370. if (mRoot->mLeftChild)
  371. {
  372. mRoot->mLeftChild->mParent = mRoot;
  373. }
  374. if (mRoot->mRightChild)
  375. {
  376. mRoot->mRightChild->mParent = mRoot;
  377. }
  378. }
  379. else
  380. {
  381. FBX_ASSERT( pTree.mSize == 0 );
  382. FBX_ASSERT( mRoot == 0 );
  383. }
  384. mSize = pTree.mSize;
  385. }
  386. return *this;
  387. }
  388. inline bool operator==(const FbxRedBlackTree& pTree) const
  389. {
  390. // Check a few quick shortcuts
  391. if( this == &pTree )
  392. return true;
  393. if( GetSize() != pTree.GetSize() )
  394. return false;
  395. // Iterator through all nodes; if we reach the end of both iterators at the same
  396. // time then we have two iterators that match.
  397. ConstIteratorType End;
  398. ConstIteratorType Iter1(Minimum());
  399. ConstIteratorType Iter2(pTree.Minimum());
  400. while( (Iter1 != End) && (Iter2 != End) &&
  401. (Iter1->GetKey() == Iter2->GetKey()) &&
  402. (Iter1->GetValue() == Iter2->GetValue()) )
  403. {
  404. ++Iter1;
  405. ++Iter2;
  406. }
  407. return Iter1 == End && Iter2 == End;
  408. }
  409. /** Ask Allocator to reserve space to hold pRecordCount elements.
  410. * \param pRecordCount
  411. */
  412. inline void Reserve(unsigned int pRecordCount)
  413. {
  414. mAllocator.Reserve(pRecordCount);
  415. }
  416. /** Get the number of elements in the tree. Takes O(1) time.
  417. * \return The number of elements in the tree.
  418. */
  419. inline int GetSize() const { return mSize; }
  420. inline bool Empty() const { return mSize == 0; }
  421. /** Insert a new element in the tree. Takes O(log n) time.
  422. * \param pData The element to insert.
  423. * \return If pData.GetKey() is already present in the tree, returns the
  424. * existing record and false; else returns the new record and true.
  425. */
  426. inline FbxPair<RecordType*, bool> Insert(const DataType& pData)
  427. {
  428. Compare lCompareKeys;
  429. bool lResult = false;
  430. RecordType* lParent = 0;
  431. RecordType* lNode = mRoot;
  432. while (lNode != 0)
  433. {
  434. const KeyType& lNodeKey = lNode->GetKey();
  435. const KeyType& lDataKey = pData.GetKey();
  436. if (lCompareKeys(lNodeKey, lDataKey) < 0)
  437. {
  438. lParent = lNode;
  439. lNode = lNode->mRightChild;
  440. }
  441. else if (lCompareKeys(lNodeKey, lDataKey) > 0)
  442. {
  443. lParent = lNode;
  444. lNode = lNode->mLeftChild;
  445. }
  446. else
  447. {
  448. break;
  449. }
  450. }
  451. if (lNode == 0)
  452. {
  453. void* lBuffer = mAllocator.AllocateRecords();
  454. lNode = new(lBuffer) RecordType(pData); //in-place new won't allocate memory, so it is safe
  455. mSize++;
  456. FBX_ASSERT(lNode == lBuffer);
  457. if (lParent)
  458. {
  459. if (lCompareKeys(lParent->GetKey(), pData.GetKey()) < 0)
  460. {
  461. FBX_ASSERT(lParent->mRightChild == 0);
  462. lParent->mRightChild = lNode;
  463. lNode->mParent = lParent;
  464. }
  465. else
  466. {
  467. FBX_ASSERT(lParent->mLeftChild == 0);
  468. lParent->mLeftChild = lNode;
  469. lNode->mParent = lParent;
  470. }
  471. }
  472. else
  473. {
  474. mRoot = lNode;
  475. }
  476. // Fix red black tree property
  477. FixNodesAfterInsertion(lNode);
  478. lResult = true;
  479. }
  480. return FbxPair<RecordType*, bool>(lNode, lResult);
  481. }
  482. /** Remove an element identified by a key from the tree. Takes O(log n) time.
  483. * \param pKey The key identifying the element to remove.
  484. */
  485. inline bool Remove(const KeyType& pKey)
  486. {
  487. Compare lCompareKeys;
  488. bool lResult = false;
  489. RecordType* lNode = mRoot;
  490. while (lNode != 0)
  491. {
  492. if (lCompareKeys(lNode->GetKey(), pKey) < 0)
  493. {
  494. lNode = lNode->mRightChild;
  495. }
  496. else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
  497. {
  498. lNode = lNode->mLeftChild;
  499. }
  500. else
  501. {
  502. break;
  503. }
  504. }
  505. if (lNode)
  506. {
  507. RemoveNode(lNode);
  508. mSize--;
  509. lNode->~RecordType();
  510. mAllocator.FreeMemory(lNode);
  511. lResult = true;
  512. }
  513. return lResult;
  514. }
  515. /** Remove all elements from the tree. Takes O(n) time. Recursive.
  516. */
  517. inline void Clear()
  518. {
  519. if (mRoot)
  520. {
  521. ClearSubTree(mRoot->mLeftChild);
  522. ClearSubTree(mRoot->mRightChild);
  523. mRoot->~RecordType();
  524. mAllocator.FreeMemory(mRoot);
  525. mRoot = 0;
  526. mSize = 0;
  527. }
  528. }
  529. /** Find the smallest element in the tree.
  530. * Takes O(log n) time.
  531. */
  532. inline const RecordType* Minimum() const
  533. {
  534. if (0 != mRoot)
  535. {
  536. return mRoot->Minimum();
  537. }
  538. else
  539. {
  540. return 0;
  541. }
  542. }
  543. /** Find the smallest element in the tree.
  544. * Takes O(log n) time.
  545. */
  546. inline RecordType* Minimum()
  547. {
  548. if (0 != mRoot)
  549. {
  550. return mRoot->Minimum();
  551. }
  552. else
  553. {
  554. return 0;
  555. }
  556. }
  557. /** Find the largest element in the tree.
  558. * Takes O(log n) time.
  559. */
  560. inline const RecordType* Maximum() const
  561. {
  562. if (0 != mRoot)
  563. {
  564. return mRoot->Maximum();
  565. }
  566. else
  567. {
  568. return 0;
  569. }
  570. }
  571. /** Find the largest element in the tree.
  572. * Takes O(log n) time.
  573. */
  574. inline RecordType* Maximum()
  575. {
  576. if (0 != mRoot)
  577. {
  578. return mRoot->Maximum();
  579. }
  580. else
  581. {
  582. return 0;
  583. }
  584. }
  585. /** Find the key-value pair with key pKey.
  586. * Takes O(log n) time.
  587. * \param pKey The key to look for.
  588. */
  589. inline const RecordType* Find(const KeyType& pKey) const
  590. {
  591. Compare lCompareKeys;
  592. const RecordType* lNode = mRoot;
  593. while (lNode != 0)
  594. {
  595. if (lCompareKeys(lNode->GetKey(), pKey) < 0)
  596. {
  597. lNode = lNode->mRightChild;
  598. }
  599. else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
  600. {
  601. lNode = lNode->mLeftChild;
  602. }
  603. else
  604. {
  605. break;
  606. }
  607. }
  608. return lNode;
  609. }
  610. /** Find the key-value pair with key pKey.
  611. * Takes O(log n) time.
  612. * \param pKey The key to look for.
  613. */
  614. inline RecordType* Find(const KeyType& pKey)
  615. {
  616. Compare lCompareKeys;
  617. RecordType* lNode = mRoot;
  618. while (lNode != 0)
  619. {
  620. if (lCompareKeys(lNode->GetKey(), pKey) < 0)
  621. {
  622. lNode = lNode->mRightChild;
  623. }
  624. else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
  625. {
  626. lNode = lNode->mLeftChild;
  627. }
  628. else
  629. {
  630. break;
  631. }
  632. }
  633. return lNode;
  634. }
  635. /** Find the key-value pair with the smallest key greater than pKey.
  636. * Takes O(log n) time.
  637. * \param pKey The key to look for.
  638. */
  639. inline const RecordType* UpperBound(const KeyType& pKey) const
  640. {
  641. Compare lCompareKeys;
  642. const RecordType* lNode = mRoot;
  643. const RecordType* lCandidate = 0;
  644. while (lNode != 0)
  645. {
  646. if (lCompareKeys(lNode->GetKey(), pKey) <= 0)
  647. {
  648. lNode = lNode->mRightChild;
  649. }
  650. else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
  651. {
  652. lCandidate = lNode;
  653. lNode = lNode->mLeftChild;
  654. }
  655. }
  656. return lCandidate;
  657. }
  658. /** Find the key-value pair with the smallest key greater than pKey.
  659. * Takes O(log n) time.
  660. * \param pKey The key to look for.
  661. */
  662. inline RecordType* UpperBound(const KeyType& pKey)
  663. {
  664. Compare lCompareKeys;
  665. RecordType* lNode = mRoot;
  666. RecordType* lCandidate = 0;
  667. while (lNode != 0)
  668. {
  669. if (lCompareKeys(lNode->GetKey(), pKey) <= 0)
  670. {
  671. lNode = lNode->mRightChild;
  672. }
  673. else if (lCompareKeys(lNode->GetKey(), pKey) > 0)
  674. {
  675. lCandidate = lNode;
  676. lNode = lNode->mLeftChild;
  677. }
  678. }
  679. return lCandidate;
  680. }
  681. protected:
  682. RecordType* mRoot;
  683. int mSize;
  684. AllocatorType mAllocator;
  685. inline RecordType* DuplicateSubTree(const RecordType* pNode)
  686. {
  687. RecordType* lNewSubTree = 0;
  688. if (pNode)
  689. {
  690. void* lBuffer = mAllocator.AllocateRecords();
  691. lNewSubTree = new(lBuffer) RecordType(*pNode); //in-place new won't allocate memory, so it is safe
  692. lNewSubTree->mLeftChild = DuplicateSubTree(pNode->mLeftChild);
  693. lNewSubTree->mRightChild = DuplicateSubTree(pNode->mRightChild);
  694. if (lNewSubTree->mLeftChild)
  695. {
  696. lNewSubTree->mLeftChild->mParent = lNewSubTree;
  697. }
  698. if (lNewSubTree->mRightChild)
  699. {
  700. lNewSubTree->mRightChild->mParent = lNewSubTree;
  701. }
  702. }
  703. return lNewSubTree;
  704. }
  705. inline void FixNodesAfterInsertion(RecordType* pNode)
  706. {
  707. RecordType* lNode = pNode;
  708. bool lDone = false;
  709. while (!lDone)
  710. {
  711. lDone = true;
  712. if (lNode->mParent == 0)
  713. {
  714. lNode->mColor = RecordType::eBlack;
  715. }
  716. else if (lNode->mParent->mColor == RecordType::eRed)
  717. {
  718. RecordType* lUncle = 0;
  719. if (lNode->mParent == lNode->mParent->mParent->mLeftChild)
  720. {
  721. lUncle = lNode->mParent->mParent->mRightChild;
  722. }
  723. else if (lNode->mParent == lNode->mParent->mParent->mRightChild)
  724. {
  725. lUncle = lNode->mParent->mParent->mLeftChild;
  726. }
  727. // since lNode->mParent is red, lNode->mParent->mParent exists
  728. if (lUncle && lUncle->mColor == RecordType::eRed)
  729. {
  730. lNode->mParent->mColor = RecordType::eBlack;
  731. lUncle->mColor = RecordType::eBlack;
  732. lNode->mParent->mParent->mColor = RecordType::eRed;
  733. lNode = lNode->mParent->mParent;
  734. lDone = false;
  735. }
  736. else
  737. {
  738. if ((lNode == lNode->mParent->mRightChild) &&
  739. (lNode->mParent == lNode->mParent->mParent->mLeftChild))
  740. {
  741. LeftRotate(lNode->mParent);
  742. lNode = lNode->mLeftChild;
  743. }
  744. else if ((lNode == lNode->mParent->mLeftChild) &&
  745. (lNode->mParent == lNode->mParent->mParent->mRightChild))
  746. {
  747. RightRotate(lNode->mParent);
  748. lNode = lNode->mRightChild;
  749. }
  750. lNode->mParent->mColor = RecordType::eBlack;
  751. lNode->mParent->mParent->mColor = RecordType::eRed;
  752. if ((lNode == lNode->mParent->mLeftChild) &&
  753. (lNode->mParent == lNode->mParent->mParent->mLeftChild))
  754. {
  755. RightRotate(lNode->mParent->mParent);
  756. }
  757. else
  758. {
  759. LeftRotate(lNode->mParent->mParent);
  760. }
  761. }
  762. }
  763. }
  764. mRoot->mColor = RecordType::eBlack;
  765. }
  766. inline void LeftRotate(RecordType* pNode)
  767. {
  768. FBX_ASSERT_RETURN(pNode);
  769. RecordType* lNode = pNode->mRightChild;
  770. FBX_ASSERT_RETURN(lNode);
  771. #ifdef _DEBUG
  772. RecordType* A = pNode->mLeftChild;
  773. RecordType* B = lNode->mLeftChild;
  774. RecordType* C = lNode->mRightChild;
  775. RecordType* Z = pNode->mParent;
  776. #endif
  777. pNode->mRightChild = lNode->mLeftChild;
  778. if (pNode->mRightChild)
  779. {
  780. pNode->mRightChild->mParent = pNode;
  781. }
  782. lNode->mParent = pNode->mParent;
  783. if (pNode->mParent == 0)
  784. {
  785. FBX_ASSERT(mRoot == pNode);
  786. mRoot = lNode;
  787. }
  788. else if (pNode == pNode->mParent->mLeftChild)
  789. {
  790. pNode->mParent->mLeftChild = lNode;
  791. }
  792. else
  793. {
  794. pNode->mParent->mRightChild = lNode;
  795. }
  796. pNode->mParent = lNode;
  797. lNode->mLeftChild = pNode;
  798. FBX_ASSERT(pNode->mLeftChild == A);
  799. FBX_ASSERT(pNode->mRightChild == B);
  800. FBX_ASSERT(pNode->mParent == lNode);
  801. FBX_ASSERT(lNode->mLeftChild == pNode);
  802. FBX_ASSERT(lNode->mRightChild == C);
  803. FBX_ASSERT(lNode->mParent == Z);
  804. FBX_ASSERT(A == 0 || A->mParent == pNode);
  805. FBX_ASSERT(B == 0 || B->mParent == pNode);
  806. FBX_ASSERT(C == 0 || C->mParent == lNode);
  807. FBX_ASSERT(Z == 0 || Z->mLeftChild == lNode || Z->mRightChild == lNode);
  808. }
  809. inline void RightRotate(RecordType* pNode)
  810. {
  811. RecordType* lNode = pNode->mLeftChild;
  812. #ifdef _DEBUG
  813. RecordType* A = lNode->mLeftChild;
  814. RecordType* B = lNode->mRightChild;
  815. RecordType* C = pNode->mRightChild;
  816. RecordType* Z = pNode->mParent;
  817. #endif
  818. pNode->mLeftChild = lNode->mRightChild;
  819. if (pNode->mLeftChild)
  820. {
  821. pNode->mLeftChild->mParent = pNode;
  822. }
  823. lNode->mParent = pNode->mParent;
  824. if (pNode->mParent == 0)
  825. {
  826. FBX_ASSERT(mRoot == pNode);
  827. mRoot = lNode;
  828. }
  829. else if (pNode == pNode->mParent->mRightChild)
  830. {
  831. pNode->mParent->mRightChild = lNode;
  832. }
  833. else
  834. {
  835. pNode->mParent->mLeftChild = lNode;
  836. }
  837. pNode->mParent = lNode;
  838. lNode->mRightChild = pNode;
  839. FBX_ASSERT(lNode->mLeftChild == A);
  840. FBX_ASSERT(lNode->mRightChild == pNode);
  841. FBX_ASSERT(lNode->mParent == Z);
  842. FBX_ASSERT(pNode->mLeftChild == B);
  843. FBX_ASSERT(pNode->mRightChild == C);
  844. FBX_ASSERT(pNode->mParent == lNode);
  845. FBX_ASSERT(A == 0 || A->mParent == lNode);
  846. FBX_ASSERT(B == 0 || B->mParent == pNode);
  847. FBX_ASSERT(C == 0 || C->mParent == pNode);
  848. FBX_ASSERT(Z == 0 || Z->mLeftChild == lNode || Z->mRightChild == lNode);
  849. }
  850. inline void RemoveNode(RecordType* pNode)
  851. {
  852. if (pNode->mLeftChild == 0)
  853. {
  854. if (pNode->mRightChild == 0)
  855. {
  856. if (pNode->mParent)
  857. {
  858. if (pNode->mParent->mLeftChild == pNode)
  859. {
  860. pNode->mParent->mLeftChild = 0;
  861. }
  862. else if (pNode->mParent->mRightChild == pNode)
  863. {
  864. pNode->mParent->mRightChild = 0;
  865. }
  866. else
  867. {
  868. FBX_ASSERT_NOW("Node not found in FbxRedBlackTree");
  869. }
  870. }
  871. else
  872. {
  873. FBX_ASSERT(mRoot == pNode);
  874. mRoot = 0;
  875. }
  876. if (pNode->mColor == RecordType::eBlack)
  877. {
  878. FixNodesAfterRemoval(pNode->mParent, 0);
  879. }
  880. }
  881. else
  882. {
  883. if (pNode->mParent)
  884. {
  885. if (pNode->mParent->mLeftChild == pNode)
  886. {
  887. pNode->mParent->mLeftChild = pNode->mRightChild;
  888. pNode->mRightChild->mParent = pNode->mParent;
  889. }
  890. else if (pNode->mParent->mRightChild == pNode)
  891. {
  892. pNode->mParent->mRightChild = pNode->mRightChild;
  893. pNode->mRightChild->mParent = pNode->mParent;
  894. }
  895. else
  896. {
  897. FBX_ASSERT_NOW("Node not found in FbxRedBlackTree");
  898. }
  899. }
  900. else
  901. {
  902. FBX_ASSERT(mRoot == pNode);
  903. mRoot = pNode->mRightChild;
  904. pNode->mRightChild->mParent = 0;
  905. }
  906. if (pNode->mColor == RecordType::eBlack)
  907. {
  908. FixNodesAfterRemoval(pNode->mRightChild->mParent, pNode->mRightChild);
  909. }
  910. }
  911. }
  912. else
  913. {
  914. if (pNode->mRightChild == 0)
  915. {
  916. if (pNode->mParent)
  917. {
  918. if (pNode->mParent->mLeftChild == pNode)
  919. {
  920. pNode->mParent->mLeftChild = pNode->mLeftChild;
  921. pNode->mLeftChild->mParent = pNode->mParent;
  922. }
  923. else if (pNode->mParent->mRightChild == pNode)
  924. {
  925. pNode->mParent->mRightChild = pNode->mLeftChild;
  926. pNode->mLeftChild->mParent = pNode->mParent;
  927. }
  928. else
  929. {
  930. FBX_ASSERT_NOW("Node not found in FbxRedBlackTree");
  931. }
  932. }
  933. else
  934. {
  935. FBX_ASSERT(mRoot == pNode);
  936. mRoot = pNode->mLeftChild;
  937. pNode->mLeftChild->mParent = 0;
  938. }
  939. if (pNode->mColor == RecordType::eBlack)
  940. {
  941. FixNodesAfterRemoval(pNode->mLeftChild->mParent, pNode->mLeftChild);
  942. }
  943. }
  944. else
  945. {
  946. RecordType* lMinRightNode = pNode->mRightChild->Minimum();
  947. RemoveNode(lMinRightNode);
  948. lMinRightNode->mColor = pNode->mColor;
  949. ReplaceNode(pNode, lMinRightNode);
  950. }
  951. }
  952. pNode->mParent = 0;
  953. pNode->mLeftChild = 0;
  954. pNode->mRightChild = 0;
  955. }
  956. inline void ReplaceNode(RecordType* pNodeToReplace, RecordType* pReplacement)
  957. {
  958. pReplacement->mParent = pNodeToReplace->mParent;
  959. if (pNodeToReplace->mParent)
  960. {
  961. if (pNodeToReplace->mParent->mLeftChild == pNodeToReplace)
  962. {
  963. pNodeToReplace->mParent->mLeftChild = pReplacement;
  964. }
  965. else if (pNodeToReplace->mParent->mRightChild == pNodeToReplace)
  966. {
  967. pNodeToReplace->mParent->mRightChild = pReplacement;
  968. }
  969. }
  970. else
  971. {
  972. FBX_ASSERT(mRoot == pNodeToReplace);
  973. mRoot = pReplacement;
  974. }
  975. pReplacement->mLeftChild = pNodeToReplace->mLeftChild;
  976. if (pReplacement->mLeftChild)
  977. {
  978. pReplacement->mLeftChild->mParent = pReplacement;
  979. }
  980. pReplacement->mRightChild = pNodeToReplace->mRightChild;
  981. if (pReplacement->mRightChild)
  982. {
  983. pReplacement->mRightChild->mParent = pReplacement;
  984. }
  985. }
  986. inline RecordType* Sibling(const RecordType* pParent, const RecordType* pNode) const
  987. {
  988. if (pParent)
  989. {
  990. if (pParent->mLeftChild == pNode)
  991. {
  992. return pParent->mRightChild;
  993. }
  994. else if (pParent->mRightChild == pNode)
  995. {
  996. return pParent->mLeftChild;
  997. }
  998. }
  999. return 0;
  1000. }
  1001. inline bool IsBlack(const RecordType* pNode)
  1002. {
  1003. return ((pNode == 0) || (pNode->mColor == RecordType::eBlack));
  1004. }
  1005. inline void FixNodesAfterRemoval(RecordType* pParent, RecordType* pNode)
  1006. {
  1007. RecordType* lParent = pParent;
  1008. RecordType* lNode = pNode;
  1009. bool lDone = false;
  1010. while (!lDone)
  1011. {
  1012. lDone = true;
  1013. if (!IsBlack(lNode))
  1014. {
  1015. lNode->mColor = RecordType::eBlack;
  1016. }
  1017. else if (lParent != NULL)
  1018. {
  1019. RecordType* lSibling = Sibling(lParent, lNode);
  1020. if (!IsBlack(lSibling))
  1021. {
  1022. lParent->mColor = RecordType::eRed;
  1023. lSibling->mColor = RecordType::eBlack;
  1024. if (lNode == lParent->mLeftChild)
  1025. {
  1026. LeftRotate(lParent);
  1027. }
  1028. else
  1029. {
  1030. RightRotate(lParent);
  1031. }
  1032. // update sibling: it may have change after rotation
  1033. // parent was not affected by this rotation
  1034. lSibling = Sibling(lParent, lNode);
  1035. }
  1036. /* check this for null sibling */
  1037. if (lSibling &&
  1038. IsBlack(lParent) &&
  1039. IsBlack(lSibling) &&
  1040. IsBlack(lSibling->mLeftChild) &&
  1041. IsBlack(lSibling->mRightChild))
  1042. {
  1043. lSibling->mColor = RecordType::eRed;
  1044. lNode = lParent;
  1045. lParent = lParent->mParent;
  1046. lDone = false;
  1047. }
  1048. else
  1049. {
  1050. if (!IsBlack(lParent) &&
  1051. IsBlack(lSibling) &&
  1052. ((lSibling == 0) || IsBlack(lSibling->mLeftChild)) &&
  1053. ((lSibling == 0) || IsBlack(lSibling->mRightChild)))
  1054. {
  1055. if (lSibling)
  1056. {
  1057. lSibling->mColor = RecordType::eRed;
  1058. }
  1059. lParent->mColor = RecordType::eBlack;
  1060. }
  1061. else if( lSibling != 0 )
  1062. {
  1063. if ((lNode == lParent->mLeftChild) &&
  1064. IsBlack(lSibling) &&
  1065. !IsBlack(lSibling->mLeftChild) &&
  1066. IsBlack(lSibling->mRightChild))
  1067. {
  1068. lSibling->mColor = RecordType::eRed;
  1069. lSibling->mLeftChild->mColor = RecordType::eBlack;
  1070. RightRotate(lSibling);
  1071. }
  1072. else if ((lNode == lParent->mRightChild) &&
  1073. IsBlack(lSibling) &&
  1074. IsBlack(lSibling->mLeftChild) &&
  1075. !IsBlack(lSibling->mRightChild))
  1076. {
  1077. lSibling->mColor = RecordType::eRed;
  1078. lSibling->mRightChild->mColor = RecordType::eBlack;
  1079. LeftRotate(lSibling);
  1080. }
  1081. // update sibling: it may have change after rotation
  1082. lSibling = Sibling(lParent, lNode);
  1083. FBX_ASSERT(lSibling != 0 && lParent != 0); // lSibling is now
  1084. // the former red
  1085. // child of the
  1086. // former sibling
  1087. if( lSibling != 0 && lParent != 0 )
  1088. {
  1089. lSibling->mColor = lParent->mColor;
  1090. lParent->mColor = RecordType::eBlack;
  1091. if (lNode == lParent->mLeftChild)
  1092. {
  1093. if (lSibling->mRightChild)
  1094. {
  1095. lSibling->mRightChild->mColor = RecordType::eBlack;
  1096. }
  1097. LeftRotate(lParent);
  1098. }
  1099. else
  1100. {
  1101. if (lSibling->mLeftChild)
  1102. {
  1103. lSibling->mLeftChild->mColor = RecordType::eBlack;
  1104. }
  1105. RightRotate(lParent);
  1106. }
  1107. }
  1108. }
  1109. }
  1110. }
  1111. }
  1112. if (mRoot)
  1113. {
  1114. mRoot->mColor = RecordType::eBlack;
  1115. }
  1116. }
  1117. inline void ClearSubTree(RecordType* pNode)
  1118. {
  1119. if (pNode)
  1120. {
  1121. ClearSubTree(pNode->mLeftChild);
  1122. ClearSubTree(pNode->mRightChild);
  1123. pNode->~RecordType();
  1124. mAllocator.FreeMemory(pNode);
  1125. }
  1126. }
  1127. inline int GetSubTreeSize(RecordType* pNode) const
  1128. {
  1129. if (pNode)
  1130. {
  1131. return GetSubTreeSize(pNode->mLeftChild) + GetSubTreeSize(pNode->mRightChild) + 1;
  1132. }
  1133. else
  1134. {
  1135. return 0;
  1136. }
  1137. }
  1138. #if 0
  1139. inline void IsSane()
  1140. {
  1141. FBX_ASSERT((mRoot == 0) || (mRoot->mColor == RecordType::eBlack));
  1142. FBX_ASSERT(((mRoot == 0) && (mSize == 0)) || (mRoot != 0) && (mSize != 0));
  1143. IsSubTreeSane(mRoot);
  1144. ComputeBlackDepth(mRoot, 0);
  1145. RecordType* lNode = mRoot;
  1146. unsigned int lLeafBlackDepth = 0;
  1147. while (lNode)
  1148. {
  1149. if (lNode->mLeftChild == 0)
  1150. {
  1151. lLeafBlackDepth = lNode->mBlackDepth + ((lNode->mColor == RecordType::eBlack) ? 1 : 0);
  1152. }
  1153. lNode = lNode->mLeftChild;
  1154. }
  1155. CheckLeavesBlackDepth(mRoot, lLeafBlackDepth);
  1156. }
  1157. inline void IsSubTreeSane(const RecordType* pNode) const
  1158. {
  1159. Compare lCompareKeys;
  1160. if (pNode)
  1161. {
  1162. FBX_ASSERT(pNode != pNode->mParent);
  1163. FBX_ASSERT(pNode != pNode->mLeftChild);
  1164. FBX_ASSERT(pNode != pNode->mRightChild);
  1165. // Check for two consecutive red nodes
  1166. FBX_ASSERT((pNode->mColor == RecordType::eBlack) ||
  1167. (pNode->mLeftChild == NULL) ||
  1168. (pNode->mLeftChild->mColor == RecordType::eBlack));
  1169. FBX_ASSERT((pNode->mColor == RecordType::eBlack) ||
  1170. (pNode->mRightChild == NULL) ||
  1171. (pNode->mRightChild->mColor == RecordType::eBlack));
  1172. // Check key ordering
  1173. FBX_ASSERT((pNode->mLeftChild == 0 ||
  1174. lCompareKeys(pNode->GetKey(), pNode->mLeftChild->GetKey()) > 0));
  1175. FBX_ASSERT((pNode->mRightChild == 0 ||
  1176. lCompareKeys(pNode->GetKey(), pNode->mRightChild->GetKey()) < 0));
  1177. IsSubTreeSane(pNode->mLeftChild);
  1178. IsSubTreeSane(pNode->mRightChild);
  1179. }
  1180. }
  1181. inline void ComputeBlackDepth(RecordType* pNode, unsigned int pDepth)
  1182. {
  1183. if( pNode )
  1184. {
  1185. pNode->mBlackDepth = pDepth;
  1186. if( pNode->mColor == RecordType::eBlack )
  1187. {
  1188. pDepth++;
  1189. }
  1190. ComputeBlackDepth(pNode->mLeftChild, pDepth);
  1191. ComputeBlackDepth(pNode->mRightChild, pDepth);
  1192. }
  1193. }
  1194. inline void CheckLeavesBlackDepth(RecordType* pNode, unsigned int pBlackDepth)
  1195. {
  1196. if( pNode )
  1197. {
  1198. if( pNode->mLeftChild == 0 || pNode->mRightChild == 0 )
  1199. {
  1200. FBX_ASSERT((pNode->mBlackDepth + ((pNode->mColor == RecordType::eBlack) ? 1 : 0)) == pBlackDepth);
  1201. }
  1202. CheckLeavesBlackDepth(pNode->mLeftChild, pBlackDepth);
  1203. CheckLeavesBlackDepth(pNode->mRightChild, pBlackDepth);
  1204. }
  1205. }
  1206. #endif
  1207. };
  1208. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  1209. #include <fbxsdk/fbxsdk_nsend.h>
  1210. #endif /*_FBXSDK_CORE_BASE_REDBLACKTREE_H_ */