FLOPC++
|
00001 // ******************** FlopCpp ********************************************** 00002 // File: MP_domain.cpp 00003 // $Id$ 00004 // Author: Tim Helge Hultberg (thh@mat.ua.pt) 00005 // Copyright (C) 2003 Tim Helge Hultberg 00006 // All Rights Reserved. 00007 //**************************************************************************** 00008 00009 #include "MP_domain.hpp" 00010 #include "MP_set.hpp" 00011 #include "MP_boolean.hpp" 00012 #include "MP_model.hpp" 00013 00014 namespace flopc { 00015 MP_domain_set::MP_domain_set(const MP_set* s, MP_index* i) 00016 : S(s), I(i) {} 00017 MP_domain_set::~MP_domain_set() {} 00018 MP_domain MP_domain_set::getDomain(MP_set* s) const { 00019 return MP_domain(const_cast<MP_domain_set*>(this)); 00020 } 00021 00022 class Functor_conditional : public Functor { 00023 public: 00024 Functor_conditional(const Functor* f, const std::vector<MP_boolean> & condition) 00025 : F(f), Condition(condition) {} 00026 virtual ~Functor_conditional() {} 00027 void operator()() const { 00028 bool goOn = true; 00029 for (size_t i = 0; i<Condition.size(); i++) { 00030 if (Condition[i]->evaluate()==false) { 00031 goOn = false; 00032 break; 00033 } 00034 } 00035 if (goOn == true) { 00036 F->operator()(); 00037 } 00038 } 00039 const Functor* F; 00040 std::vector<MP_boolean> Condition; 00041 }; 00042 } 00043 00044 using namespace flopc; 00045 00046 const MP_domain* MP_domain::Empty = 0; 00047 00048 const MP_domain& MP_domain::getEmpty() { 00049 if(Empty == 0) { 00050 Empty= new MP_domain(new MP_domain_set(&MP_set::getEmpty(),&MP_set::getEmpty())); 00051 } 00052 return *Empty; 00053 } 00054 00055 00056 MP_domain_base::MP_domain_base() : count(0), donext(0) {} 00057 MP_domain_base::~MP_domain_base() {} 00058 00059 Functor* MP_domain_base::makeInsertFunctor() const { 00060 return 0; 00061 } 00062 00063 size_t MP_domain_base::size() const { 00064 return count; 00065 } 00066 00067 00068 void MP_domain_base::display()const { 00069 std::stringstream ss; 00070 ss<<"domain_base::display() size="<<size()<<std::ends; 00071 MP_model::getCurrentModel()->getMessenger()->logMessage(5,ss.str().c_str()); 00072 } 00073 00074 MP_domain::MP_domain() : Handle<MP_domain_base*>(0), last(0) {} 00075 MP_domain::MP_domain(MP_domain_base* r) : Handle<MP_domain_base*>(r), last(r) {} 00076 MP_domain::~MP_domain() {} 00077 00078 MP_domain MP_domain::such_that(const MP_boolean& b) { 00079 if (b.operator ->() != 0) { 00080 condition.push_back(b); 00081 } 00082 return *this; 00083 } 00084 00085 void MP_domain::forall(const Functor& op) const { 00086 forall(&op); 00087 } 00088 void MP_domain::forall(const Functor* op) const { 00089 if (condition.size()>0) { 00090 last->donext = new Functor_conditional(op,condition); 00091 } else { 00092 last->donext = op; 00093 } 00094 operator->()->operator()(); 00095 } 00096 00097 const MP_set_base* MP_domain_set::getSet() const { 00098 return S; 00099 } 00100 00101 size_t MP_domain::size() const { 00102 return operator->()->getSet()->size(); 00103 } 00104 00105 int MP_domain_set::evaluate() const { 00106 return I->evaluate(); 00107 } 00108 00109 void MP_domain_set::operator()() const { 00110 if (I->isInstantiated() == true) { 00111 (*donext)(); 00112 } else { 00113 I->instantiate(); 00114 for (int k=0; k<S->size(); k++) { 00115 I->assign(k); 00116 (*donext)(); 00117 } 00118 I->assign(0); 00119 I->unInstantiate(); 00120 } 00121 } 00122 00123 MP_index* MP_domain_set::getIndex() const { 00124 return I; 00125 } 00126 00127 00128 flopc::MP_domain flopc::operator*(const flopc::MP_domain& a, const flopc::MP_domain& b) { 00129 if (a.operator->() == MP_domain::getEmpty().operator->()) { 00130 return b; 00131 } else if (b.operator->() == MP_domain::getEmpty().operator->()) { 00132 return a; 00133 } else { 00134 MP_domain retval = a; 00135 retval.last->donext = b.operator->(); 00136 const_cast<MP_domain&>(b).increment(); 00137 const_cast<MP_domain&>(a).increment(); 00138 retval.last = b.last; 00139 retval.condition.insert(retval.condition.end(),b.condition.begin(), 00140 b.condition.end()); 00141 return retval; 00142 } 00143 00144 }