int-set.cpp
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
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <gecode/int.hh>
00039
00040 namespace Gecode {
00041
00042 IntSet::IntSetObject*
00043 IntSet::IntSetObject::allocate(int n) {
00044 IntSetObject* o = new IntSetObject;
00045 o->n = n;
00046 o->r = heap.alloc<Range>(n);
00047 return o;
00048 }
00049
00050 SharedHandle::Object*
00051 IntSet::IntSetObject::copy(void) const {
00052 IntSetObject* o = allocate(n);
00053 o->size = size;
00054 for (int i=n; i--; )
00055 o->r[i]=r[i];
00056 return o;
00057 }
00058
00059 IntSet::IntSetObject::~IntSetObject(void) {
00060 heap.free<Range>(r,n);
00061 }
00062
00064 class IntSet::MinInc {
00065 public:
00066 bool operator ()(const Range &x, const Range &y);
00067 };
00068
00069 forceinline bool
00070 IntSet::MinInc::operator ()(const Range &x, const Range &y) {
00071 return x.min < y.min;
00072 }
00073
00074 void
00075 IntSet::normalize(Range* r, int n) {
00076 if (n > 0) {
00077
00078 {
00079 MinInc lt_mi;
00080 Support::quicksort<Range>(r, n, lt_mi);
00081 }
00082
00083 {
00084 int min = r[0].min;
00085 int max = r[0].max;
00086 int i = 1;
00087 int j = 0;
00088 while (i < n) {
00089 if (max+1 < r[i].min) {
00090 r[j].min = min; r[j].max = max; j++;
00091 min = r[i].min; max = r[i].max; i++;
00092 } else {
00093 max = std::max(max,r[i].max); i++;
00094 }
00095 }
00096 r[j].min = min; r[j].max = max;
00097 n=j+1;
00098 }
00099 IntSetObject* o = IntSetObject::allocate(n);
00100 unsigned int s = 0;
00101 for (int i=n; i--; ) {
00102 s += static_cast<unsigned int>(r[i].max-r[i].min+1);
00103 o->r[i]=r[i];
00104 }
00105 o->size = s;
00106 object(o);
00107 }
00108 }
00109
00110 void
00111 IntSet::init(const int r[], int n) {
00112 Range* dr = heap.alloc<Range>(n);
00113 for (int i=n; i--; ) {
00114 dr[i].min=r[i]; dr[i].max=r[i];
00115 }
00116 normalize(&dr[0],n);
00117 heap.free(dr,n);
00118 }
00119
00120 void
00121 IntSet::init(const int r[][2], int n) {
00122 Range* dr = heap.alloc<Range>(n);
00123 int j = 0;
00124 for (int i=n; i--; )
00125 if (r[i][0] <= r[i][1]) {
00126 dr[j].min=r[i][0]; dr[j].max=r[i][1]; j++;
00127 }
00128 normalize(&dr[0],j);
00129 heap.free(dr,n);
00130 }
00131
00132 void
00133 IntSet::init(int n, int m) {
00134 if (n <= m) {
00135 IntSetObject* o = IntSetObject::allocate(1);
00136 o->r[0].min = n; o->r[0].max = m;
00137 o->size = static_cast<unsigned int>(m - n + 1);
00138 object(o);
00139 }
00140 }
00141
00142 const IntSet IntSet::empty;
00143
00144 }
00145
00146
00147