irrMap.h 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. // Copyright (C) 2006-2012 by Kat'Oun
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __IRR_MAP_H_INCLUDED__
  5. #define __IRR_MAP_H_INCLUDED__
  6. #include "irrTypes.h"
  7. #include "irrMath.h"
  8. namespace irr
  9. {
  10. namespace core
  11. {
  12. //! map template for associative arrays using a red-black tree
  13. template <class KeyType, class ValueType>
  14. class map
  15. {
  16. //! red/black tree for map
  17. template <class KeyTypeRB, class ValueTypeRB>
  18. class RBTree
  19. {
  20. public:
  21. RBTree(const KeyTypeRB& k, const ValueTypeRB& v)
  22. : LeftChild(0), RightChild(0), Parent(0), Key(k),
  23. Value(v), IsRed(true) {}
  24. void setLeftChild(RBTree* p)
  25. {
  26. LeftChild=p;
  27. if (p)
  28. p->setParent(this);
  29. }
  30. void setRightChild(RBTree* p)
  31. {
  32. RightChild=p;
  33. if (p)
  34. p->setParent(this);
  35. }
  36. void setParent(RBTree* p) { Parent=p; }
  37. void setValue(const ValueTypeRB& v) { Value = v; }
  38. void setRed() { IsRed = true; }
  39. void setBlack() { IsRed = false; }
  40. RBTree* getLeftChild() const { return LeftChild; }
  41. RBTree* getRightChild() const { return RightChild; }
  42. RBTree* getParent() const { return Parent; }
  43. const ValueTypeRB& getValue() const
  44. {
  45. return Value;
  46. }
  47. ValueTypeRB& getValue()
  48. {
  49. return Value;
  50. }
  51. const KeyTypeRB& getKey() const
  52. {
  53. return Key;
  54. }
  55. bool isRoot() const
  56. {
  57. return Parent==0;
  58. }
  59. bool isLeftChild() const
  60. {
  61. return (Parent != 0) && (Parent->getLeftChild()==this);
  62. }
  63. bool isRightChild() const
  64. {
  65. return (Parent!=0) && (Parent->getRightChild()==this);
  66. }
  67. bool isLeaf() const
  68. {
  69. return (LeftChild==0) && (RightChild==0);
  70. }
  71. unsigned int getLevel() const
  72. {
  73. if (isRoot())
  74. return 1;
  75. else
  76. return getParent()->getLevel() + 1;
  77. }
  78. bool isRed() const
  79. {
  80. return IsRed;
  81. }
  82. bool isBlack() const
  83. {
  84. return !IsRed;
  85. }
  86. private:
  87. RBTree();
  88. RBTree* LeftChild;
  89. RBTree* RightChild;
  90. RBTree* Parent;
  91. KeyTypeRB Key;
  92. ValueTypeRB Value;
  93. bool IsRed;
  94. }; // RBTree
  95. public:
  96. typedef RBTree<KeyType,ValueType> Node;
  97. // We need the forward declaration for the friend declaration
  98. class ConstIterator;
  99. //! Normal Iterator
  100. class Iterator
  101. {
  102. friend class ConstIterator;
  103. public:
  104. Iterator() : Root(0), Cur(0) {}
  105. // Constructor(Node*)
  106. Iterator(Node* root) : Root(root)
  107. {
  108. reset();
  109. }
  110. // Copy constructor
  111. Iterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {}
  112. void reset(bool atLowest=true)
  113. {
  114. if (atLowest)
  115. Cur = getMin(Root);
  116. else
  117. Cur = getMax(Root);
  118. }
  119. bool atEnd() const
  120. {
  121. return Cur==0;
  122. }
  123. Node* getNode() const
  124. {
  125. return Cur;
  126. }
  127. Iterator& operator=(const Iterator& src)
  128. {
  129. Root = src.Root;
  130. Cur = src.Cur;
  131. return (*this);
  132. }
  133. void operator++(int)
  134. {
  135. inc();
  136. }
  137. void operator--(int)
  138. {
  139. dec();
  140. }
  141. Node* operator->()
  142. {
  143. return getNode();
  144. }
  145. Node& operator*()
  146. {
  147. _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
  148. return *Cur;
  149. }
  150. private:
  151. Node* getMin(Node* n) const
  152. {
  153. while(n && n->getLeftChild())
  154. n = n->getLeftChild();
  155. return n;
  156. }
  157. Node* getMax(Node* n) const
  158. {
  159. while(n && n->getRightChild())
  160. n = n->getRightChild();
  161. return n;
  162. }
  163. void inc()
  164. {
  165. // Already at end?
  166. if (Cur==0)
  167. return;
  168. if (Cur->getRightChild())
  169. {
  170. // If current node has a right child, the next higher node is the
  171. // node with lowest key beneath the right child.
  172. Cur = getMin(Cur->getRightChild());
  173. }
  174. else if (Cur->isLeftChild())
  175. {
  176. // No right child? Well if current node is a left child then
  177. // the next higher node is the parent
  178. Cur = Cur->getParent();
  179. }
  180. else
  181. {
  182. // Current node neither is left child nor has a right child.
  183. // I.e. it is either right child or root
  184. // The next higher node is the parent of the first non-right
  185. // child (i.e. either a left child or the root) up in the
  186. // hierarchy. Root's parent is 0.
  187. while(Cur->isRightChild())
  188. Cur = Cur->getParent();
  189. Cur = Cur->getParent();
  190. }
  191. }
  192. void dec()
  193. {
  194. // Already at end?
  195. if (Cur==0)
  196. return;
  197. if (Cur->getLeftChild())
  198. {
  199. // If current node has a left child, the next lower node is the
  200. // node with highest key beneath the left child.
  201. Cur = getMax(Cur->getLeftChild());
  202. }
  203. else if (Cur->isRightChild())
  204. {
  205. // No left child? Well if current node is a right child then
  206. // the next lower node is the parent
  207. Cur = Cur->getParent();
  208. }
  209. else
  210. {
  211. // Current node neither is right child nor has a left child.
  212. // It is either left child or root
  213. // The next higher node is the parent of the first non-left
  214. // child (i.e. either a right child or the root) up in the
  215. // hierarchy. Root's parent is 0.
  216. while(Cur->isLeftChild())
  217. Cur = Cur->getParent();
  218. Cur = Cur->getParent();
  219. }
  220. }
  221. Node* Root;
  222. Node* Cur;
  223. }; // Iterator
  224. //! Const Iterator
  225. class ConstIterator
  226. {
  227. friend class Iterator;
  228. public:
  229. ConstIterator() : Root(0), Cur(0) {}
  230. // Constructor(Node*)
  231. ConstIterator(const Node* root) : Root(root)
  232. {
  233. reset();
  234. }
  235. // Copy constructor
  236. ConstIterator(const ConstIterator& src) : Root(src.Root), Cur(src.Cur) {}
  237. ConstIterator(const Iterator& src) : Root(src.Root), Cur(src.Cur) {}
  238. void reset(bool atLowest=true)
  239. {
  240. if (atLowest)
  241. Cur = getMin(Root);
  242. else
  243. Cur = getMax(Root);
  244. }
  245. bool atEnd() const
  246. {
  247. return Cur==0;
  248. }
  249. const Node* getNode() const
  250. {
  251. return Cur;
  252. }
  253. ConstIterator& operator=(const ConstIterator& src)
  254. {
  255. Root = src.Root;
  256. Cur = src.Cur;
  257. return (*this);
  258. }
  259. void operator++(int)
  260. {
  261. inc();
  262. }
  263. void operator--(int)
  264. {
  265. dec();
  266. }
  267. const Node* operator->()
  268. {
  269. return getNode();
  270. }
  271. const Node& operator*()
  272. {
  273. _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
  274. return *Cur;
  275. }
  276. private:
  277. const Node* getMin(const Node* n) const
  278. {
  279. while(n && n->getLeftChild())
  280. n = n->getLeftChild();
  281. return n;
  282. }
  283. const Node* getMax(const Node* n) const
  284. {
  285. while(n && n->getRightChild())
  286. n = n->getRightChild();
  287. return n;
  288. }
  289. void inc()
  290. {
  291. // Already at end?
  292. if (Cur==0)
  293. return;
  294. if (Cur->getRightChild())
  295. {
  296. // If current node has a right child, the next higher node is the
  297. // node with lowest key beneath the right child.
  298. Cur = getMin(Cur->getRightChild());
  299. }
  300. else if (Cur->isLeftChild())
  301. {
  302. // No right child? Well if current node is a left child then
  303. // the next higher node is the parent
  304. Cur = Cur->getParent();
  305. }
  306. else
  307. {
  308. // Current node neither is left child nor has a right child.
  309. // It is either right child or root
  310. // The next higher node is the parent of the first non-right
  311. // child (i.e. either a left child or the root) up in the
  312. // hierarchy. Root's parent is 0.
  313. while(Cur->isRightChild())
  314. Cur = Cur->getParent();
  315. Cur = Cur->getParent();
  316. }
  317. }
  318. void dec()
  319. {
  320. // Already at end?
  321. if (Cur==0)
  322. return;
  323. if (Cur->getLeftChild())
  324. {
  325. // If current node has a left child, the next lower node is the
  326. // node with highest key beneath the left child.
  327. Cur = getMax(Cur->getLeftChild());
  328. }
  329. else if (Cur->isRightChild())
  330. {
  331. // No left child? Well if current node is a right child then
  332. // the next lower node is the parent
  333. Cur = Cur->getParent();
  334. }
  335. else
  336. {
  337. // Current node neither is right child nor has a left child.
  338. // It is either left child or root
  339. // The next higher node is the parent of the first non-left
  340. // child (i.e. either a right child or the root) up in the
  341. // hierarchy. Root's parent is 0.
  342. while(Cur->isLeftChild())
  343. Cur = Cur->getParent();
  344. Cur = Cur->getParent();
  345. }
  346. }
  347. const Node* Root;
  348. const Node* Cur;
  349. }; // ConstIterator
  350. //! Parent First Iterator.
  351. /** Traverses the tree from top to bottom. Typical usage is
  352. when storing the tree structure, because when reading it
  353. later (and inserting elements) the tree structure will
  354. be the same. */
  355. class ParentFirstIterator
  356. {
  357. public:
  358. ParentFirstIterator() : Root(0), Cur(0) {}
  359. explicit ParentFirstIterator(Node* root) : Root(root), Cur(0)
  360. {
  361. reset();
  362. }
  363. void reset()
  364. {
  365. Cur = Root;
  366. }
  367. bool atEnd() const
  368. {
  369. return Cur==0;
  370. }
  371. Node* getNode()
  372. {
  373. return Cur;
  374. }
  375. ParentFirstIterator& operator=(const ParentFirstIterator& src)
  376. {
  377. Root = src.Root;
  378. Cur = src.Cur;
  379. return (*this);
  380. }
  381. void operator++(int)
  382. {
  383. inc();
  384. }
  385. Node* operator -> ()
  386. {
  387. return getNode();
  388. }
  389. Node& operator* ()
  390. {
  391. _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
  392. return *getNode();
  393. }
  394. private:
  395. void inc()
  396. {
  397. // Already at end?
  398. if (Cur==0)
  399. return;
  400. // First we try down to the left
  401. if (Cur->getLeftChild())
  402. {
  403. Cur = Cur->getLeftChild();
  404. }
  405. else if (Cur->getRightChild())
  406. {
  407. // No left child? The we go down to the right.
  408. Cur = Cur->getRightChild();
  409. }
  410. else
  411. {
  412. // No children? Move up in the hierarchy until
  413. // we either reach 0 (and are finished) or
  414. // find a right uncle.
  415. while (Cur!=0)
  416. {
  417. // But if parent is left child and has a right "uncle" the parent
  418. // has already been processed but the uncle hasn't. Move to
  419. // the uncle.
  420. if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
  421. {
  422. Cur = Cur->getParent()->getRightChild();
  423. return;
  424. }
  425. Cur = Cur->getParent();
  426. }
  427. }
  428. }
  429. Node* Root;
  430. Node* Cur;
  431. }; // ParentFirstIterator
  432. //! Parent Last Iterator
  433. /** Traverse the tree from bottom to top.
  434. Typical usage is when deleting all elements in the tree
  435. because you must delete the children before you delete
  436. their parent. */
  437. class ParentLastIterator
  438. {
  439. public:
  440. ParentLastIterator() : Root(0), Cur(0) {}
  441. explicit ParentLastIterator(Node* root) : Root(root), Cur(0)
  442. {
  443. reset();
  444. }
  445. void reset()
  446. {
  447. Cur = getMin(Root);
  448. }
  449. bool atEnd() const
  450. {
  451. return Cur==0;
  452. }
  453. Node* getNode()
  454. {
  455. return Cur;
  456. }
  457. ParentLastIterator& operator=(const ParentLastIterator& src)
  458. {
  459. Root = src.Root;
  460. Cur = src.Cur;
  461. return (*this);
  462. }
  463. void operator++(int)
  464. {
  465. inc();
  466. }
  467. Node* operator -> ()
  468. {
  469. return getNode();
  470. }
  471. Node& operator* ()
  472. {
  473. _IRR_DEBUG_BREAK_IF(atEnd()) // access violation
  474. return *getNode();
  475. }
  476. private:
  477. Node* getMin(Node* n)
  478. {
  479. while(n!=0 && (n->getLeftChild()!=0 || n->getRightChild()!=0))
  480. {
  481. if (n->getLeftChild())
  482. n = n->getLeftChild();
  483. else
  484. n = n->getRightChild();
  485. }
  486. return n;
  487. }
  488. void inc()
  489. {
  490. // Already at end?
  491. if (Cur==0)
  492. return;
  493. // Note: Starting point is the node as far down to the left as possible.
  494. // If current node has an uncle to the right, go to the
  495. // node as far down to the left from the uncle as possible
  496. // else just go up a level to the parent.
  497. if (Cur->isLeftChild() && Cur->getParent()->getRightChild())
  498. {
  499. Cur = getMin(Cur->getParent()->getRightChild());
  500. }
  501. else
  502. Cur = Cur->getParent();
  503. }
  504. Node* Root;
  505. Node* Cur;
  506. }; // ParentLastIterator
  507. // AccessClass is a temporary class used with the [] operator.
  508. // It makes it possible to have different behavior in situations like:
  509. // myTree["Foo"] = 32;
  510. // If "Foo" already exists update its value else insert a new element.
  511. // int i = myTree["Foo"]
  512. // If "Foo" exists return its value.
  513. class AccessClass
  514. {
  515. // Let map be the only one who can instantiate this class.
  516. friend class map<KeyType, ValueType>;
  517. public:
  518. // Assignment operator. Handles the myTree["Foo"] = 32; situation
  519. void operator=(const ValueType& value)
  520. {
  521. // Just use the Set method, it handles already exist/not exist situation
  522. Tree.set(Key,value);
  523. }
  524. // ValueType operator
  525. operator ValueType()
  526. {
  527. Node* node = Tree.find(Key);
  528. // Not found
  529. _IRR_DEBUG_BREAK_IF(node==0) // access violation
  530. return node->getValue();
  531. }
  532. private:
  533. AccessClass(map& tree, const KeyType& key) : Tree(tree), Key(key) {}
  534. AccessClass();
  535. map& Tree;
  536. const KeyType& Key;
  537. }; // AccessClass
  538. // Constructor.
  539. map() : Root(0), Size(0) {}
  540. // Destructor
  541. ~map()
  542. {
  543. clear();
  544. }
  545. // typedefs
  546. typedef KeyType key_type;
  547. typedef ValueType value_type;
  548. typedef u32 size_type;
  549. //------------------------------
  550. // Public Commands
  551. //------------------------------
  552. //! Inserts a new node into the tree
  553. /** \param keyNew: the index for this value
  554. \param v: the value to insert
  555. \return True if successful, false if it fails (already exists) */
  556. bool insert(const KeyType& keyNew, const ValueType& v)
  557. {
  558. // First insert node the "usual" way (no fancy balance logic yet)
  559. Node* newNode = new Node(keyNew,v);
  560. if (!insert(newNode))
  561. {
  562. delete newNode;
  563. return false;
  564. }
  565. // Then attend a balancing party
  566. while (!newNode->isRoot() && (newNode->getParent()->isRed()))
  567. {
  568. if (newNode->getParent()->isLeftChild())
  569. {
  570. // If newNode is a left child, get its right 'uncle'
  571. Node* newNodesUncle = newNode->getParent()->getParent()->getRightChild();
  572. if ( newNodesUncle!=0 && newNodesUncle->isRed())
  573. {
  574. // case 1 - change the colors
  575. newNode->getParent()->setBlack();
  576. newNodesUncle->setBlack();
  577. newNode->getParent()->getParent()->setRed();
  578. // Move newNode up the tree
  579. newNode = newNode->getParent()->getParent();
  580. }
  581. else
  582. {
  583. // newNodesUncle is a black node
  584. if ( newNode->isRightChild())
  585. {
  586. // and newNode is to the right
  587. // case 2 - move newNode up and rotate
  588. newNode = newNode->getParent();
  589. rotateLeft(newNode);
  590. }
  591. // case 3
  592. newNode->getParent()->setBlack();
  593. newNode->getParent()->getParent()->setRed();
  594. rotateRight(newNode->getParent()->getParent());
  595. }
  596. }
  597. else
  598. {
  599. // If newNode is a right child, get its left 'uncle'
  600. Node* newNodesUncle = newNode->getParent()->getParent()->getLeftChild();
  601. if ( newNodesUncle!=0 && newNodesUncle->isRed())
  602. {
  603. // case 1 - change the colors
  604. newNode->getParent()->setBlack();
  605. newNodesUncle->setBlack();
  606. newNode->getParent()->getParent()->setRed();
  607. // Move newNode up the tree
  608. newNode = newNode->getParent()->getParent();
  609. }
  610. else
  611. {
  612. // newNodesUncle is a black node
  613. if (newNode->isLeftChild())
  614. {
  615. // and newNode is to the left
  616. // case 2 - move newNode up and rotate
  617. newNode = newNode->getParent();
  618. rotateRight(newNode);
  619. }
  620. // case 3
  621. newNode->getParent()->setBlack();
  622. newNode->getParent()->getParent()->setRed();
  623. rotateLeft(newNode->getParent()->getParent());
  624. }
  625. }
  626. }
  627. // Color the root black
  628. Root->setBlack();
  629. return true;
  630. }
  631. //! Replaces the value if the key already exists, otherwise inserts a new element.
  632. /** \param k The index for this value
  633. \param v The new value of */
  634. void set(const KeyType& k, const ValueType& v)
  635. {
  636. Node* p = find(k);
  637. if (p)
  638. p->setValue(v);
  639. else
  640. insert(k,v);
  641. }
  642. //! Removes a node from the tree and returns it.
  643. /** The returned node must be deleted by the user
  644. \param k the key to remove
  645. \return A pointer to the node, or 0 if not found */
  646. Node* delink(const KeyType& k)
  647. {
  648. Node* p = find(k);
  649. if (p == 0)
  650. return 0;
  651. // Rotate p down to the left until it has no right child, will get there
  652. // sooner or later.
  653. while(p->getRightChild())
  654. {
  655. // "Pull up my right child and let it knock me down to the left"
  656. rotateLeft(p);
  657. }
  658. // p now has no right child but might have a left child
  659. Node* left = p->getLeftChild();
  660. // Let p's parent point to p's child instead of point to p
  661. if (p->isLeftChild())
  662. p->getParent()->setLeftChild(left);
  663. else if (p->isRightChild())
  664. p->getParent()->setRightChild(left);
  665. else
  666. {
  667. // p has no parent => p is the root.
  668. // Let the left child be the new root.
  669. setRoot(left);
  670. }
  671. // p is now gone from the tree in the sense that
  672. // no one is pointing at it, so return it.
  673. --Size;
  674. return p;
  675. }
  676. //! Removes a node from the tree and deletes it.
  677. /** \return True if the node was found and deleted */
  678. bool remove(const KeyType& k)
  679. {
  680. Node* p = find(k);
  681. return remove(p);
  682. }
  683. //! Removes a node from the tree and deletes it.
  684. /** \return True if the node was found and deleted */
  685. bool remove(Node* p)
  686. {
  687. if (p == 0)
  688. {
  689. return false;
  690. }
  691. // Rotate p down to the left until it has no right child, will get there
  692. // sooner or later.
  693. while(p->getRightChild())
  694. {
  695. // "Pull up my right child and let it knock me down to the left"
  696. rotateLeft(p);
  697. }
  698. // p now has no right child but might have a left child
  699. Node* left = p->getLeftChild();
  700. // Let p's parent point to p's child instead of point to p
  701. if (p->isLeftChild())
  702. p->getParent()->setLeftChild(left);
  703. else if (p->isRightChild())
  704. p->getParent()->setRightChild(left);
  705. else
  706. {
  707. // p has no parent => p is the root.
  708. // Let the left child be the new root.
  709. setRoot(left);
  710. }
  711. // p is now gone from the tree in the sense that
  712. // no one is pointing at it. Let's get rid of it.
  713. delete p;
  714. --Size;
  715. return true;
  716. }
  717. //! Clear the entire tree
  718. void clear()
  719. {
  720. ParentLastIterator i(getParentLastIterator());
  721. while(!i.atEnd())
  722. {
  723. Node* p = i.getNode();
  724. i++; // Increment it before it is deleted
  725. // else iterator will get quite confused.
  726. delete p;
  727. }
  728. Root = 0;
  729. Size= 0;
  730. }
  731. //! Is the tree empty?
  732. //! \return Returns true if empty, false if not
  733. bool empty() const
  734. {
  735. return Root == 0;
  736. }
  737. //! \deprecated Use empty() instead. This method may be removed by Irrlicht 1.9
  738. _IRR_DEPRECATED_ bool isEmpty() const
  739. {
  740. return empty();
  741. }
  742. //! Search for a node with the specified key.
  743. //! \param keyToFind: The key to find
  744. //! \return Returns 0 if node couldn't be found.
  745. Node* find(const KeyType& keyToFind) const
  746. {
  747. Node* pNode = Root;
  748. while(pNode!=0)
  749. {
  750. const KeyType& key=pNode->getKey();
  751. if (keyToFind == key)
  752. return pNode;
  753. else if (keyToFind < key)
  754. pNode = pNode->getLeftChild();
  755. else //keyToFind > key
  756. pNode = pNode->getRightChild();
  757. }
  758. return 0;
  759. }
  760. //! Gets the root element.
  761. //! \return Returns a pointer to the root node, or
  762. //! 0 if the tree is empty.
  763. Node* getRoot() const
  764. {
  765. return Root;
  766. }
  767. //! Returns the number of nodes in the tree.
  768. u32 size() const
  769. {
  770. return Size;
  771. }
  772. //! Swap the content of this map container with the content of another map
  773. /** Afterwards this object will contain the content of the other object and the other
  774. object will contain the content of this object. Iterators will afterwards be valid for
  775. the swapped object.
  776. \param other Swap content with this object */
  777. void swap(map<KeyType, ValueType>& other)
  778. {
  779. core::swap(Root, other.Root);
  780. core::swap(Size, other.Size);
  781. }
  782. //------------------------------
  783. // Public Iterators
  784. //------------------------------
  785. //! Returns an iterator
  786. Iterator getIterator() const
  787. {
  788. Iterator it(getRoot());
  789. return it;
  790. }
  791. //! Returns a Constiterator
  792. ConstIterator getConstIterator() const
  793. {
  794. Iterator it(getRoot());
  795. return it;
  796. }
  797. //! Returns a ParentFirstIterator.
  798. //! Traverses the tree from top to bottom. Typical usage is
  799. //! when storing the tree structure, because when reading it
  800. //! later (and inserting elements) the tree structure will
  801. //! be the same.
  802. ParentFirstIterator getParentFirstIterator() const
  803. {
  804. ParentFirstIterator it(getRoot());
  805. return it;
  806. }
  807. //! Returns a ParentLastIterator to traverse the tree from
  808. //! bottom to top.
  809. //! Typical usage is when deleting all elements in the tree
  810. //! because you must delete the children before you delete
  811. //! their parent.
  812. ParentLastIterator getParentLastIterator() const
  813. {
  814. ParentLastIterator it(getRoot());
  815. return it;
  816. }
  817. //------------------------------
  818. // Public Operators
  819. //------------------------------
  820. //! operator [] for access to elements
  821. /** for example myMap["key"] */
  822. AccessClass operator[](const KeyType& k)
  823. {
  824. return AccessClass(*this, k);
  825. }
  826. private:
  827. //------------------------------
  828. // Disabled methods
  829. //------------------------------
  830. // Copy constructor and assignment operator deliberately
  831. // defined but not implemented. The tree should never be
  832. // copied, pass along references to it instead.
  833. explicit map(const map& src);
  834. map& operator = (const map& src);
  835. //! Set node as new root.
  836. /** The node will be set to black, otherwise core dumps may arise
  837. (patch provided by rogerborg).
  838. \param newRoot Node which will be the new root
  839. */
  840. void setRoot(Node* newRoot)
  841. {
  842. Root = newRoot;
  843. if (Root != 0)
  844. {
  845. Root->setParent(0);
  846. Root->setBlack();
  847. }
  848. }
  849. //! Insert a node into the tree without using any fancy balancing logic.
  850. /** \return false if that key already exist in the tree. */
  851. bool insert(Node* newNode)
  852. {
  853. bool result=true; // Assume success
  854. if (Root==0)
  855. {
  856. setRoot(newNode);
  857. Size = 1;
  858. }
  859. else
  860. {
  861. Node* pNode = Root;
  862. const KeyType& keyNew = newNode->getKey();
  863. while (pNode)
  864. {
  865. const KeyType& key=pNode->getKey();
  866. if (keyNew == key)
  867. {
  868. result = false;
  869. pNode = 0;
  870. }
  871. else if (keyNew < key)
  872. {
  873. if (pNode->getLeftChild() == 0)
  874. {
  875. pNode->setLeftChild(newNode);
  876. pNode = 0;
  877. }
  878. else
  879. pNode = pNode->getLeftChild();
  880. }
  881. else // keyNew > key
  882. {
  883. if (pNode->getRightChild()==0)
  884. {
  885. pNode->setRightChild(newNode);
  886. pNode = 0;
  887. }
  888. else
  889. {
  890. pNode = pNode->getRightChild();
  891. }
  892. }
  893. }
  894. if (result)
  895. ++Size;
  896. }
  897. return result;
  898. }
  899. //! Rotate left.
  900. //! Pull up node's right child and let it knock node down to the left
  901. void rotateLeft(Node* p)
  902. {
  903. Node* right = p->getRightChild();
  904. p->setRightChild(right->getLeftChild());
  905. if (p->isLeftChild())
  906. p->getParent()->setLeftChild(right);
  907. else if (p->isRightChild())
  908. p->getParent()->setRightChild(right);
  909. else
  910. setRoot(right);
  911. right->setLeftChild(p);
  912. }
  913. //! Rotate right.
  914. //! Pull up node's left child and let it knock node down to the right
  915. void rotateRight(Node* p)
  916. {
  917. Node* left = p->getLeftChild();
  918. p->setLeftChild(left->getRightChild());
  919. if (p->isLeftChild())
  920. p->getParent()->setLeftChild(left);
  921. else if (p->isRightChild())
  922. p->getParent()->setRightChild(left);
  923. else
  924. setRoot(left);
  925. left->setRightChild(p);
  926. }
  927. //------------------------------
  928. // Private Members
  929. //------------------------------
  930. Node* Root; // The top node. 0 if empty.
  931. u32 Size; // Number of nodes in the tree
  932. };
  933. } // end namespace core
  934. } // end namespace irr
  935. #endif // __IRR_MAP_H_INCLUDED__