val-set.hpp
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 namespace Gecode { namespace Int {
00039
00040
00041
00042
00043
00044 forceinline
00045 ValSet::ValSet(void)
00046 : fst(NULL), lst(NULL), n(0) {}
00047
00048 forceinline void
00049 ValSet::add(Space& home, int v) {
00050 RangeList* c = fst;
00051 RangeList** p = &fst;
00052 while (c != NULL) {
00053 if (v < c->min()) {
00054 if (v+1 == c->min()) {
00055 c->min(v); n++;
00056 return;
00057 } else {
00058 *p = new (home) RangeList(v,v,c); n++;
00059 return;
00060 }
00061 } else if (v <= c->max()) {
00062
00063 return;
00064 } else if (v == c->max()+1) {
00065 if ((c->next() != NULL) && (v+1 == c->next()->min())) {
00066 c->next()->min(c->min());
00067 *p = c->next();
00068 c->dispose(home,c);
00069 } else {
00070 c->max(v);
00071 }
00072 n++;
00073 return;
00074 } else {
00075
00076 p = reinterpret_cast<RangeList**>(c->nextRef());
00077 c = *p;
00078 }
00079 }
00080 *p = new (home) RangeList(v,v,NULL); n++;
00081 lst = *p;
00082 }
00083
00084 forceinline int
00085 ValSet::size(void) const {
00086 return n;
00087 }
00088
00089 forceinline bool
00090 ValSet::empty(void) const {
00091 return n == 0;
00092 }
00093
00094 forceinline int
00095 ValSet::min(void) const {
00096 return fst->min();
00097 }
00098
00099 forceinline int
00100 ValSet::max(void) const {
00101 return lst->max();
00102 }
00103
00104 forceinline void
00105 ValSet::update(Space& home, bool share, ValSet& vs) {
00106 (void) share;
00107 if (vs.n > 0) {
00108 n = vs.n;
00109
00110 int m = 0;
00111 for (RangeList* c = vs.fst; c != NULL; c = c->next())
00112 m++;
00113 fst = home.alloc<RangeList>(m);
00114 lst = fst + (m-1);
00115 int i=0;
00116 for (RangeList* c = vs.fst; c != NULL; c = c->next()) {
00117 fst[i].min(c->min()); fst[i].max(c->max());
00118 fst[i].next(fst+i+1);
00119 i++;
00120 }
00121 lst->next(NULL);
00122 }
00123 }
00124
00125 forceinline void
00126 ValSet::flush(void) {
00127 fst = lst = NULL;
00128 }
00129
00130 forceinline void
00131 ValSet::dispose(Space& home) {
00132 if (fst != NULL)
00133 fst->dispose(home,lst);
00134 }
00135
00136 forceinline
00137 ValSet::Ranges::Ranges(const ValSet& vs)
00138 : c(vs.fst) {}
00139
00140 forceinline bool
00141 ValSet::Ranges::operator ()(void) const {
00142 return c != NULL;
00143 }
00144
00145 forceinline void
00146 ValSet::Ranges::operator ++(void) {
00147 c = c->next();
00148 }
00149
00150 forceinline int
00151 ValSet::Ranges::min(void) const {
00152 return c->min();
00153 }
00154 forceinline int
00155 ValSet::Ranges::max(void) const {
00156 return c->max();
00157 }
00158
00159 forceinline unsigned int
00160 ValSet::Ranges::width(void) const {
00161 return c->width();
00162 }
00163
00164 template<class View>
00165 forceinline Iter::Ranges::CompareStatus
00166 ValSet::compare(View x) const {
00167 if (empty() || (x.max() < min()) || (x.min() > max()))
00168 return Iter::Ranges::CS_DISJOINT;
00169 ValSet::Ranges vsr(*this);
00170 ViewRanges<View> xr(x);
00171 return Iter::Ranges::compare(xr,vsr);
00172 }
00173
00174 template<class View>
00175 forceinline bool
00176 ValSet::subset(View x) const {
00177 if (empty() || (x.min() < min()) || (x.max() > max()))
00178 return false;
00179 ValSet::Ranges vsr(*this);
00180 ViewRanges<View> xr(x);
00181 return Iter::Ranges::subset(xr,vsr);
00182 }
00183
00184 }}
00185
00186