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

not-first-not-last.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, 2009
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 <algorithm>
00035 
00036 namespace Gecode { namespace Int { namespace Unary {
00037 
00038   template<class ManTaskView>
00039   forceinline ExecStatus
00040   notlast(Space& home, TaskViewArray<ManTaskView>& t) {
00041     sort<ManTaskView,STO_LCT,true>(t);
00042 
00043     Region r;
00044 
00045     OmegaTree<ManTaskView> o(r,t);
00046     TaskViewIter<ManTaskView,STO_LST,true> q(r,t);
00047     int* lct = r.alloc<int>(t.size());
00048 
00049     for (int i=0; i<t.size(); i++)
00050       lct[i] = t[i].lct();
00051 
00052     for (int i=0; i<t.size(); i++) {
00053       int j = -1;
00054       while (q() && (t[i].lct() > t[q.task()].lst())) {
00055         if ((j >= 0) && (o.ect() > t[q.task()].lst()))
00056           lct[q.task()] = std::min(lct[q.task()],t[j].lst());
00057         j = q.task();
00058         o.insert(j); ++q;
00059       }
00060       if ((j >= 0) && (o.ect(i) > t[i].lst()))
00061         lct[i] = std::min(lct[i],t[j].lst());
00062     }
00063 
00064     for (int i=0; i<t.size(); i++)
00065       GECODE_ME_CHECK(t[i].lct(home,lct[i]));
00066 
00067     return ES_OK;
00068   }
00069 
00070   template<class ManTask>
00071   ExecStatus
00072   notfirstnotlast(Space& home, TaskArray<ManTask>& t) {
00073     TaskViewArray<typename TaskTraits<ManTask>::TaskViewFwd> f(t);
00074     GECODE_ES_CHECK(notlast(home,f));
00075     TaskViewArray<typename TaskTraits<ManTask>::TaskViewBwd> b(t);
00076     return notlast(home,b);
00077   }
00078 
00079   template<class OptTaskView, class PL>
00080   forceinline ExecStatus
00081   notlast(Space& home, Propagator& p, TaskViewArray<OptTaskView>& t) {
00082     sort<OptTaskView,STO_LCT,true>(t);
00083 
00084     Region r;
00085 
00086     OmegaTree<OptTaskView> o(r,t);
00087     ManTaskViewIter<OptTaskView,STO_LST,true> q(r,t);
00088     int* lct = r.alloc<int>(t.size());
00089 
00090     for (int i=0; i<t.size(); i++)
00091       lct[i] = t[i].lct();
00092 
00093     for (int i=0; i<t.size(); i++) {
00094       int j = -1;
00095       while (q() && (t[i].lct() > t[q.task()].lst())) {
00096         if ((j >= 0) && (o.ect() > t[q.task()].lst()))
00097           lct[q.task()] = std::min(lct[q.task()],t[j].lst());
00098         j = q.task();
00099         o.insert(j); ++q;
00100       }
00101       if ((j >= 0) && (o.ect(i) > t[i].lst()))
00102         lct[i] = std::min(lct[i],t[j].lst());
00103     }
00104 
00105     {
00106       int n = t.size();
00107       for (int i=n; i--; )
00108         if (t[i].mandatory()) {
00109           GECODE_ME_CHECK(t[i].lct(home,lct[i]));
00110         } else if (lct[i] < t[i].ect()) {
00111           GECODE_ME_CHECK(t[i].excluded(home));
00112           t[i].cancel(home,p,PL::pc); t[i]=t[--n];
00113         }
00114       t.size(n);
00115     }
00116 
00117     return (t.size() < 2) ? home.ES_SUBSUMED(p) : ES_OK;
00118   }
00119 
00120   template<class OptTask, class PL>
00121   ExecStatus
00122   notfirstnotlast(Space& home, Propagator& p, TaskArray<OptTask>& t) {
00123     TaskViewArray<typename TaskTraits<OptTask>::TaskViewFwd> f(t);
00124     GECODE_ES_CHECK((notlast<typename TaskTraits<OptTask>::TaskViewFwd,PL>
00125                      (home,p,f)));
00126     TaskViewArray<typename TaskTraits<OptTask>::TaskViewBwd> b(t);
00127     return notlast<typename TaskTraits<OptTask>::TaskViewBwd,PL>(home,p,b);
00128   }
00129 
00130 }}}
00131 
00132 // STATISTICS: int-prop