libMesh
Loading...
Searching...
No Matches
stored_range.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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 <type_traits> // std::remove*
28#include <vector>
29#include <memory> // std::unique_ptr
30#include <functional> // std::function
31
32namespace libMesh
33{
34
54template <typename iterator_type, typename object_type>
56{
57public:
61 typedef typename std::vector<object_type> vec_type;
62 typedef const object_type * const_iterator;
63 typedef typename std::unique_ptr<vec_type, std::function<void (vec_type *)>> ptr_type;
64
68 typedef std::remove_const_t<std::remove_pointer_t<object_type>> * nonconst_object_type;
69
75 StoredRange (const unsigned int new_grainsize = libMesh::default_grainsize()) :
76 _end(),
77 _begin(),
78 _last(),
79 _first(),
80 _grainsize(new_grainsize),
81 _objs(ptr_type(new vec_type(), [](vec_type * p){delete p;}))
82 {}
83
90 StoredRange (const iterator_type & first,
91 const iterator_type & last,
92 const unsigned int new_grainsize = libMesh::default_grainsize()) :
93 _end(),
94 _begin(),
95 _last(),
96 _first(),
97 _grainsize(new_grainsize),
98 _objs(ptr_type(new vec_type(), [](vec_type * p){delete p;}))
99 {
100 this->reset(first, last);
101 }
102
114 const unsigned int new_grainsize = libMesh::default_grainsize()) :
115 _end(end_of(*objs)),
116 _begin(objs->data()),
117 _last(objs->size()),
118 _first(0),
119 _grainsize(new_grainsize),
120 _objs(ptr_type(objs, [](vec_type *){/*don't delete*/}))
121 {
122 }
123
138 const unsigned int new_grainsize = libMesh::default_grainsize()) :
139 _end(end),
140 _begin(begin),
142 _first(0),
143 _grainsize(new_grainsize),
144 _objs(nullptr)
145 {
146 }
147
162 _end(er._end),
163 _begin(er._begin),
164 _last(er._last),
165 _first(er._first),
167 _objs(nullptr)
168 {
169 // specifically, do *not* copy the vector
170 }
171
190 const const_iterator & begin_range,
191 const const_iterator & end_range):
192 _end(end_range),
193 _begin(begin_range),
194 _last(0), // Initialize these in a moment
195 _first(0),
197 _objs(nullptr)
198 {
199 // specifically, do *not* copy the vector
200
201 _first = std::distance(er._begin, _begin);
202 _last = _first + std::distance(_begin, _end);
203 }
204
211 _end(r._end),
212 _begin(r._begin),
213 _last(r._last),
214 _first(r._first),
216 _objs(nullptr)
217 {
219 beginning = r._begin,
220 ending = r._end,
221 middle = beginning + std::distance(beginning, ending)/2u;
222
223 r._end = _begin = middle;
224
225 std::size_t
226 first = r._first,
227 last = r._last,
228 half = first + (last-first)/2u;
229
230 r._last = _first = half;
231 }
232
236 ~StoredRange () = default;
237
245 reset (const iterator_type & first,
246 const iterator_type & last)
247 {
248 // _objs must be initialized in order to call reset()
250
251 _objs->clear();
252
253 for (iterator_type it=first; it!=last; ++it)
254 _objs->push_back(*it);
255
256 _begin = _objs->data();
257 _end = end_of(*_objs);
258
259 _first = 0;
260 _last = _objs->size();
261
262 return *this;
263 }
264
274 {
275 // _objs must be initialized in order to call reset()
277
278 _begin = _objs->data();
279 _end = end_of(*_objs);
280
281 _first = 0;
282 _last = _objs->size();
283
284 return *this;
285 }
286
290 const_iterator begin () const { return _begin; }
291
295 const_iterator end () const { return _end; }
296
300 std::size_t first_idx () const { return _first; }
301
305 std::size_t last_idx () const { return _last; }
306
311 std::size_t grainsize () const {return _grainsize;}
312
316 void grainsize (const unsigned int & gs) {_grainsize = gs;}
317
321 std::size_t size () const { return std::distance(_begin, _end); }
322
327 {
328 for (auto it = this->begin(), it2 = other.begin();
329 it != this->end() || it2 != other.end(); ++it, ++it2)
330 {
331 if (it == this->end() || it2 == other.end())
332 return false;
333
334 if (*it != *it2)
335 return false;
336 }
337
338 return true;
339 }
340
341 //------------------------------------------------------------------------
342 // Methods that implement Range concept
343 //------------------------------------------------------------------------
344
348 bool empty() const { return (_begin == _end); }
349
353 bool is_divisible() const { return this->grainsize() < static_cast<unsigned int>(std::distance(_begin, _end)); }
354
355private:
356
363 return objs.size() ? (objs.data()+objs.size()) : objs.data();
364 }
365
368 std::size_t _last;
369 std::size_t _first;
370 std::size_t _grainsize;
371
373};
374
375} // namespace libMesh
376
377#endif // LIBMESH_STORED_RANGE_H
The StoredRange class defines a contiguous, divisible set of objects.
static const_iterator end_of(vec_type &objs)
Helper for pre-C++26 compiles, where the compiler may have empty_vector.data() return nullptr and tec...
std::size_t first_idx() const
Index in the stored vector of the first object.
StoredRange(vec_type *objs, const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
const object_type * const_iterator
StoredRange(const nonconst_object_type *begin, const nonconst_object_type *end, const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
const_iterator begin() const
Beginning of the range.
bool operator==(const StoredRange< iterator_type, object_type > &other) const
std::vector< object_type > vec_type
Allows an StoredRange to behave like an STL container.
const_iterator end() const
End of the range.
StoredRange(const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
std::unique_ptr< vec_type, std::function< void(vec_type *)> > ptr_type
StoredRange(StoredRange< iterator_type, object_type > &r, Threads::split)
Splits the range r.
~StoredRange()=default
Destructor.
std::size_t grainsize() const
The grain size for the range.
void grainsize(const unsigned int &gs)
Set the grain size.
bool is_divisible() const
std::size_t size() const
StoredRange< iterator_type, object_type > & reset(const iterator_type &first, const iterator_type &last)
Resets the StoredRange to contain [first,last).
StoredRange< iterator_type, object_type > & reset()
Resets the range to the last specified range.
StoredRange(const iterator_type &first, const iterator_type &last, const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
const_iterator _end
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!!!
std::size_t last_idx() const
Index in the stored vector of the last object.
StoredRange(const StoredRange< iterator_type, object_type > &er)
Copy constructor.
std::remove_const_t< std::remove_pointer_t< object_type > > * nonconst_object_type
Allows conversion from non-const to const pointer objects.
const_iterator _begin
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
The libMesh namespace provides an interface to certain functionality in the library.
unsigned int default_grainsize()
libmesh_assert(ctx)
Real distance(const Point &p)