matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
VectorIterator.h
Go to the documentation of this file.
1 #ifndef MATIOCPP_VECTORITERATOR_H
2 #define MATIOCPP_VECTORITERATOR_H
3 
5 //
6 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
7 //
8 // This code is licensed under the MIT License (MIT).
9 //
10 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16 // THE SOFTWARE.
17 //
19 
20 //Most of this code has been derived from https://github.com/Microsoft/GSL/blob/master/include/gsl/span
21 //and https://github.com/robotology/idyntree/blob/master/src/core/include/iDynTree/Core/Span.h
22 
23 /*
24  * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
25  *
26  * This software may be modified and distributed under the terms of the
27  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
28  */
29 
31 
32 namespace matioCpp
33 {
34 
35 template <class Vector, bool IsConst>
37 {
38  using element_type_ = typename Vector::element_type;
39  using input_vector_type = std::conditional_t<IsConst, const Vector, Vector>;
40 
41 public:
42 
44  using value_type = std::remove_cv_t<element_type_>;
46 
47  using reference = std::conditional_t<IsConst, const element_type_, element_type_>&;
48  using pointer = std::add_pointer_t<reference>;
49 
50  vector_iterator() = default;
51 
52  MATIOCPP_CONSTEXPR vector_iterator(input_vector_type* vector, typename Vector::index_type idx) noexcept
53  : vector_(vector), index_(idx)
54  {}
55 
57  template<bool B, std::enable_if_t<!B && IsConst>* = nullptr>
59  : vector_iterator(other.vector_, other.index_)
60  {
61  }
62 
64  {
65  assert(index_ != vector_->size());
66  return *(vector_->data() + index_);
67  }
68 
70  {
71  assert(index_ != vector_->size());
72  return vector_->data() + index_;
73  }
74 
76  {
77  assert(0 <= index_ && index_ != vector_->size());
78  ++index_;
79  return *this;
80  }
81 
83  {
84  auto ret = *this;
85  ++(*this);
86  return ret;
87  }
88 
90  {
91  assert(index_ != 0 && static_cast<size_t>(index_) <= vector_->size());
92  --index_;
93  return *this;
94  }
95 
97  {
98  auto ret = *this;
99  --(*this);
100  return ret;
101  }
102 
104  {
105  auto ret = *this;
106  return ret += n;
107  }
108 
110  {
111  assert((index_ + n) >= 0 && (index_ + n) <= vector_->size());
112  index_ += n;
113  return *this;
114  }
115 
117  {
118  auto ret = *this;
119  return ret -= n;
120  }
121 
123 
125  {
126  assert(vector_ == rhs.vector_);
127  return index_ - rhs.index_;
128  }
129 
131  {
132  return *(*this + n);
133  }
134 
136  vector_iterator rhs) noexcept
137  {
138  return lhs.vector_ == rhs.vector_ && lhs.index_ == rhs.index_;
139  }
140 
142  vector_iterator rhs) noexcept
143  {
144  return !(lhs == rhs);
145  }
146 
148  vector_iterator rhs) noexcept
149  {
150  return lhs.index_ < rhs.index_;
151  }
152 
154  vector_iterator rhs) noexcept
155  {
156  return !(rhs < lhs);
157  }
158 
160  vector_iterator rhs) noexcept
161  {
162  return rhs < lhs;
163  }
164 
166  vector_iterator rhs) noexcept
167  {
168  return !(rhs > lhs);
169  }
170 
171 protected:
172  input_vector_type* vector_ = nullptr;
174 };
175 
176 template <class Span, bool IsConst>
180 {
181  return rhs + n;
182 }
183 
184 template <class Span, bool IsConst>
185 MATIOCPP_CONSTEXPR vector_iterator<Span, IsConst>
188 {
189  return rhs - n;
190 }
191 }
192 
193 #endif // MATIOCPP_VECTORITERATOR_H
#define MATIOCPP_CONSTEXPR
size_t index_type
Defines how to allocate T.
Definition: Vector.h:34
typename get_type< T >::type element_type
Defines the type specified in the template.
Definition: Vector.h:28
MATIOCPP_CONSTEXPR vector_iterator & operator+=(difference_type n)
MATIOCPP_CONSTEXPR vector_iterator operator++(int)
MATIOCPP_CONSTEXPR friend bool operator>(vector_iterator lhs, vector_iterator rhs) noexcept
MATIOCPP_CONSTEXPR friend bool operator>=(vector_iterator lhs, vector_iterator rhs) noexcept
MATIOCPP_CONSTEXPR vector_iterator(input_vector_type *vector, typename Vector::index_type idx) noexcept
std::add_pointer_t< reference > pointer
MATIOCPP_CONSTEXPR vector_iterator operator-(difference_type n) const
MATIOCPP_CONSTEXPR vector_iterator & operator-=(difference_type n)
MATIOCPP_CONSTEXPR friend bool operator==(vector_iterator lhs, vector_iterator rhs) noexcept
MATIOCPP_CONSTEXPR vector_iterator(const vector_iterator< Vector, B > &other) noexcept
MATIOCPP_CONSTEXPR difference_type operator-(vector_iterator rhs) const
input_vector_type * vector_
MATIOCPP_CONSTEXPR friend bool operator<(vector_iterator lhs, vector_iterator rhs) noexcept
typename Vector::index_type difference_type
MATIOCPP_CONSTEXPR vector_iterator operator--(int)
MATIOCPP_CONSTEXPR reference operator*() const
MATIOCPP_CONSTEXPR pointer operator->() const
MATIOCPP_CONSTEXPR vector_iterator & operator++()
MATIOCPP_CONSTEXPR reference operator[](difference_type n) const
std::conditional_t< IsConst, const element_type_, element_type_ > & reference
MATIOCPP_CONSTEXPR vector_iterator & operator--()
MATIOCPP_CONSTEXPR friend bool operator!=(vector_iterator lhs, vector_iterator rhs) noexcept
MATIOCPP_CONSTEXPR vector_iterator operator+(difference_type n) const
MATIOCPP_CONSTEXPR friend bool operator<=(vector_iterator lhs, vector_iterator rhs) noexcept
std::remove_cv_t< element_type_ > value_type
MATIOCPP_CONSTEXPR vector_iterator< Span, IsConst > operator+(typename vector_iterator< Span, IsConst >::difference_type n, vector_iterator< Span, IsConst > rhs)
MATIOCPP_CONSTEXPR vector_iterator< Span, IsConst > operator-(typename vector_iterator< Span, IsConst >::difference_type n, vector_iterator< Span, IsConst > rhs)