libMesh
stored_range.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 #include <memory> // std::unique_ptr
29 #include <functional> // std::function
30 
31 namespace libMesh
32 {
33 
53 template <typename iterator_type, typename object_type>
55 {
56 public:
60  typedef typename std::vector<object_type> vec_type;
61  typedef typename vec_type::const_iterator const_iterator;
62  typedef typename std::unique_ptr<vec_type, std::function<void (vec_type *)>> ptr_type;
63 
69  StoredRange (const unsigned int new_grainsize = 1000) :
70  _end(),
71  _begin(),
72  _last(),
73  _first(),
74  _grainsize(new_grainsize),
75  _objs(ptr_type(new vec_type(), [](vec_type * p){delete p;}))
76  {}
77 
84  StoredRange (const iterator_type & first,
85  const iterator_type & last,
86  const unsigned int new_grainsize = 1000) :
87  _end(),
88  _begin(),
89  _last(),
90  _first(),
91  _grainsize(new_grainsize),
92  _objs(ptr_type(new vec_type(), [](vec_type * p){delete p;}))
93  {
94  this->reset(first, last);
95  }
96 
108  const unsigned int new_grainsize = 1000) :
109  _end(objs->end()),
110  _begin(objs->begin()),
111  _last(objs->size()),
112  _first(0),
113  _grainsize(new_grainsize),
114  _objs(ptr_type(objs, [](vec_type *){/*don't delete*/}))
115  {
116  }
117 
132  _end(er._end),
133  _begin(er._begin),
134  _last(er._last),
135  _first(er._first),
137  _objs(nullptr)
138  {
139  // specifically, do *not* copy the vector
140  }
141 
160  const const_iterator & begin_range,
161  const const_iterator & end_range):
162  _end(end_range),
163  _begin(begin_range),
164  _last(0), // Initialize these in a moment
165  _first(0),
167  _objs(nullptr)
168  {
169  // specifically, do *not* copy the vector
170 
173  }
174 
181  _end(r._end),
182  _begin(r._begin),
183  _last(r._last),
184  _first(r._first),
186  _objs(nullptr)
187  {
189  beginning = r._begin,
190  ending = r._end,
191  middle = beginning + std::distance(beginning, ending)/2u;
192 
193  r._end = _begin = middle;
194 
195  std::size_t
196  first = r._first,
197  last = r._last,
198  half = first + (last-first)/2u;
199 
200  r._last = _first = half;
201  }
202 
206  ~StoredRange () = default;
207 
215  reset (const iterator_type & first,
216  const iterator_type & last)
217  {
218  // _objs must be initialized in order to call reset()
220 
221  _objs->clear();
222 
223  for (iterator_type it=first; it!=last; ++it)
224  _objs->push_back(*it);
225 
226  _begin = _objs->begin();
227  _end = _objs->end();
228 
229  _first = 0;
230  _last = _objs->size();
231 
232  return *this;
233  }
234 
244  {
245  // _objs must be initialized in order to call reset()
247 
248  _begin = _objs->begin();
249  _end = _objs->end();
250 
251  _first = 0;
252  _last = _objs->size();
253 
254  return *this;
255  }
256 
260  const_iterator begin () const { return _begin; }
261 
265  const_iterator end () const { return _end; }
266 
270  std::size_t first_idx () const { return _first; }
271 
275  std::size_t last_idx () const { return _last; }
276 
281  std::size_t grainsize () const {return _grainsize;}
282 
286  void grainsize (const unsigned int & gs) {_grainsize = gs;}
287 
291  std::size_t size () const { return std::distance(_begin, _end); }
292 
293  //------------------------------------------------------------------------
294  // Methods that implement Range concept
295  //------------------------------------------------------------------------
296 
300  bool empty() const { return (_begin == _end); }
301 
305  bool is_divisible() const { return this->grainsize() < static_cast<unsigned int>(std::distance(_begin, _end)); }
306 
307 private:
308 
311  std::size_t _last;
312  std::size_t _first;
313  std::size_t _grainsize;
314 
316 };
317 
318 } // namespace libMesh
319 
320 #endif // LIBMESH_STORED_RANGE_H
StoredRange(const unsigned int new_grainsize=1000)
Constructor.
Definition: stored_range.h:69
StoredRange(vec_type *objs, const unsigned int new_grainsize=1000)
Constructor.
Definition: stored_range.h:107
bool empty() const
Definition: stored_range.h:300
const_iterator _begin
Definition: stored_range.h:310
std::unique_ptr< vec_type, std::function< void(vec_type *)> > ptr_type
Definition: stored_range.h:62
StoredRange(const StoredRange< iterator_type, object_type > &er)
Copy constructor.
Definition: stored_range.h:131
~StoredRange()=default
Destructor.
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:270
The StoredRange class defines a contiguous, divisible set of objects.
Definition: stored_range.h:54
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:180
bool is_divisible() const
Definition: stored_range.h:305
std::size_t grainsize() const
The grain size for the range.
Definition: stored_range.h:281
Real distance(const Point &p)
std::size_t size() const
Definition: stored_range.h:291
const_iterator _end
Definition: stored_range.h:309
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:159
libmesh_assert(ctx)
StoredRange(const iterator_type &first, const iterator_type &last, const unsigned int new_grainsize=1000)
Constructor.
Definition: stored_range.h:84
const_iterator end() const
End of the range.
Definition: stored_range.h:265
std::vector< object_type > vec_type
Allows an StoredRange to behave like an STL container.
Definition: stored_range.h:60
std::size_t _grainsize
Definition: stored_range.h:313
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:215
vec_type::const_iterator const_iterator
Definition: stored_range.h:61
const_iterator begin() const
Beginning of the range.
Definition: stored_range.h:260
void grainsize(const unsigned int &gs)
Set the grain size.
Definition: stored_range.h:286
std::size_t last_idx() const
Index in the stored vector of the last object.
Definition: stored_range.h:275
StoredRange< iterator_type, object_type > & reset()
Resets the range to the last specified range.
Definition: stored_range.h:243