Generated on Wed Nov 1 15:04:34 2006 for Gecode by doxygen 1.4.5

int-set.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2003
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:03:26 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3512 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #include "gecode/int.hh"
00023 
00024 #include "gecode/support/sort.hh"
00025 
00026 namespace Gecode {
00027 
00029   class IntSet::MinInc {
00030   public:
00031     bool operator()(const Range &x, const Range &y);
00032   };
00033 
00034   forceinline bool
00035   IntSet::MinInc::operator()(const Range &x, const Range &y) {
00036     return x.min < y.min;
00037   }
00038 
00039   void
00040   IntSet::normalize(int n) {
00041     // Sort ranges
00042     MinInc lt_mi;
00043     Support::quicksort<Range>(&sar[0], n, lt_mi);
00044     // Conjoin continuous ranges
00045     int min = sar[0].min;
00046     int max = sar[0].max;
00047     int i = 1;
00048     int j = 0;
00049     while (i < n) {
00050       if (max+1 < sar[i].min) {
00051         sar[j].min = min; sar[j].max = max; j++;
00052         min = sar[i].min; max = sar[i].max; i++;
00053       } else {
00054         max = std::max(max,sar[i].max); i++;
00055       }
00056     }
00057     sar[j].min = min; sar[j].max = max;
00058     sar.size(j+1);
00059   }
00060 
00061   void
00062   IntSet::init(const int r[], int n) {
00063     if (n>0) {
00064       for (int i = n; i--; ) {
00065         sar[i].min=r[i]; sar[i].max=r[i];
00066       }
00067       normalize(n);
00068     }
00069   }
00070 
00071   void
00072   IntSet::init(const int r[][2], int n) {
00073     if (n>0) {
00074       int j = 0;
00075       for (int i = 0; i < n; i++)
00076         if (r[i][0] <= r[i][1]) {
00077           sar[j].min=r[i][0]; sar[j].max=r[i][1]; j++;
00078         }
00079       normalize(j);
00080     }
00081   }
00082 
00083   void
00084   IntSet::init(int n, int m) {
00085     if (n <= m) {
00086       sar[0].min = n; sar[0].max = m;
00087     } else {
00088       sar.size(0);
00089     }
00090   }
00091 
00092   const IntSet IntSet::empty;
00093 
00094 }
00095 
00096 std::ostream&
00097 operator<<(std::ostream& os, const Gecode::IntSet& is) {
00098   os << '<';
00099   for (int i = 0; i < is.size(); ) {
00100     int min = is.min(i);
00101     int max = is.max(i);
00102     if (min == max)
00103       os << min;
00104     else
00105       os << min << ".." << max;
00106     i++;
00107     if (i < is.size())
00108       os << ',';
00109   }
00110   return os << '>';
00111 }
00112 
00113 
00114 // STATISTICS: int-var
00115