Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBgset.cxx

Go to the documentation of this file.
00001 
00006 /*----------------------------------------------------------------------------*/
00007 /* C Tool Box: Designed and implemented by:                                   */
00008 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00009 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00010 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00011 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00012 /*------------------------------------------+---------------------------------*/
00013 
00014 #include <assert.h>
00015 
00016 #include "CTB.hxx"
00017 #include "CTB_Trace.hxx"
00018 #include "CTBgset.hxx"
00019 
00025 //------------------------------------------+-----------------------------------
00027 
00028 template <class K, class V, class C, class T, class AK, class AV>
00029 bool CTBgset<K,V,C,T,AK,AV>::Insert(const T& obj)
00030 {
00031   CTB_Trace("CTBgset::Insert(const T&)");
00032   AK    getkey;                             // key access function object
00033   int   i_mtype;
00034   Node* p_parent = LocateMatchOrBound(getkey(obj),i_mtype);
00035 
00036   if (p_parent && i_mtype == 0) return false; // key already present
00037   InsertNode(new Node(obj),p_parent,i_mtype); // create node and insert
00038   return true;
00039 }
00040 
00041 //------------------------------------------+-----------------------------------
00043 
00044 template <class K, class V, class C, class T, class AK, class AV>
00045 CTBgsetIterator<K,V,C,T,AK,AV> CTBgset<K,V,C,T,AK,AV>::Find(const K& key)
00046 {
00047   CTB_Trace("CTBgset::Find(const K&)");
00048   int   i_mtype;
00049   Node* p_node = LocateMatchOrBound(key,i_mtype);
00050   
00051   if (p_node && i_mtype == 0) return Iterator((Gset*) this,p_node);
00052   return Iterator();
00053 }
00054 
00055 //------------------------------------------+-----------------------------------
00057 
00058 template <class K, class V, class C, class T, class AK, class AV>
00059 CTBgsetBrowser<K,V,C,T,AK,AV> CTBgset<K,V,C,T,AK,AV>::Find(const K& key) const
00060 {
00061   CTB_Trace("CTBgset::Find(const K&) const");
00062   int   i_mtype;
00063   Node* p_node = LocateMatchOrBound(key,i_mtype);
00064   
00065   if (p_node && i_mtype == 0) return Browser((Gset*) this,p_node);
00066   return Browser();
00067 }
00068 
00069 //------------------------------------------+-----------------------------------
00071 
00075 template <class K, class V, class C, class T, class AK, class AV>
00076 bool CTBgset<K,V,C,T,AK,AV>::Delete(const K& key)
00077 {
00078   CTB_Trace("CTBgset::Delete(const K&)");
00079   int   i_mtype;
00080   Node* p_node = LocateMatchOrBound(key,i_mtype);
00081 
00082   if (!p_node || i_mtype != 0) return false; // quit if key not found
00083   delete p_node;                            // destruct will do proper unlink
00084   return true;
00085 }
00086 
00087 //------------------------------------------+-----------------------------------
00089 
00093 template <class K, class V, class C, class T, class AK, class AV>
00094 bool CTBgset<K,V,C,T,AK,AV>::Delete(const CTBgsetIterator<K,V,C,T,AK,AV>& p)
00095 {
00096   CTB_Trace("CTBgset::Delete(const CTBgsetIterator&)");
00097   Node* p_node = p;
00098 
00099   delete p_node;
00100   return p_node;                            // return true if iterator valid
00101 }
00102 
00103 //------------------------------------------+-----------------------------------
00105 
00106 template <class K, class V, class C, class T, class AK, class AV>
00107 void CTBgset<K,V,C,T,AK,AV>::Clear()
00108 {
00109   CTB_Trace("CTBgset::Clear()");
00110   CTBbtree::Clear();
00111   return;
00112 }
00113 
00114 //------------------------------------------+-----------------------------------
00116 
00123 template <class K, class V, class C, class T, class AK, class AV>
00124 CTBgsetNode<K,V,C,T,AK,AV>* CTBgset<K,V,C,T,AK,AV>::LocateMatchOrBound(
00125                               const K& key, int& i_mtype) const
00126 {
00127   CTB_Trace("CTBgset::LocateMatchOrBound(const K&,int&)");
00128   C      compare;                           // compare function object
00129   Node*  p_cur = (Node*) RNode();
00130 
00131   i_mtype = 0;
00132   
00133   if (p_cur) {
00134     for (;;) {
00135       int    i_cmp = compare(key,p_cur->Key());
00136       Node*  p_next;
00137       
00138       if (i_cmp == 0) return p_cur;         // match found
00139 
00140       if (i_cmp < 0) {                      // key < current
00141         p_next = p_cur->Left();             // try to follow left tree
00142         if (!p_next) {                      // if none found quit
00143           i_mtype = -1;                     // new node will be left child
00144           return p_cur;
00145         }
00146 
00147       } else {                              // key > current
00148         p_next = p_cur->Right();            // try to follow right tree
00149         if (!p_next) {                      // if none found quit
00150           i_mtype = 1;                      // new node will be right child
00151           return p_cur;
00152         }
00153       }
00154 
00155       p_cur = p_next;
00156     }
00157   }
00158   return 0;
00159 }
00160 
00161 //------------------------------------------+-----------------------------------
00163 
00164 template <class K, class V, class C, class T, class AK, class AV>
00165 CTBgsetNode<K,V,C,T,AK,AV>* CTBgset<K,V,C,T,AK,AV>::LocateMatchOrPrev(
00166                               const K& key, bool& b_found) const
00167 {
00168   CTB_Trace("CTBgset::LocateMatchOrPrev(const K&,bool&)");
00169   int   i_mtype;
00170   Node* p_node = LocateMatchOrBound(key,i_mtype);
00171   
00172   b_found = p_node && i_mtype == 0;
00173   if (p_node && i_mtype < 0) {              // if key < node
00174     p_node = p_node->Prev();                // take previous node
00175   }
00176   return p_node;
00177 }
00178 
00179 //------------------------------------------+-----------------------------------
00181 
00182 template <class K, class V, class C, class T, class AK, class AV>
00183 CTBgsetNode<K,V,C,T,AK,AV>* CTBgset<K,V,C,T,AK,AV>::LocateMatchOrNext(
00184                               const K& key, bool& b_found) const
00185 {
00186   CTB_Trace("CTBgset::LocateMatchOrNext(const K&,bool&)");
00187   int   i_mtype;
00188   Node* p_node = LocateMatchOrBound(key,i_mtype);
00189   
00190   b_found = p_node && i_mtype == 0;
00191   if (p_node && i_mtype > 0) {              // if key > node
00192     p_node = p_node->Next();                // take next node
00193   }
00194   return p_node;
00195 }
00196 

Generated at Fri Oct 24 18:11:28 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001