OKlibrary  0.2.1.6
AssignmentsWithBuffer.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 25.12.2009 (Swansea)
00002 /* Copyright 2009, 2011 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00014 #ifndef ASSIGNMENTSWITHBUFFER_nBaLk810
00015 #define ASSIGNMENTSWITHBUFFER_nBaLk810
00016 
00017 #include <vector>
00018 #include <stack>
00019 #include <cassert>
00020 
00021 #include <OKlib/Satisfiability/ProblemInstances/Variables/traits/index_type.hpp>
00022 #include <OKlib/Satisfiability/ProblemInstances/Literals/traits/var_type.hpp>
00023 #include <OKlib/Satisfiability/ProblemInstances/Literals/traits/cond_type.hpp>
00024 #include <OKlib/Satisfiability/ProblemInstances/Literals/var.hpp>
00025 #include <OKlib/Satisfiability/ProblemInstances/Literals/cond.hpp>
00026 #include <OKlib/Satisfiability/Values/StatusTypes.hpp>
00027 
00028 namespace OKlib {
00029  namespace Satisfiability {
00030   namespace Assignments {
00031    namespace TotalAssignments {
00032 
00049      template <typename Lit>
00050      class BAssignmentWithQueue {
00051      public :
00052 
00053        typedef Lit literal_type;
00054        typedef typename OKlib::Literals::traits::var_type<literal_type>::type variable_type;
00055        typedef typename OKlib::Variables::traits::index_type<variable_type>::type index_type;
00056 
00057        typedef OKlib::Satisfiability::Values::Assignment_status value_type;
00058 
00060        BAssignmentWithQueue() : n(0), next_lit(phi.begin()) {}
00061        BAssignmentWithQueue(const index_type n_) : n(n_), V(n+1,OKlib::Satisfiability::Values::unassigned) {
00062          assert(n >= 0);
00063          phi.reserve(n);
00064          next_lit = phi.begin();
00065        }
00066        BAssignmentWithQueue(const BAssignmentWithQueue& other) :
00067          n(other.n), V(other.V), phi(other.phi) {
00068          phi.reserve(n);
00069          next_lit = phi.end() - other.size();
00070          assert(size() == other.size());
00071        }
00072        BAssignmentWithQueue& operator =(const BAssignmentWithQueue& rhs) {
00073          n = rhs.n;
00074          V = rhs.V;
00075          phi = rhs.phi;
00076          phi.reserve(n);
00077          next_lit = phi.end() - rhs.size();
00078          assert(size() == rhs.size());
00079          return *this;
00080        }
00081 
00083        void resize(const index_type n_) {
00084          assert(n_ >= 0);
00085          const index_type old_size = size();
00086          n = n_;
00087          V.resize(n+1,OKlib::Satisfiability::Values::unassigned);
00088          phi.reserve(n);
00089          next_lit = phi.end() - old_size;
00090        }
00091 
00093        value_type operator[] (const variable_type v) const {
00094          assert(index_type(v) <= n);
00095          return V[index_type(v)];
00096        }
00098        value_type operator() (const literal_type x) const {
00099          assert(index_type(OKlib::Literals::var(x)) <= n);
00100          if (OKlib::Literals::cond(x))
00101            return V[index_type(OKlib::Literals::var(x))];
00102          else
00103            return -V[index_type(OKlib::Literals::var(x))];
00104        }
00105 
00112        bool push(const literal_type x) {
00113          assert(index_type(OKlib::Literals::var(x)) <= n);
00114          switch (operator()(x)) {
00115          case OKlib::Satisfiability::Values::val0 :
00116            return false;
00117          case OKlib::Satisfiability::Values::val1 :
00118            return true;
00119          default :
00120            assert(phi.size() < phi.capacity());
00121            phi.push_back(x);
00122            V[index_type(OKlib::Literals::var(x))] = value_type(OKlib::Literals::cond(x));
00123            return true;
00124          }
00125        }
00126 
00128        literal_type top() const {
00129          assert(not empty());
00130          return *next_lit;
00131        }
00133        void pop() {
00134         assert(next_lit != phi.end());
00135          ++next_lit;
00136        }
00138        index_type size() const { return phi.end() - next_lit; }
00140        bool empty() const { return next_lit == phi.end(); }
00141 
00142      private :
00143 
00144        index_type n;
00145        typedef std::vector<value_type> vector_t;
00146        vector_t V;
00147        typedef std::vector<literal_type> pass_t;
00148        pass_t phi;
00149        typedef typename pass_t::const_iterator iterator_t;
00150        iterator_t next_lit; // iterator into phi
00151      };
00152 
00153 
00161      template <typename Lit>
00162      class BAssignmentWithRQueue : public BAssignmentWithQueue<Lit> {
00163        typedef BAssignmentWithQueue<Lit> base_class;
00164      public :
00165 
00166        BAssignmentWithRQueue(const typename base_class::index_type n) : base_class(n) {}
00167 
00168        bool push_forced(const typename base_class::literal_type x) {
00169          return this -> push(x);
00170        }
00171        bool push_free(const typename base_class::literal_type x) {
00172          if (not this -> push_forced(x)) return false;
00173          S.push(-- (this -> phi.end()));
00174          return true;
00175        }
00176 
00177        void reset() {
00178          // XXX
00179        }
00180 
00181      private :
00182 
00183        using typename base_class::iterator_t;
00184        typedef std::stack<typename base_class::iterator_t> stack_t;
00185        stack_t S;
00186      };
00187 
00188     
00189 
00190    } 
00191   }
00192  }
00193 }
00194 
00195 #endif