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

src/ell/IterableNeighbors.cc

Go to the documentation of this file.
00001 
00002 #include "ell/IterableNeighbors.hh"
00003 #include <biu/assertbiu.hh>
00004 #include <algorithm>
00005 #include <biu/RandomNumberFactory.hh>
00006 
00007 namespace ell {
00008     
00009     SuccessiveNeighborList::SuccessiveNeighborList(const IterableNeighbors* source_)
00010      :  source(source_)
00011     { }
00012     
00013     SuccessiveNeighborList::~SuccessiveNeighborList() 
00014     {}
00015   
00016     State*
00017     SuccessiveNeighborList::next(State::NeighborList::ItState *itstate, State* const elem) const 
00018     {
00019         assertbiu(elem != NULL, "Elem is not allowed to be NULL");
00020         assertbiu(itstate != NULL, "no itstate given (NULL)");
00021 
00022         ItState* myitstate = static_cast<ItState*>(itstate);
00023         assertbiu(myitstate->idx < source->getNeighborNumber(), "call is too late");
00024         
00025         do {
00026             // undo last move
00027             source->undoNeighborChange(myitstate->idx, elem);
00028             // update itstate and perform next move
00029             myitstate->idx++;
00030             // if new neighbor is valid and available
00031         } while (   myitstate->idx < source->getNeighborNumber()
00032                     && source->applyNeighborChange(myitstate->idx, elem) == NULL); 
00033         if ( myitstate->idx < source->getNeighborNumber())
00034             return elem;
00035         else {
00036             return NULL;
00037         }
00038     }
00039 
00040     State* 
00041     SuccessiveNeighborList::first(State::NeighborList::ItState** itstate) const 
00042     {
00043         assertbiu(itstate != NULL, "itstate is not allowed to be NULL");
00044         if (*itstate != NULL) delete *itstate;
00045 
00046         ItState* myitstate = new ItState();
00047         *itstate = myitstate;
00048         
00049         State* firstNeigh = NULL;
00050         myitstate->idx = 0;
00051           // find first valid neighbor
00052         while(  myitstate->idx < source->getNeighborNumber()
00053                 && (firstNeigh = source->getNeighbor(myitstate->idx,firstNeigh)) == NULL) 
00054         {
00055             myitstate->idx++;
00056         }
00057         if (myitstate->idx < source->getNeighborNumber())
00058             return firstNeigh;
00059         else {
00060             return NULL;
00061         }
00062     }
00063     
00064     
00065 } // namespace
00066 
00067 
00068 namespace ell {
00069     
00070     RandomNeighborList::RandomNeighborList(const IterableNeighbors* source_)
00071      :  source(source_), idx2apply(source_->getNeighborNumber(),0)
00072     {
00073           // initialize index array
00074         for(size_t i=0; i < idx2apply.size(); i++) {
00075             idx2apply[i] = i;
00076         }
00077           // shuffle index array using biu::RNF random number generator
00078         std::random_shuffle<std::vector<size_t>::iterator>
00079             (idx2apply.begin(), idx2apply.end(), biu::RNF::pt_getRN);
00080     }
00081     
00082     RandomNeighborList::~RandomNeighborList() 
00083     {}
00084   
00085     State*
00086     RandomNeighborList::next(State::NeighborList::ItState *itstate, State* elem) const 
00087     {
00088         assertbiu(elem != NULL, "Elem is not allowed to be NULL");
00089 
00090         ItState* myitstate = static_cast<ItState*>(itstate);
00091         
00092         do {
00093             // undo last move
00094             source->undoNeighborChange(idx2apply[myitstate->pos], elem);
00095             // update itstate and perform next move
00096             myitstate->pos++;
00097             // if new neighbor is valid and available
00098         } while (   myitstate->pos < idx2apply.size()
00099                     && source->applyNeighborChange(idx2apply[myitstate->pos], elem) == NULL);
00100         
00101         if ( myitstate->pos < idx2apply.size())
00102             return elem;
00103         else {
00104             return NULL;
00105         }
00106     }
00107 
00108     State* 
00109     RandomNeighborList::first(State::NeighborList::ItState** itstate) const 
00110     {
00111         assertbiu(itstate != NULL, "itstate is not allowed to be NULL");
00112         if (*itstate != NULL) delete *itstate;
00113 
00114         ItState* myitstate = new ItState();
00115         *itstate = myitstate;
00116         
00117         State* firstNeigh = NULL;
00118         
00119         myitstate->pos = 0;
00120           // find first valid neighbor
00121         while(  myitstate->pos < idx2apply.size()
00122                 && (firstNeigh = source->getNeighbor(idx2apply[myitstate->pos],firstNeigh)) == NULL) 
00123         {
00124             myitstate->pos++;
00125         }
00126         if (myitstate->pos < idx2apply.size())
00127             return firstNeigh;
00128         else {
00129             return NULL;
00130         }
00131     }
00132     
00133     
00134 } // namespace