libMesh
int_range.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
62  const iterator & operator++ () {
63  ++_i;
64  return *this;
65  }
66 
68  iterator returnval(*this);
69  ++_i;
70  return returnval;
71  }
72 
73  bool operator== (const iterator & j) const {
74  return ( _i == j._i );
75  }
76 
77  bool operator!= (const iterator & j) const {
78  return !(*this == j);
79  }
80 
81  private:
82  T _i;
83  };
84 
85  template <typename U, typename V>
86  IntRange(U begin, V end) :
87  _begin(cast_int<T>(begin)),
88  _end(cast_int<T>(end))
89  {}
90 
91  iterator begin() const { return _begin; }
92 
93  iterator end () const { return _end; }
94 
95 private:
97 };
98 
99 
100 
105 template <typename T>
106 IntRange<std::size_t> index_range(const std::vector<T> & vec)
107 {
108  return IntRange<std::size_t>(0, vec.size());
109 }
110 
111 
115 template <typename T>
117 {
118  return {0, vec.size()};
119 }
120 
121 
125 template <typename T>
127 {
128  return {0, vec.size()};
129 }
130 
131 
132 
136 template <typename T>
138 {
139  return {vec.first_local_index(), vec.last_local_index()};
140 }
141 
142 } // namespace libMesh
143 
144 #endif // LIBMESH_INT_RANGE_H
libMesh::NumericVector::last_local_index
virtual numeric_index_type last_local_index() const =0
libMesh::index_range
IntRange< std::size_t > index_range(const std::vector< T > &vec)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:106
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::IntRange::iterator::operator++
const iterator & operator++()
Definition: int_range.h:62
libMesh::IntRange::iterator::operator==
bool operator==(const iterator &j) const
Definition: int_range.h:73
libMesh::IntRange::IntRange
IntRange(U begin, V end)
Definition: int_range.h:86
libMesh::IntRange::iterator
Definition: int_range.h:56
libMesh::IntRange::iterator::operator*
T operator*() const
Definition: int_range.h:60
libMesh::IntRange::iterator::iterator
iterator(T i)
Definition: int_range.h:58
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::IntRange
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
libMesh::DenseSubVector::size
virtual unsigned int size() const override
Definition: dense_subvector.h:90
libMesh::IntRange::iterator::_i
T _i
Definition: int_range.h:82
libMesh::cast_int
Tnew cast_int(Told oldvar)
Definition: libmesh_common.h:610
libMesh::IntRange::end
iterator end() const
Definition: int_range.h:93
libMesh::DenseVector::size
virtual unsigned int size() const override
Definition: dense_vector.h:92
libMesh::DenseSubVector
Defines a dense subvector for use in finite element computations.
Definition: dense_subvector.h:43
libMesh::IntRange::_begin
iterator _begin
Definition: int_range.h:96
libMesh::IntRange::begin
iterator begin() const
Definition: int_range.h:91
libMesh::IntRange::iterator::operator!=
bool operator!=(const iterator &j) const
Definition: int_range.h:77
libMesh::IntRange::_end
iterator _end
Definition: int_range.h:96
libMesh::DenseVector
Defines a dense vector for use in Finite Element-type computations.
Definition: meshless_interpolation_function.h:39
libMesh::NumericVector::first_local_index
virtual numeric_index_type first_local_index() const =0