libMesh
int_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_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  iterator (T i) : _i(i) {}
59 
60  T operator* () const { return _i; }
61 
63  {
64  ++_i;
65  return *this;
66  }
67 
69  {
70  iterator returnval(*this);
71  ++_i;
72  return returnval;
73  }
74 
75  bool operator== (const iterator & j) const
76  {
77  return ( _i == j._i );
78  }
79 
80  bool operator!= (const iterator & j) const
81  {
82  return !(*this == j);
83  }
84 
85  private:
86  T _i;
87  };
88 
89  template <typename U, typename V>
90  IntRange(U begin, V end) :
91  _begin(cast_int<T>(begin)),
92  _end(cast_int<T>(end))
93  {}
94 
95  iterator begin() const { return _begin; }
96 
97  iterator end () const { return _end; }
98 
99 private:
101 };
102 
103 
104 
110 template <typename T>
111 auto index_range(const T & sizable)
112 {
113  return IntRange<decltype(sizable.size())>(0, sizable.size());
114 }
115 
116 
120 template <typename T>
122 {
123  return {vec.first_local_index(), vec.last_local_index()};
124 }
125 
126 
127 
133 template <typename T>
134 IntRange<T> make_range(T beg, T end)
135 {
136  return {beg, end};
137 }
138 
139 
140 
154 template <typename T>
156 {
157  return {T(0), end};
158 }
159 
160 } // namespace libMesh
161 
162 #endif // LIBMESH_INT_RANGE_H
iterator _begin
Definition: int_range.h:100
iterator end() const
Definition: int_range.h:97
iterator begin() const
Definition: int_range.h:95
bool operator==(const iterator &j) const
Definition: int_range.h:75
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:43
The libMesh namespace provides an interface to certain functionality in the library.
IntRange(U begin, V end)
Definition: int_range.h:90
Tnew cast_int(Told oldvar)
bool operator!=(const iterator &j) const
Definition: int_range.h:80
const iterator & operator++()
Definition: int_range.h:62
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:134
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:111