Generated on Tue Dec 16 13:34:02 2008 for ell-3.0.0 by doxygen 1.5.1

src/ell/StatePairCollector.cc

Go to the documentation of this file.
00001 
00002 #include "ell/StatePairCollector.hh"
00003 
00004 #include <biu/assertbiu.hh>
00005 
00006 //##############################################################################
00007 //##############################################################################
00008 
00009 namespace ell {
00010     
00011     SPC_Counting::SPC_Counting()
00012      :  lastAdded_s1(NULL)
00013         , lastAdded_s2(NULL)
00014         , statePairCount(0)
00015     {
00016     }
00017     
00018     SPC_Counting::~SPC_Counting()
00019     {
00020         clearLastAdded();
00021     }
00022     
00023       // This function is used to track all added intermediate States.
00024       // @param s the added State
00025     void 
00026     SPC_Counting::add(const StatePair& s) {
00027         assertbiu(s.first != NULL, "first of the collected State pair is NULL");
00028         assertbiu(s.second!=NULL, "second of the collected State pair is NULL");
00029         
00030           // store a state copy
00031         lastAdded_s1 = s.first->clone(lastAdded_s1);
00032         lastAdded_s2 = s.second->clone(lastAdded_s2);
00033           // increase counter
00034         statePairCount++;
00035     }
00036     
00037       // This function is used to track all added intermediate States.
00038       // @param s the added State
00039     void 
00040     SPC_Counting::add(const State* const s1_, const State*const s2_) {
00041         assertbiu(s1_ != NULL, "first of the collected State pair is NULL");
00042         assertbiu(s2_ != NULL, "second of the collected State pair is NULL");
00043         
00044           // store a state copy
00045         lastAdded_s1 = s1_->clone(lastAdded_s1);
00046         lastAdded_s2 = s2_->clone(lastAdded_s2);
00047           // increase counter
00048         statePairCount++;
00049     }
00050     
00051       // Returns number of added States.
00052       // @return the number of added states
00053     size_t 
00054     SPC_Counting::size() const {
00055         return statePairCount;
00056     }
00057      
00058       // Returns last added State.
00059       // @return the last added State
00060     const SPC_Counting::StatePair 
00061     SPC_Counting::getLastAdded() const {
00062         return StatePair(lastAdded_s1,lastAdded_s2);
00063     }
00064 
00065 } // namespace ell
00066 
00067 //##############################################################################
00068 //##############################################################################
00069 
00070 
00071 namespace ell {
00072     
00073     SPC_Listing::SPC_Listing()
00074       : statePairList() 
00075     {}
00076     
00077     SPC_Listing::~SPC_Listing()
00078     {
00079           // delete all collected State objects
00080         for (   List::iterator it = statePairList.begin();
00081                 it != statePairList.end(); it++ ) 
00082         {
00083             delete it->first;
00084             delete it->second;          
00085         }
00086           // clear invalid pointer list
00087         statePairList.clear();
00088     }
00089 
00090       // This function is used to track all added intermediate State pairs.
00091       // @param s the added State
00092     void 
00093     SPC_Listing::add(const StatePair& s) {
00094         assertbiu(s.first != NULL, "first of the collected State pair is NULL");
00095         assertbiu(s.second!=NULL, "second of the collected State pair is NULL");
00096           // store a copy of s
00097         statePairList.push_back(StatePair(s.first->clone(),s.second->clone()));
00098     }
00099     
00100       // This function is used to track all added intermediate States.
00101       // @param s the added State
00102     void 
00103     SPC_Listing::add(const State* const s1_, const State*const s2_) {
00104         assertbiu(s1_ != NULL, "first of the collected State pair is NULL");
00105         assertbiu(s2_ != NULL, "second of the collected State pair is NULL");
00106           // store a state copy
00107         statePairList.push_back(StatePair(s1_->clone(),s2_->clone()));
00108     }
00109 
00110       // Returns number of added States.
00111       // @return the number of added states
00112     size_t 
00113     SPC_Listing::size() const {
00114         return statePairList.size();
00115     }
00116      
00117       // Returns last added State.
00118       // @return the last added State
00119     const SPC_Listing::StatePair
00120     SPC_Listing::getLastAdded() const {
00121         return *(statePairList.rbegin());
00122     }
00123     
00125 
00126       // Returns a list of all added States.
00127       // @return the State list
00128     const SPC_Listing::List&
00129     SPC_Listing::getList() const {
00130         return statePairList;
00131     }
00132     
00133 
00134 } // namespace ell
00135 
00136 
00137 //##############################################################################
00138 //##############################################################################
00139 
00140 namespace ell
00141 {
00142 
00143     SPC_Outstream::SPC_Outstream(   std::ostream& out_ 
00144                                     , const std::string& separator
00145                                     , const std::string& toAppend)
00146      :  SPC_Counting()
00147         , out(out_)
00148         , strSep(separator)
00149         , strAppend(toAppend)
00150     {
00151     }
00152     
00153     SPC_Outstream::~SPC_Outstream()
00154     {
00155           // ensure that output is stored by the stream
00156         out.flush();
00157     }
00158 
00159       // This function is used to track all added intermediate States.
00160       // @param s the added State
00161     void 
00162     SPC_Outstream::add(const StatePair& s) {
00163         assertbiu(s.first != NULL, "first of the collected State pair is NULL");
00164         assertbiu(s.second!=NULL, "second of the collected State pair is NULL");
00165           // call handler of superclass
00166         SPC_Counting::add(s);
00167           // print to stream
00168         printToStream();
00169     }
00170 
00171     void 
00172     SPC_Outstream::add(const State* const s1_, const State*const s2_) {
00173         assertbiu(s1_ != NULL, "first of the collected State pair is NULL");
00174         assertbiu(s2_ != NULL, "second of the collected State pair is NULL");
00175           // call handler of superclass
00176         SPC_Counting::add(s1_, s2_);
00177           // print to stream
00178         printToStream();
00179     }
00180 
00181 } // namespace ell
00182 
00183 
00184 //##############################################################################
00185 //##############################################################################
00186 
00187 namespace ell {
00188 
00189     SPC_MinPair::SPC_MinPair( const bool pairsAddedInAscOrder_ )
00190      :  SPC_Counting()
00191         , pairsAddedInAscOrder(pairsAddedInAscOrder_)
00192         , minPair_s1(NULL)
00193         , minPair_s2(NULL)
00194         , minPair_max(NULL)
00195     {
00196     }
00197     
00198     SPC_MinPair::~SPC_MinPair()
00199     {
00200           // clear local data structures
00201         clearMinPair();
00202     }
00203 
00204       // This function is used to track all added intermediate States.
00205       // @param s the added State
00206     void 
00207     SPC_MinPair::add(const StatePair& s) {
00208         assertbiu(s.first != NULL, "first of the collected State pair is NULL");
00209         assertbiu(s.second!=NULL, "second of the collected State pair is NULL");
00210           // call handler of superclass
00211         SPC_Counting::add(s);
00212           // check if last added pair is smaller
00213         checkMinPair();
00214     }
00215 
00216     void 
00217     SPC_MinPair::add(const State* const s1_, const State*const s2_) {
00218         assertbiu(s1_ != NULL, "first of the collected State pair is NULL");
00219         assertbiu(s2_ != NULL, "second of the collected State pair is NULL");
00220           // call handler of superclass
00221         SPC_Counting::add(s1_, s2_);
00222           // check if last added pair is smaller
00223         checkMinPair();
00224     }
00225 
00226       // Returns last minimal added State pair.
00227       // @return the minimal added State pair
00228     const SPC_Counting::StatePair 
00229     SPC_MinPair::getMinPair() const {
00230         return StatePair(minPair_s1,minPair_s2);
00231     }
00232     
00233     void
00234     SPC_MinPair::checkMinPair() {
00235         
00236           // quick check if adding follows ascending order and one was already
00237           // added
00238         if (pairsAddedInAscOrder && minPair_s1 != NULL) {
00239             return;
00240         }
00241           // check if first call
00242         if ( minPair_s1 == NULL ) 
00243         {
00244               // set up pointer
00245             minPair_s1 = lastAdded_s1->clone();
00246             minPair_s2 = lastAdded_s2->clone();
00247               // set pointer to smaller of both
00248             minPair_max = (minPair_s1->operator <(*minPair_s2))?minPair_s1:minPair_s2;
00249             return;
00250         }
00251           // get max of the last added pair
00252         const State* lastMax = (lastAdded_s1->operator <(*lastAdded_s2))?lastAdded_s1:lastAdded_s2;
00253           // check if max of last added pair is smaller minPair max
00254         if ( lastMax->operator <(*minPair_max)) {
00255               // set up pointer
00256             minPair_s1 = lastAdded_s1->clone(minPair_s1);
00257             minPair_s2 = lastAdded_s2->clone(minPair_s2);
00258               // set pointer to smaller of both
00259             minPair_max = (lastMax==lastAdded_s1)?minPair_s1:minPair_s2;
00260         }
00261     }
00262 
00263 } // namespace ell
00264 
00265 
00266 //##############################################################################
00267 //##############################################################################
00268