tuple-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 {
00041
00042 typedef ::Gecode::TupleSet::Tuple Tuple;
00043
00047 class FullTupleCompare {
00048 private:
00050 int arity;
00051 public:
00053 forceinline
00054 FullTupleCompare(int a) : arity(a) {}
00056 forceinline bool
00057 operator ()(const Tuple& a, const Tuple& b) {
00058 for (int i = 0; i < arity; i++)
00059 if (a[i] < b[i])
00060 return true;
00061 else if (a[i] > b[i])
00062 return false;
00063 return a < b;
00064 }
00065 };
00066
00073 class TuplePosCompare {
00074 private:
00076 int pos;
00077 public:
00079 forceinline
00080 TuplePosCompare(int p) : pos(p) {}
00082 forceinline bool
00083 operator ()(const Tuple& a, const Tuple& b) {
00084 if (a[pos] == b[pos])
00085 return a < b;
00086 else
00087 return a[pos] < b[pos];
00088 }
00089 };
00090
00091 }
00092
00093 namespace Gecode {
00094
00095 void
00096 TupleSet::TupleSetI::finalize(void) {
00097 assert(!finalized());
00098 assert(tuples == NULL);
00099
00100
00101 IntArgs ia(arity);
00102 for (int i = arity; i--; )
00103 ia[i] = Int::Limits::max+1;
00104 int real_min = min, real_max = max;
00105 add(ia);
00106 min = real_min; max = real_max;
00107
00108
00109 domsize = static_cast<unsigned int>(max - min) + 1;
00110
00111
00112 tuples = heap.alloc<Tuple*>(arity);
00113 tuple_data = heap.alloc<Tuple>(size*arity+1);
00114 tuple_data[size*arity] = NULL;
00115 nullpointer = tuple_data+(size*arity);
00116
00117
00118 for (int i = arity; i--; )
00119 tuples[i] = tuple_data + (i * size);
00120 for (int i = size; i--; )
00121 tuples[0][i] = data + (i * arity);
00122
00123 FullTupleCompare ftc(arity);
00124 Support::quicksort(tuples[0], size, ftc);
00125 assert(tuples[0][size-1][0] == ia[0]);
00126 int* new_data = heap.alloc<int>(size*arity);
00127 for (int t = size; t--; )
00128 for (int i = arity; i--; )
00129 new_data[t*arity + i] = tuples[0][t][i];
00130
00131 heap.rfree(data);
00132 data = new_data;
00133 excess = -1;
00134
00135
00136 for (int i = arity; i--; )
00137 for (int t = size; t--; )
00138 tuples[i][t] = data + (t * arity);
00139 for (int i = arity; i-->1; ) {
00140 TuplePosCompare tpc(i);
00141 Support::quicksort(tuples[i], size, tpc);
00142 }
00143
00144
00145 last = heap.alloc<Tuple*>(domsize*arity);
00146 for (int i = arity; i--; ) {
00147 Tuple* t = tuples[i];
00148 for (unsigned int d = 0; d < domsize; ++d) {
00149 while (t && *t && (*t)[i] < static_cast<int>(min+d)) {
00150 ++t;
00151 }
00152 if (t && *t && (*t)[i] == static_cast<int>(min+d)) {
00153 last[(i*domsize) + d] = t;
00154 ++t;
00155 } else {
00156 last[(i*domsize) + d] = nullpointer;
00157 }
00158 }
00159 }
00160
00161 assert(finalized());
00162 }
00163
00164 void
00165 TupleSet::TupleSetI::resize(void) {
00166 assert(excess == 0);
00167 int ndatasize = static_cast<int>(1+size*1.5);
00168 data = heap.realloc<int>(data, size * arity, ndatasize * arity);
00169 excess = ndatasize - size;
00170 }
00171
00172 SharedHandle::Object*
00173 TupleSet::TupleSetI::copy(void) const {
00174 assert(finalized());
00175 TupleSetI* d = new TupleSetI;
00176 d->arity = arity;
00177 d->size = size;
00178 d->excess = excess;
00179 d->min = min;
00180 d->max = max;
00181 d->domsize = domsize;
00182
00183
00184 d->data = heap.alloc<int>(size*arity);
00185 heap.copy(&d->data[0], &data[0], size*arity);
00186
00187
00188 d->tuples = heap.alloc<Tuple*>(arity);
00189 d->tuple_data = heap.alloc<Tuple>(size*arity+1);
00190 d->tuple_data[size*arity] = NULL;
00191 d->nullpointer = d->tuple_data+(size*arity);
00192
00193
00194 for (int i = arity; i--; )
00195 d->tuples[i] = d->tuple_data + (i * size);
00196 for (int a = arity; a--; ) {
00197 for (int i = size; i--; ) {
00198 d->tuples[a][i] = d->data + (tuples[a][i]-data);
00199 }
00200 }
00201
00202
00203 d->last = heap.alloc<Tuple*>(domsize*arity);
00204 for (int i = static_cast<int>(domsize)*arity; i--; ) {
00205 d->last[i] = d->tuple_data + (last[i]-tuple_data);
00206 }
00207
00208 return d;
00209 }
00210
00211 TupleSet::TupleSetI::~TupleSetI(void) {
00212 excess = -2;
00213 heap.rfree(tuples);
00214 heap.rfree(tuple_data);
00215 heap.rfree(data);
00216 heap.rfree(last);
00217 }
00218
00219 }
00220
00221
00222