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