int-set.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00042 MinInc lt_mi;
00043 Support::quicksort<Range>(&sar[0], n, lt_mi);
00044
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
00115