xc
DqPtrs.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // XC program; finite element analysis code
4 // for structural analysis and design.
5 //
6 // Copyright (C) Luis C. Pérez Tato
7 //
8 // This program derives from OpenSees <http://opensees.berkeley.edu>
9 // developed by the «Pacific earthquake engineering research center».
10 //
11 // Except for the restrictions that may arise from the copyright
12 // of the original program (see copyright_opensees.txt)
13 // XC is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // This software is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program.
26 // If not, see <http://www.gnu.org/licenses/>.
27 //----------------------------------------------------------------------------
28 //DqPtrs.h
29 //deque de pointers (se emplear en la clase Set).
30 
31 
32 #ifndef DQPTRS_H
33 #define DQPTRS_H
34 
35 #include "utility/kernel/CommandEntity.h"
36 #include <deque>
37 #include <set>
38 #include "utility/actor/actor/MovableID.h"
39 #include <boost/iterator/indirect_iterator.hpp>
40 
41 
42 
43 namespace XC {
44 
56 template <class T>
57 class DqPtrs: public CommandEntity, protected std::deque<T *>
58  {
59  public:
60  typedef typename std::deque<T *> lst_ptr;
61  typedef typename lst_ptr::const_iterator const_iterator;
62  typedef typename lst_ptr::iterator iterator;
63  typedef typename lst_ptr::reference reference;
64  typedef typename lst_ptr::const_reference const_reference;
65  typedef typename lst_ptr::size_type size_type;
66  typedef typename lst_ptr::value_type value_type;
67  typedef typename lst_ptr::difference_type difference_type;
68  typedef boost::indirect_iterator<iterator> indIterator;
69  public:
70  DqPtrs(CommandEntity *owr= nullptr);
71  DqPtrs(const DqPtrs &);
72  explicit DqPtrs(const std::deque<T *> &ts);
73  explicit DqPtrs(const std::set<const T *> &ts);
74  DqPtrs &operator=(const DqPtrs &);
75  DqPtrs &operator+=(const DqPtrs &);
76  void extend(const DqPtrs &);
77  //void extend_cond(const DqPtrs &,const std::string &cond);
78  bool push_back(T *);
79  bool push_front(T *);
80  inline bool empty(void) const
81  { return lst_ptr::empty(); }
82  inline iterator begin(void)
83  { return lst_ptr::begin(); }
84  const_iterator begin(void) const
85  { return lst_ptr::begin(); }
86  iterator end(void)
87  { return lst_ptr::end(); }
88  const_iterator end(void) const
89  { return lst_ptr::end(); }
90  inline indIterator indBegin(void)
91  { return indIterator(lst_ptr::begin()); }
92  inline indIterator indEnd(void)
93  { return indIterator(lst_ptr::end()); }
94  const T &get(const size_t &i) const;
95  void clear(void);
96  void clearAll(void);
97  inline size_type size(void) const
98  { return lst_ptr::size(); }
99  bool in(const T *) const;
100  bool remove(T *);
101  //void sort_on_prop(const std::string &cod,const bool &ascending= true);
102 
103  boost::python::list getPythonList(void);
104 
105  const ID &getTags(void) const;
106  T *findTag(const size_t &);
107  template <class InputIterator>
108  void insert(iterator pos, InputIterator f, InputIterator l)
109  { lst_ptr::insert(pos,f,l); }
110  template <class InputIterator>
111  void insert_unique(iterator pos, InputIterator f, InputIterator l)
112  {
113  DqPtrs<T> tmp;
114  //Filter those already in the container.
115  for(InputIterator i= f;i!=l;i++)
116  {
117  T *ptr= *i;
118  if(!this->in(ptr))
119  { tmp.push_back(ptr); }
120  }
121  lst_ptr::insert(pos,tmp.begin(),tmp.end()); //Add only new ones.
122  }
123 
124 
125  int sendTags(int posSz,int posDbTag,DbTagData &dt,Communicator &comm);
126  const ID &receiveTags(int posSz,int pDbTg,DbTagData &dt,const Communicator &comm);
127 
128  };
129 
131 template <class T>
133  : CommandEntity(owr),lst_ptr() {}
134 
136 template <class T>
138  : CommandEntity(other), lst_ptr(other)
139  {}
140 
142 template <class T>
143 DqPtrs<T>::DqPtrs(const std::deque<T *> &ts)
144  : CommandEntity(), lst_ptr(ts)
145  {}
146 
148 template <class T>
149 DqPtrs<T>::DqPtrs(const std::set<const T *> &st)
150  : CommandEntity(), lst_ptr()
151  {
152  typename std::set<const T *>::const_iterator k;
153  k= st.begin();
154  for(;k!=st.end();k++)
155  lst_ptr::push_back(const_cast<T *>(*k));
156  }
157 
159 template <class T>
161  {
163  lst_ptr::operator=(other);
164  return *this;
165  }
166 
168 template <class T>
170  {
171  extend(other);
172  return *this;
173  }
174 
175 
178 template <class T>
179 void DqPtrs<T>::extend(const DqPtrs &other)
180  {
181  for( const_iterator i= other.begin();i!=other.end();i++)
182  push_back(*i);
183  }
184 
187 template <class T>
188 boost::python::list DqPtrs<T>::getPythonList(void)
189  {
190  boost::python::list retval;
191  for(iterator i= begin();i!=end();i++)
192  retval.append(boost::ref(*i));
193  return retval;
194  }
195 
197 template<class T>
199  { lst_ptr::clear(); }
200 
202 template<class T>
204  {
205  clear();
207  }
208 
210 template<class T>
211 const T &DqPtrs<T>::get(const size_t &i) const
212  {
213  const T *ptr= lst_ptr::at(i);
214  return *ptr;
215  }
216 
218 template<class T>
219 bool DqPtrs<T>::in(const T *ptr) const
220  {
221  bool retval= false;
222  for(const_iterator i= begin();i!= end();i++)
223  if(*i==ptr)
224  {
225  retval= true;
226  break;
227  }
228  return retval;
229  }
230 
232 template<class T>
233 bool DqPtrs<T>::remove(T *ptr)
234  {
235  bool retval= false;
236  for(const_iterator i= begin();i!= end();i++)
237  if(*i==ptr)
238  {
239  retval= true;
240  this->erase(i);
241  break;
242  }
243  return retval;
244  }
245 
246 template <class T>
247 bool DqPtrs<T>::push_back(T *t)
248  {
249  bool retval= false;
250  if(t)
251  {
252  if(find(begin(),end(),t) == end()) //It's a new element.
253  {
254  lst_ptr::push_back(t);
255  retval= true;
256  }
257  }
258  else
259  std::cerr << getClassName() << "::" << __FUNCTION__
260  << "; attempt to insert a null pointer." << std::endl;
261  return retval;
262  }
263 
264 template <class T>
265 bool DqPtrs<T>::push_front(T *t)
266  {
267  bool retval= false;
268  if(t)
269  {
270  if(find(begin(),end(),t) == end()) //New element.
271  {
272  lst_ptr::push_front(t);
273  retval= true;
274  }
275  }
276  else
277  std::cerr << getClassName() << "::" << __FUNCTION__
278  << "; attempt to insert a null pointer." << std::endl;
279  return retval;
280  }
281 
283 template <class T>
284 const ID &DqPtrs<T>::getTags(void) const
285  {
286  static ID retval;
287  const int sz= size();
288  if(sz>0)
289  {
290  retval.resize(sz);
291  int loc =0;
292  // loop over objs in deque adding their dbTag to the ID
293  for(const_iterator i= begin();i!=end();i++)
294  {
295  retval(loc)= (*i)->getTag();
296  loc++;
297  }
298  }
299  return retval;
300  }
301 
303 template <class T>
304 T *DqPtrs<T>::findTag(const size_t &tag)
305  {
306  for(const_iterator i= this->begin();i!=this->end();i++)
307  if(static_cast<size_t>((*i)->getTag())==tag) return *i;
308  return nullptr;
309  }
310 
311 
313 template <class T>
314 int DqPtrs<T>::sendTags(int posSz,int posDbTag,DbTagData &dt,Communicator &comm)
315  {
316  const int sz= size();
317  int res= comm.sendInt(sz,dt,CommMetaData(posSz));
318  if(sz>0)
319  {
320  const ID &tags= getTags();
321  res+= comm.sendID(tags,dt,CommMetaData(posDbTag));
322  }
323  if(res<0)
324  std::cerr << "DqPtrs<T>::sendDbTags - failed to send the IDs.\n";
325  return res;
326  }
327 
329 template <class T>
330 const ID &DqPtrs<T>::receiveTags(int posSz,int posDbTag,DbTagData &dt,const Communicator &comm)
331  {
332  static ID retval;
333  int sz= 0;
334  int res= comm.receiveInt(sz,dt,CommMetaData(posSz));
335  if(sz>0)
336  {
337  retval.resize(sz);
338  res= comm.receiveID(retval,dt,CommMetaData(posDbTag));
339  }
340  if(res<0)
341  std::cerr << "DqPtrs<T>::receiveTags - failed to receive the IDs.\n";
342  return retval;
343  }
344 
345 } //end of XC namespace
346 
347 
348 #endif
349 
int sendInt(const int &, DbTagData &, const CommMetaData &)
Sends the integer through the communicator argument.
Definition: Communicator.cc:875
DqPtrs & operator=(const DqPtrs &)
Assignment operator.
Definition: DqPtrs.h:160
boost::python::list getPythonList(void)
Return a python list containing the pointers to the objects in this container.
Definition: DqPtrs.h:188
Communication parameters between processes.
Definition: Communicator.h:66
T * findTag(const size_t &)
Returns a pointer to the object identified by the tag argument.
Definition: DqPtrs.h:304
int receiveInt(int &, DbTagData &, const CommMetaData &) const
Receives the integers through the communicator argument.
Definition: Communicator.cc:935
int resize(const int &newSize, const int &fill_value=0)
Changes the size of the array.
Definition: ID.cpp:187
EntityWithOwner & operator=(const EntityWithOwner &)
Assignment operator.
Definition: EntityWithOwner.cc:53
Vector that stores the dbTags of the class members.
Definition: DbTagData.h:44
Vector of integers.
Definition: ID.h:95
Data about the index, size,,...
Definition: CommMetaData.h:39
DqPtrs(CommandEntity *owr=nullptr)
Constructor.
Definition: DqPtrs.h:132
DqPtrs & operator+=(const DqPtrs &)
+= (union) operator.
Definition: DqPtrs.h:169
const ID & getTags(void) const
Returns the tags of the objects.
Definition: DqPtrs.h:284
Objet that can execute python scripts.
Definition: CommandEntity.h:40
void clearAll(void)
Clears out the list of pointers and erases the properties of the object (if any). ...
Definition: DqPtrs.h:203
int sendID(const ID &, const int &)
Sends vector.
Definition: Communicator.cc:73
const ID & receiveTags(int posSz, int pDbTg, DbTagData &dt, const Communicator &comm)
Sends the dbTags of the sets through the communicator argument.
Definition: DqPtrs.h:330
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35
int sendTags(int posSz, int posDbTag, DbTagData &dt, Communicator &comm)
Sends the dbTags of the sets trough the communicator argument.
Definition: DqPtrs.h:314
void clear(void)
Clears out the list of pointers.
Definition: DqPtrs.h:198
int receiveID(ID &v, const int &) const
Receives el vector.
Definition: Communicator.cc:80
void clearPyProps(void)
Clear python properties map.
Definition: EntityWithProperties.cc:33
Pointer to (nodes, elements, points, lines,...) container.
Definition: DqPtrs.h:57
bool in(const T *) const
Returns true if the pointer is in the container.
Definition: DqPtrs.h:219
void extend(const DqPtrs &)
Extend this container with the pointers from the container being passed as parameter.
Definition: DqPtrs.h:179