Generated on Thu Mar 16 2017 03:24:17 for Gecode by doxygen 1.8.13
view.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2005
8  *
9  * Last modified:
10  * $Date: 2016-09-02 15:36:56 +0200 (Fri, 02 Sep 2016) $ by $Author: schulte $
11  * $Revision: 15162 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode {
39 
44  template<class View>
45  class ConstView {
46  public:
48  typedef typename View::VarImpType VarImpType;
50  typedef typename View::VarType VarType;
52 
53  unsigned int degree(void) const;
56  double afc(const Space& home) const;
58  static bool varderived(void);
60  VarImpType* varimp(void) const;
62 
64 
65  bool assigned(void) const;
68 
70 
71  static void schedule(Space& home, Propagator& p, ModEvent me);
74  static ModEvent me(const ModEventDelta& med);
76  static ModEventDelta med(ModEvent me);
78 
80 
81 
88  void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
90  void cancel(Space& home, Propagator& p, PropCond pc);
92  void reschedule(Space& home, Propagator& p, PropCond pc);
94  void subscribe(Space& home, Advisor& a);
96  void cancel(Space& home, Advisor& a);
98 
100 
101  static ModEvent modevent(const Delta& d);
104 
106 
107  void update(Space& home, bool share, ConstView& y);
110  };
111 
112 
113 
118  template<class Var>
119  class VarImpView {
120  public:
122  typedef Var VarType;
124  typedef typename Var::VarImpType VarImpType;
125  protected:
127  VarImpType* x;
129  VarImpView(void);
131  VarImpView(VarImpType* y);
133  void varimp(VarImpType* y);
134  public:
136 
137  static bool varderived(void);
140  VarImpType* varimp(void) const;
142  unsigned int degree(void) const;
144  double afc(const Space& home) const;
146 
148 
149  bool assigned(void) const;
152 
154 
155  static void schedule(Space& home, Propagator& p, ModEvent me);
158  static ModEvent me(const ModEventDelta& med);
160  static ModEventDelta med(ModEvent me);
162 
164 
165 
172  void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
174  void cancel(Space& home, Propagator& p, PropCond pc);
176  void reschedule(Space& home, Propagator& p, PropCond pc);
178  void subscribe(Space& home, Advisor& a);
180  void cancel(Space& home, Advisor& a);
182 
184 
185  static ModEvent modevent(const Delta& d);
188 
190 
191  void update(Space& home, bool share, VarImpView<Var>& y);
194  };
195 
200  template<class VarA, class VarB>
202  bool same(const VarImpView<VarA>& x, const VarImpView<VarB>& y);
204  template<class ViewA, class ViewB>
205  bool before(const ViewA& x, const ViewB& y);
207 
208 
213  template<class View>
214  class DerivedView {
215  public:
217  typedef typename View::VarImpType VarImpType;
219  typedef typename View::VarType VarType;
220  protected:
222  View x;
224  DerivedView(void);
225  public:
227  DerivedView(const View& y);
229 
230  static bool varderived(void);
233  VarImpType* varimp(void) const;
235  View base(void) const;
237  unsigned int degree(void) const;
239  double afc(const Space& home) const;
241 
243 
244  bool assigned(void) const;
247 
249 
250  static void schedule(Space& home, Propagator& p, ModEvent me);
253  static ModEvent me(const ModEventDelta& med);
255  static ModEventDelta med(ModEvent);
257 
259 
260 
267  void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
269  void cancel(Space& home, Propagator& p, PropCond pc);
271  void reschedule(Space& home, Propagator& p, PropCond pc);
273  void subscribe(Space& home, Advisor& a);
275  void cancel(Space& home, Advisor& a);
277 
279 
280  static ModEvent modevent(const Delta& d);
283 
285 
286  void update(Space& home, bool share, DerivedView<View>& y);
289  };
290 
291 
296  template<class ViewA, class ViewB>
297  bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&);
302  template<class Var, class View>
303  bool shared(const VarImpView<Var>&, const ConstView<View>&);
308  template<class ViewA, class ViewB>
309  bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&);
314  template<class View, class Var>
315  bool shared(const ConstView<View>&, const VarImpView<Var>&);
320  template<class ViewA, class ViewB>
321  bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&);
326  template<class VarA, class VarB>
327  bool shared(const VarImpView<VarA>&, const VarImpView<VarB>&);
332  template<class Var, class View>
333  bool shared(const VarImpView<Var>&, const DerivedView<View>&);
338  template<class View, class Var>
339  bool shared(const DerivedView<View>&, const VarImpView<Var>&);
344  template<class ViewA, class ViewB>
345  bool shared(const DerivedView<ViewA>&, const DerivedView<ViewB>&);
346 
347 
348  /*
349  * Constant view: has no variable implementation
350  *
351  */
352  template<class View>
353  forceinline unsigned int
355  return 0;
356  }
357  template<class View>
358  forceinline double
359  ConstView<View>::afc(const Space&) const {
360  return 0.0;
361  }
362  template<class View>
363  forceinline bool
365  return false;
366  }
367  template<class View>
368  forceinline typename View::VarImpType*
370  return NULL;
371  }
372  template<class View>
373  forceinline bool
375  return true;
376  }
377  template<class View>
378  forceinline void
380  bool schedule) {
381  if (schedule)
382  View::schedule(home,p,ME_GEN_ASSIGNED);
383  }
384  template<class View>
385  forceinline void
387  }
388  template<class View>
389  forceinline void
391  View::schedule(home,p,ME_GEN_ASSIGNED);
392  }
393  template<class View>
394  forceinline void
396  }
397  template<class View>
398  forceinline void
400  }
401  template<class View>
402  forceinline void
404  View::schedule(home,p,me);
405  }
406  template<class View>
409  return View::me(med);
410  }
411  template<class View>
414  return View::med(me);
415  }
416  template<class View>
419  (void) d;
420  return ME_GEN_NONE;
421  }
422  template<class View>
423  forceinline void
425  }
426 
427  /*
428  * Variable view: contains a pointer to a variable implementation
429  *
430  */
431  template<class Var>
434  : x(NULL) {}
435  template<class Var>
438  : x(y) {}
439  template<class Var>
440  forceinline void
442  x=y;
443  }
444  template<class Var>
445  forceinline bool
447  return true;
448  }
449  template<class Var>
450  forceinline typename Var::VarImpType*
452  return x;
453  }
454  template<class Var>
455  forceinline unsigned int
457  return x->degree();
458  }
459  template<class Var>
460  forceinline double
461  VarImpView<Var>::afc(const Space& home) const {
462  return x->afc(home);
463  }
464  template<class Var>
465  forceinline bool
467  return x->assigned();
468  }
469  template<class Var>
470  forceinline void
472  bool schedule) {
473  x->subscribe(home,p,pc,schedule);
474  }
475  template<class Var>
476  forceinline void
478  x->cancel(home,p,pc);
479  }
480  template<class Var>
481  forceinline void
483  x->reschedule(home,p,pc);
484  }
485  template<class Var>
486  forceinline void
488  x->subscribe(home,a);
489  }
490  template<class Var>
491  forceinline void
493  x->cancel(home,a);
494  }
495  template<class Var>
496  forceinline void
498  VarImpType::schedule(home,p,me);
499  }
500  template<class Var>
503  return VarImpType::me(med);
504  }
505  template<class Var>
508  return VarImpType::med(me);
509  }
510  template<class Var>
513  return VarImpType::modevent(d);
514  }
515  template<class Var>
516  forceinline void
518  x = y.x->copy(home,share);
519  }
520 
521  /*
522  * Derived view: contain the base view from which they are derived
523  *
524  */
525 
526  template<class View>
529 
530  template<class View>
533  : x(y) {}
534 
535  template<class View>
536  forceinline bool
538  return View::varderived();
539  }
540 
541  template<class View>
542  forceinline typename View::VarImpType*
544  return x.varimp();
545  }
546 
547  template<class View>
548  forceinline View
550  return x;
551  }
552 
553  template<class View>
554  forceinline unsigned int
556  return x.degree();
557  }
558  template<class View>
559  forceinline double
560  DerivedView<View>::afc(const Space& home) const {
561  return x.afc(home);
562  }
563  template<class View>
564  forceinline bool
566  return x.assigned();
567  }
568 
569  template<class View>
570  forceinline void
572  return View::schedule(home,p,me);
573  }
574  template<class View>
577  return View::me(med);
578  }
579  template<class View>
582  return View::med(me);
583  }
584 
585  template<class View>
586  forceinline void
588  bool schedule) {
589  x.subscribe(home,p,pc,schedule);
590  }
591  template<class View>
592  forceinline void
594  x.cancel(home,p,pc);
595  }
596  template<class View>
597  forceinline void
599  x.reschedule(home,p,pc);
600  }
601  template<class View>
602  forceinline void
604  x.subscribe(home,a);
605  }
606  template<class View>
607  forceinline void
609  x.cancel(home,a);
610  }
611  template<class View>
614  return View::modevent(d);
615  }
616  template<class View>
617  forceinline void
619  x.update(home,share,y.x);
620  }
621 
622 
623  /*
624  * Tests whether two views are the same
625  *
626  */
627 
629  template<class ViewA, class ViewB>
630  forceinline bool
632  return false;
633  }
635  template<class Var, class View>
636  forceinline bool
638  return false;
639  }
641  template<class ViewA, class ViewB>
642  forceinline bool
644  return false;
645  }
647  template<class Var, class View>
648  forceinline bool
650  return false;
651  }
653  template<class View, class Var>
654  forceinline bool
656  return false;
657  }
659  template<class Var>
660  forceinline bool
661  same(const VarImpView<Var>& x, const VarImpView<Var>& y) {
662  return x.varimp() == y.varimp();
663  }
665  template<class ViewA, class ViewB>
666  forceinline bool
668  return same(x.base(),y.base());
669  }
670 
671 
672  /*
673  * Tests whether one view is before the other
674  *
675  */
676  template<class ViewA, class ViewB>
677  forceinline bool
678  before(const ViewA& x, const ViewB& y) {
679  return x.varimp() < y.varimp();
680  }
681 
682 
683  /*
684  * Testing whether two views share the same variable
685  *
686  */
687 
688  template<class ViewA, class ViewB>
689  forceinline bool
691  return false;
692  }
693  template<class Var, class View>
694  forceinline bool
696  return false;
697  }
698  template<class ViewA, class ViewB>
699  forceinline bool
701  return false;
702  }
703  template<class View, class Var>
704  forceinline bool
706  return false;
707  }
708  template<class ViewA, class ViewB>
709  forceinline bool
711  return false;
712  }
713  template<class VarA, class VarB>
714  forceinline bool
716  return (static_cast<VarImpBase*>(x.varimp()) ==
717  static_cast<VarImpBase*>(y.varimp()));
718  }
719  template<class Var, class View>
720  forceinline bool
722  return (View::varderived() &&
723  static_cast<VarImpBase*>(x.varimp()) ==
724  static_cast<VarImpBase*>(y.varimp()));
725  }
726  template<class View, class Var>
727  forceinline bool
729  return (View::varderived() &&
730  static_cast<VarImpBase*>(x.varimp()) ==
731  static_cast<VarImpBase*>(y.varimp()));
732  }
733  template<class ViewA, class ViewB>
734  forceinline bool
736  return (ViewA::varderived() && ViewB::varderived() &&
737  static_cast<VarImpBase*>(x.varimp()) ==
738  static_cast<VarImpBase*>(y.varimp()));
739  }
740 
741 }
742 
743 // STATISTICS: kernel-var
VarImpType * varimp(void) const
Return dummy variable implementation of view.
Definition: view.hpp:369
View base(void) const
Return view from which this view is derived.
Definition: view.hpp:549
View::VarType VarType
The variable type corresponding to the constant view.
Definition: view.hpp:50
double afc(const Space &home) const
Return accumulated failure count.
Definition: view.hpp:359
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to view.
Definition: view.hpp:379
static ModEvent modevent(const Delta &d)
Return modification event.
Definition: view.hpp:418
bool assigned(void) const
Test whether view is assigned.
Definition: view.hpp:374
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:85
void update(Space &home, bool share, ConstView &y)
Update this view to be a clone of view y.
Definition: view.hpp:424
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition: core.hpp:149
void reschedule(Space &home, Propagator &p, PropCond pc)
Re-schedule propagator p with propagation condition pc.
Definition: view.hpp:390
AFC afc
Definition: afc.cpp:139
int ModEvent
Type for modification events.
Definition: core.hpp:142
void varimp(VarImpType *y)
Set variable implementation to y.
Definition: view.hpp:441
Base-class for propagators.
Definition: core.hpp:1012
Base-class for advisors.
Definition: core.hpp:1212
bool shared(const DerivedView< ViewA > &, const DerivedView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:735
VarImpView(void)
Default constructor.
Definition: view.hpp:433
Base-class for constant views.
Definition: view.hpp:45
Computation spaces.
Definition: core.hpp:1672
Base-class for derived views.
Definition: view.hpp:214
void cancel(Space &home, Propagator &p, PropCond pc)
Cancel subscription of propagator p with propagation condition pc to view.
Definition: view.hpp:386
Gecode::IntSet d(v, 7)
unsigned int degree(void) const
Return degree (number of subscribed propagators and advisors)
Definition: view.hpp:354
View::VarImpType VarImpType
The variable implementation type corresponding to the constant view.
Definition: view.hpp:48
struct Gecode::@554::NNF::@60::@62 a
For atomic nodes.
bool same(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether two views are the same.
Definition: view.hpp:631
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
static ModEvent me(const ModEventDelta &med)
Return modification event for view type in med.
Definition: view.hpp:408
int PropCond
Type for propagation conditions.
Definition: core.hpp:152
void subscribe(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:75
static bool varderived(void)
Return whether this view is derived from a VarImpView.
Definition: view.hpp:364
bool before(const ViewA &x, const ViewB &y)
Definition: view.hpp:678
Base-class for variable implementation views.
Definition: view.hpp:119
Var VarType
The variable type corresponding to the view.
Definition: view.hpp:122
static void schedule(Space &home, Propagator &p, ModEvent me)
Schedule propagator p with modification event me.
Definition: view.hpp:403
Var::VarImpType VarImpType
The variable implementation type corresponding to the view.
Definition: view.hpp:124
View::VarType VarType
The variable type belonging to the View.
Definition: view.hpp:219
const double base
Base for geometric restart sequence.
Definition: search.hh:122
Base class for variables.
Definition: var.hpp:44
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Generic domain change information to be supplied to advisors.
Definition: core.hpp:277
View::VarImpType VarImpType
The variable implementation type belonging to the View.
Definition: view.hpp:217
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:173
const ModEvent ME_GEN_NONE
Generic modification event: no modification.
Definition: core.hpp:147
View x
View from which this view is derived.
Definition: view.hpp:222
VarImpType * varimp(void) const
Return variable implementation of view.
Definition: view.hpp:543
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:690
bool same(const DerivedView< ViewA > &x, const DerivedView< ViewB > &y)
Test whether two views are the same.
Definition: view.hpp:667
Gecode toplevel namespace
void reschedule(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:96
VarImpType * x
Pointer to variable implementation.
Definition: view.hpp:127
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
static ModEventDelta med(ModEvent me)
Translate modification event me to modification event delta for view.
Definition: view.hpp:413