libMesh
int_range.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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  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  bool operator== (const iterator & j) const
82  {
83  return ( _i == j._i );
84  }
85 
86  bool operator!= (const iterator & j) const
87  {
88  return !(*this == j);
89  }
90 
91  private:
92  T _i;
93  };
94 
95  template <typename U, typename V>
96  IntRange(U begin, V end) :
97  _begin(cast_int<T>(begin)),
98  _end(cast_int<T>(end))
99  {}
100 
101  iterator begin() const { return _begin; }
102 
103  iterator end () const { return _end; }
104 
105 private:
107 };
108 
109 
110 
116 template <typename T>
117 auto index_range(const T & sizable)
118 {
119  return IntRange<decltype(sizable.size())>(0, sizable.size());
120 }
121 
122 
126 template <typename T>
128 {
129  return {vec.first_local_index(), vec.last_local_index()};
130 }
131 
132 
133 
139 template <typename T>
140 IntRange<T> make_range(T beg, T end)
141 {
142  return {beg, end};
143 }
144 
145 
146 
160 template <typename T>
162 {
163  return {T(0), end};
164 }
165 
166 } // namespace libMesh
167 
168 #endif // LIBMESH_INT_RANGE_H
iterator _begin
Definition: int_range.h:106
iterator end() const
Definition: int_range.h:103
iterator begin() const
Definition: int_range.h:101
bool operator==(const iterator &j) const
Definition: int_range.h:81
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
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:96
Tnew cast_int(Told oldvar)
std::input_iterator_tag iterator_category
Definition: int_range.h:58
bool operator!=(const iterator &j) const
Definition: int_range.h:86
const iterator & operator++()
Definition: int_range.h:68
virtual numeric_index_type first_local_index() const =0
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:140
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:117