Generated on Thu Apr 11 13:59:16 2019 for Gecode by doxygen 1.6.3

filter.cpp

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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/kernel.hh>
00035 
00036 namespace Gecode {
00037 
00038   /*
00039    * Trace filter expressions
00040    *
00041    */
00042   bool
00043   TFE::Node::decrement(void) {
00044     if (--use == 0) {
00045       if ((l != NULL) && l->decrement())
00046         delete l;
00047       if ((r != NULL) && r->decrement())
00048         delete r;
00049       return true;
00050     }
00051     return false;
00052   }
00053 
00054 
00055   forceinline void
00056   TFE::init(Group g, char what) {
00057     n = new Node;
00058     n->t = NT_GROUP;
00059     n->g = g;
00060     n->n = 1;
00061     n->w = what;
00062   }
00063 
00064   inline TFE
00065   TFE::negate(void) const {
00066     Node* m = new Node;
00067     m->t = NT_NEGATE;
00068     m->n = n->n;
00069     m->l = n; n->use++;
00070     return TFE(m);
00071   }
00072 
00073   TFE::TFE(PropagatorGroup g) {
00074     init(g,(1 << ViewTraceInfo::PROPAGATOR) | (1 << ViewTraceInfo::POST));
00075   }
00076 
00077   TFE::TFE(BrancherGroup g) {
00078     init(g,(1 << ViewTraceInfo::BRANCHER));
00079   }
00080 
00081   TFE
00082   TFE::other(void) {
00083     TFE e;
00084     e.init(Group::all,(1 << ViewTraceInfo::OTHER));
00085     return e;
00086   }
00087 
00088   TFE::TFE(const TFE& e) : n(e.n) {
00089     n->use++;
00090   }
00091 
00092   TFE&
00093   TFE::operator =(const TFE& e) {
00094     if (&e != this) {
00095       if (n->decrement())
00096         delete n;
00097       n = e.n;
00098       n->use++;
00099     }
00100     return *this;
00101   }
00102 
00103   TFE&
00104   TFE::operator +=(const TFE& e) {
00105     Node* a = new Node;
00106     a->t = NT_ADD;
00107     a->n = n->n + e.n->n;
00108     a->l = n;
00109     a->r = e.n; e.n->use++;
00110     n = a;
00111     return *this;
00112   }
00113 
00114   TFE&
00115   TFE::operator -=(const TFE& e) {
00116     return operator +=(e.negate());
00117   }
00118 
00119   TFE::~TFE(void) {
00120     if (n->decrement())
00121       delete n;
00122   }
00123 
00124 
00125   TFE
00126   operator -(const TFE& e) {
00127     return e.negate();
00128   }
00129 
00130   TFE
00131   propagator(PropagatorGroup g) {
00132     TFE e;
00133     e.init(g,(1 << ViewTraceInfo::PROPAGATOR));
00134     return e;
00135   }
00136 
00137   TFE
00138   post(PropagatorGroup g) {
00139     TFE e;
00140     e.init(g,(1 << ViewTraceInfo::POST));
00141     return e;
00142   }
00143 
00144 
00145 
00146   /*
00147    * Trace filters
00148    *
00149    */
00150 
00151 
00152   forceinline
00153   TraceFilter::TFO::StackFrame::StackFrame(void) {}
00154   forceinline
00155   TraceFilter::TFO::StackFrame::StackFrame(TFE::Node* n0, bool neg0)
00156     : n(n0), neg(neg0) {}
00157 
00158   void
00159   TraceFilter::TFO::fill(TFE::Node* n) {
00160     Region region;
00161     Support::DynamicStack<StackFrame,Region> next(region);
00162     int i=0;
00163     next.push(StackFrame(n,false));
00164     do {
00165       StackFrame s = next.pop();
00166       switch (s.n->t) {
00167       case TFE::NT_GROUP:
00168         f[i].g = s.n->g; f[i].neg = s.neg; f[i].what=s.n->w;
00169         i++;
00170         break;
00171       case TFE::NT_NEGATE:
00172         next.push(StackFrame(s.n->l,!s.neg));
00173         break;
00174       case TFE::NT_ADD:
00175         next.push(StackFrame(s.n->l,s.neg));
00176         next.push(StackFrame(s.n->r,s.neg));
00177         break;
00178       default: GECODE_NEVER;
00179       }
00180     } while (!next.empty());
00181   }
00182 
00183   TraceFilter::TFO::~TFO(void) {
00184     heap.free<Filter>(f,n);
00185   }
00186 
00187   TraceFilter::TraceFilter(void) : SharedHandle(new TFO) {}
00188 
00189   TraceFilter::TraceFilter(const TFE& e) : SharedHandle(new TFO(e)) {}
00190 
00191   TraceFilter::TraceFilter(PropagatorGroup g) : SharedHandle(new TFO(g)) {}
00192 
00193   TraceFilter::TraceFilter(BrancherGroup g) : SharedHandle(new TFO(g)) {}
00194 
00195   TraceFilter::TraceFilter(const TraceFilter& tf) : SharedHandle(tf) {}
00196 
00197   TraceFilter&
00198   TraceFilter::operator =(const TraceFilter& tf) {
00199     return static_cast<TraceFilter&>(SharedHandle::operator =(tf));
00200   }
00201 
00202   TraceFilter TraceFilter::all;
00203 
00204 }
00205 
00206 // STATISTICS: kernel-trace