FLOPC++
MP_domain.cpp
Go to the documentation of this file.
1 // ******************** FlopCpp **********************************************
2 // File: MP_domain.cpp
3 // $Id$
4 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
5 // Copyright (C) 2003 Tim Helge Hultberg
6 // All Rights Reserved.
7 //****************************************************************************
8 
9 #include "MP_domain.hpp"
10 #include "MP_set.hpp"
11 #include "MP_boolean.hpp"
12 #include "MP_model.hpp"
13 
14 namespace flopc {
16  : S(s), I(i) {}
19  return MP_domain(const_cast<MP_domain_set*>(this));
20  }
21 
22  class Functor_conditional : public Functor {
23  public:
24  Functor_conditional(const Functor* f, const std::vector<MP_boolean> & condition)
25  : F(f), Condition(condition) {}
26  virtual ~Functor_conditional() {}
27  void operator()() const {
28  bool goOn = true;
29  for (size_t i = 0; i<Condition.size(); i++) {
30  if (Condition[i]->evaluate()==false) {
31  goOn = false;
32  break;
33  }
34  }
35  if (goOn == true) {
36  F->operator()();
37  }
38  }
39  const Functor* F;
40  std::vector<MP_boolean> Condition;
41  };
42 }
43 
44 using namespace flopc;
45 
46 const MP_domain* MP_domain::Empty = NULL;
47 
49  if(Empty==NULL) {
51  }
52  return *Empty;
53 }
54 
55 
56 MP_domain_base::MP_domain_base() : count(0), donext(0) {}
58 
60  return NULL;
61 }
62 
63 size_t MP_domain_base::size() const {
64  return count;
65 }
66 
67 
69  std::stringstream ss;
70  ss<<"domain_base::display() size="<<size()<<std::ends;
71  MP_model::getCurrentModel()->getMessenger()->logMessage(5,ss.str().c_str());
72 }
73 
77 
79  if (b.operator ->() != 0) {
80  condition.push_back(b);
81  }
82  return *this;
83 }
84 
85 void MP_domain::Forall(const Functor* op) const {
86  if (condition.size()>0) {
88  } else {
89  last->donext = op;
90  }
91  operator->()->operator()();
92 }
93 
95  return S;
96 }
97 
98 size_t MP_domain::size() const {
99  return operator->()->getSet()->size();
100 }
101 
103  return I->evaluate();
104 }
105 
107  if (I->isInstantiated() == true) {
108  (*donext)();
109  } else {
110  I->instantiate();
111  for (int k=0; k<S->size(); k++) {
112  I->assign(k);
113  (*donext)();
114  }
115  I->assign(0);
116  I->unInstantiate();
117  }
118 }
119 
121  return I;
122 }
123 
124 
126  if (a.operator->() == MP_domain::getEmpty().operator->()) {
127  return b;
128  } else if (b.operator->() == MP_domain::getEmpty().operator->()) {
129  return a;
130  } else {
131  MP_domain retval = a;
132  retval.last->donext = b.operator->();
133  const_cast<MP_domain&>(b).increment();
134  const_cast<MP_domain&>(a).increment();
135  retval.last = b.last;
136  retval.condition.insert(retval.condition.end(),b.condition.begin(),
137  b.condition.end());
138  return retval;
139  }
140 
141 }
static MP_set & getEmpty()
gets the distinct &#39;empty&#39; MP_set.
Definition: MP_set.cpp:17
MP_domain such_that(const MP_boolean &b)
Special conditional creation of a subset.
Definition: MP_domain.cpp:78
virtual Functor * makeInsertFunctor() const
Definition: MP_domain.cpp:59
const MP_set * S
Definition: MP_domain.hpp:130
Handle< MP_domain_base * > last
Definition: MP_domain.hpp:96
std::vector< MP_boolean > Condition
Definition: MP_domain.cpp:40
static const MP_domain * Empty
Definition: MP_domain.hpp:97
Functor_conditional(const Functor *f, const std::vector< MP_boolean > &condition)
Definition: MP_domain.cpp:24
static MP_model * getCurrentModel()
Definition: MP_model.cpp:26
MP_domain_set(const MP_set *s, MP_index *i)
Constructor taking a set pointer and an index pointer.
Definition: MP_domain.cpp:15
Messenger * getMessenger()
Definition: MP_model.hpp:235
void Forall(const Functor *op) const
Special conditional operation on the domain.
Definition: MP_domain.cpp:85
Representation of an index.This is one of the main public interface classes. It is used to iterate th...
Definition: MP_index.hpp:53
int evaluate() const
Definition: MP_index.hpp:57
const Functor * donext
Definition: MP_domain.hpp:45
Utility for doing reference counted pointers.
virtual size_t size() const
Definition: MP_domain.cpp:63
void operator()() const
Definition: MP_domain.cpp:27
virtual int size() const
getter for the cardinality of this MP_set.
Definition: MP_set.hpp:106
Range over which some other constuct is defined.This is one of the main public interface classes...
Definition: MP_domain.hpp:110
Reference to a set of index values.
Definition: MP_domain.hpp:30
MP_domain getDomain(MP_set *s) const
Definition: MP_domain.cpp:18
void operator()() const
Definition: MP_domain.cpp:106
std::vector< MP_boolean > condition
Definition: MP_domain.hpp:95
bool isInstantiated() const
Definition: MP_index.hpp:63
size_t size() const
returns number of elements in the domain.
Definition: MP_domain.cpp:98
const T & operator->() const
const MP_set_base * getSet() const
Getter for the set used in construction.
Definition: MP_domain.cpp:94
Reference counted class for all &quot;boolean&quot; types of data.This contains counters to ConstantBase pointe...
Definition: MP_boolean.hpp:45
Range over which some other constuct is defined.This is one of the main public interface classes...
Definition: MP_domain.hpp:61
friend class MP_domain
Definition: MP_domain.hpp:31
static const MP_domain & getEmpty()
returns a reference to the &quot;empty&quot; set.
Definition: MP_domain.cpp:48
void unInstantiate()
Definition: MP_index.hpp:76
Function object. Often used.
Representation of a set for indexing into some other construct.This is one of the main public interfa...
Definition: MP_set.hpp:78
int evaluate() const
Definition: MP_domain.cpp:102
Constant operator*(const Constant &a, const Constant &b)
Returns the product of two constants.This is used in the formation of an expression.
Internal representation of a &quot;set&quot;.
Definition: MP_set.hpp:28
MP_index * getIndex() const
Getter for the index used in construction.
Definition: MP_domain.cpp:120
virtual void logMessage(int level, const char *const msg)
Definition: MP_model.hpp:37
void display() const
Definition: MP_domain.cpp:68
void instantiate()
Definition: MP_index.hpp:82
virtual ~MP_domain_base()
Definition: MP_domain.cpp:57
void assign(int i)
Definition: MP_index.hpp:70