map_vs.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. // RAVEN STANDARD TEMPLATE LIBRARY
  3. // (c) 2002 Activision
  4. //
  5. //
  6. // Map
  7. // ---
  8. // This map is based on a red black tree, which guarentees balanced data, no mater what
  9. // order elements are added. The map uses a memory pool for storage of node data.
  10. //
  11. //
  12. ////////////////////////////////////////////////////////////////////////////////////////
  13. #if !defined(RATL_MAP_VS_INC)
  14. #define RATL_MAP_VS_INC
  15. ////////////////////////////////////////////////////////////////////////////////////////
  16. // Includes
  17. ////////////////////////////////////////////////////////////////////////////////////////
  18. #if defined(RA_DEBUG_LINKING)
  19. #pragma message("...including map_vs.h")
  20. #endif
  21. #if !defined(RATL_COMMON_INC)
  22. #include "ratl_common.h"
  23. #endif
  24. #if !defined(RATL_POOL_VS_INC)
  25. #include "pool_vs.h"
  26. #endif
  27. namespace ratl
  28. {
  29. // this is private to the set, but you have no access to it, soooo..
  30. class tree_node
  31. {
  32. int mParent;
  33. int mLeft;
  34. int mRight;
  35. public:
  36. enum
  37. {
  38. RED_BIT = 0x40000000, // to save space we are putting the red bool in a high bit
  39. // this is in the parent only
  40. NULL_NODE = 0x3fffffff, // this must not have the red bit set
  41. };
  42. void init()
  43. {
  44. mLeft = NULL_NODE;
  45. mRight = NULL_NODE;
  46. mParent = NULL_NODE | RED_BIT;
  47. }
  48. int left() const
  49. {
  50. return mLeft;
  51. }
  52. int right() const
  53. {
  54. return mRight;
  55. }
  56. int parent() const
  57. {
  58. return mParent & (~RED_BIT);
  59. }
  60. bool red() const
  61. {
  62. return !!(mParent & RED_BIT);
  63. }
  64. void set_left(int l)
  65. {
  66. mLeft = l;
  67. }
  68. void set_right(int r)
  69. {
  70. mRight = r;
  71. }
  72. void set_parent(int p)
  73. {
  74. mParent &= RED_BIT;
  75. mParent |= p;
  76. }
  77. void set_red(bool isRed)
  78. {
  79. if (isRed)
  80. {
  81. mParent |= RED_BIT;
  82. }
  83. else
  84. {
  85. mParent &= ~RED_BIT;
  86. }
  87. }
  88. };
  89. //fixme void *, comparison function pointer-ize this for code bloat.
  90. template<class T, int IS_MULTI>
  91. class tree_base
  92. {
  93. public:
  94. typedef typename T TStorageTraits;
  95. typedef typename T::TValue TTValue;
  96. ////////////////////////////////////////////////////////////////////////////////////
  97. // Capacity Enum
  98. ////////////////////////////////////////////////////////////////////////////////////
  99. enum
  100. {
  101. CAPACITY = T::CAPACITY,
  102. };
  103. private:
  104. pool_base<TStorageTraits> mPool; // The Allocation Data Pool
  105. int mRoot;
  106. int mLastAdd;
  107. void link_left(int node,int left)
  108. {
  109. T::node(mPool[node]).set_left(left);
  110. if (left!=tree_node::NULL_NODE)
  111. {
  112. T::node(mPool[left]).set_parent(node);
  113. }
  114. }
  115. void link_right(int node,int right)
  116. {
  117. T::node(mPool[node]).set_right(right);
  118. if (right!=tree_node::NULL_NODE)
  119. {
  120. T::node(mPool[right]).set_parent(node);
  121. }
  122. }
  123. ////////////////////////////////////////////////////////////////////////////////////
  124. // This is the map internal recursive find function - do not use externally
  125. ////////////////////////////////////////////////////////////////////////////////////
  126. int find_internal(const TTValue &key, int at) const
  127. {
  128. // FAIL TO FIND?
  129. //---------------
  130. if (at==tree_node::NULL_NODE)
  131. {
  132. return tree_node::NULL_NODE;
  133. }
  134. // Should We Search Left?
  135. //------------------------
  136. if (key<mPool[at])
  137. {
  138. return find_internal(key, T::node(mPool[at]).left());
  139. }
  140. // Should We Search Right?
  141. //------------------------
  142. else if (mPool[at]<key)
  143. {
  144. return find_internal(key, T::node(mPool[at]).right());
  145. }
  146. // FOUND!
  147. //--------
  148. return at;
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////////
  151. // This is the map internal recursive find function - do not use externally
  152. ////////////////////////////////////////////////////////////////////////////////////
  153. int find_internal(const TTValue &key, int target, int at, int& parent) const
  154. {
  155. // FAIL TO FIND?
  156. //---------------
  157. if (at==tree_node::NULL_NODE)
  158. {
  159. parent = tree_node::NULL_NODE;
  160. return tree_node::NULL_NODE;
  161. }
  162. // FOUND!
  163. //--------
  164. if (at==target)
  165. {
  166. if (at==mRoot)
  167. {
  168. parent = tree_node::NULL_NODE;
  169. }
  170. return at;
  171. }
  172. // Should We Search Left?
  173. //------------------------
  174. if (key<mPool[at])
  175. {
  176. parent = at;
  177. return find_internal(key, target, T::node(mPool[at]).left(), parent);
  178. }
  179. parent = at;
  180. return find_internal(key, target, T::node(mPool[at]).right(), parent);
  181. }
  182. ////////////////////////////////////////////////////////////////////////////////////
  183. // This is the map internal recursive insertion - do not use externally
  184. ////////////////////////////////////////////////////////////////////////////////////
  185. int insert_internal(const TTValue &key, int &at)
  186. {
  187. // If At Is A NULL_NODE, We Have Found A Leaf.
  188. //----------------------------------------------
  189. if (at==tree_node::NULL_NODE)
  190. {
  191. if (mRoot==tree_node::NULL_NODE)
  192. {
  193. mRoot = mLastAdd;
  194. }
  195. return tree_node::NULL_NODE; // There Are No Excess Red Children (No Childeren At All, Actually)
  196. }
  197. int nxtChild; // The Child We Will Eventually Add Underneath
  198. int altChild; // The "other" Child
  199. bool nxtRotateLeft;
  200. int excessRedChild; // If The Insert Results In An Excess Red Child, This Will Be It
  201. // Choose Which Side To Add The New Node Under
  202. //---------------------------------------------
  203. if (key < mPool[at]) // The Key Classes Must Support A < Operator
  204. {
  205. int tmp=T::node(mPool[at]).left();
  206. excessRedChild = insert_internal(key,tmp);
  207. link_left(at,tmp);//T::node(mPool[at]).set_left(tmp);
  208. if (tmp==tree_node::NULL_NODE)
  209. {
  210. link_left(at,mLastAdd);//T::node(mPool[at]).set_left(mLastAdd); // If mLeft Of The Current Node Is NULL, We Must Have Added DIRECTLY Below nAt
  211. }
  212. nxtChild = T::node(mPool[at]).left();
  213. altChild = T::node(mPool[at]).right();
  214. nxtRotateLeft = false;
  215. }
  216. else if (mPool[at] < key)
  217. {
  218. int tmp=T::node(mPool[at]).right();
  219. excessRedChild = insert_internal(key,tmp);
  220. link_right(at,tmp); // T::node(mPool[at]).set_right(tmp);
  221. if (tmp==tree_node::NULL_NODE)
  222. {
  223. link_right(at,mLastAdd); // T::node(mPool[at]).set_right(mLastAdd); // If mRight Of The Current Node Is NULL, We Must Have Added DIRECTLY Below nAt
  224. }
  225. nxtChild = T::node(mPool[at]).right();
  226. altChild = T::node(mPool[at]).left();
  227. nxtRotateLeft = true;
  228. }
  229. // Exact Match
  230. //-------------
  231. else
  232. {
  233. // the node of interest is at
  234. return tree_node::NULL_NODE;
  235. }
  236. // If The Add Resulted In An Excess Red Child, We Need To Change Colors And Rotate
  237. //---------------------------------------------------------------------------------
  238. if (excessRedChild!=tree_node::NULL_NODE)
  239. {
  240. // If Both Childeren Are Red, Just Switch And Be Done
  241. //----------------------------------------------------
  242. if (T::node(mPool[at]).right()!=tree_node::NULL_NODE &&
  243. T::node(mPool[at]).left()!=tree_node::NULL_NODE &&
  244. T::node(mPool[T::node(mPool[at]).right()]).red() &&
  245. T::node(mPool[T::node(mPool[at]).left()]).red())
  246. {
  247. set_colors(T::node(mPool[at]), true, false);
  248. }
  249. else
  250. {
  251. int excessRedChildCompare =
  252. (nxtRotateLeft)?(T::node(mPool[nxtChild]).right()):(T::node(mPool[nxtChild]).left());
  253. if (excessRedChild==excessRedChildCompare)
  254. {
  255. // Single Rotation
  256. //-----------------
  257. rotate(at, nxtRotateLeft);
  258. }
  259. else
  260. {
  261. if (nxtRotateLeft)
  262. {
  263. int nxt=T::node(mPool[at]).right();
  264. rotate(nxt, false);
  265. link_right(at,nxt); // T::node(mPool[at]).set_right(nxt);
  266. }
  267. else
  268. {
  269. int nxt=T::node(mPool[at]).left();
  270. rotate(nxt,true);
  271. link_left(at,nxt); // T::node(mPool[at]).set_left(nxt);
  272. }
  273. rotate(at, nxtRotateLeft);
  274. }
  275. set_colors(T::node(mPool[at]), false, true);
  276. }
  277. }
  278. if (T::node(mPool[at]).red())
  279. {
  280. if (T::node(mPool[at]).left()!=tree_node::NULL_NODE &&
  281. T::node(mPool[T::node(mPool[at]).left()]).red())
  282. {
  283. return T::node(mPool[at]).left();
  284. }
  285. if (T::node(mPool[at]).right()!=tree_node::NULL_NODE &&
  286. T::node(mPool[T::node(mPool[at]).right()]).red())
  287. {
  288. return T::node(mPool[at]).right();
  289. }
  290. }
  291. return tree_node::NULL_NODE;
  292. }
  293. ////////////////////////////////////////////////////////////////////////////////////
  294. // This is the map internal recursive erase - do not use externally
  295. ////////////////////////////////////////////////////////////////////////////////////
  296. bool erase_internal(const TTValue &key, int& at)
  297. {
  298. // If At Is A NULL_NODE, We Have Found A Leaf.
  299. //---------------------------------------------
  300. if (at==tree_node::NULL_NODE)
  301. {
  302. return true;
  303. }
  304. //==============================================================================
  305. // Now The Question Is, Do We Need To Continue Searching?
  306. //==============================================================================
  307. // Recurse To The Left?
  308. //----------------------
  309. if (key < mPool[at])
  310. {
  311. int a=T::node(mPool[at]).left();
  312. bool r=erase_internal(key, a);
  313. link_left(at,a); // T::node(mPool[at]).set_left(a);
  314. if (!r) // If It Was Not Red, We Need To Rebalance
  315. {
  316. return rebalance(at, true);
  317. }
  318. return true;
  319. }
  320. // Recurse To The Right?
  321. //-----------------------
  322. if (mPool[at] < key)
  323. {
  324. int a=T::node(mPool[at]).right();
  325. bool r=erase_internal(key, a);
  326. link_right(at,a); // T::node(mPool[at]).set_right(a);
  327. if (!r) // If It Was Not Red, We Need To Rebalance
  328. {
  329. return rebalance(at, false);
  330. }
  331. return true;
  332. }
  333. //==============================================================================
  334. // At This Point, We Must Have Discovered An Exact Match For Our Key
  335. //==============================================================================
  336. // Are There Any Open Childeren Slots?
  337. //-------------------------------------
  338. if (T::node(mPool[at]).left()==tree_node::NULL_NODE || T::node(mPool[at]).right()==tree_node::NULL_NODE)
  339. {
  340. bool atWasRed = T::node(mPool[at]).red();
  341. int oldAt = at;
  342. at = (T::node(mPool[at]).left()==tree_node::NULL_NODE)?(T::node(mPool[at]).right()):(T::node(mPool[at]).left()); // If Left Is Null, At Goes Right
  343. // Actually Free It!
  344. //-------------------
  345. mPool.free(oldAt);
  346. // If We Are Now At A Null Node, Just Return
  347. //-------------------------------------------
  348. if (at==tree_node::NULL_NODE)
  349. {
  350. return atWasRed;
  351. }
  352. // Otherwise, Mark The New Child As Red, And Return That Fact Up
  353. //---------------------------------------------------------------
  354. T::node(mPool[at]).set_red(false);
  355. return true;
  356. }
  357. //==============================================================================
  358. // There Are No Childeren To Link With, We Are In The Middle Of The Tree.
  359. // We Need To Find An Open Leaf, Swap Data With That Leaf, and Then Go Find It
  360. //==============================================================================
  361. // Find A Successor Leaf
  362. //-----------------------
  363. int at_parent = T::node(mPool[at]).parent();
  364. int successor = T::node(mPool[at]).right();
  365. int parent_successor=-1;
  366. while(T::node(mPool[successor]).left()!=tree_node::NULL_NODE)
  367. {
  368. parent_successor=successor;
  369. successor = T::node(mPool[successor]).left();
  370. }
  371. int successor_right = T::node(mPool[successor]).right();
  372. link_left(successor,T::node(mPool[at]).left());
  373. bool red=T::node(mPool[successor]).red();
  374. T::node(mPool[successor]).set_red(T::node(mPool[at]).red());
  375. T::node(mPool[at]).set_red(red);
  376. if (parent_successor!=-1)
  377. {
  378. link_right(successor,T::node(mPool[at]).right());
  379. link_left(parent_successor,at);
  380. }
  381. else
  382. {
  383. link_right(successor,at);
  384. }
  385. if (at_parent!=tree_node::NULL_NODE)
  386. {
  387. if (T::node(mPool[at_parent]).left()==at)
  388. {
  389. //my parents left child
  390. link_left(at_parent,successor);
  391. }
  392. else
  393. {
  394. assert(T::node(mPool[at_parent]).right()==at); // better be my parents right child then
  395. link_right(at_parent,successor);
  396. }
  397. }
  398. link_left(at,tree_node::NULL_NODE);
  399. link_right(at,successor_right);
  400. at=successor;
  401. int a=T::node(mPool[at]).right();
  402. bool r=erase_internal(key, a);
  403. link_right(at,a); // T::node(mPool[at]).set_right(a);
  404. // And Keep Going
  405. //----------------
  406. if (!r) // If It Was Not Red, We Need To Rebalance
  407. {
  408. return rebalance(at, false);
  409. }
  410. return true;
  411. }
  412. ////////////////////////////////////////////////////////////////////////////////////
  413. // HELPER: Change the color of a node and children
  414. ////////////////////////////////////////////////////////////////////////////////////
  415. void set_colors(tree_node& at, bool red, bool childRed)
  416. {
  417. at.set_red(red);
  418. if (at.left()!=tree_node::NULL_NODE)
  419. {
  420. T::node(mPool[at.left()]).set_red(childRed);
  421. }
  422. if (at.right()!=tree_node::NULL_NODE)
  423. {
  424. T::node(mPool[at.right()]).set_red(childRed);
  425. }
  426. }
  427. ////////////////////////////////////////////////////////////////////////////////////
  428. // HELPER: Rotate node located at (at) either left or right
  429. ////////////////////////////////////////////////////////////////////////////////////
  430. void rotate(int& at, bool left)
  431. {
  432. int t;
  433. if (left)
  434. {
  435. assert(T::node(mPool[at]).right()!=tree_node::NULL_NODE);
  436. t = T::node(mPool[at]).right();
  437. link_right(at,T::node(mPool[t]).left()); // T::node(mPool[at]).set_right(T::node(mPool[t]).left());
  438. link_left(t,at); // T::node(mPool[t]).set_left(at);
  439. at = t;
  440. }
  441. else
  442. {
  443. assert(T::node(mPool[at]).left()!=tree_node::NULL_NODE);
  444. t = T::node(mPool[at]).left();
  445. link_left(at,T::node(mPool[t]).right()); // T::node(mPool[at]).set_left(T::node(mPool[t]).right());
  446. link_right(t,at); //T::node(mPool[t]).set_right(at);
  447. at = t;
  448. }
  449. }
  450. ////////////////////////////////////////////////////////////////////////////////////
  451. // HELPER: Localally rebalance the tree here
  452. ////////////////////////////////////////////////////////////////////////////////////
  453. bool rebalance(int& at, bool left)
  454. {
  455. // Decide Which Child, Left Or Right?
  456. //------------------------------------
  457. int w = (left)?(T::node(mPool[at]).right()):(T::node(mPool[at]).left()); // w is the child of at
  458. if (w==tree_node::NULL_NODE)
  459. {
  460. bool atWasRed = T::node(mPool[at]).red(); // Remember what mPool[at] WAS
  461. T::node(mPool[at]).set_red(false); // Mark mPool[at] as BLACK
  462. return atWasRed; // Return what it used to be
  463. }
  464. // Get A Reference To The Child W, And Record It's Children x And y
  465. //------------------------------------------------------------------
  466. tree_node& wAt = T::node(mPool[w]);
  467. int x = (left)?(wAt.left()):(wAt.right());// x and y are the grand children of at
  468. int y = (left)?(wAt.right()):(wAt.left());
  469. // Is The Child Black?
  470. //---------------------
  471. if (!wAt.red())
  472. {
  473. // If Both X and Y are Empty, Or Both Are Red
  474. //--------------------------------------------
  475. if ((x==tree_node::NULL_NODE || !T::node(mPool[x]).red()) &&
  476. (y==tree_node::NULL_NODE || !T::node(mPool[y]).red()))
  477. {
  478. bool atWasRed = T::node(mPool[at]).red(); // Remember what mPool[at] WAS
  479. T::node(mPool[at]).set_red(false); // Mark mPool[at] as BLACK
  480. wAt.set_red(true); // Mark The Child As RED
  481. return atWasRed; // Return what it used to be
  482. }
  483. // If Y Is Valid
  484. //---------------
  485. if (y!=tree_node::NULL_NODE && T::node(mPool[y]).red())
  486. {
  487. wAt.set_red(T::node(mPool[at]).red());
  488. rotate(at, left);
  489. T::node(mPool[T::node(mPool[at]).left()]).set_red(false);
  490. T::node(mPool[T::node(mPool[at]).right()]).set_red(false);
  491. return true;
  492. }
  493. // X Must Be Valid
  494. //-----------------
  495. T::node(mPool[x]).set_red(T::node(mPool[at]).red());
  496. T::node(mPool[at]).set_red(false);
  497. if (left)
  498. {
  499. int r=T::node(mPool[at]).right();
  500. rotate(r,false);
  501. link_right(at,r); // T::node(mPool[at]).set_right(r);
  502. }
  503. else
  504. {
  505. int r=T::node(mPool[at]).left();
  506. rotate(r,true);
  507. link_left(at,r); // T::node(mPool[at]).set_left(r);
  508. }
  509. rotate(at, left);
  510. return true;
  511. }
  512. // The Child Must Have Been Red
  513. //------------------------------
  514. wAt.set_red(T::node(mPool[at]).red()); // Switch Child Color
  515. T::node(mPool[at]).set_red(true);
  516. rotate(at, left); // Rotate At
  517. // Select The Next Rebalance Child And Recurse
  518. //----------------------------------------------
  519. if (left)
  520. {
  521. int r=T::node(mPool[at]).left();
  522. rebalance(r,true);
  523. link_left(at,r); // T::node(mPool[at]).set_left(r);
  524. }
  525. else
  526. {
  527. int r=T::node(mPool[at]).right();
  528. rebalance(r,false);
  529. link_right(at,r); //T::node(mPool[at]).set_right(r);
  530. }
  531. return true;
  532. }
  533. ////////////////////////////////////////////////////////////////////////////////////
  534. // This is the map internal recursive front function - do not use externally
  535. ////////////////////////////////////////////////////////////////////////////////////
  536. int front(int at) const
  537. {
  538. if (at!=tree_node::NULL_NODE &&
  539. T::node(mPool[at]).left()!=tree_node::NULL_NODE)
  540. {
  541. return front(T::node(mPool[at]).left());
  542. }
  543. return at;
  544. }
  545. ////////////////////////////////////////////////////////////////////////////////////
  546. // This is the map internal recursive back function - do not use externally
  547. ////////////////////////////////////////////////////////////////////////////////////
  548. int back(int at) const
  549. {
  550. if (at!=tree_node::NULL_NODE && T::node(mPool[at]).right()!=tree_node::NULL_NODE)
  551. {
  552. return back(T::node(mPool[at]).right());
  553. }
  554. return at;
  555. }
  556. protected:
  557. int front() const
  558. {
  559. return front(mRoot);
  560. }
  561. int back() const
  562. {
  563. return back(mRoot);
  564. }
  565. ////////////////////////////////////////////////////////////////////////////////////
  566. // This is the map internal recursive next function - do not use externally
  567. ////////////////////////////////////////////////////////////////////////////////////
  568. int next(int at) const
  569. {
  570. assert(at!=tree_node::NULL_NODE);
  571. const TTValue& kAt = mPool[at];
  572. const tree_node& nAt = T::node(kAt);
  573. if (nAt.right()!=tree_node::NULL_NODE)
  574. {
  575. return front(nAt.right());
  576. }
  577. int child = at;
  578. int parent = tree_node::NULL_NODE;
  579. find_internal(kAt, at, mRoot, parent);
  580. while(parent!=tree_node::NULL_NODE && (child==T::node(mPool[parent]).right()))
  581. {
  582. child = parent;
  583. find_internal(mPool[parent], parent, mRoot, parent);
  584. }
  585. return parent;
  586. }
  587. ////////////////////////////////////////////////////////////////////////////////////
  588. // This is the map internal recursive previous function - do not use externally
  589. ////////////////////////////////////////////////////////////////////////////////////
  590. int previous(int at) const
  591. {
  592. assert(at!=tree_node::NULL_NODE);
  593. const TTValue& kAt = mPool[at];
  594. const tree_node& nAt = T::node(mPool[at]);
  595. if (kAt.left()!=tree_node::NULL_NODE)
  596. {
  597. return back(kAt.left());
  598. }
  599. int child = at;
  600. int parent = tree_node::NULL_NODE;
  601. find_internal(nAt, at, mRoot, parent);
  602. while(parent!=tree_node::NULL_NODE && (child==T::node(mPool[parent]).left()))
  603. {
  604. child = parent;
  605. find_internal(mPool[parent], parent, mRoot, parent);
  606. }
  607. return parent;
  608. }
  609. public:
  610. ////////////////////////////////////////////////////////////////////////////////////
  611. // Constructor
  612. ////////////////////////////////////////////////////////////////////////////////////
  613. tree_base() : mRoot(tree_node::NULL_NODE), mLastAdd(-1)
  614. {
  615. }
  616. ////////////////////////////////////////////////////////////////////////////////////
  617. // How Many Objects Are In This Map
  618. ////////////////////////////////////////////////////////////////////////////////////
  619. int size() const
  620. {
  621. return (mPool.size());
  622. }
  623. ////////////////////////////////////////////////////////////////////////////////////
  624. // Are There Any Objects In This Map?
  625. ////////////////////////////////////////////////////////////////////////////////////
  626. bool empty() const
  627. {
  628. return (mPool.empty());
  629. }
  630. ////////////////////////////////////////////////////////////////////////////////////
  631. // Is This Map Filled?
  632. ////////////////////////////////////////////////////////////////////////////////////
  633. bool full() const
  634. {
  635. return (mPool.full());
  636. }
  637. ////////////////////////////////////////////////////////////////////////////////////
  638. // Clear All Data From The Map
  639. ////////////////////////////////////////////////////////////////////////////////////
  640. void clear()
  641. {
  642. mRoot = tree_node::NULL_NODE;
  643. mPool.clear();
  644. }
  645. ////////////////////////////////////////////////////////////////////////////////////
  646. // Adds Element Value At Location Key - O(log n)
  647. ////////////////////////////////////////////////////////////////////////////////////
  648. void alloc_key(const TTValue &key)
  649. {
  650. //fixme handle duplicates more sensibly?
  651. assert(!full());
  652. mLastAdd = mPool.alloc(key); // Grab A New One
  653. T::node(mPool[mLastAdd]).init(); // Initialize Our Data And Color
  654. }
  655. ////////////////////////////////////////////////////////////////////////////////////
  656. // Allocs an item, when filled, call insert_alloced
  657. ////////////////////////////////////////////////////////////////////////////////////
  658. TTValue & alloc_key()
  659. {
  660. assert(!full());
  661. mLastAdd = mPool.alloc(); // Grab A New One
  662. T::node(mPool[mLastAdd]).init(); // Initialize Our Data And Color
  663. return mPool[mLastAdd];
  664. }
  665. ////////////////////////////////////////////////////////////////////////////////////
  666. // Allocs an item (raw), when constucted, call insert_alloced
  667. ////////////////////////////////////////////////////////////////////////////////////
  668. TRatlNew *alloc_key_raw()
  669. {
  670. assert(!full());
  671. TRatlNew *ret=mPool.alloc_raw(); // Grab A New One
  672. mLastAdd = mPool.pointer_to_index(ret);
  673. T::node(mPool[mLastAdd]).init(); // Initialize Our Data And Color
  674. return ret;
  675. }
  676. template<class CAST_TO>
  677. CAST_TO *verify_alloc_key(CAST_TO *p) const
  678. {
  679. return mPool.verify_alloc(p);
  680. }
  681. void insert_alloced_key()
  682. {
  683. assert(mLastAdd>=0&&mLastAdd<CAPACITY);
  684. assert(!IS_MULTI || find_index(mPool[mLastAdd])!=tree_node::NULL_NODE); //fixme handle duplicates more sensibly?
  685. insert_internal(mPool[mLastAdd],mRoot);
  686. assert(mRoot!=tree_node::NULL_NODE);
  687. T::node(mPool[mRoot]).set_red(false);
  688. T::node(mPool[mRoot]).set_parent(tree_node::NULL_NODE);
  689. }
  690. int index_of_alloced_key() const
  691. {
  692. assert(mLastAdd>=0&&mLastAdd<CAPACITY);
  693. return mLastAdd;
  694. }
  695. ////////////////////////////////////////////////////////////////////////////////////
  696. // Removes The Element Pointed To At (it) And Decrements (it) - O((log n)^2)
  697. ////////////////////////////////////////////////////////////////////////////////////
  698. void erase_index(int i)
  699. {
  700. assert(i>=0&&i<CAPACITY);
  701. assert(mRoot!=tree_node::NULL_NODE);
  702. //fixme this is lame to have to look by key to erase
  703. erase_internal(mPool[i],mRoot);
  704. if (mRoot!=tree_node::NULL_NODE)
  705. {
  706. T::node(mPool[mRoot]).set_red(false);
  707. T::node(mPool[mRoot]).set_parent(tree_node::NULL_NODE);
  708. }
  709. }
  710. ////////////////////////////////////////////////////////////////////////////////////
  711. // Seach For A Given Key. Will Return -1 if Failed - O(log n)
  712. ////////////////////////////////////////////////////////////////////////////////////
  713. int find_index(const TTValue &key) const
  714. {
  715. return find_internal(key, mRoot);
  716. }
  717. const TTValue &index_to_key(int i) const
  718. {
  719. assert(i>=0&&i<CAPACITY);
  720. return mPool[i];
  721. }
  722. //fixme lower bound, upper bound, equal range
  723. };
  724. template<class T,int IS_MULTI>
  725. class set_base : public tree_base<T,IS_MULTI>
  726. {
  727. public:
  728. typedef typename T TStorageTraits;
  729. typedef typename T::TValue TTValue;
  730. ////////////////////////////////////////////////////////////////////////////////////
  731. // Capacity Enum
  732. ////////////////////////////////////////////////////////////////////////////////////
  733. enum
  734. {
  735. CAPACITY = T::CAPACITY
  736. };
  737. ////////////////////////////////////////////////////////////////////////////////////
  738. // Adds Element Value At Location Key - O(log n)
  739. ////////////////////////////////////////////////////////////////////////////////////
  740. void insert(const TTValue &key)
  741. {
  742. assert(!IS_MULTI || find_index(key)==tree_node::NULL_NODE); //fixme handle duplicates more sensibly?
  743. alloc_key(key);
  744. insert_alloced_key();
  745. }
  746. ////////////////////////////////////////////////////////////////////////////////////
  747. // Allocs an item, when filled, call insert_alloced
  748. ////////////////////////////////////////////////////////////////////////////////////
  749. TTValue & alloc()
  750. {
  751. return alloc_key();
  752. }
  753. ////////////////////////////////////////////////////////////////////////////////////
  754. // Allocs an item (raw), when constucted, call insert_alloced
  755. ////////////////////////////////////////////////////////////////////////////////////
  756. TRatlNew *alloc_raw()
  757. {
  758. return alloc_key_raw();
  759. }
  760. template<class CAST_TO>
  761. CAST_TO *verify_alloc(CAST_TO *p) const
  762. {
  763. return verify_alloc_key(p);
  764. }
  765. void insert_alloced()
  766. {
  767. insert_alloced_key();
  768. }
  769. ////////////////////////////////////////////////////////////////////////////////////
  770. // Removes The First Element With Key (key) - O(log n)
  771. ////////////////////////////////////////////////////////////////////////////////////
  772. void erase(const TTValue &key)
  773. {
  774. //fixme this is a double search currently
  775. int i=find_index(key);
  776. if (i!=tree_node::NULL_NODE)
  777. {
  778. erase_index(i);
  779. }
  780. }
  781. ////////////////////////////////////////////////////////////////////////////////////
  782. // Iterator
  783. //
  784. // A map is sorted in ascending order on the KEY type. ++ and -- are both
  785. // O((log n)^2) operations
  786. //
  787. ////////////////////////////////////////////////////////////////////////////////////
  788. class iterator
  789. {
  790. friend class set_base<TStorageTraits,IS_MULTI>;
  791. friend class const_iterator;
  792. int mLoc;
  793. set_base<TStorageTraits,IS_MULTI>* mOwner;
  794. public:
  795. iterator(set_base<TStorageTraits,IS_MULTI> *owner=0, int loc=tree_node::NULL_NODE) :
  796. mOwner(owner),
  797. mLoc(loc)
  798. {
  799. }
  800. iterator(const iterator &o) :
  801. mOwner(o.mOwner),
  802. mLoc(o.mLoc)
  803. {
  804. }
  805. void operator=(const iterator &o)
  806. {
  807. mOwner=o.mOwner;
  808. mLoc=o.mLoc;
  809. }
  810. iterator operator++() //prefix
  811. {
  812. assert(mOwner);
  813. mLoc=mOwner->next(mLoc);
  814. return *this;
  815. }
  816. iterator operator++(int) //postfix
  817. {
  818. assert(mOwner);
  819. iterator old(*this);
  820. mLoc=mOwner->next(mLoc);
  821. return old;
  822. }
  823. iterator operator--() //prefix
  824. {
  825. assert(mOwner);
  826. mLoc=mOwner->previous(mLoc);
  827. return *this;
  828. }
  829. iterator operator--(int) //postfix
  830. {
  831. assert(mOwner);
  832. iterator old(*this);
  833. mLoc=mOwner->previous(mLoc);
  834. return old;
  835. }
  836. bool operator!=(const iterator p) const {return (mLoc!=p.mLoc || mOwner!=p.mOwner);}
  837. bool operator==(const iterator p) const {return (mLoc==p.mLoc && mOwner==p.mOwner);}
  838. const TTValue & operator*() const
  839. {
  840. assert(mOwner);
  841. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  842. return mOwner->index_to_key(mLoc);
  843. }
  844. const TTValue * operator->() const
  845. {
  846. assert(mOwner);
  847. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  848. return &mOwner->index_to_key(mLoc);
  849. }
  850. };
  851. class const_iterator
  852. {
  853. friend class set_base<TStorageTraits,IS_MULTI>;
  854. int mLoc;
  855. const set_base<TStorageTraits,IS_MULTI>* mOwner;
  856. public:
  857. const_iterator(const set_base<TStorageTraits,IS_MULTI> *owner=0, int loc=tree_node::NULL_NODE) :
  858. mOwner(owner),
  859. mLoc(loc)
  860. {
  861. }
  862. const_iterator(const const_iterator &o) :
  863. mOwner(o.mOwner),
  864. mLoc(o.mLoc)
  865. {
  866. }
  867. const_iterator(const iterator &o) :
  868. mOwner(o.mOwner),
  869. mLoc(o.mLoc)
  870. {
  871. }
  872. void operator=(const const_iterator &o)
  873. {
  874. mOwner=o.mOwner;
  875. mLoc=o.mLoc;
  876. }
  877. void operator=(const iterator &o)
  878. {
  879. mOwner=o.mOwner;
  880. mLoc=o.mLoc;
  881. }
  882. const_iterator operator++() //prefix
  883. {
  884. assert(mOwner);
  885. mLoc=mOwner->next(mLoc);
  886. return *this;
  887. }
  888. const_iterator operator++(int) //postfix
  889. {
  890. assert(mOwner);
  891. const_iterator old(*this);
  892. mLoc=mOwner->next(mLoc);
  893. return old;
  894. }
  895. const_iterator operator--() //prefix
  896. {
  897. assert(mOwner);
  898. mLoc=mOwner->previous(mLoc);
  899. return *this;
  900. }
  901. const_iterator operator--(int) //postfix
  902. {
  903. assert(mOwner);
  904. const_iterator old(*this);
  905. mLoc=mOwner->previous(mLoc);
  906. return old;
  907. }
  908. bool operator!=(const const_iterator p) const {return (mLoc!=p.mLoc || mOwner!=p.mOwner);}
  909. bool operator==(const const_iterator p) const {return (mLoc==p.mLoc && mOwner==p.mOwner);}
  910. bool operator!=(const iterator p) const {return (mLoc!=p.mLoc || mOwner!=p.mOwner);}
  911. bool operator==(const iterator p) const {return (mLoc==p.mLoc && mOwner==p.mOwner);}
  912. const TTValue & operator*() const
  913. {
  914. assert(mOwner);
  915. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  916. return mOwner->index_to_key(mLoc);
  917. }
  918. const TTValue * operator->() const
  919. {
  920. assert(mOwner);
  921. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  922. return &mOwner->index_to_key(mLoc);
  923. }
  924. };
  925. friend class iterator;
  926. friend class const_iterator;
  927. ////////////////////////////////////////////////////////////////////////////////////
  928. // Seach For A Given Key. Will Return end() if Failed - O(log n)
  929. ////////////////////////////////////////////////////////////////////////////////////
  930. iterator find(const TTValue &key)
  931. {
  932. return iterator(this,find_index(key));
  933. }
  934. ////////////////////////////////////////////////////////////////////////////////////
  935. // Get An Iterator To The Smallest Element - O(log n)
  936. ////////////////////////////////////////////////////////////////////////////////////
  937. iterator begin()
  938. {
  939. return iterator(this, front());
  940. }
  941. ////////////////////////////////////////////////////////////////////////////////////
  942. // Get An Iterator To The Largest Element - O(log n)
  943. ////////////////////////////////////////////////////////////////////////////////////
  944. iterator rbegin()
  945. {
  946. return iterator(this, back());
  947. }
  948. ////////////////////////////////////////////////////////////////////////////////////
  949. // The Invalid Iterator, Use As A Stop Condition In Your For Loops - O(1)
  950. ////////////////////////////////////////////////////////////////////////////////////
  951. iterator end()
  952. {
  953. return iterator(this);
  954. }
  955. ////////////////////////////////////////////////////////////////////////////////////
  956. // Seach For A Given Key. Will Return end() if Failed - O(log n)
  957. ////////////////////////////////////////////////////////////////////////////////////
  958. const_iterator find(const TTValue &key) const
  959. {
  960. return const_iterator(this, find_index(key));
  961. }
  962. ////////////////////////////////////////////////////////////////////////////////////
  963. // Get An const_iterator To The Smallest Element - O(log n)
  964. ////////////////////////////////////////////////////////////////////////////////////
  965. const_iterator begin() const
  966. {
  967. return const_iterator(this, front());
  968. }
  969. ////////////////////////////////////////////////////////////////////////////////////
  970. // Get An const_iterator To The Largest Element - O(log n)
  971. ////////////////////////////////////////////////////////////////////////////////////
  972. const_iterator rbegin() const
  973. {
  974. return const_iterator(this, back());
  975. }
  976. ////////////////////////////////////////////////////////////////////////////////////
  977. // The Invalid const_iterator, Use As A Stop Condition In Your For Loops - O(1)
  978. ////////////////////////////////////////////////////////////////////////////////////
  979. const_iterator end() const
  980. {
  981. return const_iterator(this);
  982. }
  983. ////////////////////////////////////////////////////////////////////////////////////
  984. // Removes The Element Pointed To At (it) And Decrements (it) - O((log n)^2)
  985. ////////////////////////////////////////////////////////////////////////////////////
  986. void erase(const iterator &it)
  987. {
  988. assert(it.mOwner==this && it.mLoc>=0&&it.mLoc<CAPACITY);
  989. erase_index(it.mLoc);
  990. }
  991. ////////////////////////////////////////////////////////////////////////////////////
  992. //
  993. ////////////////////////////////////////////////////////////////////////////////////
  994. iterator lower_bound(const TTValue &key)
  995. {
  996. return iterator(this, find_index(key));
  997. }
  998. ////////////////////////////////////////////////////////////////////////////////////
  999. //
  1000. ////////////////////////////////////////////////////////////////////////////////////
  1001. iterator upper_bound(const TTValue &key)
  1002. {
  1003. // fixme, this don't work
  1004. iterator ubound(this, find_index(key));
  1005. ubound++;
  1006. return ubound;
  1007. }
  1008. };
  1009. template<class T, int ARG_CAPACITY>
  1010. class set_vs : public set_base<storage::value_semantics_node<T,ARG_CAPACITY,tree_node>,0 >
  1011. {
  1012. public:
  1013. typedef typename storage::value_semantics_node<T,ARG_CAPACITY,tree_node> TStorageTraits;
  1014. typedef typename TStorageTraits::TValue TTValue;
  1015. enum
  1016. {
  1017. CAPACITY = ARG_CAPACITY
  1018. };
  1019. set_vs() {}
  1020. };
  1021. template<class T, int ARG_CAPACITY>
  1022. class set_os : public set_base<storage::object_semantics_node<T,ARG_CAPACITY,tree_node>,0 >
  1023. {
  1024. public:
  1025. typedef typename storage::object_semantics_node<T,ARG_CAPACITY,tree_node> TStorageTraits;
  1026. typedef typename TStorageTraits::TValue TTValue;
  1027. enum
  1028. {
  1029. CAPACITY = ARG_CAPACITY
  1030. };
  1031. set_os() {}
  1032. };
  1033. template<class T, int ARG_CAPACITY, int ARG_MAX_CLASS_SIZE>
  1034. class set_is : public set_base<storage::virtual_semantics_node<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE,tree_node>,0 >
  1035. {
  1036. public:
  1037. typedef typename storage::virtual_semantics_node<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE,tree_node> TStorageTraits;
  1038. typedef typename TStorageTraits::TValue TTValue;
  1039. enum
  1040. {
  1041. CAPACITY = ARG_CAPACITY,
  1042. MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE
  1043. };
  1044. set_is() {}
  1045. };
  1046. template<class K,class V,int IS_MULTI>
  1047. class map_base : public tree_base<K,IS_MULTI>
  1048. {
  1049. public:
  1050. typedef typename K TKeyStorageTraits;
  1051. typedef typename K::TValue TKTValue;
  1052. typedef typename V TValueStorageTraits;
  1053. typedef typename V::TValue TVTValue;
  1054. ////////////////////////////////////////////////////////////////////////////////////
  1055. // Capacity Enum
  1056. ////////////////////////////////////////////////////////////////////////////////////
  1057. enum
  1058. {
  1059. CAPACITY = K::CAPACITY
  1060. };
  1061. private:
  1062. array_base<TValueStorageTraits> mValues;
  1063. public:
  1064. map_base()
  1065. {
  1066. compile_assert<K::CAPACITY==V::CAPACITY>();
  1067. }
  1068. void clear()
  1069. {
  1070. tree_base<K,IS_MULTI>::clear();
  1071. mValues.clear();
  1072. }
  1073. ////////////////////////////////////////////////////////////////////////////////////
  1074. // Adds Element Value At Location Key - O(log n)
  1075. ////////////////////////////////////////////////////////////////////////////////////
  1076. void insert(const TKTValue &key,const TVTValue &value)
  1077. {
  1078. assert(!IS_MULTI || find_index(key)==tree_node::NULL_NODE); //fixme handle duplicates more sensibly?
  1079. alloc_key(key);
  1080. insert_alloced_key();
  1081. assert(check_validity());
  1082. mValues.construct(index_of_alloced_key(),value);
  1083. }
  1084. ////////////////////////////////////////////////////////////////////////////////////
  1085. // Adds Element Value At Location Key returns a reference
  1086. ////////////////////////////////////////////////////////////////////////////////////
  1087. TVTValue &insert(const TKTValue &key)
  1088. {
  1089. assert(!IS_MULTI || find_index(key)==tree_node::NULL_NODE); //fixme handle duplicates more sensibly?
  1090. alloc_key(key);
  1091. insert_alloced_key();
  1092. int idx=index_of_alloced_key();
  1093. assert(check_validity());
  1094. mValues.construct(idx);
  1095. return mValues[idx];
  1096. }
  1097. ////////////////////////////////////////////////////////////////////////////////////
  1098. // Adds Element Value At Location Key returns a rew pointer for placement new
  1099. ////////////////////////////////////////////////////////////////////////////////////
  1100. TRatlNew *insert_raw(const TKTValue &key)
  1101. {
  1102. assert(!IS_MULTI || find_index(key)==tree_node::NULL_NODE); //fixme handle duplicates more sensibly?
  1103. alloc_key(key);
  1104. insert_alloced_key();
  1105. assert(check_validity());
  1106. return mValues.alloc_raw(index_of_alloced_key());
  1107. }
  1108. ////////////////////////////////////////////////////////////////////////////////////
  1109. // After calling alloc_key*, you may call this to alloc the value
  1110. ////////////////////////////////////////////////////////////////////////////////////
  1111. TVTValue &alloc_value()
  1112. {
  1113. mValues.construct(index_of_alloced_key());
  1114. return mValues[index_of_alloced_key()];
  1115. }
  1116. ////////////////////////////////////////////////////////////////////////////////////
  1117. // After calling alloc_key*, you may call this to alloc_raw the value
  1118. ////////////////////////////////////////////////////////////////////////////////////
  1119. TRatlNew *alloc_value_raw()
  1120. {
  1121. return mValues.alloc_raw(index_of_alloced_key());
  1122. }
  1123. template<class CAST_TO>
  1124. CAST_TO *verify_alloc(CAST_TO *p) const
  1125. {
  1126. return mValues.verify_alloc(p);
  1127. }
  1128. ////////////////////////////////////////////////////////////////////////////////////
  1129. // Removes The First Element With Key (key) - O(log n)
  1130. ////////////////////////////////////////////////////////////////////////////////////
  1131. void erase(const TKTValue &key)
  1132. {
  1133. //fixme this is a double search currently
  1134. int i=find_index(key);
  1135. if (i!=tree_node::NULL_NODE)
  1136. {
  1137. erase_index(i);
  1138. mValues.destruct(i);
  1139. }
  1140. }
  1141. ////////////////////////////////////////////////////////////////////////////////////
  1142. // Iterator
  1143. //
  1144. // A map is sorted in ascending order on the KEY type. ++ and -- are both
  1145. // O((log n)^2) operations
  1146. //
  1147. ////////////////////////////////////////////////////////////////////////////////////
  1148. class const_iterator;
  1149. class iterator
  1150. {
  1151. friend class map_base<K,V, IS_MULTI>;
  1152. friend class const_iterator;
  1153. int mLoc;
  1154. map_base<K,V, IS_MULTI>* mOwner;
  1155. public:
  1156. iterator(map_base<K,V, IS_MULTI> *owner=0, int loc=tree_node::NULL_NODE) :
  1157. mOwner(owner),
  1158. mLoc(loc)
  1159. {
  1160. }
  1161. iterator(const iterator &o) :
  1162. mOwner(o.mOwner),
  1163. mLoc(o.mLoc)
  1164. {
  1165. }
  1166. void operator=(const iterator &o)
  1167. {
  1168. mOwner=o.mOwner;
  1169. mLoc=o.mLoc;
  1170. }
  1171. iterator operator++() //prefix
  1172. {
  1173. assert(mOwner);
  1174. mLoc=mOwner->next(mLoc);
  1175. return *this;
  1176. }
  1177. iterator operator++(int) //postfix
  1178. {
  1179. assert(mOwner);
  1180. iterator old(*this);
  1181. mLoc=mOwner->next(mLoc);
  1182. return old;
  1183. }
  1184. iterator operator--() //prefix
  1185. {
  1186. assert(mOwner);
  1187. mLoc=mOwner->previous(mLoc);
  1188. return *this;
  1189. }
  1190. iterator operator--(int) //postfix
  1191. {
  1192. assert(mOwner);
  1193. iterator old(*this);
  1194. mLoc=mOwner->previous(mLoc);
  1195. return old;
  1196. }
  1197. bool operator!=(const iterator &p) const {return (mLoc!=p.mLoc || mOwner!=p.mOwner);}
  1198. bool operator==(const iterator &p) const {return (mLoc==p.mLoc && mOwner==p.mOwner);}
  1199. TVTValue & operator*() const
  1200. {
  1201. assert(mOwner);
  1202. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1203. return mOwner->mValues[mLoc];
  1204. }
  1205. const TKTValue & key() const
  1206. {
  1207. assert(mOwner);
  1208. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1209. return mOwner->index_to_key(mLoc);
  1210. }
  1211. TVTValue & value() const
  1212. {
  1213. assert(mOwner);
  1214. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1215. return mOwner->mValues[mLoc];
  1216. }
  1217. TVTValue * operator->() const
  1218. {
  1219. assert(mOwner);
  1220. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1221. return &mOwner->mValues[mLoc];
  1222. }
  1223. };
  1224. class const_iterator
  1225. {
  1226. friend class map_base<K,V,IS_MULTI>;
  1227. int mLoc;
  1228. const map_base<K,V,IS_MULTI>* mOwner;
  1229. public:
  1230. const_iterator(const map_base<K,V,IS_MULTI> *owner=0, int loc=tree_node::NULL_NODE) :
  1231. mOwner(owner),
  1232. mLoc(loc)
  1233. {
  1234. }
  1235. const_iterator(const const_iterator &o) :
  1236. mOwner(o.mOwner),
  1237. mLoc(o.mLoc)
  1238. {
  1239. }
  1240. const_iterator(const iterator &o) :
  1241. mOwner(o.mOwner),
  1242. mLoc(o.mLoc)
  1243. {
  1244. }
  1245. void operator=(const const_iterator &o)
  1246. {
  1247. mOwner=o.mOwner;
  1248. mLoc=o.mLoc;
  1249. }
  1250. void operator=(const iterator &o)
  1251. {
  1252. mOwner=o.mOwner;
  1253. mLoc=o.mLoc;
  1254. }
  1255. const_iterator operator++() //prefix
  1256. {
  1257. assert(mOwner);
  1258. mLoc=mOwner->next(mLoc);
  1259. return *this;
  1260. }
  1261. const_iterator operator++(int) //postfix
  1262. {
  1263. assert(mOwner);
  1264. const_iterator old(*this);
  1265. mLoc=mOwner->next(mLoc);
  1266. return old;
  1267. }
  1268. const_iterator operator--() //prefix
  1269. {
  1270. assert(mOwner);
  1271. mLoc=mOwner->previous(mLoc);
  1272. return *this;
  1273. }
  1274. const_iterator operator--(int) //postfix
  1275. {
  1276. assert(mOwner);
  1277. const_iterator old(*this);
  1278. mLoc=mOwner->previous(mLoc);
  1279. return old;
  1280. }
  1281. bool operator!=(const const_iterator &p) const {return (mLoc!=p.mLoc || mOwner!=p.mOwner);}
  1282. bool operator==(const const_iterator &p) const {return (mLoc==p.mLoc && mOwner==p.mOwner);}
  1283. bool operator!=(const iterator &p) const {return (mLoc!=p.mLoc || mOwner!=p.mOwner);}
  1284. bool operator==(const iterator &p) const {return (mLoc==p.mLoc && mOwner==p.mOwner);}
  1285. const TVTValue & operator*() const
  1286. {
  1287. assert(mOwner);
  1288. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1289. return mOwner->mValues[mLoc];
  1290. }
  1291. const TKTValue & key() const
  1292. {
  1293. assert(mOwner);
  1294. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1295. return mOwner->index_to_key(mLoc);
  1296. }
  1297. const TVTValue & value() const
  1298. {
  1299. assert(mOwner);
  1300. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1301. return mOwner->mValues[mLoc];
  1302. }
  1303. const TVTValue * operator->() const
  1304. {
  1305. assert(mOwner);
  1306. assert(mLoc>=0&&mLoc<CAPACITY); // deferencing end()?
  1307. return &mOwner->mValues[mLoc];
  1308. }
  1309. };
  1310. friend class iterator;
  1311. friend class const_iterator;
  1312. ////////////////////////////////////////////////////////////////////////////////////
  1313. // Seach For A Given Key. Will Return end() if Failed - O(log n)
  1314. ////////////////////////////////////////////////////////////////////////////////////
  1315. iterator find(const TKTValue &key)
  1316. {
  1317. return iterator(this,find_index(key));
  1318. }
  1319. ////////////////////////////////////////////////////////////////////////////////////
  1320. // Get An Iterator To The Smallest Element - O(log n)
  1321. ////////////////////////////////////////////////////////////////////////////////////
  1322. iterator begin()
  1323. {
  1324. return iterator(this, front());
  1325. }
  1326. ////////////////////////////////////////////////////////////////////////////////////
  1327. // Get An Iterator To The Largest Element - O(log n)
  1328. ////////////////////////////////////////////////////////////////////////////////////
  1329. iterator rbegin()
  1330. {
  1331. return iterator(this, back());
  1332. }
  1333. ////////////////////////////////////////////////////////////////////////////////////
  1334. // The Invalid Iterator, Use As A Stop Condition In Your For Loops - O(1)
  1335. ////////////////////////////////////////////////////////////////////////////////////
  1336. iterator end()
  1337. {
  1338. return iterator(this);
  1339. }
  1340. ////////////////////////////////////////////////////////////////////////////////////
  1341. // Seach For A Given Key. Will Return end() if Failed - O(log n)
  1342. ////////////////////////////////////////////////////////////////////////////////////
  1343. const_iterator find(const TKTValue &key) const
  1344. {
  1345. return const_iterator(this, find_index(key));
  1346. }
  1347. ////////////////////////////////////////////////////////////////////////////////////
  1348. // Get An const_iterator To The Smallest Element - O(log n)
  1349. ////////////////////////////////////////////////////////////////////////////////////
  1350. const_iterator begin() const
  1351. {
  1352. return const_iterator(this, front());
  1353. }
  1354. ////////////////////////////////////////////////////////////////////////////////////
  1355. // Get An const_iterator To The Largest Element - O(log n)
  1356. ////////////////////////////////////////////////////////////////////////////////////
  1357. const_iterator rbegin() const
  1358. {
  1359. return const_iterator(this, back());
  1360. }
  1361. ////////////////////////////////////////////////////////////////////////////////////
  1362. // The Invalid const_iterator, Use As A Stop Condition In Your For Loops - O(1)
  1363. ////////////////////////////////////////////////////////////////////////////////////
  1364. const_iterator end() const
  1365. {
  1366. return const_iterator(this);
  1367. }
  1368. ////////////////////////////////////////////////////////////////////////////////////
  1369. // Removes The Element Pointed To At (it) And Decrements (it) - O((log n)^2)
  1370. ////////////////////////////////////////////////////////////////////////////////////
  1371. void erase(const iterator &it)
  1372. {
  1373. assert(it.mOwner==this && it.mLoc>=0&&it.mLoc<CAPACITY);
  1374. erase_index(it.mLoc);
  1375. mValues.destruct(it.mLoc);
  1376. }
  1377. private:
  1378. bool check_validity()
  1379. {
  1380. #if (0)
  1381. int cnt=0;
  1382. iterator it=begin();
  1383. for (;it!=end();it++)
  1384. {
  1385. cnt++;
  1386. }
  1387. // assert(cnt==size());
  1388. return cnt==size();
  1389. #else
  1390. return true;
  1391. #endif
  1392. }
  1393. };
  1394. template<class K,class V, int ARG_CAPACITY>
  1395. class map_vs : public map_base<
  1396. storage::value_semantics_node<K,ARG_CAPACITY,tree_node>,
  1397. storage::value_semantics<V,ARG_CAPACITY>,
  1398. 0 >
  1399. {
  1400. public:
  1401. typedef typename storage::value_semantics<V,ARG_CAPACITY> VStorageTraits;
  1402. typedef typename VStorageTraits::TValue TTValue;
  1403. enum
  1404. {
  1405. CAPACITY = ARG_CAPACITY
  1406. };
  1407. map_vs() {}
  1408. };
  1409. template<class K,class V, int ARG_CAPACITY>
  1410. class map_os : public map_base<
  1411. storage::value_semantics_node<K,ARG_CAPACITY,tree_node>,
  1412. storage::object_semantics<V,ARG_CAPACITY>,
  1413. 0 >
  1414. {
  1415. public:
  1416. typedef typename storage::object_semantics<V,ARG_CAPACITY> VStorageTraits;
  1417. typedef typename VStorageTraits::TValue TTValue;
  1418. enum
  1419. {
  1420. CAPACITY = ARG_CAPACITY
  1421. };
  1422. map_os() {}
  1423. };
  1424. template<class K,class V, int ARG_CAPACITY,int ARG_MAX_CLASS_SIZE>
  1425. class map_is : public map_base<
  1426. storage::value_semantics_node<K,ARG_CAPACITY,tree_node>,
  1427. storage::virtual_semantics<V,ARG_CAPACITY,ARG_MAX_CLASS_SIZE>,
  1428. 0 >
  1429. {
  1430. public:
  1431. typedef typename storage::virtual_semantics<V,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> VStorageTraits;
  1432. typedef typename VStorageTraits::TValue TTValue;
  1433. enum
  1434. {
  1435. CAPACITY = ARG_CAPACITY,
  1436. MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE
  1437. };
  1438. map_is() {}
  1439. };
  1440. }
  1441. #endif