/usr/include/gecode/int/sequence/view.hpp is in libgecode-dev 5.1.0-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* David Rijsman <David.Rijsman@quintiq.com>
*
* Contributing authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* David Rijsman, 2009
* Christian Schulte, 2009
*
* Last modified:
* $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $
* $Revision: 14967 $
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Gecode { namespace Int { namespace Sequence {
/**
* \brief Class for advising the propagator
*/
template<class View>
class SupportAdvisor : public Advisor {
public:
/// Index position
int i;
/// Initialize
SupportAdvisor(Space& home, Propagator& p, Council<SupportAdvisor>& c,
int i0);
/// Copy during cloning
SupportAdvisor(Space& home, bool share, SupportAdvisor& a);
/// Dispose advisor
void dispose(Space& home, Council<SupportAdvisor>& c);
};
template<class View>
forceinline
SupportAdvisor<View>::SupportAdvisor(Space& home, Propagator& p,
Council<SupportAdvisor>& c,int i0)
: Advisor(home,p,c), i(i0) {
}
template<class View>
forceinline
SupportAdvisor<View>::SupportAdvisor(Space& home, bool share,
SupportAdvisor& a)
: Advisor(home,share,a), i(a.i) {
}
template<class View>
forceinline void
SupportAdvisor<View>::dispose(Space& home, Council<SupportAdvisor>& c) {
Advisor::dispose(home,c);
}
/**
* \brief Class for view value support structure
*/
template<class View, class Val,bool iss>
class ViewValSupport {
public:
/// Initialize
void init(Space& home, ViewArray<View>& x,Val s, int i, int q);
/// Update
void update(Space& home, bool share, ViewValSupport<View,Val,iss>& vvs, int n0);
/// Advise
ExecStatus advise(Space& home,ViewArray<View>& a,Val s,int i,int q, int j,const Delta& d);
/// Propagate
ExecStatus propagate(Space& home,ViewArray<View>& a,Val s,int i,int q,int l,int u);
/// Return true if sequence j has been violated
bool violated(int j, int q, int l, int u) const;
/// Check if retired
bool retired(void) const;
/// Allocate an instance
static ViewValSupport* allocate(Space&,int);
private:
/// Schedules the invoking support to be concluded (to be shaved)
ExecStatus schedule_conclusion(ViewArray<View>& a,Val s,int i);
/// Returns true if a conclusion to be made has been scheduled
bool conlusion_scheduled(void) const;
/// Retires invoking instance
void retire(void);
/// Return number of forced to s elements in sequence j
int values(int j, int q) const;
/// Checks if x[i] has been shaved
bool shaved(const View& x, Val s, int i) const;
/// Push up the cumulative array
bool pushup(ViewArray<View>& a,Val s,int i,int q,int idx, int v);
/// Make conclusion
ExecStatus conclude(Space& home,ViewArray<View>& a,Val s,int i);
/// Returns true if a[idx]=s not possible taking into account a[i]=s if iss or a[i]!=s if ! iss
bool s_not_possible(ViewArray<View>& a, Val s, int i, int idx) const;
/// Returns true if a[idx] can be not s taking into account a[i]=s if iss or a[i]!=s if ! iss
bool alternative_not_possible(ViewArray<View>& a,Val s,int i, int idx) const;
/// Returns true if there are potential violations marked
bool has_potential_violation(void) const;
/// Returns next potential violation and removes it
int next_potential_violation(void);
/// Adds potential violation i
void potential_violation(int i);
// Sets element idx of cumulative array to v
void set(int idx, int v, int q, int n);
/// Adds potential violations for location idx in cumulative array
void potential_violation(int idx, int q, int n);
/// Cumulative array
int* y;
/// Potential violation set
Violations v;
};
template<class View, class Val,bool iss>
forceinline ViewValSupport<View,Val,iss>*
ViewValSupport<View,Val,iss>::allocate(Space& home, int n) {
return home.alloc<ViewValSupport<View,Val,iss> >(n);
}
template<class View, class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::has_potential_violation(void) const {
return !v.empty();
}
template<class View, class Val,bool iss>
forceinline int
ViewValSupport<View,Val,iss>::next_potential_violation(void) {
return static_cast<int>(v.get());
}
template<class View, class Val,bool iss>
forceinline void
ViewValSupport<View,Val,iss>::potential_violation(int k) {
v.add(static_cast<unsigned int>(k));
}
template<class View, class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::retired(void) const {
return NULL == y;
}
template<class View, class Val,bool iss>
forceinline void
ViewValSupport<View,Val,iss>::retire(void) {
y = NULL;
}
template<class View,class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::alternative_not_possible
(ViewArray<View>& a, Val s, int i, int idx) const {
(void) i;
return includes(a[idx-1],s) || (iss && (idx-1 == i));
}
template<class View, class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::s_not_possible
(ViewArray<View>& a, Val s, int i, int idx) const {
(void) i;
return excludes(a[idx-1],s) || (!iss && (i == idx-1));
}
template<class View, class Val,bool iss>
forceinline void
ViewValSupport<View,Val,iss>::init(Space& home, ViewArray<View>& a, Val s,
int i, int q) {
y = home.alloc<int>(a.size()+1);
v.init(home,static_cast<unsigned int>(a.size()));
y[0] = 0;
for ( int l=0; l<a.size(); l++ ) {
if ( alternative_not_possible(a,s,i,l+1) ) {
y[l+1] = y[l] + 1;
} else {
y[l+1] = y[l];
}
if ( l+1 >= q ) {
potential_violation(l+1-q);
}
if ( l <= a.size() - q ) {
potential_violation(l);
}
}
}
template<class View, class Val,bool iss>
forceinline void
ViewValSupport<View,Val,iss>::update(Space& home, bool share,
ViewValSupport<View,Val,iss>& vvs,
int n0) {
y = NULL;
if ( !vvs.retired() ) {
y = home.alloc<int>(n0);
for ( int l=0; l<n0; l++ ) {
y[l] = vvs.y[l];
}
v.update(home,share,vvs.v);
// = &home.construct<S>(S::key_compare(),S::allocator_type(home));
}
}
template<class View,class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::shaved(const View& x, Val s, int) const {
if (iss)
return excludes(x,s);
else
return includes(x,s);
}
template<class View,class Val,bool iss>
forceinline ExecStatus
ViewValSupport<View,Val,iss>::schedule_conclusion(ViewArray<View>& a, Val s,
int i) {
if (!retired()) {
if ((iss && includes(a[i],s)) || (!iss && excludes(a[i],s)))
return ES_FAILED;
y[0] = 1;
potential_violation(0);
}
return ES_OK;
}
template<class View,class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::conlusion_scheduled(void) const {
return !retired() && y[0] > 0;
}
template<class View,class Val,bool iss>
forceinline int
ViewValSupport<View,Val,iss>::values(int j, int q) const {
return y[j+q]-y[j];
}
template<class View,class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::violated(int j, int q, int l, int u) const {
return values(j,q) < l || values(j,q) > u;
}
template<class View,class Val,bool iss>
forceinline ExecStatus
ViewValSupport<View,Val,iss>::conclude(Space& home,ViewArray<View>& a,
Val s, int i) {
if ( iss ) {
GECODE_ME_CHECK(exclude(home,a[i],s));
} else {
GECODE_ME_CHECK(include(home,a[i],s));
}
retire();
return ES_OK;
}
template<class View,class Val,bool iss>
forceinline void
ViewValSupport<View,Val,iss>::potential_violation(int idx, int q, int n) {
if ( idx >= q ) {
potential_violation(idx-q);
}
if ( idx <= n - q - 1) {
potential_violation(idx);
}
}
template<class View,class Val,bool iss>
forceinline void
ViewValSupport<View,Val,iss>::set(int idx, int v, int q, int n) {
assert(y[idx]<=v);
if ( y[idx] < v ) {
y[idx] = v;
potential_violation(idx,q,n);
}
}
template<class View,class Val,bool iss>
forceinline bool
ViewValSupport<View,Val,iss>::pushup(ViewArray<View>& a,Val s,int i,int q,int idx, int v) {
if ( !retired() ) {
int n = a.size() + 1;
set(idx,y[idx]+v,q,n);
if ( y[idx] > idx ) {
return false;
}
int t = idx;
// repair y on the left
while ( idx > 0 && ((y[idx]-y[idx-1]>1) || ((y[idx]-y[idx-1]==1 && s_not_possible(a,s,i,idx)))) ) {
if ( s_not_possible(a,s,i,idx) ) {
set(idx-1,y[idx],q,n);
} else {
set(idx-1,y[idx]-1,q,n);
}
if ( y[idx-1]>idx-1 ) {
return false;
}
idx -= 1;
}
idx = t;
// repair y on the right
while ( idx < a.size() && ((y[idx]-y[idx+1]>0) || ((y[idx]-y[idx+1]==0) && alternative_not_possible(a,s,i,idx+1))) ) {
if ( alternative_not_possible(a,s,i,idx+1) ) {
set(idx+1,y[idx]+1,q,n);
} else {
set(idx+1,y[idx],q,n);
}
idx += 1;
}
}
return true;
}
template<class View,class Val,bool iss>
forceinline ExecStatus
ViewValSupport<View,Val,iss>::advise(Space&, ViewArray<View>& a,
Val s,int i,int q, int j,
const Delta&) {
ExecStatus status = ES_FIX;
if (!retired()) {
if ((j == i) && shaved(a[j],s,j)) {
retire();
} else {
switch (takes(a[j],s)) {
case TS_YES:
if (y[j+1]-y[j] == 0) {
if (!pushup(a,s,i,q,j+1,1)) {
GECODE_ES_CHECK(schedule_conclusion(a,s,i));
}
}
break;
case TS_NO:
if (y[j+1]-y[j] > 0) {
if (!pushup(a,s,i,q,j,y[j+1]-y[j])) {
GECODE_ES_CHECK(schedule_conclusion(a,s,i));
}
}
break;
case TS_MAYBE:
break;
default:
GECODE_NEVER;
}
if ( has_potential_violation() )
status = ES_NOFIX;
}
}
return status;
}
template<class View,class Val,bool iss>
forceinline ExecStatus
ViewValSupport<View,Val,iss>::propagate(Space& home,ViewArray<View>& a,Val s,int i,int q,int l,int u) {
if ( !retired() ) {
if ( conlusion_scheduled() ) {
return conclude(home,a,s,i);
}
while (has_potential_violation()) {
int j = next_potential_violation();
if (violated(j,q,l,u)) {
int forced_to_s = values(j,q);
if (forced_to_s < l) {
if (!pushup(a,s,i,q,j+q,l-forced_to_s))
return conclude(home,a,s,i);
} else {
if (!pushup(a,s,i,q,j,forced_to_s-u))
return conclude(home,a,s,i);
}
if (violated(j,q,l,u))
return conclude(home,a,s,i);
}
}
}
return ES_OK;
}
template<class View,class Val,bool iss>
ViewValSupportArray<View,Val,iss>::ViewValSupportArray(void) : xs(NULL), n(0) {
}
template<class View,class Val,bool iss>
ViewValSupportArray<View,Val,iss>::ViewValSupportArray(const ViewValSupportArray<View,Val,iss>& a) {
n = a.n; xs = a.xs;
}
template<class View,class Val,bool iss>
ViewValSupportArray<View,Val,iss>::ViewValSupportArray(Space& home,ViewArray<View>& x, Val s, int q) : xs(NULL) {
n = x.size();
if ( n > 0 ) {
xs = ViewValSupport<View,Val,iss>::allocate(home,n);
for (int i = n; i--; ) {
xs[i].init(home,x,s,i,q);
}
}
}
template<class View,class Val,bool iss>
ViewValSupportArray<View,Val,iss>::ViewValSupportArray(Space& home, int n0) : xs(NULL) {
n = n0;
if (n>0) {
xs = ViewValSupport<View,Val,iss>::allocate(home,n);
}
}
template<class View,class Val,bool iss>
forceinline int
ViewValSupportArray<View,Val,iss>::size(void) const {
return n;
}
template<class View,class Val,bool iss>
forceinline ViewValSupport<View,Val,iss>&
ViewValSupportArray<View,Val,iss>::operator [](int i) {
assert((i >= 0) && (i < size()));
return xs[i];
}
template<class View,class Val,bool iss>
forceinline const ViewValSupport<View,Val,iss>&
ViewValSupportArray<View,Val,iss>::operator [](int i) const {
assert((i >= 0) && (i < size()));
return xs[i];
}
template<class View,class Val,bool iss>
void
ViewValSupportArray<View,Val,iss>::update(Space& home, bool share, ViewValSupportArray<View,Val,iss>& a) {
n = a.size();
if (n>0) {
xs = ViewValSupport<View,Val,iss>::allocate(home,n);
for (int i=n; i--; ) {
xs[i].update(home,share,a[i],n+1);
}
}
}
template<class View,class Val,bool iss>
ExecStatus
ViewValSupportArray<View,Val,iss>::propagate(Space& home,ViewArray<View>& a,Val s,int q,int l,int u) {
for (int i=n; i--; ) {
GECODE_ES_CHECK(xs[i].propagate(home,a,s,i,q,l,u));
}
return ES_OK;
}
template<class View,class Val,bool iss>
ExecStatus
ViewValSupportArray<View,Val,iss>::advise(Space& home,ViewArray<View>& a,Val s,int q,int j,const Delta& d) {
ExecStatus status = ES_FIX;
for (int i=n; i--; ) {
if ( ES_NOFIX == xs[i].advise(home,a,s,i,q,j,d) )
status = ES_NOFIX;
}
return status;
}
}}}
// STATISTICS: int-prop
|