Actual source code: ALE_containers.hh

  1: #ifndef included_ALE_containers_hh
  2: #define included_ALE_containers_hh
  3: // This should be included indirectly -- only by including ALE.hh

  5: #include <boost/multi_index_container.hpp>
  6: #include <boost/multi_index/member.hpp>
  7: #include <boost/multi_index/ordered_index.hpp>
  8: #include <boost/multi_index/composite_key.hpp>

 10: #include <iostream>
 11: #include <map>
 12: #include <set>
 13: #include <vector>

 15: #ifndef  included_ALE_exception_hh
 16: #include <ALE_exception.hh>
 17: #endif
 18: #ifndef  included_ALE_mem_hh
 19: #include <ALE_mem.hh>
 20: #endif
 21: #ifndef  included_ALE_log_hh
 22: #include <ALE_log.hh>
 23: #endif

 25: namespace ALE {
 26:   class ParallelObject {
 27:   protected:
 28:     int         _debug;
 29:     MPI_Comm    _comm;
 30:     int         _commRank;
 31:     int         _commSize;
 32:     std::string _name;
 33:     PetscObject _petscObj;
 34:     void __init(MPI_Comm comm) {
 35:       static PetscCookie objType = -1;
 36:       //const char        *id_name = ALE::getClassName<T>();
 37:       const char        *id_name = "ParallelObject";
 38:       PetscErrorCode     ierr;

 40:       if (objType < 0) {
 41:         PetscLogClassRegister(&objType, id_name);CHKERROR(ierr, "Error in PetscLogClassRegister");
 42:       }
 43:       this->_comm = comm;
 44:       MPI_Comm_rank(this->_comm, &this->_commRank); CHKERROR(ierr, "Error in MPI_Comm_rank");
 45:       MPI_Comm_size(this->_comm, &this->_commSize); CHKERROR(ierr, "Error in MPI_Comm_size");
 46: #ifdef USE_PETSC_OBJ
 47:       PetscObjectCreateGeneric(this->_comm, objType, id_name, &this->_petscObj);CHKERROR(ierr, "Error in PetscObjectCreate");
 48: #endif
 49:       //ALE::restoreClassName<T>(id_name);
 50:     };
 51:   public:
 52:     ParallelObject(MPI_Comm comm = PETSC_COMM_SELF, const int debug = 0) : _debug(debug), _petscObj(NULL) {__init(comm);}
 53:     virtual ~ParallelObject() {
 54: #ifdef USE_PETSC_OBJ
 55:       if (this->_petscObj) {
 56:         PetscErrorCode PetscObjectDestroy(this->_petscObj); CHKERROR(ierr, "Failed in PetscObjectDestroy");
 57:         this->_petscObj = NULL;
 58:       }
 59: #endif
 60:     };
 61:   public:
 62:     int                debug()    const {return this->_debug;};
 63:     void               setDebug(const int debug) {this->_debug = debug;};
 64:     MPI_Comm           comm()     const {return this->_comm;};
 65:     int                commSize() const {return this->_commSize;};
 66:     int                commRank() const {return this->_commRank;}
 67: #ifdef USE_PETSC_OBJ
 68:     PetscObject        petscObj() const {return this->_petscObj;};
 69: #endif
 70:     const std::string& getName() const {return this->_name;};
 71:     void               setName(const std::string& name) {this->_name = name;};
 72:   };

 74:   // Use for ArrowSections
 75:   template<typename Source_, typename Target_>
 76:   struct MinimalArrow {
 77:     typedef Source_ source_type;
 78:     typedef Target_ target_type;
 79:     source_type source;
 80:     target_type target;
 81:     MinimalArrow() {};
 82:     MinimalArrow(const source_type& source, const target_type& target) : source(source), target(target) {};
 83:     MinimalArrow(const MinimalArrow& a) : source(a.source), target(a.target) {};
 84:     friend std::ostream& operator<<(std::ostream& os, const MinimalArrow& a) {
 85:       os << a.source << " ----> " << a.target;
 86:       return os;
 87:     }
 88:     // Comparisons
 89:     class less_than {
 90:     public:
 91:       bool operator()(const MinimalArrow& p, const MinimalArrow& q) const {
 92:         return((p.source < q.source) || ((p.source == q.source) && (p.target < q.target)));
 93:       };
 94:     };
 95:     typedef less_than Cmp;
 96:     bool operator==(const MinimalArrow& q) const {
 97:       return((this->source == q.source) && (this->target == q.target));
 98:     };
 99:     bool operator!=(const MinimalArrow& q) const {
100:       return((this->source != q.source) || (this->target != q.target));
101:     };
102:     bool operator<(const MinimalArrow& q) const {
103:       return((this->source < q.source) || ((this->source == q.source) && (this->target < q.target)));
104:     };
105:   };

107:   //
108:   // This is a set of classes and class templates describing an interface to point containers.
109:   //
110: 
111:   // Basic object
112:   class Point {
113:   public:
114:     typedef ALE_ALLOCATOR<Point> allocator;
115:     int32_t prefix, index;
116:     // Constructors
117:     Point() : prefix(0), index(0){};
118:     Point(int p) : prefix(p), index(0){};
119:     Point(int p, int i) : prefix(p), index(i){};
120:     Point(const Point& p) : prefix(p.prefix), index(p.index){};
121:     // Comparisons
122:     class less_than {
123:     public:
124:       bool operator()(const Point& p, const Point& q) const {
125:         return( (p.prefix < q.prefix) || ((p.prefix == q.prefix) && (p.index < q.index)));
126:       };
127:     };
128:     typedef less_than Cmp;
129: 
130:     bool operator==(const Point& q) const {
131:       return ( (this->prefix == q.prefix) && (this->index == q.index) );
132:     };
133:     bool operator!=(const Point& q) const {
134:       return ( (this->prefix != q.prefix) || (this->index != q.index) );
135:     };
136:     bool operator<(const Point& q) const {
137:       return( (this->prefix < q.prefix) || ((this->prefix == q.prefix) && (this->index < q.index)));
138:     };
139:     void operator+=(const Point& q) {
140:       this->prefix += q.prefix;
141:       this->index  += q.index;
142:     };
143:     // Printing
144:     friend std::ostream& operator<<(std::ostream& os, const Point& p) {
145:       os << "(" << p.prefix << ", "<< p.index << ")";
146:       return os;
147:     };
148:   };

150:   template <typename Element_>
151:   class array : public std::vector<Element_, ALE_ALLOCATOR<Element_> > {
152:   public:
153:     array()             : std::vector<Element_, ALE_ALLOCATOR<Element_> >(){};
154:     array(int32_t size) : std::vector<Element_, ALE_ALLOCATOR<Element_> >(size){};
155:     //
156:     template <typename ostream_type>
157:     void view(ostream_type& os, const char *name = NULL) {
158:       os << "Viewing array";
159:       if(name != NULL) {
160:         os << " " << name;
161:       }
162:       os << " of size " << (int) this->size() << std::endl;
163:       os << "[";
164:       for(unsigned int cntr = 0; cntr < this->size(); cntr++) {
165:         Element_ e = (*this)[cntr];
166:         os << e;
167:       }
168:       os << " ]" << std::endl;
169: 
170:     };
171:   };


174:   template <typename Element_>
175:   class set : public std::set<Element_, typename Element_::less_than,  ALE_ALLOCATOR<Element_> > {
176:   public:
177:     // Encapsulated types
178:     typedef std::set<Element_, typename Element_::less_than, ALE_ALLOCATOR<Element_> > base_type;
179:     typedef typename base_type::iterator                                               iterator;
180:     // Basic interface
181:     set()        : std::set<Element_, typename Element_::less_than, ALE_ALLOCATOR<Element_> >(){};
182:     set(Point p) : std::set<Element_, typename Element_::less_than, ALE_ALLOCATOR<Element_> >(){insert(p);};
183:     // Redirection:
184:     // FIX: it is a little weird that methods aren't inheritec,
185:     //      but perhaps can be fixed by calling insert<Element_> (i.e., insert<Point> etc)?

187:     std::pair<iterator, bool>
188:     insert(const Element_& e) { return base_type::insert(e); };

190:     iterator
191:     insert(iterator position, const Element_& e) {return base_type::insert(position,e);};

193:     template <class InputIterator>
194:     void
195:     insert(InputIterator b, InputIterator e) { return base_type::insert(b,e);};

197: 
198:     // Extensions to std::set interface
199:     bool contains(const Element_& e) {return (this->find(e) != this->end());};
200:     void join(Obj<set> s) {
201:       for(iterator s_itor = s->begin(); s_itor != s->end(); s_itor++) {
202:         this->insert(*s_itor);
203:       }
204:     };
205:     void meet(Obj<set> s) {// this should be called 'intersect' (the verb)
206:       set removal;
207:       for(iterator self_itor = this->begin(); self_itor != this->end(); self_itor++) {
208:         Element_ e = *self_itor;
209:         if(!s->contains(e)){
210:           removal.insert(e);
211:         }
212:       }
213:       for(iterator rem_itor = removal.begin(); rem_itor != removal.end(); rem_itor++) {
214:         Element_ ee = *rem_itor;
215:         this->erase(ee);
216:       }
217:     };
218:     void subtract(Obj<set> s) {
219:       set removal;
220:       for(iterator self_itor = this->begin(); self_itor != this->end(); self_itor++) {
221:         Element_ e = *self_itor;
222:         if(s->contains(e)){
223:           removal.insert(e);
224:         }
225:       }
226:       for(iterator rem_itor = removal.begin(); rem_itor != removal.end(); rem_itor++) {
227:         Element_ ee = *rem_itor;
228:         this->erase(ee);
229:       }
230:     };

232:     //
233:     template <typename ostream_type>
234:     void view(ostream_type& os, const char *name = NULL) {
235:       os << "Viewing set";
236:       if(name != NULL) {
237:         os << " " << name;
238:       }
239:       os << " of size " << (int) this->size() << std::endl;
240:       os << "[";
241:       for(iterator s_itor = this->begin(); s_itor != this->end(); s_itor++) {
242:         Element_ e = *s_itor;
243:         os << e;
244:       }
245:       os << " ]" << std::endl;
246:     };
247:   };

249:   template <typename X>
250:   struct singleton {
251:     X first;
252:     //
253:     singleton(const X& x)         : first(x) {};
254:     singleton(const singleton& s) : first(s.first) {};
255:   };

257:   template <typename X, typename Y>
258:   struct pair : public std::pair<X,Y> {
259:     pair() : std::pair<X,Y>(){};
260:     pair(const pair& p) : std::pair<X,Y>(p.first, p.second) {};
261:     pair(const X& x, const Y& y) : std::pair<X,Y>(x,y) {};
262:     ~pair(){};
263:     friend std::ostream& operator<<(std::ostream& os, const pair& p) {
264:       os << "<" << p.first << ", "<< p.second << ">";
265:       return os;
266:     };
267:   };// struct pair

269:   //
270:   // Arrow definitions
271:   //
272:   template<typename Source_, typename Target_, typename Color_>
273:   struct  Arrow { //: public ALE::def::Arrow<Source_, Target_, Color_> {
274:     typedef Arrow   arrow_type;
275:     typedef Source_ source_type;
276:     typedef Target_ target_type;
277:     typedef Color_  color_type;
278:     source_type source;
279:     target_type target;
280:     color_type  color;
281:     // Arrow modifiers
282:     struct sourceChanger {
283:       sourceChanger(const source_type& newSource) : _newSource(newSource) {};
284:       void operator()(arrow_type& a) {a.source = this->_newSource;}
285:     private:
286:       source_type _newSource;
287:     };
288: 
289:     struct targetChanger {
290:       targetChanger(const target_type& newTarget) : _newTarget(newTarget) {};
291:       void operator()(arrow_type& a) { a.target = this->_newTarget;}
292:     private:
293:       const target_type _newTarget;
294:     };
295:     // Flipping
296:     template <typename OtherSource_, typename OtherTarget_, typename OtherColor_>
297:     struct rebind {
298:       typedef Arrow<OtherSource_, OtherTarget_, OtherColor_> type;
299:     };
300:     struct flip {
301:       typedef Arrow<target_type, source_type, color_type> type;
302:       type arrow(const arrow_type& a) { return type(a.target, a.source, a.color);};
303:     };
304:   public:
305:     //
306:     // Basic interface
307:     Arrow(const source_type& s, const target_type& t, const color_type& c) : source(s), target(t), color(c) {};
308:     Arrow(const Arrow& a) : source(a.source), target(a.target), color(a.color) {};
309:     ~Arrow(){};
310:     //
311:     // Extended interface
312:     // Printing
313:     template <typename Stream_>
314:     friend Stream_& operator<<(Stream_& os, const Arrow& a) {
315:       os << a.source << " --(" << a.color << ")--> " << a.target;
316:       return os;
317:     }
318:   };// struct Arrow

320:   // Defines a sequence representing a subset of a multi_index container Index_.
321:   // A sequence defines output (input in std terminology) iterators for traversing an Index_ object.
322:   // Upon dereferencing values are extracted from each result record using a ValueExtractor_ object.
323:   template <typename Index_, typename ValueExtractor_ = ::boost::multi_index::identity<typename Index_::value_type> >
324:   struct IndexSequence {
325:     typedef Index_                                   index_type;
326:     typedef ValueExtractor_                          extractor_type;
327:     //
328:     template <typename Sequence_ = IndexSequence>
329:     class iterator {
330:     public:
331:       // Parent sequence type
332:       typedef Sequence_                              sequence_type;
333:       // Standard iterator typedefs
334:       typedef std::input_iterator_tag                iterator_category;
335:       typedef typename extractor_type::result_type   value_type;
336:       typedef int                                    difference_type;
337:       typedef value_type*                            pointer;
338:       typedef value_type&                            reference;
339:       // Underlying iterator type
340:       typedef typename index_type::iterator          itor_type;
341:     protected:
342:       // Parent sequence
343:       sequence_type&  _sequence;
344:       // Underlying iterator
345:       itor_type      _itor;
346:       // Member extractor
347:       extractor_type _ex;
348:     public:
349:       iterator(sequence_type& sequence, itor_type itor)       : _sequence(sequence),_itor(itor) {};
350:       iterator(const iterator& iter)                          : _sequence(iter._sequence),_itor(iter._itor) {}
351:       virtual ~iterator() {};
352:       virtual bool              operator==(const iterator& iter) const {return this->_itor == iter._itor;};
353:       virtual bool              operator!=(const iterator& iter) const {return this->_itor != iter._itor;};
354:       // FIX: operator*() should return a const reference, but it won't compile that way, because _ex() returns const value_type
355:       virtual const value_type  operator*() const {return _ex(*(this->_itor));};
356:       virtual iterator   operator++() {++this->_itor; return *this;};
357:       virtual iterator   operator++(int n) {iterator tmp(*this); ++this->_itor; return tmp;};
358:     };// class iterator
359:   protected:
360:     index_type& _index;
361:   public:
362:     //
363:     // Basic interface
364:     //
365:     IndexSequence(const IndexSequence& seq)  : _index(seq._index) {};
366:     IndexSequence(index_type& index)         : _index(index) {};
367:     virtual ~IndexSequence() {};
368:     //
369:     // Extended interface
370:     //
371:     virtual bool         empty() {return this->_index.empty();};

373:     virtual typename index_type::size_type  size()  {
374:       typename index_type::size_type sz = 0;
375:       for(typename index_type::iterator itor = this->_index.begin(); itor != this->_index.end(); itor++) {
376:         ++sz;
377:       }
378:       return sz;
379:     };
380:     template<typename ostream_type>
381:     void view(ostream_type& os, const char* label = NULL){
382:       if(label != NULL) {
383:         os << "Viewing " << label << " sequence:" << std::endl;
384:       }
385:       os << "[";
386:       for(iterator<> i = this->begin(); i != this->end(); i++) {
387:         os << " "<< *i;
388:       }
389:       os << " ]" << std::endl;
390:     };
391:   };// class IndexSequence

393: } // namespace ALE


396: #endif