[gecode-users] int vs. unsigned int for Matrix dimensions

Gregory Crosswhite gcross at phys.washington.edu
Mon Jan 24 21:12:38 CET 2011


Well then, I am glad that I got the patch ready over the weekend.  :-)

The patch has been attached to this e-mail as the file "patch";  it is 
the result of running "svn diff" against revision 11556.

I have modified "gecode/kernel/array.hpp" along the lines that we 
discussed, and have added a new test to your suite under 
"test/array.cpp" to verify that it works as expected.

While I was add it, I added another feature.  The patch includes a file 
"gecode/iter/wrap-val.hpp" (and a patch to "gecode/iter.hh" to include 
this file) which contains an iterator class "WrapVal" that wraps another 
iterator and upon dereferencing returns the the result of calling 
".val()" on the dereferenced wrapped iterator, as well as a function 
"wrap_val" that is a convenience function for constructing a "WrapVal" 
(since this causes the template parameters to automatically be inferred).

That might sound complicated, but the point of it is to allow someone to 
do the following:

     #include <algorithm>
     #include <gecode/iter.hh>
     #include <iterator>
     #include <vector>
     ...
     using namespace Gecode;
     using namespace Gecode::Iter;
     using namespace std;
     ...
     IntVarArray a;
     ...
     vector<int> v;
     copy(wrap_val(a.begin()),wrap_val(a.end()),back_inserter(x));

which has the effect of copying all of the values from the array a into 
the vector v, since a.begin() returns an iterator that dereferences to 
an IntVar and wrap_val(a.begin()) returns a wrapped iterator that 
returns the result of calling .val() on the IntVar.

The basic use case of the iterators is as follows:

     IntVarArgs a;
     ...
     for(IntVarArgs::iterator i = a.begin(); i != a.end(); ++i) { 
rel(space,*i,IRT_EQ,0); }

Of course, you could more easily do this with rel(space,a,IRT_EQ,0).  A 
more interesting example is:

     IntVarArgs a;
     ...
     for(IntVarArgs::iterator i = a.begin(); i != a.end()-1; ++i) { 
rel(space,*i > *(i+1)+2); }

More powerfully, iterators give you the ability to work with STL algorithms:

     #include <algorithm>
     ...
     void f(IntVar& v) { ... }
     ...
     IntVarArgs a;
     ...
     std::for_each(a.begin(),a.end(),f);

The iterators returned by a.begin() and a.end() dereference to a 
reference to an IntVar/BoolVar (or a view in the case of ViewArray).  If 
you are interested in iterating over the *values* of the array --- that 
is, the value currently assigned to to each variable then you can use 
the function wrap_val to wrap these iterators so that dereferencing them 
returns the value assigned to the variable.  For example, the last two 
lines in the following have exactly the same result:

     #include <algorithm>
     #include <iostream>
     ...
     void f(IntVar& v) { std::cout << v.val() << " "; }
     void g(int i) { std::cout << i << " "; }
     ...
     IntVarArgs a;
     ...
     std::for_each(a.begin(),a.end(),f); std::cout << std::endl;
     std::for_each(wrap_val(a.begin()),wrap_val(a.end()),g); std::cout 
<< std::endl;

Or if you want to copy the values assigned to the variables into a 
std::vector:

     #include <algorithm>
     #include <iterator>
     #include <vector>
     ...
     IntVarArray a;
     ...
     std::vector<int> v;
     std::copy(wrap_val(a.begin()),wrap_val(a.end()),std::back_inserter(x));

Or if you are interested in computing the sum of the values in the 
variables:

     #include <numeric>
     ...
     IntVarArray a;
     ...
     int sum = std::accumulate(wrap_val(a.begin()),wrap_val(a.end()),0);

Or alternatively if you are interested in counting the number of zeros 
in the variables:

     #include <algorithm>
     ...
     IntVarArray a;
     ...
     unsigned int number_of_zeros = 
std::count(wrap_val(a.begin()),wrap_val(a.end()),0);

===

Anyway, I hope that this patch can make it into Gecode this week; you 
have my official permission to release it under whatever license Gecode 
uses.

Let me know if you have any questions!  :-)

Cheers,
Gregory Crosswhite



On 01/24/2011 10:54 AM, Christian Schulte wrote:
> Christian Schulte<cschulte at ...>  writes:
>
> Hi again,
>
> There is one thing I forgot to say: we intend to release end of this week
> (hopefully). So if your patch arrives until then, it'll be included in the
> next version.
>
> Cheers
> Christian
>
>
>
>
> _______________________________________________
> Gecode users mailing list
> users at gecode.org
> https://www.gecode.org/mailman/listinfo/gecode-users




-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch
URL: <http://www.gecode.org/pipermail/users/attachments/20110124/cc90c8c6/attachment-0001.asc>


More information about the users mailing list