MP_domain.cpp

Go to the documentation of this file.
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 = NULL;
00047 
00048 const MP_domain& MP_domain::getEmpty() {
00049     if(Empty==NULL) {
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 NULL;
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     if (condition.size()>0) {
00087         last->donext = new Functor_conditional(op,condition);
00088     } else {
00089         last->donext = op;
00090     }
00091     operator->()->operator()();
00092 }
00093 
00094 const MP_set_base* MP_domain_set::getSet() const {
00095     return S;
00096 }
00097 
00098 size_t MP_domain::size() const {
00099     return operator->()->getSet()->size();
00100 }
00101 
00102 int MP_domain_set::evaluate() const {
00103     return I->evaluate();
00104 }
00105 
00106 void MP_domain_set::operator()() const {
00107     if (I->isInstantiated() == true) {
00108         (*donext)(); 
00109     } else {
00110         I->instantiate();
00111         for (int k=0; k<S->size(); k++) {
00112             I->assign(k);
00113             (*donext)();
00114         }
00115         I->assign(0);
00116         I->unInstantiate();
00117     }
00118 }
00119 
00120 MP_index* MP_domain_set::getIndex() const {
00121     return I;
00122 }
00123 
00124 
00125 flopc::MP_domain flopc::operator*(const flopc::MP_domain& a, const flopc::MP_domain& b) {
00126     if (a.operator->() == MP_domain::getEmpty().operator->()) {
00127         return b;
00128     } else if (b.operator->() == MP_domain::getEmpty().operator->()) {
00129         return a;
00130     } else {
00131         MP_domain retval = a;
00132         retval.last->donext = b.operator->();
00133         const_cast<MP_domain&>(b).increment();
00134         const_cast<MP_domain&>(a).increment();
00135         retval.last = b.last;
00136         retval.condition.insert(retval.condition.end(),b.condition.begin(),
00137                                 b.condition.end());
00138         return retval;
00139     }
00140 
00141 }

Generated on Fri Aug 26 03:02:58 2011 for FLOPC++ by  doxygen 1.4.7