libMesh
int_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_INT_RANGE_H
21 #define LIBMESH_INT_RANGE_H
22 
23 #include "libmesh/libmesh_common.h" // cast_int
24 
25 // C++ includes
26 #include <vector>
27 
28 namespace libMesh
29 {
30 
31 // Forward declarations
32 template <typename T> class DenseSubVector;
33 template <typename T> class DenseVector;
34 template <typename T> class NumericVector;
35 
52 template <typename T>
53 class IntRange
54 {
55 public:
56  class iterator {
57  public:
58  using iterator_category = std::input_iterator_tag;
59  using value_type = T;
60  using difference_type = T;
61  using pointer = T;
62  using reference = T&;
63 
64  explicit iterator (T i) : _i(i) {}
65 
66  T operator* () const { return _i; }
67 
69  {
70  ++_i;
71  return *this;
72  }
73 
75  {
76  iterator returnval(*this);
77  ++_i;
78  return returnval;
79  }
80 
81  template <typename T2>
82  void operator+= (T2 n)
83  {
84  _i += cast_int<T>(n);
85  }
86 
87  template <typename T2>
88  iterator operator+ (T2 n) const
89  {
90  iterator returnval(_i);
91  returnval += n;
92  return returnval;
93  }
94 
95  bool operator== (const iterator & j) const
96  {
97  return ( _i == j._i );
98  }
99 
100  bool operator!= (const iterator & j) const
101  {
102  return !(*this == j);
103  }
104 
105  private:
106  T _i;
107  };
108 
110 
111  template <typename U, typename V>
113  _begin(cast_int<T>(begin)),
114  _end(cast_int<T>(end))
115  {}
116 
117  // Signature needed for use in threads_pthread.h
119  const const_iterator & begin,
120  const const_iterator & end) :
121  _begin(begin),
122  _end(end)
123  {}
124 
125  iterator begin() const { return _begin; }
126 
127  iterator end () const { return _end; }
128 
129  std::size_t size () const
130  { return *_end - *_begin; }
131 
132  // For thread splitting control
133  std::size_t grainsize () const { return libMesh::default_grainsize(); }
134 
135 private:
137 };
138 
139 
140 template <typename T, typename T2>
142 {
143  return i + n;
144 }
145 
146 
152 template <typename T>
153 auto index_range(const T & sizable)
154 {
155  return IntRange<decltype(sizable.size())>(0, sizable.size());
156 }
157 
158 
162 template <typename T>
164 {
165  return {vec.first_local_index(), vec.last_local_index()};
166 }
167 
168 
169 
175 template <typename T>
176 IntRange<T> make_range(T beg, T end)
177 {
178  return {beg, end};
179 }
180 
181 
182 
196 template <typename T>
198 {
199  return {T(0), end};
200 }
201 
202 } // namespace libMesh
203 
204 #endif // LIBMESH_INT_RANGE_H
iterator _begin
Definition: int_range.h:136
iterator end() const
Definition: int_range.h:127
iterator begin() const
Definition: int_range.h:125
std::size_t size() const
Definition: int_range.h:129
bool operator==(const iterator &j) const
Definition: int_range.h:95
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
std::size_t grainsize() const
Definition: int_range.h:133
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:44
The libMesh namespace provides an interface to certain functionality in the library.
IntRange(U begin, V end)
Definition: int_range.h:112
Tnew cast_int(Told oldvar)
iterator operator+(T2 n) const
Definition: int_range.h:88
std::input_iterator_tag iterator_category
Definition: int_range.h:58
bool operator!=(const iterator &j) const
Definition: int_range.h:100
const iterator & operator++()
Definition: int_range.h:68
unsigned int default_grainsize()
Definition: libmesh_base.h:117
IntRange(const IntRange &, const const_iterator &begin, const const_iterator &end)
Definition: int_range.h:118
virtual numeric_index_type first_local_index() const =0
IntRange< T >::iterator const_iterator
Definition: int_range.h:109
Order operator+(Order o, T p)
Definition: enum_order.h:93
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition: int_range.h:176
virtual numeric_index_type last_local_index() const =0
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:153