as_map.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_map.h
  25. //
  26. // This class is used for mapping a value to another
  27. //
  28. #ifndef AS_MAP_H
  29. #define AS_MAP_H
  30. template <class KEY, class VAL> struct asSMapNode;
  31. template <class KEY, class VAL> class asCMap
  32. {
  33. public:
  34. asCMap();
  35. ~asCMap();
  36. int Insert(const KEY &key, const VAL &value);
  37. int GetCount() const;
  38. const KEY &GetKey(const asSMapNode<KEY,VAL> *cursor) const;
  39. const VAL &GetValue(const asSMapNode<KEY,VAL> *cursor) const;
  40. VAL &GetValue(asSMapNode<KEY,VAL> *cursor);
  41. void Erase(asSMapNode<KEY,VAL> *cursor);
  42. void EraseAll();
  43. // Returns true as long as cursor is valid
  44. bool MoveTo(asSMapNode<KEY,VAL> **out, const KEY &key) const;
  45. bool MoveFirst(asSMapNode<KEY,VAL> **out) const;
  46. bool MoveLast(asSMapNode<KEY,VAL> **out) const;
  47. bool MoveNext(asSMapNode<KEY,VAL> **out, asSMapNode<KEY,VAL> *cursor) const;
  48. bool MovePrev(asSMapNode<KEY,VAL> **out, asSMapNode<KEY,VAL> *cursor) const;
  49. // For debugging only
  50. int CheckIntegrity(asSMapNode<KEY,VAL> *node) const;
  51. protected:
  52. void BalanceInsert(asSMapNode<KEY,VAL> *node);
  53. void BalanceErase(asSMapNode<KEY,VAL> *child, asSMapNode<KEY,VAL> *parent);
  54. int EraseAll(asSMapNode<KEY,VAL> *node);
  55. int RotateLeft(asSMapNode<KEY,VAL> *node);
  56. int RotateRight(asSMapNode<KEY,VAL> *node);
  57. asSMapNode<KEY,VAL> *root;
  58. asSMapNode<KEY,VAL> dummy;
  59. int count;
  60. };
  61. //---------------------------------------------------------------------------
  62. // Implementation
  63. // Properties of a Red-Black Tree
  64. //
  65. // 1. The root is always black
  66. // 2. All single paths from the root to leafs
  67. // contain the same amount of black nodes
  68. // 3. No red node can have a red node as parent
  69. #define ISRED(x) ((x != 0) && (x)->isRed)
  70. #define ISBLACK(x) (!ISRED(x))
  71. template <class KEY, class VAL> struct asSMapNode
  72. {
  73. asSMapNode() {parent = 0; left = 0; right = 0; isRed = true;}
  74. asSMapNode *parent;
  75. asSMapNode *left;
  76. asSMapNode *right;
  77. bool isRed;
  78. KEY key;
  79. VAL value;
  80. };
  81. template <class KEY, class VAL>
  82. asCMap<KEY, VAL>::asCMap()
  83. {
  84. root = 0;
  85. count = 0;
  86. }
  87. template <class KEY, class VAL>
  88. asCMap<KEY, VAL>::~asCMap()
  89. {
  90. EraseAll();
  91. }
  92. template <class KEY, class VAL>
  93. void asCMap<KEY, VAL>::EraseAll()
  94. {
  95. EraseAll(root);
  96. root = 0;
  97. }
  98. template <class KEY, class VAL>
  99. int asCMap<KEY, VAL>::EraseAll(asSMapNode<KEY, VAL> *p)
  100. {
  101. if( p == 0 ) return -1;
  102. EraseAll( p->left );
  103. EraseAll( p->right );
  104. typedef asSMapNode<KEY,VAL> node_t;
  105. asDELETE(p,node_t);
  106. count--;
  107. return 0;
  108. }
  109. template <class KEY, class VAL>
  110. int asCMap<KEY, VAL>::GetCount() const
  111. {
  112. return count;
  113. }
  114. template <class KEY, class VAL>
  115. int asCMap<KEY, VAL>::Insert(const KEY &key, const VAL &value)
  116. {
  117. typedef asSMapNode<KEY,VAL> node_t;
  118. asSMapNode<KEY,VAL> *nnode = asNEW(node_t);
  119. nnode->key = key;
  120. nnode->value = value;
  121. // Insert the node
  122. if( root == 0 )
  123. root = nnode;
  124. else
  125. {
  126. asSMapNode<KEY,VAL> *p = root;
  127. for(;;)
  128. {
  129. if( nnode->key < p->key )
  130. {
  131. if( p->left == 0 )
  132. {
  133. nnode->parent = p;
  134. p->left = nnode;
  135. break;
  136. }
  137. else
  138. p = p->left;
  139. }
  140. else
  141. {
  142. if( p->right == 0 )
  143. {
  144. nnode->parent = p;
  145. p->right = nnode;
  146. break;
  147. }
  148. else
  149. p = p->right;
  150. }
  151. }
  152. }
  153. BalanceInsert(nnode);
  154. count++;
  155. return 0;
  156. }
  157. template <class KEY, class VAL>
  158. void asCMap<KEY, VAL>::BalanceInsert(asSMapNode<KEY, VAL> *node)
  159. {
  160. // The node, that is red, can't have a red parent
  161. while( node != root && node->parent->isRed )
  162. {
  163. // Check color of uncle
  164. if( node->parent == node->parent->parent->left )
  165. {
  166. asSMapNode<KEY,VAL> *uncle = node->parent->parent->right;
  167. if( ISRED(uncle) )
  168. {
  169. // B
  170. // R R
  171. // N
  172. // Change color on parent, uncle, and grand parent
  173. node->parent->isRed = false;
  174. uncle->isRed = false;
  175. node->parent->parent->isRed = true;
  176. // Continue balancing from grand parent
  177. node = node->parent->parent;
  178. }
  179. else
  180. {
  181. // B
  182. // R B
  183. // N
  184. if( node == node->parent->right )
  185. {
  186. // Make the node a left child
  187. node = node->parent;
  188. RotateLeft(node);
  189. }
  190. // Change color on parent and grand parent
  191. // Then rotate grand parent to the right
  192. node->parent->isRed = false;
  193. node->parent->parent->isRed = true;
  194. RotateRight(node->parent->parent);
  195. }
  196. }
  197. else
  198. {
  199. asSMapNode<KEY,VAL> *uncle = node->parent->parent->left;
  200. if( ISRED(uncle) )
  201. {
  202. // B
  203. // R R
  204. // N
  205. // Change color on parent, uncle, and grand parent
  206. // Continue balancing from grand parent
  207. node->parent->isRed = false;
  208. uncle->isRed = false;
  209. node = node->parent->parent;
  210. node->isRed = true;
  211. }
  212. else
  213. {
  214. // B
  215. // B R
  216. // N
  217. if( node == node->parent->left )
  218. {
  219. // Make the node a right child
  220. node = node->parent;
  221. RotateRight(node);
  222. }
  223. // Change color on parent and grand parent
  224. // Then rotate grand parent to the right
  225. node->parent->isRed = false;
  226. node->parent->parent->isRed = true;
  227. RotateLeft(node->parent->parent);
  228. }
  229. }
  230. }
  231. root->isRed = false;
  232. }
  233. // For debugging purposes only
  234. template <class KEY, class VAL>
  235. int asCMap<KEY, VAL>::CheckIntegrity(asSMapNode<KEY, VAL> *node) const
  236. {
  237. if( node == 0 )
  238. {
  239. if( root == 0 )
  240. return 0;
  241. else if( ISRED(root) )
  242. return -1;
  243. else
  244. node = root;
  245. }
  246. int left = 0, right = 0;
  247. if( node->left )
  248. left = CheckIntegrity(node->left);
  249. if( node->right )
  250. right = CheckIntegrity(node->right);
  251. if( left != right || left == -1 )
  252. return -1;
  253. if( ISBLACK(node) )
  254. return left+1;
  255. return left;
  256. }
  257. // Returns true if successful
  258. template <class KEY, class VAL>
  259. bool asCMap<KEY, VAL>::MoveTo(asSMapNode<KEY,VAL> **out, const KEY &key) const
  260. {
  261. asSMapNode<KEY,VAL> *p = root;
  262. while( p )
  263. {
  264. if( key < p->key )
  265. p = p->left;
  266. else if( key == p->key )
  267. {
  268. if( out ) *out = p;
  269. return true;
  270. }
  271. else
  272. p = p->right;
  273. }
  274. if( out ) *out = 0;
  275. return false;
  276. }
  277. template <class KEY, class VAL>
  278. void asCMap<KEY, VAL>::Erase(asSMapNode<KEY,VAL> *cursor)
  279. {
  280. if( cursor == 0 ) return;
  281. asSMapNode<KEY,VAL> *node = cursor;
  282. //---------------------------------------------------
  283. // Choose the node that will replace the erased one
  284. asSMapNode<KEY,VAL> *remove;
  285. if( node->left == 0 || node->right == 0 )
  286. remove = node;
  287. else
  288. {
  289. remove = node->right;
  290. while( remove->left ) remove = remove->left;
  291. }
  292. //--------------------------------------------------
  293. // Remove the node
  294. asSMapNode<KEY,VAL> *child;
  295. if( remove->left )
  296. child = remove->left;
  297. else
  298. child = remove->right;
  299. if( child ) child->parent = remove->parent;
  300. if( remove->parent )
  301. {
  302. if( remove == remove->parent->left )
  303. remove->parent->left = child;
  304. else
  305. remove->parent->right = child;
  306. }
  307. else
  308. root = child;
  309. // If we remove a black node we must make sure the tree is balanced
  310. if( ISBLACK(remove) )
  311. BalanceErase(child, remove->parent);
  312. //----------------------------------------
  313. // Replace the erased node with the removed one
  314. if( remove != node )
  315. {
  316. if( node->parent )
  317. {
  318. if( node->parent->left == node )
  319. node->parent->left = remove;
  320. else
  321. node->parent->right = remove;
  322. }
  323. else
  324. root = remove;
  325. remove->isRed = node->isRed;
  326. remove->parent = node->parent;
  327. remove->left = node->left;
  328. if( remove->left ) remove->left->parent = remove;
  329. remove->right = node->right;
  330. if( remove->right ) remove->right->parent = remove;
  331. }
  332. typedef asSMapNode<KEY,VAL> node_t;
  333. asDELETE(node,node_t);
  334. count--;
  335. }
  336. // Call method only if removed node was black
  337. // child is the child of the removed node
  338. template <class KEY, class VAL>
  339. void asCMap<KEY, VAL>::BalanceErase(asSMapNode<KEY, VAL> *child, asSMapNode<KEY, VAL> *parent)
  340. {
  341. // If child is red
  342. // Color child black
  343. // Terminate
  344. // These tests assume brother is to the right.
  345. // 1. Brother is red
  346. // Color parent red and brother black
  347. // Rotate parent left
  348. // Transforms to 2b
  349. // 2a. Parent and brother is black, brother's children are black
  350. // Color brother red
  351. // Continue test with parent as child
  352. // 2b. Parent is red, brother is black, brother's children are black
  353. // Color parent black and brother red
  354. // Terminate
  355. // 3. Brother is black, and brother's left is red and brother's right is black
  356. // Color brother red and brother's left black
  357. // Rotate brother to right
  358. // Transforms to 4.
  359. // 4. Brother is black, brother's right is red
  360. // Color brother's right black
  361. // Color brother to color of parent
  362. // Color parent black
  363. // Rotate parent left
  364. // Terminate
  365. while( child != root && ISBLACK(child) )
  366. {
  367. if( child == parent->left )
  368. {
  369. asSMapNode<KEY,VAL> *brother = parent->right;
  370. // Case 1
  371. if( ISRED(brother) )
  372. {
  373. brother->isRed = false;
  374. parent->isRed = true;
  375. RotateLeft(parent);
  376. brother = parent->right;
  377. }
  378. // Case 2
  379. if( brother == 0 ) break;
  380. if( ISBLACK(brother->left) && ISBLACK(brother->right) )
  381. {
  382. // Case 2b
  383. if( ISRED(parent) )
  384. {
  385. parent->isRed = false;
  386. brother->isRed = true;
  387. break;
  388. }
  389. brother->isRed = true;
  390. child = parent;
  391. parent = child->parent;
  392. }
  393. else
  394. {
  395. // Case 3
  396. if( ISBLACK(brother->right) )
  397. {
  398. brother->left->isRed = false;
  399. brother->isRed = true;
  400. RotateRight(brother);
  401. brother = parent->right;
  402. }
  403. // Case 4
  404. brother->isRed = parent->isRed;
  405. parent->isRed = false;
  406. brother->right->isRed = false;
  407. RotateLeft(parent);
  408. break;
  409. }
  410. }
  411. else
  412. {
  413. asSMapNode<KEY,VAL> *brother = parent->left;
  414. // Case 1
  415. if( ISRED(brother) )
  416. {
  417. brother->isRed = false;
  418. parent->isRed = true;
  419. RotateRight(parent);
  420. brother = parent->left;
  421. }
  422. // Case 2
  423. if( brother == 0 ) break;
  424. if( ISBLACK(brother->left) && ISBLACK(brother->right) )
  425. {
  426. // Case 2b
  427. if( ISRED(parent) )
  428. {
  429. parent->isRed = false;
  430. brother->isRed = true;
  431. break;
  432. }
  433. brother->isRed = true;
  434. child = parent;
  435. parent = child->parent;
  436. }
  437. else
  438. {
  439. // Case 3
  440. if( ISBLACK(brother->left) )
  441. {
  442. brother->right->isRed = false;
  443. brother->isRed = true;
  444. RotateLeft(brother);
  445. brother = parent->left;
  446. }
  447. // Case 4
  448. brother->isRed = parent->isRed;
  449. parent->isRed = false;
  450. brother->left->isRed = false;
  451. RotateRight(parent);
  452. break;
  453. }
  454. }
  455. }
  456. if( child )
  457. child->isRed = false;
  458. }
  459. template <class KEY, class VAL>
  460. int asCMap<KEY, VAL>::RotateRight(asSMapNode<KEY, VAL> *node)
  461. {
  462. // P L //
  463. // / \ / \ //
  464. // L R => Ll P //
  465. // / \ / \ //
  466. // Ll Lr Lr R //
  467. if( node->left == 0 ) return -1;
  468. asSMapNode<KEY,VAL> *left = node->left;
  469. // Update parent
  470. if( node->parent )
  471. {
  472. asSMapNode<KEY,VAL> *parent = node->parent;
  473. if( parent->left == node )
  474. parent->left = left;
  475. else
  476. parent->right = left;
  477. left->parent = parent;
  478. }
  479. else
  480. {
  481. root = left;
  482. left->parent = 0;
  483. }
  484. // Move left's right child to node's left child
  485. node->left = left->right;
  486. if( node->left ) node->left->parent = node;
  487. // Put node as left's right child
  488. left->right = node;
  489. node->parent = left;
  490. return 0;
  491. }
  492. template <class KEY, class VAL>
  493. int asCMap<KEY, VAL>::RotateLeft(asSMapNode<KEY, VAL> *node)
  494. {
  495. // P R //
  496. // / \ / \ //
  497. // L R => P Rr //
  498. // / \ / \ //
  499. // Rl Rr L Rl //
  500. if( node->right == 0 ) return -1;
  501. asSMapNode<KEY,VAL> *right = node->right;
  502. // Update parent
  503. if( node->parent )
  504. {
  505. asSMapNode<KEY,VAL> *parent = node->parent;
  506. if( parent->right == node )
  507. parent->right = right;
  508. else
  509. parent->left = right;
  510. right->parent = parent;
  511. }
  512. else
  513. {
  514. root = right;
  515. right->parent = 0;
  516. }
  517. // Move right's left child to node's right child
  518. node->right = right->left;
  519. if( node->right ) node->right->parent = node;
  520. // Put node as right's left child
  521. right->left = node;
  522. node->parent = right;
  523. return 0;
  524. }
  525. template <class KEY, class VAL>
  526. const VAL &asCMap<KEY, VAL>::GetValue(const asSMapNode<KEY,VAL> *cursor) const
  527. {
  528. if( cursor == 0 )
  529. return dummy.value;
  530. return cursor->value;
  531. }
  532. template <class KEY, class VAL>
  533. VAL &asCMap<KEY, VAL>::GetValue(asSMapNode<KEY,VAL> *cursor)
  534. {
  535. if( cursor == 0 )
  536. return dummy.value;
  537. return cursor->value;
  538. }
  539. template <class KEY, class VAL>
  540. const KEY &asCMap<KEY, VAL>::GetKey(const asSMapNode<KEY,VAL> *cursor) const
  541. {
  542. if( cursor == 0 )
  543. return dummy.key;
  544. return cursor->key;
  545. }
  546. template <class KEY, class VAL>
  547. bool asCMap<KEY, VAL>::MoveFirst(asSMapNode<KEY,VAL> **out) const
  548. {
  549. *out = root;
  550. if( root == 0 ) return false;
  551. while( (*out)->left )
  552. *out = (*out)->left;
  553. return true;
  554. }
  555. template <class KEY, class VAL>
  556. bool asCMap<KEY, VAL>::MoveLast(asSMapNode<KEY,VAL> **out) const
  557. {
  558. *out = root;
  559. if( root == 0 ) return false;
  560. while( (*out)->right )
  561. *out = (*out)->right;
  562. return true;
  563. }
  564. template <class KEY, class VAL>
  565. bool asCMap<KEY, VAL>::MoveNext(asSMapNode<KEY,VAL> **out, asSMapNode<KEY,VAL> *cursor) const
  566. {
  567. if( cursor == 0 )
  568. {
  569. *out = 0;
  570. return false;
  571. }
  572. if( cursor->right == 0 )
  573. {
  574. // Move upwards until we find a parent node to the right
  575. while( cursor->parent && cursor->parent->right == cursor )
  576. cursor = cursor->parent;
  577. cursor = cursor->parent;
  578. *out = cursor;
  579. if( cursor == 0 )
  580. return false;
  581. return true;
  582. }
  583. cursor = cursor->right;
  584. while( cursor->left )
  585. cursor = cursor->left;
  586. *out = cursor;
  587. return true;
  588. }
  589. template <class KEY, class VAL>
  590. bool asCMap<KEY, VAL>::MovePrev(asSMapNode<KEY,VAL> **out, asSMapNode<KEY,VAL> *cursor) const
  591. {
  592. if( cursor == 0 )
  593. {
  594. *out = 0;
  595. return false;
  596. }
  597. if( cursor->left == 0 )
  598. {
  599. // Move upwards until we find a parent node to the left
  600. while( cursor->parent && cursor->parent->left == cursor )
  601. cursor = cursor->parent;
  602. cursor = cursor->parent;
  603. *out = cursor;
  604. if( cursor == 0 )
  605. return false;
  606. return true;
  607. }
  608. cursor = cursor->left;
  609. while( cursor->right )
  610. cursor = cursor->right;
  611. *out = cursor;
  612. return true;
  613. }
  614. #endif