libMesh
stored_range.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_STORED_RANGE_H
21 #define LIBMESH_STORED_RANGE_H
22 
23 // Local includes
24 #include "libmesh/threads.h"
25 
26 // C++ includes
27 #include <vector>
28 
29 namespace libMesh
30 {
31 
51 template <typename iterator_type, typename object_type>
53 {
54 public:
58  typedef typename std::vector<object_type>::const_iterator const_iterator;
59 
65  StoredRange (const unsigned int new_grainsize = 1000) :
66  _end(),
67  _begin(),
68  _last(),
69  _first(),
70  _grainsize(new_grainsize),
71  _objs(new std::vector<object_type>()),
72  _should_release(true)
73  {}
74 
81  StoredRange (const iterator_type & first,
82  const iterator_type & last,
83  const unsigned int new_grainsize = 1000) :
84  _end(),
85  _begin(),
86  _last(),
87  _first(),
88  _grainsize(new_grainsize),
89  _objs(new std::vector<object_type>()),
90  _should_release(true)
91  {
92  this->reset(first, last);
93  }
94 
106  StoredRange (std::vector<object_type> * objs,
107  const unsigned int new_grainsize = 1000) :
108  _end(objs->end()),
109  _begin(objs->begin()),
110  _last(objs->size()),
111  _first(0),
112  _grainsize(new_grainsize),
113  _objs(objs),
114  _should_release(false)
115  {
116  }
117 
132  _end(er._end),
133  _begin(er._begin),
134  _last(er._last),
135  _first(er._first),
137  _objs(NULL),
138  _should_release(false)
139  {
140  // specifically, do *not* copy the vector
141  }
142 
161  const const_iterator & begin_range,
162  const const_iterator & end_range):
163  _end(end_range),
164  _begin(begin_range),
165  _last(0), // Initialize these in a moment
166  _first(0),
168  _objs(NULL),
169  _should_release(false)
170  {
171  // specifically, do *not* copy the vector
172 
175  }
176 
183  _end(r._end),
184  _begin(r._begin),
185  _last(r._last),
186  _first(r._first),
188  _objs(NULL),
189  _should_release(false)
190  {
191  const_iterator
192  beginning = r._begin,
193  ending = r._end,
194  middle = beginning + std::distance(beginning, ending)/2u;
195 
196  r._end = _begin = middle;
197 
198  std::size_t
199  first = r._first,
200  last = r._last,
201  half = first + (last-first)/2u;
202 
203  r._last = _first = half;
204  }
205 
210  {
211  if (_should_release)
212  delete _objs;
213  }
214 
222  reset (const iterator_type & first,
223  const iterator_type & last)
224  {
225  _objs->clear();
226 
227  for (iterator_type it=first; it!=last; ++it)
228  _objs->push_back(*it);
229 
230  _begin = _objs->begin();
231  _end = _objs->end();
232 
233  _first = 0;
234  _last = _objs->size();
235 
236  return *this;
237  }
238 
248  {
249  _begin = _objs->begin();
250  _end = _objs->end();
251 
252  _first = 0;
253  _last = _objs->size();
254 
255  return *this;
256  }
257 
261  const_iterator begin () const { return _begin; }
262 
266  const_iterator end () const { return _end; }
267 
271  std::size_t first_idx () const { return _first; }
272 
276  std::size_t last_idx () const { return _last; }
277 
282  std::size_t grainsize () const {return _grainsize;}
283 
287  void grainsize (const unsigned int & gs) {_grainsize = gs;}
288 
292  std::size_t size () const { return std::distance(_begin, _end); }
293 
294  //------------------------------------------------------------------------
295  // Methods that implement Range concept
296  //------------------------------------------------------------------------
297 
301  bool empty() const { return (_begin == _end); }
302 
306  bool is_divisible() const { return this->grainsize() < static_cast<unsigned int>(std::distance(_begin, _end)); }
307 
308 private:
309 
310  const_iterator _end;
311  const_iterator _begin;
312  std::size_t _last;
313  std::size_t _first;
314  std::size_t _grainsize;
315 
316  // TODO: Make this a std::shared_ptr in the future!
317  std::vector<object_type> * _objs;
319 };
320 
321 } // namespace libMesh
322 
323 #endif // LIBMESH_STORED_RANGE_H
StoredRange(const unsigned int new_grainsize=1000)
Constructor.
Definition: stored_range.h:65
~StoredRange()
Destructor.
Definition: stored_range.h:209
const_iterator _begin
Definition: stored_range.h:311
std::vector< object_type > * _objs
Definition: stored_range.h:317
StoredRange(const StoredRange< iterator_type, object_type > &er)
Copy constructor.
Definition: stored_range.h:131
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
Definition: threads_none.h:63
std::size_t first_idx() const
Index in the stored vector of the first object.
Definition: stored_range.h:271
std::size_t size() const
Definition: stored_range.h:292
The StoredRange class defines a contiguous, divisible set of objects.
Definition: stored_range.h:52
The libMesh namespace provides an interface to certain functionality in the library.
StoredRange(StoredRange< iterator_type, object_type > &r, Threads::split)
Splits the range r.
Definition: stored_range.h:182
const_iterator begin() const
Beginning of the range.
Definition: stored_range.h:261
Real distance(const Point &p)
std::size_t last_idx() const
Index in the stored vector of the last object.
Definition: stored_range.h:276
const_iterator _end
Definition: stored_range.h:310
StoredRange(const StoredRange< iterator_type, object_type > &er, const const_iterator &begin_range, const const_iterator &end_range)
NOTE: When using pthreads this constructor is MANDATORY!!!
Definition: stored_range.h:160
bool is_divisible() const
Definition: stored_range.h:306
std::size_t grainsize() const
The grain size for the range.
Definition: stored_range.h:282
StoredRange(std::vector< object_type > *objs, const unsigned int new_grainsize=1000)
Constructor.
Definition: stored_range.h:106
const_iterator end() const
End of the range.
Definition: stored_range.h:266
StoredRange(const iterator_type &first, const iterator_type &last, const unsigned int new_grainsize=1000)
Constructor.
Definition: stored_range.h:81
std::vector< object_type >::const_iterator const_iterator
Allows an StoredRange to behave like an STL container.
Definition: stored_range.h:58
std::size_t _grainsize
Definition: stored_range.h:314
StoredRange< iterator_type, object_type > & reset(const iterator_type &first, const iterator_type &last)
Resets the StoredRange to contain [first,last).
Definition: stored_range.h:222
void grainsize(const unsigned int &gs)
Set the grain size.
Definition: stored_range.h:287
bool empty() const
Definition: stored_range.h:301
StoredRange< iterator_type, object_type > & reset()
Resets the range to the last specified range.
Definition: stored_range.h:247