Generated on Mon Aug 25 11:35:34 2008 for Gecode by doxygen 1.5.6

print.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00005  *
00006  *  Copyright:
00007  *     Patrick Pekczynski, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2007-11-06 15:09:01 +0100 (Tue, 06 Nov 2007) $ by $Author: tack $
00011  *     $Revision: 5205 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "gecode/cpltset.hh"
00039 #include "gecode/support/buddy/kernel.h"
00040 
00041 namespace Gecode { namespace CpltSet {
00042 
00043   /* 
00044    * Printing a bound
00045    *
00046    */
00047   template <class I>
00048   static void
00049   printBound(std::ostream& os, I& r) {
00050     os << '{';
00051     while (r()) {
00052       if (r.min() == r.max()) {
00053         os << r.min();
00054       } else if (r.min()+1 == r.max()) {
00055         os << r.min() << "," << r.max();
00056       } else {
00057         os << r.min() << ".." << r.max();
00058       }
00059       ++r;
00060       if (!r()) break;
00061       os << ',';
00062     }
00063     os << '}';
00064   }
00065 
00067   bool printBddDom(std::ostream& os, int off, int min, int width, bool first,
00068                    char* profile, bdd& r) {
00069     if (r == bdd_true())
00070     {
00071       if (!first)
00072         os << ",";
00073       bool assigned = true;
00074       int last = -1;
00075       int lastUnknown = -1;
00076       for (int i = 0; i<width; i++) {
00077         if (profile[i] < 0) {
00078           assigned = false; lastUnknown = i;
00079         }
00080         if (profile[i] == 1) {
00081           last = i; lastUnknown = i;
00082         }
00083       }
00084       if (assigned) {
00085         os << "{";
00086         for (int i = 0; i<=last; i++) {
00087           if (profile[i] == 1)
00088             os << min+i << (i<last ? "," : "");
00089         }
00090         os << "}";
00091       } else {
00092         os << "{";
00093         for (int i = 0; i<=last; i++) {
00094           if (profile[i] == 1)
00095             os << min+i << (i<last ? "," : "");
00096         }
00097         os << "}..{";
00098         for (int i = 0; i<=lastUnknown; i++) {
00099           if (profile[i] != 0)
00100             os << min+i << (i<lastUnknown ? "," : "");
00101         }
00102         os << "}";
00103       }
00104       return false;
00105     }
00106 
00107     if (r == bdd_false())
00108       return first;
00109 
00110     if (bdd_low(r) != bdd_false()) {
00111       profile[bddlevel2var[r.getlevel()]-off] = 0;
00112       for (int v=bdd_low(r).getlevel()-1 ; v>r.getlevel() ; --v) {
00113         profile[bddlevel2var[v]-off] = -1;
00114       }
00115       bdd rlow = bdd_low(r);
00116       first = printBddDom(os, off, min, width, first, profile, rlow);
00117     }
00118 
00119     if (bdd_high(r) != bdd_false()) {
00120       profile[bddlevel2var[r.getlevel()]-off] = 1;
00121       for (int v=bdd_high(r).getlevel()-1 ; v>r.getlevel() ; --v) {
00122         profile[bddlevel2var[v]-off] = -1;
00123       }
00124       bdd rhigh = bdd_high(r);
00125       first = printBddDom(os, off, min, width, first, profile, rhigh);
00126     }
00127     return first;
00128   }
00129 
00130 }}
00131 
00132 using namespace Gecode;
00133 using namespace Gecode::CpltSet;
00134 
00139 std::ostream&
00140 operator<<(std::ostream& os, const CpltSetView& x) {
00141   bool assigned = x.assigned();
00142   if (assigned) {
00143     GlbValues<CpltSetView> glb(x);
00144     Iter::Values::ToRanges<GlbValues<CpltSetView> > glbr(glb);
00145     printBound(os, glbr);
00146   } else {
00147     bdd dom = x.dom();
00148     os << "{";
00149     char* profile = Memory::bmalloc<char>(x.tableWidth());
00150     for (int i=x.tableWidth(); i--;)
00151       profile[i] = -1;
00152     printBddDom(os, x.offset(), x.initialLubMin(), x.tableWidth(), true, 
00153                 profile, dom);
00154     Memory::free(profile);
00155     os << "}";
00156   }
00157   return os;
00158 }
00159 
00160 std::ostream&
00161 operator<< (std::ostream& os, const CpltSetVar& x) {
00162   CpltSetView xv(x);
00163   return os << xv;
00164 }
00165 
00166 // STATISTICS: cpltset-var