[gecode-users] Formatting of Int view output

Christian Schulte cschulte at kth.se
Fri Nov 21 10:28:25 CET 2008


Hi Chris,

thanks for pointing out that bug. The fix is even easier (but your stuff of
course works as well): the problem is that we do not thread through the
state of the output stream. Really stupid mistake from us, but well there
you go...

We'll fix it for 3.0

Thanks
Christian

--
Christian Schulte, www.ict.kth.se/~cschulte/


-----Original Message-----
From: users-bounces at gecode.org [mailto:users-bounces at gecode.org] On Behalf
Of Chris Mears
Sent: Friday, November 21, 2008 3:51 AM
To: Gecode Users mailing list
Subject: [gecode-users] Formatting of Int view output

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();
  }

_______________________________________________
Gecode users mailing list
users at gecode.org
https://www.gecode.org/mailman/listinfo/gecode-users





More information about the gecode-users mailing list