int_range.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 // libMesh includes
26 #include "numeric_vector.h"
27 
28 // C++ includes
29 #include <vector>
30 
31 namespace libMesh
32 {
33 
50 template <typename T>
51 class IntRange
52 {
53 public:
54  class iterator {
55  public:
56  iterator (T i) : _i(i) {}
57 
58  T operator* () const { return _i; }
59 
60  const iterator & operator++ () {
61  ++_i;
62  return *this;
63  }
64 
66  iterator returnval(*this);
67  ++_i;
68  return returnval;
69  }
70 
71  bool operator== (const iterator & j) const {
72  return ( _i == j._i );
73  }
74 
75  bool operator!= (const iterator & j) const {
76  return !(*this == j);
77  }
78 
79  private:
80  T _i;
81  };
82 
83  template <typename U, typename V>
84  IntRange(U begin, V end) :
85  _begin(cast_int<T>(begin)),
86  _end(cast_int<T>(end))
87  {}
88 
89  iterator begin() const { return _begin; }
90 
91  iterator end () const { return _end; }
92 
93 private:
95 };
96 
97 
98 
103 template <typename T>
104 IntRange<std::size_t> index_range(const std::vector<T> & vec)
105 {
106  return IntRange<std::size_t>(0, vec.size());
107 }
108 
109 
110 
114 template <typename T>
116 {
117  return {vec.first_local_index(), vec.last_local_index()};
118 }
119 
120 } // namespace libMesh
121 
122 #endif // LIBMESH_INT_RANGE_H
iterator _begin
Definition: int_range.h:94
iterator end() const
Definition: int_range.h:91
iterator begin() const
Definition: int_range.h:89
bool operator==(const iterator &j) const
Definition: int_range.h:71
IntRange< std::size_t > index_range(const std::vector< T > &vec)
Definition: int_range.h:104
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: diff_context.h:40
IntRange(U begin, V end)
Definition: int_range.h:84
Tnew cast_int(Told oldvar)
bool operator!=(const iterator &j) const
Definition: int_range.h:75
const iterator & operator++()
Definition: int_range.h:60
virtual numeric_index_type first_local_index() const =0
iterator _end
Definition: int_range.h:94
virtual numeric_index_type last_local_index() const =0