[gecode-users] Formatting of Int view output
Chris Mears
cmears at infotech.monash.edu.au
Fri Nov 21 03:51:07 CET 2008
Hello,
In Gecode 2.2.0 the formatting of integer variables doesn't quite work
correctly. For example, I tried to print a matrix of variables with
padding, like this:
for (int i = 0 ; i < 6 ; i++)
{
for (int j = 0 ; j < 6 ; j++)
cout << setw(15) << m(i,j);
cout << endl;
}
"m" is a Matrix<IntVarArray>. "setw(15)" is intended to make each
element of the matrix use 15 characters, right-justified. The output
looks like this (fixed-width font required):
3 {1..2,4..6} {1..2,4..6} [4..6] {2,4..6} {1..2,5}
[1..2] {1..3,5..6} [1..3] {2,5} {1..3,5..6} 4
[4..6] [2..6] [1..5] [4..6] [2..6] {1..3,5}
[1..2] [5..6] [4..5] 3 [4..6] [1..2]
[4..6] [2..6] [2..6] 1 [3..6] {2..3,5}
[4..5] [1..5] [1..5] {2,4..5} [1..5] 6
The problem can be seen in the first item in the second row. In the
first row, the "3" is correctly placed, but beneath it the "[1..2]" is
in the wrong spot -- the "]" should be directly beneath the "3". The
reason is that the first thing printed -- the "[" -- is padded, and the
rest of the range isn't. That is, because the range is printed
piece-by-piece, only the first piece is padded.
I do not know the proper C++ solution, but it seems like this would
work. The idea is to print the entire range "[1..2]" as one unit. In
gecode/int/view/print.cc, I changed the print_view method to build up
the range in a stringstream and then print that at the end. This fixes
the problem I saw. There are other places (e.g. print_scale, and the
Bool versions) where this would need to be fixed too.
Is this the right way to do it?
template <class View>
inline static std::ostream&
print_view(std::ostream& os, const View& x) {
std::stringstream ss;
if (x.assigned()) {
ss << x.val();
} else if (x.range()) {
ss << '[' << x.min() << ".." << x.max() << ']';
} else {
ss << '{';
ViewRanges<View> r(x);
while (true) {
if (r.min() == r.max()) {
ss << r.min();
} else {
ss << r.min() << ".." << r.max();
}
++r;
if (!r()) break;
ss << ',';
}
ss << '}';
}
return os << ss.str();
}
More information about the gecode-users
mailing list