Generated on Tue Apr 18 10:22:07 2017 for Gecode by doxygen 1.6.3

trace-filter.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2016
00008  *
00009  *  Last modified:
00010  *     $Date: 2017-03-17 23:04:57 +0100 (Fri, 17 Mar 2017) $ by $Author: schulte $
00011  *     $Revision: 15597 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode {
00039 
00040   class TraceFilter;
00041 
00046   class TFE {
00047     friend GECODE_KERNEL_EXPORT TFE operator -(const TFE& r);
00048     friend GECODE_KERNEL_EXPORT TFE propagator(PropagatorGroup g);
00049     friend GECODE_KERNEL_EXPORT TFE post(PropagatorGroup g);
00050     friend class TraceFilter;
00051   protected:
00053     enum NodeType {
00054       NT_GROUP,    
00055       NT_NEGATE,   
00056       NT_ADD       
00057     };
00059     class Node : public HeapAllocated {
00060     public:
00062       unsigned int use;
00064       NodeType t;
00066       int n;
00068       Group g;
00070       char w;
00072       Node *l, *r;
00074       Node(void);
00076       GECODE_KERNEL_EXPORT
00077       bool decrement(void);
00078     };
00080     Node* n;
00082     TFE(void);
00084     TFE(Node* n);
00086     void init(Group g, char what);
00088     TFE negate(void) const;
00089   public:
00091     GECODE_KERNEL_EXPORT
00092     TFE(PropagatorGroup g);
00094     GECODE_KERNEL_EXPORT
00095     TFE(BrancherGroup g);
00097     GECODE_KERNEL_EXPORT
00098     static TFE other(void);
00100     GECODE_KERNEL_EXPORT
00101     TFE(const TFE& e);
00103     GECODE_KERNEL_EXPORT
00104     TFE& operator =(const TFE& e);
00106     GECODE_KERNEL_EXPORT
00107     TFE& operator +=(const TFE& e);
00109     GECODE_KERNEL_EXPORT
00110     TFE& operator -=(const TFE& e);
00112     GECODE_KERNEL_EXPORT
00113     ~TFE(void);
00114   };
00115 
00117   TFE operator +(TFE l, const TFE& r);
00119   TFE operator +(const TFE& e);
00121   TFE operator -(TFE l, const TFE& r);
00123   GECODE_KERNEL_EXPORT
00124   TFE operator -(const TFE& e);
00126   GECODE_KERNEL_EXPORT
00127   TFE propagator(PropagatorGroup g);
00129   GECODE_KERNEL_EXPORT
00130   TFE post(PropagatorGroup g);
00131 
00132 
00137   class TraceFilter : public SharedHandle {
00138   protected:
00140     class TFO : public SharedHandle::Object {
00141     public:
00143       struct Filter {
00145         Group g;
00147         bool neg;
00149         char what;
00150       };
00151       class StackFrame {
00152       public:
00154         TFE::Node* n;
00156         bool neg;
00158         StackFrame(void);
00160         StackFrame(TFE::Node* n, bool neg);
00161       };
00163       int n;
00165       Filter* f;
00167       GECODE_KERNEL_EXPORT
00168       void fill(TFE::Node* n);
00170       TFO(void);
00172       TFO(const TFE& e);
00174       TFO(PropagatorGroup g);
00176       TFO(BrancherGroup g);
00178       TFO(const TFO& o);
00180       bool operator ()(const ViewTraceInfo& vti) const;
00182       bool operator ()(PropagatorGroup pg) const;
00184       bool operator ()(BrancherGroup bg) const;
00186       GECODE_KERNEL_EXPORT
00187       virtual Object* copy(void) const;
00189       GECODE_KERNEL_EXPORT
00190       virtual ~TFO(void);
00191     };
00192   public:
00194     GECODE_KERNEL_EXPORT
00195     TraceFilter(void);
00197     GECODE_KERNEL_EXPORT
00198     TraceFilter(const TFE& e);
00200     GECODE_KERNEL_EXPORT
00201     TraceFilter(PropagatorGroup g);
00203     GECODE_KERNEL_EXPORT
00204     TraceFilter(BrancherGroup g);
00206     GECODE_KERNEL_EXPORT
00207     TraceFilter(const TraceFilter& tf);
00209     GECODE_KERNEL_EXPORT
00210     TraceFilter& operator =(const TraceFilter& tf);
00212     bool operator ()(const ViewTraceInfo& vti) const;
00214     bool operator ()(PropagatorGroup pg) const;
00216     bool operator ()(BrancherGroup bg) const;
00218     GECODE_KERNEL_EXPORT
00219     static TraceFilter all;
00220   };
00221 
00222 
00223 
00224   /*
00225    * Trace expression filters
00226    *
00227    */
00228 
00229   forceinline
00230   TFE::Node::Node(void)
00231     : use(1), l(NULL), r(NULL) {}
00232   forceinline
00233   TFE::TFE(void) : n(NULL) {}
00234   forceinline
00235   TFE::TFE(TFE::Node* n0) : n(n0) {}
00236 
00237   forceinline TFE
00238   operator +(TFE l, const TFE& r) {
00239     l += r; return l;
00240   }
00241   forceinline TFE
00242   operator +(const TFE& e) {
00243     return e;
00244   }
00245   forceinline TFE
00246   operator -(TFE l, const TFE& r) {
00247     l -= r; return l;
00248   }
00249 
00250 
00251   /*
00252    * Trace filters
00253    *
00254    */
00255   forceinline
00256   TraceFilter::TFO::TFO(void) : n(0), f(NULL) {}
00257   forceinline
00258   TraceFilter::TFO::TFO(const TFE& e)
00259     : n(e.n->n),
00260       f((n == 0) ? NULL : heap.alloc<Filter>(n)) {
00261     if (n > 0)
00262       fill(e.n);
00263   }
00264   forceinline
00265   TraceFilter::TFO::TFO(PropagatorGroup g) : n(1) {
00266     f = heap.alloc<Filter>(1);
00267     f[0].g = g; f[0].neg = false;
00268     f[0].what = 1 << ViewTraceInfo::PROPAGATOR;
00269 
00270   }
00271   forceinline
00272   TraceFilter::TFO::TFO(BrancherGroup g) : n(1) {
00273     f = heap.alloc<Filter>(1);
00274     f[0].g = g; f[0].neg = false;
00275     f[0].what = 1 << ViewTraceInfo::BRANCHER;
00276 
00277   }
00278   forceinline
00279   TraceFilter::TFO::TFO(const TFO& o) : n(o.n) {
00280     if (n > 0) {
00281       f = heap.alloc<Filter>(n);
00282       for (int i=n; i--; )
00283         f[i] = o.f[i];
00284     } else {
00285       f = NULL;
00286     }
00287   }
00288   forceinline bool
00289   TraceFilter::TFO::operator ()(const ViewTraceInfo& vti) const {
00290     if (n == 0)
00291       return true;
00292     for (int i=n; i--; )
00293       if (f[i].what & (1 << vti.what())) {
00294         // Group is of the right type
00295         switch (vti.what()) {
00296         case ViewTraceInfo::PROPAGATOR:
00297           if (f[i].g.in(vti.propagator().group()) != f[i].neg)
00298             return true;
00299           break;
00300         case ViewTraceInfo::BRANCHER:
00301           if (f[i].g.in(vti.brancher().group()) != f[i].neg)
00302             return true;
00303           break;
00304         case ViewTraceInfo::POST:
00305           if (f[i].g.in(vti.post()) != f[i].neg)
00306             return true;
00307           break;
00308         case ViewTraceInfo::OTHER:
00309           return true;
00310         default:
00311           GECODE_NEVER;
00312         }
00313       }
00314     return false;
00315   }
00316 
00317   forceinline bool
00318   TraceFilter::operator ()(const ViewTraceInfo& vti) const {
00319     return static_cast<TFO*>(object())->operator ()(vti);
00320   }
00321 
00322   forceinline bool
00323   TraceFilter::TFO::operator ()(PropagatorGroup pg) const {
00324     if (n == 0)
00325       return true;
00326     for (int i=n; i--; )
00327       if ((f[i].what & (1 << ViewTraceInfo::PROPAGATOR)) &&
00328           (f[i].g.in(pg) != f[i].neg))
00329         return true;
00330     return false;
00331   }
00332 
00333   forceinline bool
00334   TraceFilter::operator ()(PropagatorGroup pg) const {
00335     return static_cast<TFO*>(object())->operator ()(pg);
00336   }
00337 
00338   forceinline bool
00339   TraceFilter::TFO::operator ()(BrancherGroup bg) const {
00340     if (n == 0)
00341       return true;
00342     for (int i=n; i--; )
00343       if ((f[i].what & (1 << ViewTraceInfo::BRANCHER)) &&
00344           (f[i].g.in(bg) != f[i].neg))
00345         return true;
00346     return false;
00347   }
00348 
00349   forceinline bool
00350   TraceFilter::operator ()(BrancherGroup bg) const {
00351     return static_cast<TFO*>(object())->operator ()(bg);
00352   }
00353 
00354 }
00355 
00356 // STATISTICS: kernel-trace