[gecode-users] std::set to Gecode::IntVar conversion - my quick and dirty solution

Martin Mann mmann at informatik.uni-freiburg.de
Thu May 11 15:31:32 CEST 2006


Grégoire Dooms schrieb:
> Martin Mann wrote:
>> Moin Christian,
>>
>> mhh.. ok I ll have a look if I can write a worthy conversion! ;)
>> Will have to have a deeper look into IntVar implementations..
>>
> You can find two in the contribs/graph/view/iter.icc
> They are called StlToGecodeRangeIterator and StlToGecodeValIterator
> See example below.

Thanks to Grégoire Doom and his source code example I got the idea to 
write a Range iterator, that can be used in the constructor of a 
Gecode::IntSet object.

It is not a final solution, because I would like to have a direct 
conversion from std::set<int> to Gecode::IntVar, but with a 
Gecode::IntSet step between it is usable.

I didnt used the graph package, that already have got the feature 
std::iterator -> Gecode::Iterator, because I ve got trouble to compile 
the package on my cygwin system with the included boost version. I gave 
it up at the moment.

my class looks like that:

---------------------------------------------------------
/**
  * provides a constant Gecode RangeIterator of a std::set<int> that
  * calculates the ranges on demand.
  */
class GC_StlSetRangeIterator
{
  private:
    //! the set this iterator operates on
   const std::set<int>* data;
    //! the current position in data
   std::set<int>::const_iterator actElem;
    //! == true if the iterator has reached the end
   bool noFurtherRange;
     //! the current range
   int nextMin, nextMax;
     //! searchs for the next range and sets the inner members
   void getNextRange() {
     if (data==NULL || actElem == data->end()) {
       noFurtherRange = true;
       return;
     }
	 // init and identify next range
     nextMin = *actElem;
     nextMax = nextMin;
	 // build next range
     while ( (++actElem != data->end()) && (*actElem == (nextMax+1)))
       nextMax++;
   }

  public:
   GC_StlSetRangeIterator();
   GC_StlSetRangeIterator(const std::set<int>* data_);
   virtual ~GC_StlSetRangeIterator() {}

   void init(const std::set<int>* const data_) {
     data = data_;
     noFurtherRange = false;
     getNextRange();
   }
	
   bool operator()(void) const { return !noFurtherRange; }
   void operator++(void) { getNextRange(); }

   int min(void) const { return nextMin; }
   int max(void) const { return nextMax; }
   unsigned int width(void) { return nextMax-nextMin+1; }
};

----------------------------------------------------------------

and can be used like that:

----------------------------------------------------------------

#include <int.hh>
#include <set>
#include <iostream>
#include <GC_StlSetRangeIterator.hh>

int main(int argc, char** argv) {
   std::set<int> s;
   for(int i=0; i<5; i++) {
     s.insert(i);
     s.insert(i+7);
     s.insert(i*5);
   }

   GC_StlSetRangeIterator it(&s);

   Gecode::IntSet is(it);

   std::cout<<"new IntSet = " <<is <<std::endl;

   return 0;
}
----------------------------------------------------------------

there may be much better implementations or possibilies.. ;)

Thanks to all of you!

Martin




More information about the gecode-users mailing list