irrMap.h 23 KB

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