Embedded Template Library 1.0
multi_span.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_MULTI_SPAN_INCLUDED
32#define ETL_MULTI_SPAN_INCLUDED
33
34#include "platform.h"
35#include "iterator.h"
36#include "algorithm.h"
37#include "vector.h"
38#include "span.h"
39
43
44namespace etl
45{
46 template <typename T>
48 {
49 public:
50
51 typedef T element_type;
52 typedef typename etl::remove_cv<T>::type value_type;
53 typedef size_t size_type;
54 typedef T& reference;
55 typedef const T& const_reference;
56 typedef T* pointer;
57 typedef const T* const_pointer;
58
59 typedef etl::span<T> span_type;
61
62 //*************************************************************************
64 //*************************************************************************
65 class iterator : public etl::iterator<ETL_OR_STD::forward_iterator_tag, element_type>
66 {
67 public:
68
69 friend class multi_span;
70
71 iterator()
72 : p_current(ETL_NULLPTR)
73 , p_end(ETL_NULLPTR)
74 , p_value(ETL_NULLPTR)
75 {
76 }
77
78 //*****************************************
79 iterator& operator ++()
80 {
81 if (p_current != p_end)
82 {
83 ++p_value;
84
85 if (p_value == p_current->end())
86 {
87 do
88 {
89 ++p_current;
90 } while ((p_current != p_end) && p_current->empty());
91
92 if (p_current != p_end)
93 {
94 p_value = p_current->begin();
95 }
96 else
97 {
98 p_value = ETL_NULLPTR;
99 }
100 }
101 }
102
103 return *this;
104 }
105
106 //*****************************************
107 iterator operator ++(int)
108 {
109 iterator temp = *this;
110
111 operator ++();
112
113 return temp;
114 }
115
116 //*************************************************************************
118 //*************************************************************************
119 reference operator *()
120 {
121 return *p_value;
122 }
123
124 //*************************************************************************
126 //*************************************************************************
127 const_reference operator *() const
128 {
129 return *p_value;
130 }
131
132 //*************************************************************************
134 //*************************************************************************
135 pointer operator ->()
136 {
137 return *p_value;
138 }
139
140 //*************************************************************************
142 //*************************************************************************
143 const_pointer operator ->() const
144 {
145 return *p_value;
146 }
147
148 //*************************************************************************
150 //*************************************************************************
151 friend bool operator ==(const iterator& lhs, const iterator& rhs)
152 {
153 return (lhs.p_current == rhs.p_current);
154 }
155
156 //*************************************************************************
158 //*************************************************************************
159 friend bool operator !=(const iterator& lhs, const iterator& rhs)
160 {
161 return !(lhs == rhs);
162 }
163
164 private:
165
166 typedef typename span_list_type::iterator span_list_iterator;
167
168 //*****************************************
169 iterator(span_list_iterator p_current_, span_list_iterator p_end_)
170 : p_current(p_current_)
171 , p_end(p_end_)
172 , p_value(ETL_NULLPTR)
173 {
174 if (p_current != p_end)
175 {
176 p_value = p_current->begin();
177 }
178 }
179
180 typedef const span_type* span_list_pointer;
181
182 span_list_pointer p_current;
183 span_list_pointer p_end;
184 pointer p_value;
185 };
186
187 //*************************************************************************
189 //*************************************************************************
190 ETL_CONSTEXPR multi_span(span_list_type span_list_)
191 : span_list(span_list_)
192 {
193 }
194
195 //*************************************************************************
198 //*************************************************************************
199 template <typename TContainer>
200 ETL_CONSTEXPR multi_span(TContainer& a) ETL_NOEXCEPT
201 : span_list(a.data(), a.data() + a.size())
202 {
203 }
204
205 //*************************************************************************
208 //*************************************************************************
209 template <typename TContainer>
210 ETL_CONSTEXPR multi_span(const TContainer& a) ETL_NOEXCEPT
211 : span_list(a.data(), a.data() + a.size())
212 {
213 }
214
215 //*************************************************************************
217 //*************************************************************************
218 template <typename TIterator>
219 ETL_CONSTEXPR multi_span(TIterator begin_, TIterator end_)
220 : span_list(etl::addressof(*begin_), etl::distance(begin_, end_))
221 {
222 }
223
224 //*************************************************************************
226 //*************************************************************************
227 template <typename TIterator>
228 ETL_CONSTEXPR multi_span(TIterator begin_, size_t length_)
229 : span_list(etl::addressof(*begin_), length_)
230 {
231 }
232
233 //*************************************************************************
235 //*************************************************************************
236 ETL_CONSTEXPR multi_span(const multi_span& other)
237 : span_list(other.span_list)
238 {
239 }
240
241 //*************************************************************************
243 //*************************************************************************
244 ETL_CONSTEXPR multi_span& operator = (const multi_span & other)
245 {
246 span_list = other.span_list;
247
248 return *this;
249 }
250
251 //*************************************************************************
253 //*************************************************************************
254 ETL_CONSTEXPR iterator begin() const
255 {
256 return iterator(span_list.begin(), span_list.end());
257 }
258
259 //*************************************************************************
261 //*************************************************************************
262 ETL_CONSTEXPR iterator end() const
263 {
264 return iterator(span_list.end(), span_list.end());
265 }
266
267 //*************************************************************************
269 //*************************************************************************
270 ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
271 {
272 size_t total_n_spans = 0U;
273
274 for (typename span_list_type::iterator itr = span_list.begin();
275 itr != span_list.end();
276 ++itr)
277 {
278 total_n_spans += itr->size();
279 }
280
281 return total_n_spans;
282 }
283
284 //*************************************************************************
286 //*************************************************************************
287 ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
288 {
289 if (span_list.empty())
290 {
291 return true;
292 }
293 else
294 {
295 return size() == 0U;
296 }
297 }
298
299 //*************************************************************************
301 //*************************************************************************
302 ETL_CONSTEXPR14 size_t size_bytes() const ETL_NOEXCEPT
303 {
304 size_t total_n_spans_bytes = 0U;
305
306 for (typename span_list_type::iterator itr = span_list.begin();
307 itr != span_list.end();
308 ++itr)
309 {
310 total_n_spans_bytes += itr->size_bytes();
311 }
312
313 return total_n_spans_bytes;
314 }
315
316 //*************************************************************************
318 //*************************************************************************
319 ETL_CONSTEXPR size_t size_spans() const ETL_NOEXCEPT
320 {
321 return span_list.size();
322 }
323
324 private:
325
326 span_list_type span_list;
327 };
328}
329
330#endif
331
Iterator.
Definition: multi_span.h:66
friend bool operator==(const iterator &lhs, const iterator &rhs)
== operator
Definition: multi_span.h:151
reference operator*()
Definition: multi_span.h:119
pointer operator->()
-> operator
Definition: multi_span.h:135
friend bool operator!=(const iterator &lhs, const iterator &rhs)
!= operator
Definition: multi_span.h:159
Definition: multi_span.h:48
ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
Returns the number of elements in the multi_span.
Definition: multi_span.h:270
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the multi_span size is zero.
Definition: multi_span.h:287
ETL_CONSTEXPR multi_span(span_list_type span_list_)
Constructor.
Definition: multi_span.h:190
ETL_CONSTEXPR multi_span & operator=(const multi_span &other)
Assignment operator.
Definition: multi_span.h:244
ETL_CONSTEXPR14 size_t size_bytes() const ETL_NOEXCEPT
Returns the size of the multi_span.
Definition: multi_span.h:302
ETL_CONSTEXPR size_t size_spans() const ETL_NOEXCEPT
Returns the number of spans in the multi_span.
Definition: multi_span.h:319
Span - Fixed Extent.
Definition: span.h:62
ETL_NODISCARD ETL_CONSTEXPR iterator begin() const ETL_NOEXCEPT
Returns an iterator to the beginning of the span.
Definition: span.h:256
ETL_NODISCARD ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the span size is zero.
Definition: span.h:304
ETL_NODISCARD ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the span.
Definition: span.h:312
ETL_NODISCARD ETL_CONSTEXPR iterator end() const ETL_NOEXCEPT
Returns an iterator to the end of the span.
Definition: span.h:272
ETL_CONSTEXPR17 T * addressof(T &t)
Definition: addressof.h:51
bitset_ext
Definition: absolute.h:38
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: iterator.h:931
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition: iterator.h:961
iterator
Definition: iterator.h:399