Generated on Sat Jun 2 2018 07:17:44 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  * Contributing authors:
7  * Samuel Gagnon <samuel.gagnon92@gmail.com>
8  *
9  * Copyright:
10  * Christian Schulte, 2005
11  * Samuel Gagnon, 2018
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(void) const;
58  static bool varderived(void);
60  VarImpType* varimp(void) const;
61 #ifdef GECODE_HAS_CBS
62  unsigned int id(void) const;
64 #endif
65 
66 
68 
69  bool assigned(void) const;
72 
74 
75  static void schedule(Space& home, Propagator& p, ModEvent me);
78  static ModEvent me(const ModEventDelta& med);
80  static ModEventDelta med(ModEvent me);
82 
84 
85 
92  void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
94  void cancel(Space& home, Propagator& p, PropCond pc);
96  void reschedule(Space& home, Propagator& p, PropCond pc);
102  void subscribe(Space& home, Advisor& a, bool fail=false);
104  void cancel(Space& home, Advisor& a, bool fail=false);
106 
108 
109  static ModEvent modevent(const Delta& d);
112 
114 
115  void update(Space& home, ConstView& y);
118  };
119 
120 
121 
126  template<class Var>
127  class VarImpView {
128  public:
130  typedef Var VarType;
132  typedef typename Var::VarImpType VarImpType;
133  protected:
135  VarImpType* x;
137  VarImpView(void);
139  VarImpView(VarImpType* y);
141  void varimp(VarImpType* y);
142  public:
144 
145  static bool varderived(void);
148  VarImpType* varimp(void) const;
150  unsigned int degree(void) const;
152  double afc(void) const;
153 #ifdef GECODE_HAS_CBS
154  unsigned int id(void) const;
156 #endif
157 
158 
160 
161  bool assigned(void) const;
164 
166 
167  static void schedule(Space& home, Propagator& p, ModEvent me);
170  static ModEvent me(const ModEventDelta& med);
172  static ModEventDelta med(ModEvent me);
174 
176 
177 
184  void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
186  void cancel(Space& home, Propagator& p, PropCond pc);
188  void reschedule(Space& home, Propagator& p, PropCond pc);
194  void subscribe(Space& home, Advisor& a, bool fail=false);
196  void cancel(Space& home, Advisor& a, bool fail=false);
198 
200 
201  static ModEvent modevent(const Delta& d);
204 
206 
207  void update(Space& home, VarImpView<Var>& y);
210  };
211 
216  template<class VarA, class VarB>
218  bool same(const VarImpView<VarA>& x, const VarImpView<VarB>& y);
220  template<class ViewA, class ViewB>
221  bool before(const ViewA& x, const ViewB& y);
223 
224 
229  template<class View>
230  class DerivedView {
231  public:
233  typedef typename View::VarImpType VarImpType;
235  typedef typename View::VarType VarType;
236  protected:
238  View x;
240  DerivedView(void);
241  public:
243  DerivedView(const View& y);
245 
246  static bool varderived(void);
249  VarImpType* varimp(void) const;
251  View base(void) const;
253  unsigned int degree(void) const;
255  double afc(void) const;
256 #ifdef GECODE_HAS_CBS
257  unsigned int id(void) const;
259 #endif
260 
261 
263 
264  bool assigned(void) const;
267 
269 
270  static void schedule(Space& home, Propagator& p, ModEvent me);
273  static ModEvent me(const ModEventDelta& med);
275  static ModEventDelta med(ModEvent);
277 
279 
280 
287  void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
289  void cancel(Space& home, Propagator& p, PropCond pc);
291  void reschedule(Space& home, Propagator& p, PropCond pc);
297  void subscribe(Space& home, Advisor& a, bool fail=false);
299  void cancel(Space& home, Advisor& a, bool fail=false);
301 
303 
304  static ModEvent modevent(const Delta& d);
307 
309 
310  void update(Space& home, DerivedView<View>& y);
313  };
314 
315 
320  template<class ViewA, class ViewB>
321  bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&);
326  template<class Var, class View>
327  bool shared(const VarImpView<Var>&, const ConstView<View>&);
332  template<class ViewA, class ViewB>
333  bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&);
338  template<class View, class Var>
339  bool shared(const ConstView<View>&, const VarImpView<Var>&);
344  template<class ViewA, class ViewB>
345  bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&);
350  template<class VarA, class VarB>
351  bool shared(const VarImpView<VarA>&, const VarImpView<VarB>&);
356  template<class Var, class View>
357  bool shared(const VarImpView<Var>&, const DerivedView<View>&);
362  template<class View, class Var>
363  bool shared(const DerivedView<View>&, const VarImpView<Var>&);
368  template<class ViewA, class ViewB>
369  bool shared(const DerivedView<ViewA>&, const DerivedView<ViewB>&);
370 
371 
372  /*
373  * Constant view: has no variable implementation
374  *
375  */
376  template<class View>
377  forceinline unsigned int
379  return 0;
380  }
381  template<class View>
382  forceinline double
383  ConstView<View>::afc(void) const {
384  return 0.0;
385  }
386  template<class View>
387  forceinline bool
389  return false;
390  }
391  template<class View>
392  forceinline typename View::VarImpType*
394  return NULL;
395  }
396 #ifdef GECODE_HAS_CBS
397  template<class View>
398  forceinline unsigned int
399  ConstView<View>::id(void) const {
400  return 0;
401  }
402 #endif
403  template<class View>
404  forceinline bool
406  return true;
407  }
408  template<class View>
409  forceinline void
411  bool schedule) {
412  if (schedule)
413  View::schedule(home,p,ME_GEN_ASSIGNED);
414  }
415  template<class View>
416  forceinline void
418  }
419  template<class View>
420  forceinline void
422  View::schedule(home,p,ME_GEN_ASSIGNED);
423  }
424  template<class View>
425  forceinline void
427  }
428  template<class View>
429  forceinline void
431  }
432  template<class View>
433  forceinline void
435  View::schedule(home,p,me);
436  }
437  template<class View>
440  return View::me(med);
441  }
442  template<class View>
445  return View::med(me);
446  }
447  template<class View>
450  (void) d;
451  return ME_GEN_NONE;
452  }
453  template<class View>
454  forceinline void
456  }
457 
458  /*
459  * Variable view: contains a pointer to a variable implementation
460  *
461  */
462  template<class Var>
465  : x(NULL) {}
466  template<class Var>
469  : x(y) {}
470  template<class Var>
471  forceinline void
473  x=y;
474  }
475  template<class Var>
476  forceinline bool
478  return true;
479  }
480  template<class Var>
481  forceinline typename Var::VarImpType*
483  return x;
484  }
485  template<class Var>
486  forceinline unsigned int
488  return x->degree();
489  }
490  template<class Var>
491  forceinline double
492  VarImpView<Var>::afc(void) const {
493  return x->afc();
494  }
495 #ifdef GECODE_HAS_CBS
496  template<class Var>
497  forceinline unsigned int
498  VarImpView<Var>::id(void) const {
499  return x->id();
500  }
501 #endif
502  template<class Var>
503  forceinline bool
505  return x->assigned();
506  }
507  template<class Var>
508  forceinline void
510  bool schedule) {
511  x->subscribe(home,p,pc,schedule);
512  }
513  template<class Var>
514  forceinline void
516  x->cancel(home,p,pc);
517  }
518  template<class Var>
519  forceinline void
521  x->reschedule(home,p,pc);
522  }
523  template<class Var>
524  forceinline void
526  x->subscribe(home,a,fail);
527  }
528  template<class Var>
529  forceinline void
530  VarImpView<Var>::cancel(Space& home, Advisor& a, bool fail) {
531  x->cancel(home,a,fail);
532  }
533  template<class Var>
534  forceinline void
536  VarImpType::schedule(home,p,me);
537  }
538  template<class Var>
541  return VarImpType::me(med);
542  }
543  template<class Var>
546  return VarImpType::med(me);
547  }
548  template<class Var>
551  return VarImpType::modevent(d);
552  }
553  template<class Var>
554  forceinline void
556  x = y.x->copy(home);
557  }
558 
559  /*
560  * Derived view: contain the base view from which they are derived
561  *
562  */
563 
564  template<class View>
567 
568  template<class View>
571  : x(y) {}
572 
573  template<class View>
574  forceinline bool
576  return View::varderived();
577  }
578 
579  template<class View>
580  forceinline typename View::VarImpType*
582  return x.varimp();
583  }
584 
585  template<class View>
586  forceinline View
588  return x;
589  }
590 
591  template<class View>
592  forceinline unsigned int
594  return x.degree();
595  }
596  template<class View>
597  forceinline double
599  return x.afc();
600  }
601 #ifdef GECODE_HAS_CBS
602  template<class View>
603  forceinline unsigned int
604  DerivedView<View>::id(void) const {
605  return x.id();
606  }
607 #endif
608  template<class View>
609  forceinline bool
611  return x.assigned();
612  }
613 
614  template<class View>
615  forceinline void
617  return View::schedule(home,p,me);
618  }
619  template<class View>
622  return View::me(med);
623  }
624  template<class View>
627  return View::med(me);
628  }
629 
630  template<class View>
631  forceinline void
633  bool schedule) {
634  x.subscribe(home,p,pc,schedule);
635  }
636  template<class View>
637  forceinline void
639  x.cancel(home,p,pc);
640  }
641  template<class View>
642  forceinline void
644  x.reschedule(home,p,pc);
645  }
646  template<class View>
647  forceinline void
649  x.subscribe(home,a,fail);
650  }
651  template<class View>
652  forceinline void
653  DerivedView<View>::cancel(Space& home, Advisor& a, bool fail) {
654  x.cancel(home,a,fail);
655  }
656  template<class View>
659  return View::modevent(d);
660  }
661  template<class View>
662  forceinline void
664  x.update(home,y.x);
665  }
666 
667 
668  /*
669  * Tests whether two views are the same
670  *
671  */
672 
674  template<class ViewA, class ViewB>
675  forceinline bool
677  return false;
678  }
680  template<class Var, class View>
681  forceinline bool
683  return false;
684  }
686  template<class ViewA, class ViewB>
687  forceinline bool
689  return false;
690  }
692  template<class Var, class View>
693  forceinline bool
695  return false;
696  }
698  template<class View, class Var>
699  forceinline bool
701  return false;
702  }
704  template<class Var>
705  forceinline bool
707  return x.varimp() == y.varimp();
708  }
710  template<class ViewA, class ViewB>
711  forceinline bool
713  return same(x.base(),y.base());
714  }
715 
716 
717  /*
718  * Tests whether one view is before the other
719  *
720  */
721  template<class ViewA, class ViewB>
722  forceinline bool
723  before(const ViewA& x, const ViewB& y) {
724  return x.varimp() < y.varimp();
725  }
726 
727 
728  /*
729  * Testing whether two views share the same variable
730  *
731  */
732 
733  template<class ViewA, class ViewB>
734  forceinline bool
736  return false;
737  }
738  template<class Var, class View>
739  forceinline bool
741  return false;
742  }
743  template<class ViewA, class ViewB>
744  forceinline bool
746  return false;
747  }
748  template<class View, class Var>
749  forceinline bool
751  return false;
752  }
753  template<class ViewA, class ViewB>
754  forceinline bool
756  return false;
757  }
758  template<class VarA, class VarB>
759  forceinline bool
761  return (static_cast<VarImpBase*>(x.varimp()) ==
762  static_cast<VarImpBase*>(y.varimp()));
763  }
764  template<class Var, class View>
765  forceinline bool
767  return (View::varderived() &&
768  static_cast<VarImpBase*>(x.varimp()) ==
769  static_cast<VarImpBase*>(y.varimp()));
770  }
771  template<class View, class Var>
772  forceinline bool
774  return (View::varderived() &&
775  static_cast<VarImpBase*>(x.varimp()) ==
776  static_cast<VarImpBase*>(y.varimp()));
777  }
778  template<class ViewA, class ViewB>
779  forceinline bool
781  return (ViewA::varderived() && ViewB::varderived() &&
782  static_cast<VarImpBase*>(x.varimp()) ==
783  static_cast<VarImpBase*>(y.varimp()));
784  }
785 
786 }
787 
788 // STATISTICS: kernel-var
VarImpType * varimp(void) const
Return dummy variable implementation of view.
Definition: view.hpp:393
View base(void) const
Return view from which this view is derived.
Definition: view.hpp:587
View::VarType VarType
The variable type corresponding to the constant view.
Definition: view.hpp:50
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to view.
Definition: view.hpp:410
static ModEvent modevent(const Delta &d)
Return modification event.
Definition: view.hpp:449
bool assigned(void) const
Test whether view is assigned.
Definition: view.hpp:405
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:81
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition: core.hpp:69
void reschedule(Space &home, Propagator &p, PropCond pc)
Re-schedule propagator p with propagation condition pc.
Definition: view.hpp:421
AFC afc
Definition: afc.cpp:135
int ModEvent
Type for modification events.
Definition: core.hpp:62
void varimp(VarImpType *y)
Set variable implementation to y.
Definition: view.hpp:472
Base-class for propagators.
Definition: core.hpp:1023
Base-class for advisors.
Definition: core.hpp:1251
bool shared(const DerivedView< ViewA > &, const DerivedView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:780
#define forceinline
Definition: config.hpp:185
VarImpView(void)
Default constructor.
Definition: view.hpp:464
Base-class for constant views.
Definition: view.hpp:45
Computation spaces.
Definition: core.hpp:1701
Base-class for derived views.
Definition: view.hpp:230
void cancel(Space &home, Propagator &p, PropCond pc)
Cancel subscription of propagator p with propagation condition pc to view.
Definition: view.hpp:417
Gecode::IntSet d(v, 7)
unsigned int degree(void) const
Return degree (number of subscribed propagators and advisors)
Definition: view.hpp:378
View::VarImpType VarImpType
The variable implementation type corresponding to the constant view.
Definition: view.hpp:48
bool same(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether two views are the same.
Definition: view.hpp:676
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
static ModEvent me(const ModEventDelta &med)
Return modification event for view type in med.
Definition: view.hpp:439
int PropCond
Type for propagation conditions.
Definition: core.hpp:72
void subscribe(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:71
static bool varderived(void)
Return whether this view is derived from a VarImpView.
Definition: view.hpp:388
bool before(const ViewA &x, const ViewB &y)
Definition: view.hpp:723
Base-class for variable implementation views.
Definition: view.hpp:127
Var VarType
The variable type corresponding to the view.
Definition: view.hpp:130
static void schedule(Space &home, Propagator &p, ModEvent me)
Schedule propagator p with modification event me.
Definition: view.hpp:434
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:765
Var::VarImpType VarImpType
The variable implementation type corresponding to the view.
Definition: view.hpp:132
View::VarType VarType
The variable type belonging to the View.
Definition: view.hpp:235
const double base
Base for geometric restart sequence.
Definition: search.hh:124
Base class for variables.
Definition: var.hpp:40
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Generic domain change information to be supplied to advisors.
Definition: core.hpp:203
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
View::VarImpType VarImpType
The variable implementation type belonging to the View.
Definition: view.hpp:233
double afc(void) const
Return accumulated failure count.
Definition: view.hpp:383
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:43
const ModEvent ME_GEN_NONE
Generic modification event: no modification.
Definition: core.hpp:67
Post propagator for SetVar x
Definition: set.hh:765
View x
View from which this view is derived.
Definition: view.hpp:238
void update(Space &home, ConstView &y)
Update this view to be a clone of view y.
Definition: view.hpp:455
VarImpType * varimp(void) const
Return variable implementation of view.
Definition: view.hpp:581
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:735
bool same(const DerivedView< ViewA > &x, const DerivedView< ViewB > &y)
Test whether two views are the same.
Definition: view.hpp:712
Gecode toplevel namespace
void reschedule(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:92
VarImpType * x
Pointer to variable implementation.
Definition: view.hpp:135
int ModEventDelta
Modification event deltas.
Definition: core.hpp:89
void update(IntSet &y, Space &home, IntSet &py)
Definition: rel.hpp:103
static ModEventDelta med(ModEvent me)
Translate modification event me to modification event delta for view.
Definition: view.hpp:444