FlopCpp trunk
MP_set.hpp
Go to the documentation of this file.
1// ******************** FlopCpp **********************************************
2// File: MP_set.hpp
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#ifndef _MP_set_hpp_
10#define _MP_set_hpp_
11
12#include <iostream>
13#include <sstream>
14#include <string>
15#include <vector>
16#include <map>
17
18#include "MP_domain.hpp"
19#include "MP_index.hpp"
20#include "MP_utilities.hpp"
21
22namespace flopc {
23
28class MP_set_base : public MP_index , public Named {
29public:
30 MP_set_base() : Cyclic(false) {}
31
32 virtual int size() const = 0;
33 virtual operator MP_domain() const = 0;
34 virtual MP_domain operator()(const MP_index_exp& i) const = 0;
35 void display()const;
36
37 int check(int i) const {
38 if ((i>=0) && (i<size())) {
39 return i;
40 } else {
41 if (Cyclic == true) {
42 return mod(i,size());
43 } else {
44 return outOfBound;
45 }
46 }
47 }
48 int checkStage(int i) const {
49 if ((i>=0) && (i<size())) {
50 return i*isStage();
51 } else {
52 if (Cyclic == true) {
53 return mod(i,size())*isStage();
54 } else {
55 return outOfBound;
56 }
57 }
58 }
60 virtual int isStage() const {
61 return 0;
62 }
63
64 bool Cyclic;
65};
66
67
78class MP_set : public MP_set_base {
79public:
81 MP_set(int i = 0): cardinality(i) {}
87 return i->getDomain(const_cast<MP_set*>(this));
88 }
90 operator MP_domain() const {
91 return new MP_domain_set(this,const_cast<MP_set*>(this));
92 }
97 return (MP_domain(new MP_domain_set(this,this))).such_that(b);
98 }
102 void cyclic() {
103 Cyclic = true;
104 }
106 virtual int size() const {
107 return cardinality;
108 }
109 int last() {
110 return cardinality-1;
111 }
113 static MP_set &getEmpty();
114private:
115 static MP_set Empty;
116 int cardinality;
117};
118
119class MP_stage : public MP_set {
120public:
121 MP_stage(int i = 0): MP_set(i) {}
122 virtual int isStage() const {
123 return 1;
124 }
125};
126
127template <int nbr> class MP_subset;
128
134template<int nbr> class InsertFunctor : public Functor {
135public:
136 InsertFunctor( MP_subset<nbr>* s, std::vector<MP_index_exp> i)
137 : S(s), I(i) {}
138 void operator()() const {
139 std::vector<int> elm(nbr);
140 for (int i=0; i<nbr; i++) {
141 elm[i] = I[i]->evaluate();
142 }
143 S->insert(elm);
144 }
145private:
147 std::vector<MP_index_exp> I;
148};
149
150template <int nbr> class SubsetRef;
151
160template <int nbr>
161class MP_subset : public MP_set {
162 friend class MP_domain_subset<nbr>;
163 friend class SubsetRef<nbr>;
164public:
165 MP_subset(const MP_set& s1,
166 const MP_set& s2=MP_set::getEmpty(),
167 const MP_set& s3=MP_set::getEmpty(),
168 const MP_set& s4=MP_set::getEmpty(),
169 const MP_set& s5=MP_set::getEmpty()) {
170 S = makeVector<nbr,const MP_set*>(&s1,&s2,&s3,&s4,&s5);
171 }
172 void display(const std::string& s = "") const
173 {
174// Messenger &msgr = *MP_model::getCurrentModel()->getMessenger();
175// msgr.logMessage(5,s.c_str());
176// std::map<std::vector<int>, int>::const_iterator i;
177// for (i = elements.begin(); i != elements.end(); i++)
178// {
179// std::stringstream ss;
180// for (int j=0; j<nbr; j++)
181// {
182// ss<<(*i).first[j]<<" ";
183// }
184// ss<<(*i).second<<std::ends;
185// msgr.logMessage(5,ss.str().c_str());
186// }
187 }
188
189 MP_subset(std::vector<const MP_set*> s) : S(s) {}
190
192
193 int operator()(int i1, int i2=0, int i3=0, int i4=0, int i5=0) {
194 std::map<std::vector<int>, int>::const_iterator pos;
195 pos = elements.find(makeVector<nbr>(i1, i2, i3, i4, i5));
196 if (pos==elements.end()) {
197 return outOfBound;
198 } else {
199 return pos->second;
200 }
201 }
202
204 const MP_index_exp& i2=MP_index::getEmpty(),
207 const MP_index_exp& i5=MP_index::getEmpty()) {
208 return *new SubsetRef<nbr>(this,i1,i2,i3,i4,i5);
209 }
210
211 const MP_domain& operator()(const SUBSETREF& s) {
212 return MP_domain(s);
213 }
214
215 int evaluate(const std::vector<MP_index*>& I) const {
216 std::vector<int> vi;
217 for (int k=0; k<nbr; k++) {
218 int temp = I[k]->evaluate();
219 vi.push_back(temp);
220 }
221 std::map<std::vector<int>, int>::const_iterator pos;
222 pos = elements.find(vi);
223 if (pos==elements.end()) {
224 return outOfBound;
225 } else {
226 return pos->second;
227 }
228 }
229
230 void insert(const std::vector<int> &args) {
231 bool isOk = true;
232 for (int i=0; i<nbr; i++) {
233 if ( S[i]->check(args[i]) == outOfBound ) {
234 isOk = false;
235 }
236 }
237 if (isOk == true) {
238 std::map<std::vector<int>, int>::const_iterator pos;
239 pos = elements.find(args);
240 if (pos==elements.end()) { // insert if not existent
241 const int v = static_cast<int>(elements.size());
242 elements[args] = v;
243 }
244 }
245 }
246 void insert(int i1, int i2=0, int i3=0, int i4=0, int i5=0) {
247 insert(makeVector<nbr>(i1, i2, i3, i4, i5));
248 }
254 return *new InsertFunctor<nbr>(this,makeVector<nbr>(i1, i2, i3, i4, i5));
255 }
256 virtual int size() const {
257 return static_cast<int>(elements.size());
258 }
259
260private:
261 std::vector<const MP_set*> S;
262 std::map<std::vector<int>, int> elements;
263};
264
273 class SUBSETREF : public MP_index_base {
274 public:
275 virtual MP_index* getIndex() const {
276 return 0;
277 }
278 virtual MP_domain getDomain(MP_set* s) const {
279 return MP_domain::getEmpty();
280 }
281 int evaluate() const {
282 return 0;
283 }
284 };
285
293 template <int nbr>
294 class SubsetRef : public SUBSETREF {
295 public:
297 const MP_index_exp& i1,
298 const MP_index_exp& i2,
299 const MP_index_exp& i3,
300 const MP_index_exp& i4,
301 const MP_index_exp& i5) :
302 S(s),I1(i1),I2(i2),I3(i3),I4(i4),I5(i5) {}
303 void display()const
304 {
305// Messenger &msgr=*MP_model::getCurrentModel()->getMessenger();
306// msgr.logMessage(5,toString().c_str());
307 }
308
309 operator MP_domain() const {
310// MP_domain_base* base;
311// base = new MP_domain_subset<nbr>(S,
312// makeVector<nbr>(I1->getIndex(), I2->getIndex(),
313// I3->getIndex(), I4->getIndex(),
314// I5->getIndex()) );
315// base->such_that(B);
316// return base;
317 return new MP_domain_subset<nbr>(S,
318 makeVector<nbr>(I1->getIndex(), I2->getIndex(),
319 I3->getIndex(), I4->getIndex(),
320 I5->getIndex()) );
321 }
322
323 virtual MP_domain getDomain(MP_set* s) const {
324 return new MP_domain_subset<nbr>(S,
325 makeVector<nbr>(I1->getIndex(), I2->getIndex(),
326 I3->getIndex(), I4->getIndex(),
327 I5->getIndex()) );
328 }
329
331 B = b;
332 return *this;
333 }
334
335 int evaluate() const {
336 std::vector<MP_index_exp> I = makeVector<nbr>(I1,I2,I3,I4,I5);
337 std::vector<int> vi;
338 for (int k=0; k<nbr; k++) {
339 int temp = I[k]->evaluate();
340 vi.push_back(temp);
341 }
342 std::map<std::vector<int>, int>::const_iterator pos;
343 pos = S->elements.find(vi);
344 if (pos==S->elements.end()) {
345 return outOfBound;
346 } else {
347 return pos->second;
348 }
349 }
351 return S;
352 }
356 };
357
358} // End of namespace flopc
359#endif
Function object.
Internal representation of a "set".
Definition: MP_set.hpp:134
void operator()() const
Definition: MP_set.hpp:138
InsertFunctor(MP_subset< nbr > *s, std::vector< MP_index_exp > i)
Definition: MP_set.hpp:136
Reference counted class for all "boolean" types of data.
Definition: MP_boolean.hpp:45
Range over which some other constuct is defined.
Definition: MP_domain.hpp:110
Range over which some other constuct is defined.
Definition: MP_domain.hpp:166
Range over which some other constuct is defined.
Definition: MP_domain.hpp:61
MP_domain such_that(const MP_boolean &b)
Special conditional creation of a subset.
static const MP_domain & getEmpty()
returns a reference to the "empty" set.
Internal representation of a index.
Definition: MP_index.hpp:26
Representation of an expression involving an index.
Definition: MP_index.hpp:145
static const MP_index_exp & getEmpty()
Return the unique empty expression.
Representation of an index.
Definition: MP_index.hpp:53
static MP_index & getEmpty()
returns a reference to the distinct "empty" index.
Internal representation of a "set".
Definition: MP_set.hpp:28
virtual int isStage() const
Definition: MP_set.hpp:60
int checkStage(int i) const
Definition: MP_set.hpp:48
virtual int size() const =0
void display() const
virtual MP_domain operator()(const MP_index_exp &i) const =0
int check(int i) const
Definition: MP_set.hpp:37
Representation of a set for indexing into some other construct.
Definition: MP_set.hpp:78
int last()
Definition: MP_set.hpp:109
virtual int size() const
getter for the cardinality of this MP_set.
Definition: MP_set.hpp:106
MP_domain such_that(const MP_boolean &b)
constructs a domain by subsetting this MP_set where the MP_boolean evaluates to 'true'
Definition: MP_set.hpp:96
MP_domain operator()(const MP_index_exp &i) const
Constructs an MP_domain on the stack given an index expression into the set.
Definition: MP_set.hpp:86
void cyclic()
setter for 'cyclic' property
Definition: MP_set.hpp:102
static MP_set & getEmpty()
gets the distinct 'empty' MP_set.
MP_set(int i=0)
constructs a set with specific cardinality.
Definition: MP_set.hpp:81
virtual int isStage() const
Definition: MP_set.hpp:122
MP_stage(int i=0)
Definition: MP_set.hpp:121
Internal representation of a "set".
Definition: MP_set.hpp:161
void insert(int i1, int i2=0, int i3=0, int i4=0, int i5=0)
Definition: MP_set.hpp:246
friend class SubsetRef< nbr >
Definition: MP_set.hpp:163
const MP_domain & operator()(const SUBSETREF &s)
Definition: MP_set.hpp:211
MP_subset(const MP_set &s1, const MP_set &s2=MP_set::getEmpty(), const MP_set &s3=MP_set::getEmpty(), const MP_set &s4=MP_set::getEmpty(), const MP_set &s5=MP_set::getEmpty())
Definition: MP_set.hpp:165
int evaluate(const std::vector< MP_index * > &I) const
Definition: MP_set.hpp:215
const InsertFunctor< nbr > & insert(MP_index_exp i1, MP_index_exp i2=MP_index_exp::getEmpty(), MP_index_exp i3=MP_index_exp::getEmpty(), MP_index_exp i4=MP_index_exp::getEmpty(), MP_index_exp i5=MP_index_exp::getEmpty())
Definition: MP_set.hpp:249
int operator()(int i1, int i2=0, int i3=0, int i4=0, int i5=0)
Definition: MP_set.hpp:193
void display(const std::string &s="") const
Definition: MP_set.hpp:172
virtual int size() const
getter for the cardinality of this MP_set.
Definition: MP_set.hpp:256
SubsetRef< nbr > & operator()(const MP_index_exp &i1, const MP_index_exp &i2=MP_index::getEmpty(), const MP_index_exp &i3=MP_index::getEmpty(), const MP_index_exp &i4=MP_index::getEmpty(), const MP_index_exp &i5=MP_index::getEmpty())
Definition: MP_set.hpp:203
void insert(const std::vector< int > &args)
Definition: MP_set.hpp:230
MP_subset(std::vector< const MP_set * > s)
Definition: MP_set.hpp:189
Utility interface class for adding a string name onto a structure.
Internal representation of a "set".
Definition: MP_set.hpp:273
virtual MP_domain getDomain(MP_set *s) const
Definition: MP_set.hpp:278
int evaluate() const
Definition: MP_set.hpp:281
virtual MP_index * getIndex() const
Definition: MP_set.hpp:275
Internal representation of a "set".
Definition: MP_set.hpp:294
SubsetRef & such_that(const MP_boolean &b)
Definition: MP_set.hpp:330
MP_index_exp I3
Definition: MP_set.hpp:355
MP_index_exp I4
Definition: MP_set.hpp:355
MP_index_exp I1
Definition: MP_set.hpp:355
void display() const
Definition: MP_set.hpp:303
MP_subset< nbr > * S
Definition: MP_set.hpp:354
SubsetRef(MP_subset< nbr > *s, const MP_index_exp &i1, const MP_index_exp &i2, const MP_index_exp &i3, const MP_index_exp &i4, const MP_index_exp &i5)
Definition: MP_set.hpp:296
MP_index * getIndex() const
Definition: MP_set.hpp:350
virtual MP_domain getDomain(MP_set *s) const
Definition: MP_set.hpp:323
int evaluate() const
Definition: MP_set.hpp:335
MP_index_exp I2
Definition: MP_set.hpp:355
MP_index_exp I5
Definition: MP_set.hpp:355
MP_boolean B
Definition: MP_set.hpp:353
Constant pos(const Constant &c)
for returning non-negative value of the constant.
All flopc++ code is contained within the flopc namespace.
Definition: flopc.hpp:49
int mod(int a, int b)
return the strictly positive modulus of two integers
const int outOfBound
Distinct return value on conditions where an index goes out of bounds.