Embedded Template Library 1.0
Loading...
Searching...
No Matches
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 //*****************************************
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 //*****************************************
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 //*************************************************************************
128 {
129 return *p_value;
130 }
131
132 //*************************************************************************
134 //*************************************************************************
135 pointer operator ->()
136 {
137 return &operator*();
138 }
139
140 //*************************************************************************
142 //*************************************************************************
144 {
145 return &operator*();
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 while ((p_current != p_end) && p_current->empty())
177 {
178 ++p_current;
179 }
180
181 if (p_current != p_end)
182 {
183 p_value = p_current->begin();
184 }
185 else
186 {
187 p_value = ETL_NULLPTR;
188 }
189 }
190 }
191
192 typedef const span_type* span_list_pointer;
193
194 span_list_pointer p_current;
195 span_list_pointer p_end;
196 pointer p_value;
197 };
198
199 //*************************************************************************
201 //*************************************************************************
203 : span_list(span_list_)
204 {
205 }
206
207 //*************************************************************************
210 //*************************************************************************
211 template <typename TContainer>
212 ETL_CONSTEXPR14 multi_span(TContainer& a) ETL_NOEXCEPT
213 : span_list(a.data(), a.data() + a.size())
214 {
215 }
216
217 //*************************************************************************
220 //*************************************************************************
221 template <typename TContainer>
222 ETL_CONSTEXPR14 multi_span(const TContainer& a) ETL_NOEXCEPT
223 : span_list(a.data(), a.data() + a.size())
224 {
225 }
226
227 //*************************************************************************
229 //*************************************************************************
230 template <typename TIterator>
232 : span_list(etl::addressof(*begin_), etl::distance(begin_, end_))
233 {
234 }
235
236 //*************************************************************************
238 //*************************************************************************
239 template <typename TIterator>
240 ETL_CONSTEXPR14 multi_span(TIterator begin_, size_t length_)
241 : span_list(etl::addressof(*begin_), length_)
242 {
243 }
244
245 //*************************************************************************
247 //*************************************************************************
248 ETL_CONSTEXPR14 multi_span(const multi_span& other)
249 : span_list(other.span_list)
250 {
251 }
252
253 //*************************************************************************
255 //*************************************************************************
256 ETL_CONSTEXPR14 multi_span& operator = (const multi_span & other)
257 {
258 span_list = other.span_list;
259
260 return *this;
261 }
262
263 //*************************************************************************
265 //*************************************************************************
266 ETL_CONSTEXPR14 iterator begin() const
267 {
268 return iterator(span_list.begin(), span_list.end());
269 }
270
271 //*************************************************************************
273 //*************************************************************************
274 ETL_CONSTEXPR14 iterator end() const
275 {
276 return iterator(span_list.end(), span_list.end());
277 }
278
279 //*************************************************************************
281 //*************************************************************************
282 ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
283 {
284 size_t total_n_spans = 0U;
285
286 for (typename span_list_type::iterator itr = span_list.begin();
287 itr != span_list.end();
288 ++itr)
289 {
290 total_n_spans += itr->size();
291 }
292
293 return total_n_spans;
294 }
295
296 //*************************************************************************
298 //*************************************************************************
299 ETL_CONSTEXPR14 bool empty() const ETL_NOEXCEPT
300 {
301 if (span_list.empty())
302 {
303 return true;
304 }
305 else
306 {
307 return size() == 0U;
308 }
309 }
310
311 //*************************************************************************
313 //*************************************************************************
314 ETL_CONSTEXPR14 size_t size_bytes() const ETL_NOEXCEPT
315 {
316 size_t total_n_spans_bytes = 0U;
317
318 for (typename span_list_type::iterator itr = span_list.begin();
319 itr != span_list.end();
320 ++itr)
321 {
322 total_n_spans_bytes += itr->size_bytes();
323 }
324
325 return total_n_spans_bytes;
326 }
327
328 //*************************************************************************
330 //*************************************************************************
331 ETL_CONSTEXPR14 size_t size_spans() const ETL_NOEXCEPT
332 {
333 return span_list.size();
334 }
335
336 private:
337
338 span_list_type span_list;
339 };
340}
341
342#endif
343
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 multi_span(TIterator begin_, size_t length_)
Constructor.
Definition multi_span.h:240
ETL_CONSTEXPR14 multi_span(const multi_span &other)
Copy Constructor.
Definition multi_span.h:248
ETL_CONSTEXPR14 size_t size_spans() const ETL_NOEXCEPT
Returns the number of spans in the multi_span.
Definition multi_span.h:331
ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
Returns the number of elements in the multi_span.
Definition multi_span.h:282
ETL_CONSTEXPR14 multi_span & operator=(const multi_span &other)
Assignment operator.
Definition multi_span.h:256
ETL_CONSTEXPR14 multi_span(const TContainer &a) ETL_NOEXCEPT
Definition multi_span.h:222
ETL_CONSTEXPR14 multi_span(TIterator begin_, TIterator end_)
Constructor.
Definition multi_span.h:231
ETL_CONSTEXPR14 multi_span(span_list_type span_list_)
Constructor.
Definition multi_span.h:202
ETL_CONSTEXPR14 size_t size_bytes() const ETL_NOEXCEPT
Returns the size of the multi_span.
Definition multi_span.h:314
ETL_CONSTEXPR14 multi_span(TContainer &a) ETL_NOEXCEPT
Definition multi_span.h:212
ETL_CONSTEXPR14 bool empty() const ETL_NOEXCEPT
Returns true if the multi_span size is zero.
Definition multi_span.h:299
ETL_NODISCARD ETL_CONSTEXPR iterator begin() const ETL_NOEXCEPT
Returns an iterator to the beginning of the span.
Definition span.h:191
ETL_NODISCARD ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the span size is zero.
Definition span.h:239
ETL_NODISCARD ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the span.
Definition span.h:247
ETL_NODISCARD ETL_CONSTEXPR iterator end() const ETL_NOEXCEPT
Returns an iterator to the end of the span.
Definition span.h:207
ETL_CONSTEXPR17 etl::enable_if<!etl::is_same< T, etl::nullptr_t >::value, T >::type * addressof(T &t)
Definition addressof.h:52
bitset_ext
Definition absolute.h:38
iterator
Definition iterator.h:399
pair holds two objects of arbitrary type
Definition utility.h:164