Hey Christian,
Yes, I am fine with the WrapVal iterator being removed from the patch,
especially since it is not necessary for it to be in Gecode as it needs
no access to the internals of the arrays.
The tweaked patch and added documentation all look good to me as well.
So in summary, responding to all three of your queries: everything looks
fine to me --- thanks for including my patch in Gecode! :-)
Cheers,
Greg
On 01/25/2011 02:01 AM, Christian Schulte wrote:
Hi,
While I buy into the iterator stuff I have to admit that I am not
(yet) excited over the wrap stuff: that looks very very specific.
There are only few cases where you really can rely on the fact that
variables are assigned.
For example, a common misconception is that the print() function of a
script can only be called on a solution and hence all variables are
assigned. This is wrong, printing must also be able to deal with not
yet assigned variables as the print function can be called on any
script and not only solutions. This for example happens when you click
a node in Gist.
So I will include the iterators but not the wrap stuff. Is that okay?
Thanks a lot for your effort!
Christian
--
Christian Schulte, www.ict.kth.se/~cschulte/
*From:*[email protected] [mailto:[email protected]] *On
Behalf Of *Gregory Crosswhite
*Sent:* Monday, January 24, 2011 10:57 PM
*To:* [email protected]
*Subject:* Re: [gecode-users] int vs. unsigned int for Matrix dimensions
Oops, I just realized that I forgot to include in the patch an extra
line in Makefile.in to install the new header. The amended patch is
attached to this e-mail.
Also, I forgot in my previous to mention that also included in the
patch is a test for the WrapVal iterator in test/iter.hpp; that test
program includes checks on some of the examples that I described in
the e-mail to make sure that they have the expected result.
Cheers,
Greg
On 01/24/2011 12:12 PM, Gregory Crosswhite wrote:
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@...> <mailto:cschulte@...> 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
[email protected] <mailto:[email protected]>
https://www.gecode.org/mailman/listinfo/gecode-users
_______________________________________________
Gecode users mailing list
[email protected] <mailto:[email protected]>
https://www.gecode.org/mailman/listinfo/gecode-users
_______________________________________________
Gecode users mailing list
[email protected]
https://www.gecode.org/mailman/listinfo/gecode-users