Expression Templates Library (ETL)
iterable.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 namespace etl {
16 
22 template <typename D, bool DMA = false>
23 struct iterable {
24  using derived_t = D;
25 
30  derived_t& as_derived() noexcept {
31  return *static_cast<derived_t*>(this);
32  }
33 
38  const derived_t& as_derived() const noexcept {
39  return *static_cast<const derived_t*>(this);
40  }
41 
46  auto begin() noexcept {
47  if constexpr (DMA) {
48  as_derived().ensure_cpu_up_to_date();
49  return as_derived().memory_start();
50  } else {
51  return typename derived_t::iterator{as_derived(), 0};
52  }
53  }
54 
59  auto end() noexcept {
60  if constexpr (DMA) {
61  as_derived().ensure_cpu_up_to_date();
62  return as_derived().memory_end();
63  } else {
64  return typename derived_t::iterator{as_derived(), etl::size(as_derived())};
65  }
66  }
67 
72  auto cbegin() const noexcept {
73  if constexpr (DMA) {
74  as_derived().ensure_cpu_up_to_date();
75  return as_derived().memory_start();
76  } else {
77  return typename derived_t::const_iterator{as_derived(), 0};
78  }
79  }
80 
85  auto cend() const noexcept {
86  if constexpr (DMA) {
87  as_derived().ensure_cpu_up_to_date();
88  return as_derived().memory_end();
89  } else {
91  }
92  }
93 
98  auto begin() const noexcept {
99  if constexpr (DMA) {
100  as_derived().ensure_cpu_up_to_date();
101  return as_derived().memory_start();
102  } else {
103  return typename derived_t::const_iterator{as_derived(), 0};
104  }
105  }
106 
111  auto end() const noexcept {
112  if constexpr (DMA) {
113  as_derived().ensure_cpu_up_to_date();
114  return as_derived().memory_end();
115  } else {
117  }
118  }
119 };
120 
121 } //end of namespace etl
CRTP class to inject iterators functions.
Definition: iterable.hpp:23
auto begin() const noexcept
Return an iterator to the first element of the matrix.
Definition: iterable.hpp:98
D D
The number of dimensions.
Definition: dyn_matrix_view.hpp:24
An unary expression.
Definition: unary_expr.hpp:126
auto end() noexcept
Return an iterator to the past-the-end element of the matrix.
Definition: iterable.hpp:59
auto cbegin() const noexcept
Return an iterator to the first element of the matrix.
Definition: iterable.hpp:72
const derived_t & as_derived() const noexcept
Returns a reference to the derived object, i.e. the object using the CRTP injector.
Definition: iterable.hpp:38
Root namespace for the ETL library.
Definition: adapter.hpp:15
auto end() const noexcept
Return an iterator to the past-the-end element of the matrix.
Definition: iterable.hpp:111
Configurable iterator for ETL expressions.
Definition: iterator.hpp:24
auto begin() noexcept
Return an iterator to the first element of the matrix.
Definition: iterable.hpp:46
auto cend() const noexcept
Return an iterator to the past-the-end element of the matrix.
Definition: iterable.hpp:85
derived_t & as_derived() noexcept
Returns a reference to the derived object, i.e. the object using the CRTP injector.
Definition: iterable.hpp:30
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108