00001
00002
00003
00004
00005
00006
00007 #ifndef __cmt_map_h__
00008 #define __cmt_map_h__
00009
00010 #include "cmt_std.h"
00011 #include "cmt_string.h"
00012 #include "cmt_vector.h"
00013
00014 #include
00015
00025 template <class K, class T> class cmt_node
00026 {
00027 public:
00028
00033 cmt_node (const K& key, T& t) :
00034 m_left (0),
00035 m_key (key),
00036 m_t (&t),
00037 m_right (0)
00038 {
00039 }
00040
00044 ~cmt_node ()
00045 {
00046 clear ();
00047 }
00048
00053 void clear ()
00054 {
00055 if (m_left == 0) return;
00056
00057 delete m_left;
00058 m_left = 0;
00059
00060 m_t = 0;
00061
00062 if (m_right == 0) return;
00063
00064 delete m_right;
00065 m_right = 0;
00066 }
00067
00071 void add (const K& key, T& t)
00072 {
00073 if (key < m_key)
00074 {
00075 if (m_left == 0)
00076 {
00077 m_left = new cmt_node (key, t);
00078 }
00079 else
00080 {
00081 m_left->add (key, t);
00082 }
00083 }
00084 else if (key > m_key)
00085 {
00086 if (m_right == 0)
00087 {
00088 m_right = new cmt_node (key, t);
00089 }
00090 else
00091 {
00092 m_right->add (key, t);
00093 }
00094 }
00095 else
00096 {
00097 m_t = &t;
00098 }
00099 }
00100
00105 bool has (const K& key) const
00106 {
00107 if (key < m_key)
00108 {
00109 if (m_left == 0) return (false);
00110 else return (m_left->has (key));
00111 }
00112 else if (key > m_key)
00113 {
00114 if (m_right == 0) return (false);
00115 else return (m_right->has (key));
00116 }
00117 else
00118 {
00119 return (true);
00120 }
00121 }
00122
00128 T* find (const K& key) const
00129 {
00130 if (key < m_key)
00131 {
00132 if (m_left == 0) return (0);
00133 else return (m_left->find (key));
00134 }
00135 else if (key > m_key)
00136 {
00137 if (m_right == 0) return (0);
00138 else return (m_right->find (key));
00139 }
00140 else
00141 {
00142 return (m_t);
00143 }
00144 }
00145
00151 const cmt_node* find_node (const K& key) const
00152 {
00153 if (key < m_key)
00154 {
00155 if (m_left == 0) return (0);
00156 else return (m_left->find_node (key));
00157 }
00158 else if (key > m_key)
00159 {
00160 if (m_right == 0) return (0);
00161 else return (m_right->find_node (key));
00162 }
00163 else
00164 {
00165 return (this);
00166 }
00167 }
00168
00169 protected:
00170 cmt_node* m_left;
00171 K m_key;
00172 T* m_t;
00173 cmt_node* m_right;
00174 };
00175
00176
00177 template <class K, class T> class cmt_vnode
00178 {
00179 public:
00180
00185 cmt_vnode (const K& key, T& t) :
00186 m_left (0),
00187 m_key (key),
00188 m_t (t),
00189 m_right (0)
00190 {
00191 }
00192
00196 ~cmt_vnode ()
00197 {
00198 clear ();
00199 }
00200
00205 void clear ()
00206 {
00207 if (m_left == 0) return;
00208
00209 delete m_left;
00210 m_left = 0;
00211
00212 if (m_right == 0) return;
00213
00214 delete m_right;
00215 m_right = 0;
00216 }
00217
00221 void add (const K& key, T& t)
00222 {
00223 if (key < m_key)
00224 {
00225 if (m_left == 0)
00226 {
00227 m_left = new cmt_vnode (key, t);
00228 }
00229 else
00230 {
00231 m_left->add (key, t);
00232 }
00233 }
00234 else if (key > m_key)
00235 {
00236 if (m_right == 0)
00237 {
00238 m_right = new cmt_vnode (key, t);
00239 }
00240 else
00241 {
00242 m_right->add (key, t);
00243 }
00244 }
00245 else
00246 {
00247 m_t = t;
00248 }
00249 }
00250
00255 bool has (const K& key) const
00256 {
00257 if (key < m_key)
00258 {
00259 if (m_left == 0) return (false);
00260 else return (m_left->has (key));
00261 }
00262 else if (key > m_key)
00263 {
00264 if (m_right == 0) return (false);
00265 else return (m_right->has (key));
00266 }
00267 else
00268 {
00269 return (true);
00270 }
00271 }
00272
00278 const T* find (const K& key) const
00279 {
00280 if (key < m_key)
00281 {
00282 if (m_left == 0) return (0);
00283 else return (m_left->find (key));
00284 }
00285 else if (key > m_key)
00286 {
00287 if (m_right == 0) return (0);
00288 else return (m_right->find (key));
00289 }
00290 else
00291 {
00292 return (&m_t);
00293 }
00294 }
00295
00301 const cmt_vnode* find_node (const K& key) const
00302 {
00303 if (key < m_key)
00304 {
00305 if (m_left == 0) return (0);
00306 else return (m_left->find_node (key));
00307 }
00308 else if (key > m_key)
00309 {
00310 if (m_right == 0) return (0);
00311 else return (m_right->find_node (key));
00312 }
00313 else
00314 {
00315 return (this);
00316 }
00317 }
00318
00319 protected:
00320 cmt_vnode* m_left;
00321 K m_key;
00322 T m_t;
00323 cmt_vnode* m_right;
00324 };
00325
00326
00333 template <class K, class T> class cmt_map
00334 {
00335 public:
00336
00340 cmt_map () : m_top (0)
00341 {
00342 }
00343
00347 ~cmt_map ()
00348 {
00349 clear ();
00350 }
00351
00355 void clear ()
00356 {
00357 if (m_top != 0)
00358 {
00359 delete m_top;
00360 m_top = 0;
00361 }
00362 }
00363
00369 void add (const K& key, T& t)
00370 {
00371 if (m_top == 0)
00372 {
00373 m_top = new cmt_node (key, t);
00374 }
00375 else
00376 {
00377 m_top->add (key, t);
00378 }
00379 }
00380
00381 void add (const K& key, T* t)
00382 {
00383 if (m_top == 0)
00384 {
00385 m_top = new cmt_node (key, *t);
00386 }
00387 else
00388 {
00389 m_top->add (key, *t);
00390 }
00391 }
00392
00396 bool has (const K& key) const
00397 {
00398 if (m_top != 0)
00399 {
00400 return (m_top->has (key));
00401 }
00402 else
00403 {
00404 return (false);
00405 }
00406 }
00407
00412 T* find (const K& key) const
00413 {
00414 if (m_top != 0)
00415 {
00416 return (m_top->find (key));
00417 }
00418 else
00419 {
00420 return (0);
00421 }
00422 }
00423
00428 const cmt_node* find_node (const K& key) const
00429 {
00430 if (m_top == 0) return (0);
00431
00432 return (m_top->find_node (key));
00433 }
00434
00435 private:
00436 cmt_node* m_top;
00437 };
00438
00439 template <class K, class T> class cmt_vmap
00440 {
00441 public:
00442
00446 cmt_vmap () : m_top (0)
00447 {
00448 }
00449
00453 ~cmt_vmap ()
00454 {
00455 clear ();
00456 }
00457
00461 void clear ()
00462 {
00463 if (m_top != 0)
00464 {
00465 delete m_top;
00466 m_top = 0;
00467 }
00468 }
00469
00475 void add (const K& key, T& t)
00476 {
00477 if (m_top == 0)
00478 {
00479 m_top = new cmt_vnode (key, t);
00480 }
00481 else
00482 {
00483 m_top->add (key, t);
00484 }
00485 }
00486
00490 bool has (const K& key) const
00491 {
00492 if (m_top != 0)
00493 {
00494 return (m_top->has (key));
00495 }
00496 else
00497 {
00498 return (false);
00499 }
00500 }
00501
00506 const T* find (const K& key) const
00507 {
00508 if (m_top != 0)
00509 {
00510 return (m_top->find (key));
00511 }
00512 else
00513 {
00514 return (0);
00515 }
00516 }
00517
00522 const cmt_vnode* find_node (const K& key) const
00523 {
00524 if (m_top == 0) return (0);
00525
00526 return (m_top->find_node (key));
00527 }
00528
00529 private:
00530 cmt_vnode* m_top;
00531 };
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682 #endif
00683