Main?Page | Class?Hierarchy | Class?List | File?List | Class?Members | File?Members

cmt_map.h

Go to the documentation of this file.
00001 //-----------------------------------------------------------
00002 // Copyright Christian Arnault LAL-Orsay CNRS
00003 // 
00004 // See the complete license in cmt_license.txt "http://www.cecill.info". 
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   T& operator [] (const K& key)
00538   {
00539     T* t = find (key);
00540     
00541     if (t == 0)
00542       {
00543         static T t;
00544         
00545         add (key, t);
00546         return ((*this)[key]);
00547       }
00548     else
00549       {
00550         return (*t);
00551       }
00552   }
00553     */
00554 
00555 
00556 /* ------------------------------------------------------------
00557 
00558    This is a future possible implementation for the remove node function
00559 
00560   void add (cmt_node* other)
00561   {
00562     if (other->m_key < m_key)
00563       {
00564         if (m_left == 0)
00565           {
00566             m_left = other;
00567           }
00568         else
00569           {
00570             m_left->add (other);
00571           }
00572       }
00573     else if (other->m_key > m_key)
00574       { 
00575         if (m_right == 0)
00576           {
00577             m_right = other;
00578           }
00579         else
00580           {
00581             m_right->add (other);
00582           }
00583       }
00584     else
00585       {
00586         m_t = other->m_t;
00587       }
00588   }
00589 
00590   cmt_node* remove_greatest ()
00591   {
00592     if (m_right == 0) return (this);
00593 
00594     cmt_node* result = m_right->remove_greatest ();
00595     if (result == m_right) m_right = 0;
00596 
00597     return (result);
00598   }
00599 
00600   cmt_node* remove_node (const K& key)
00601   {
00602     if (key < m_key)
00603       { 
00604         if (m_left != 0)
00605           {
00606             cmt_node* node = m_left->remove_node (key);
00607             if (node != 0)
00608               {
00609                 delete m_left;
00610                 m_left = node;
00611               }
00612           }
00613 
00614         return (0);
00615       }
00616     else if (key > m_key)
00617       { 
00618         if (m_right != 0)
00619           {
00620             cmt_node* node = m_right->remove_node (key);
00621             if (node != 0)
00622               {
00623                 delete m_right;
00624                 m_right = node;
00625               }
00626           }
00627 
00628         return (0);
00629       }
00630     else
00631       {
00632         // This node will be removed.
00633         // The replacement will be returned.
00634 
00635         if (m_left == 0)
00636           {
00637             cmt_node* rep = m_right;
00638             m_right == 0;
00639             return (rep);
00640           }
00641         else if (m_right == 0)
00642           {
00643             cmt_node* rep = m_left;
00644             m_left == 0;
00645             return (rep);
00646           }
00647         else
00648           {
00649             // here both sides are busy
00650 
00651             cmt_node* rep = remove_greatest ();
00652 
00653             rep->add (this);
00654 
00655             return (rep);
00656           }
00657 
00658         return (this);
00659       }
00660   }
00661 
00662 
00663   void cmt_map::remove (const K& key)
00664   {
00665     if (m_top == 0) return;
00666 
00667     cmt_node* node = m_top->remove (key);
00668     if (node == 0) return;
00669 
00670     if (node == m_top) 
00671       {
00672       }
00673     else
00674       {
00675       }
00676   }
00677 
00678 
00679 
00680  ------------------------------------------------------------ */
00681 
00682 #endif
00683 

Generated on Mon May 2 10:25:05 2005 for CMT by 1.3.5