Generated on Tue May 22 09:40:10 2018 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     Support::DynamicStack<StackFrame,Heap> next(heap);
00161     int i=0;
00162     next.push(StackFrame(n,false));
00163     do {
00164       StackFrame s = next.pop();
00165       switch (s.n->t) {
00166       case TFE::NT_GROUP:
00167         f[i].g = s.n->g; f[i].neg = s.neg; f[i].what=s.n->w;
00168         i++;
00169         break;
00170       case TFE::NT_NEGATE:
00171         next.push(StackFrame(s.n->l,!s.neg));
00172         break;
00173       case TFE::NT_ADD:
00174         next.push(StackFrame(s.n->l,s.neg));
00175         next.push(StackFrame(s.n->r,s.neg));
00176         break;
00177       default: GECODE_NEVER;
00178       }
00179     } while (!next.empty());
00180   }
00181 
00182   TraceFilter::TFO::~TFO(void) {
00183     heap.free<Filter>(f,n);
00184   }
00185 
00186   TraceFilter::TraceFilter(void) : SharedHandle(new TFO) {}
00187 
00188   TraceFilter::TraceFilter(const TFE& e) : SharedHandle(new TFO(e)) {}
00189 
00190   TraceFilter::TraceFilter(PropagatorGroup g) : SharedHandle(new TFO(g)) {}
00191 
00192   TraceFilter::TraceFilter(BrancherGroup g) : SharedHandle(new TFO(g)) {}
00193 
00194   TraceFilter::TraceFilter(const TraceFilter& tf) : SharedHandle(tf) {}
00195 
00196   TraceFilter&
00197   TraceFilter::operator =(const TraceFilter& tf) {
00198     return static_cast<TraceFilter&>(SharedHandle::operator =(tf));
00199   }
00200 
00201   TraceFilter TraceFilter::all;
00202 
00203 }
00204 
00205 // STATISTICS: kernel-trace