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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 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_ARRAY_VIEW_INCLUDED
#define ETL_ARRAY_VIEW_INCLUDED
#include "platform.h"
#include "memory.h"
#include "array.h"
#include "iterator.h"
#include "error_handler.h"
#include "exception.h"
#include "nullptr.h"
#include "hash.h"
#include "algorithm.h"
#include "type_traits.h"
#if ETL_USING_STL && ETL_USING_CPP11
#include <array>
#endif
///\defgroup array array
/// A wrapper for arrays
///\ingroup containers
namespace etl
{
//***************************************************************************
/// The base class for array_view exceptions.
//***************************************************************************
class array_view_exception : public exception
{
public:
array_view_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
///\ingroup stack
/// The exception thrown when the index is out of bounds.
//***************************************************************************
class array_view_bounds : public array_view_exception
{
public:
array_view_bounds(string_type file_name_, numeric_type line_number_)
: array_view_exception(ETL_ERROR_TEXT("array_view:bounds", ETL_ARRAY_VIEW_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
///\ingroup stack
/// The exception thrown when the view is uninitialised.
//***************************************************************************
class array_view_uninitialised : public array_view_exception
{
public:
array_view_uninitialised(string_type file_name_, numeric_type line_number_)
: array_view_exception(ETL_ERROR_TEXT("array_view:uninitialised", ETL_ARRAY_VIEW_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Array view.
//***************************************************************************
template <typename T>
class array_view
{
public:
typedef T value_type;
typedef size_t size_type;
typedef const T& const_reference;
typedef const T* const_pointer;
typedef const T* const_iterator;
typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
#if defined(ETL_ARRAY_VIEW_IS_MUTABLE)
typedef T* pointer;
typedef T& reference;
typedef T* iterator;
typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
#else
typedef const_pointer pointer;
typedef const_reference reference;
typedef const_pointer iterator;
typedef const_reverse_iterator reverse_iterator;
#endif
//*************************************************************************
/// Default constructor.
//*************************************************************************
ETL_CONSTEXPR array_view() ETL_NOEXCEPT
: mbegin(ETL_NULLPTR),
mend(ETL_NULLPTR)
{
}
#if ETL_USING_CPP11
//*************************************************************************
/// Construct from etl::array.
//*************************************************************************
template <typename U, size_t Size, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
ETL_CONSTEXPR array_view(etl::array<U, Size>& a) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
//*************************************************************************
/// Construct from etl::array.
//*************************************************************************
template <typename U, size_t Size, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
ETL_CONSTEXPR array_view(const etl::array<U, Size>& a) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
#else
//*************************************************************************
/// Construct from etl::array.
//*************************************************************************
template <typename U, size_t Size>
ETL_CONSTEXPR array_view(etl::array<U, Size>& a, typename etl::enable_if<etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<U>::type>::value, void>::type* = 0) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
//*************************************************************************
/// Construct from etl::array.
//*************************************************************************
template <typename U, size_t Size>
ETL_CONSTEXPR array_view(const etl::array<U, Size>& a, typename etl::enable_if<etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<U>::type>::value, void>::type* = 0) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
#endif
#if ETL_USING_STL && ETL_USING_CPP11
//*************************************************************************
/// Construct from std::array.
//*************************************************************************
template <typename U, size_t Size, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
ETL_CONSTEXPR array_view(std::array<U, Size>& a) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
//*************************************************************************
/// Construct from std::array.
//*************************************************************************
template <typename U, size_t Size, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
ETL_CONSTEXPR array_view(const std::array<U, Size>& a) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
#endif
#if ETL_USING_CPP11
//*************************************************************************
/// Construct from a container or other type that supports
/// data() and size() member functions.
//*************************************************************************
template <typename TContainer, typename = typename etl::enable_if<!etl::is_pointer<etl::remove_reference_t<TContainer>>::value &&
!etl::is_array<etl::remove_reference_t<TContainer>>::value &&
etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<typename etl::remove_reference_t<TContainer>::value_type>>::value, void>::type>
ETL_CONSTEXPR array_view(TContainer&& a) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
#else
//*************************************************************************
/// Construct from a container or other type that supports
/// data() and size() member functions.
//*************************************************************************
template <typename TContainer>
ETL_CONSTEXPR array_view(TContainer& a, typename etl::enable_if<!etl::is_pointer<typename etl::remove_reference<TContainer>::type>::value &&
!etl::is_array<TContainer>::value &&
etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<typename etl::remove_reference<TContainer>::type::value_type>::type>::value, void>::type* = 0) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
//*************************************************************************
/// Construct from a container or other type that supports
/// data() and size() member functions.
//*************************************************************************
template <typename TContainer>
ETL_CONSTEXPR array_view(const TContainer& a, typename etl::enable_if<!etl::is_pointer<typename etl::remove_reference<TContainer>::type>::value &&
!etl::is_array<TContainer>::value &&
etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<typename etl::remove_reference<TContainer>::type::value_type>::type>::value, void>::type* = 0) ETL_NOEXCEPT
: mbegin(a.data())
, mend(a.data() + a.size())
{
}
#endif
//*************************************************************************
/// Construct from iterators
//*************************************************************************
template <typename TIterator>
ETL_CONSTEXPR array_view(const TIterator begin_, const TIterator end_) ETL_NOEXCEPT
: mbegin(etl::to_address(begin_)),
mend(etl::to_address(begin_) + etl::distance(begin_, end_))
{
}
//*************************************************************************
/// Construct from iterator and size
//*************************************************************************
template <typename TIterator,
typename TSize>
ETL_CONSTEXPR array_view(const TIterator begin_, const TSize size_) ETL_NOEXCEPT
: mbegin(etl::to_address(begin_)),
mend(etl::to_address(begin_) + size_)
{
}
//*************************************************************************
/// Construct from C array
//*************************************************************************
template<size_t Array_Size>
ETL_CONSTEXPR array_view(T(&begin_)[Array_Size]) ETL_NOEXCEPT
: mbegin(begin_),
mend(begin_ + Array_Size)
{
}
//*************************************************************************
/// Copy constructor
//*************************************************************************
ETL_CONSTEXPR array_view(const array_view& other) ETL_NOEXCEPT
: mbegin(other.mbegin),
mend(other.mend)
{
}
//*************************************************************************
/// Returns a reference to the first element.
//*************************************************************************
reference front()
{
return *mbegin;
}
//*************************************************************************
/// Returns a const reference to the first element.
//*************************************************************************
const_reference front() const
{
return *mbegin;
}
//*************************************************************************
/// Returns a reference to the last element.
//*************************************************************************
reference back()
{
return *(mend - 1);
}
//*************************************************************************
/// Returns a const reference to the last element.
//*************************************************************************
const_reference back() const
{
return *(mend - 1);
}
//*************************************************************************
/// Returns a pointer to the first element of the internal storage.
//*************************************************************************
pointer data() ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
/// Returns a const pointer to the first element of the internal storage.
//*************************************************************************
const_pointer data() const ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
/// Returns an iterator to the beginning of the array.
//*************************************************************************
iterator begin() ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
/// Returns a const iterator to the beginning of the array.
//*************************************************************************
const_iterator begin() const ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
/// Returns a const iterator to the beginning of the array.
//*************************************************************************
const_iterator cbegin() const ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
/// Returns an iterator to the end of the array.
//*************************************************************************
iterator end() ETL_NOEXCEPT
{
return mend;
}
//*************************************************************************
/// Returns a const iterator to the end of the array.
//*************************************************************************
const_iterator end() const ETL_NOEXCEPT
{
return mend;
}
//*************************************************************************
// Returns a const iterator to the end of the array.
//*************************************************************************
const_iterator cend() const ETL_NOEXCEPT
{
return mend;
}
//*************************************************************************
// Returns an reverse iterator to the reverse beginning of the array.
//*************************************************************************
reverse_iterator rbegin() ETL_NOEXCEPT
{
return reverse_iterator(mend);
}
//*************************************************************************
/// Returns a const reverse iterator to the reverse beginning of the array.
//*************************************************************************
const_reverse_iterator rbegin() const ETL_NOEXCEPT
{
return const_reverse_iterator(mend);
}
//*************************************************************************
/// Returns a const reverse iterator to the reverse beginning of the array.
//*************************************************************************
const_reverse_iterator crbegin() const ETL_NOEXCEPT
{
return const_reverse_iterator(mend);
}
//*************************************************************************
/// Returns a reverse iterator to the end of the array.
//*************************************************************************
reverse_iterator rend() ETL_NOEXCEPT
{
return reverse_iterator(mbegin);
}
//*************************************************************************
/// Returns a const reverse iterator to the end of the array.
//*************************************************************************
const_reverse_iterator rend() const ETL_NOEXCEPT
{
return const_reverse_iterator(mbegin);
}
//*************************************************************************
/// Returns a const reverse iterator to the end of the array.
//*************************************************************************
const_reverse_iterator crend() const ETL_NOEXCEPT
{
return const_reverse_iterator(mbegin);
}
//*************************************************************************
/// Returns <b>true</b> if the array size is zero.
//*************************************************************************
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
{
return (mbegin == mend);
}
//*************************************************************************
/// Returns the size of the array.
//*************************************************************************
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
{
return static_cast<size_t>(mend - mbegin);
}
//*************************************************************************
/// Returns the maximum possible size of the array.
//*************************************************************************
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
{
return size();
}
//*************************************************************************
/// Assign from a view.
//*************************************************************************
array_view& operator=(const array_view& other) ETL_NOEXCEPT
{
mbegin = other.mbegin;
mend = other.mend;
return *this;
}
//*************************************************************************
/// Assign from iterators
//*************************************************************************
template <typename TIterator>
void assign(const TIterator begin_, const TIterator end_)
{
mbegin = etl::to_address(begin_);
mend = etl::to_address(begin_) + etl::distance(begin_, end_);
}
//*************************************************************************
/// Assign from iterator and size.
//*************************************************************************
template <typename TIterator,
typename TSize>
void assign(const TIterator begin_, const TSize size_)
{
mbegin = etl::to_address(begin_);
mend = etl::to_address(begin_) + size_;
}
#if defined(ETL_ARRAY_VIEW_IS_MUTABLE)
//*************************************************************************
/// Returns a reference to the indexed value.
//*************************************************************************
reference operator[](const size_t i) ETL_NOEXCEPT
{
return mbegin[i];
}
#endif
//*************************************************************************
/// Returns a const reference to the indexed value.
//*************************************************************************
const_reference operator[](const size_t i) const ETL_NOEXCEPT
{
return mbegin[i];
}
#if defined(ETL_ARRAY_VIEW_IS_MUTABLE)
//*************************************************************************
/// Returns a reference to the indexed value.
//*************************************************************************
reference at(const size_t i)
{
ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(array_view_uninitialised));
ETL_ASSERT(i < size(), ETL_ERROR(array_view_bounds));
return mbegin[i];
}
#endif
//*************************************************************************
/// Returns a const reference to the indexed value.
//*************************************************************************
const_reference at(const size_t i) const
{
ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(array_view_uninitialised));
ETL_ASSERT(i < size(), ETL_ERROR(array_view_bounds));
return mbegin[i];
}
//*************************************************************************
/// Swaps with another array_view.
//*************************************************************************
void swap(array_view& other) ETL_NOEXCEPT
{
using ETL_OR_STD::swap; // Allow ADL
swap(mbegin, other.mbegin);
swap(mend, other.mend);
}
//*************************************************************************
/// Shrinks the view by moving its start forward.
//*************************************************************************
void remove_prefix(const size_type n) ETL_NOEXCEPT
{
if (n < size())
mbegin += n;
else
mbegin = mend;
}
//*************************************************************************
/// Shrinks the view by moving its end backward.
//*************************************************************************
void remove_suffix(const size_type n) ETL_NOEXCEPT
{
if (n < size())
mend -= n;
else
mend = mbegin;
}
//*************************************************************************
/// Fills the array.
//*************************************************************************
void fill(const T& value)
{
etl::fill(begin(), end(), value);
}
//*************************************************************************
/// Equality for array views.
//*************************************************************************
friend bool operator == (const array_view<T>& lhs, const array_view<T>& rhs)
{
return (lhs.size() == rhs.size()) &&
etl::equal(lhs.begin(), lhs.end(), rhs.begin());
}
//*************************************************************************
/// Inequality for array views.
//*************************************************************************
friend bool operator != (const array_view<T>& lhs, const array_view<T>& rhs)
{
return !(lhs == rhs);
}
//*************************************************************************
/// Less-than for array views.
//*************************************************************************
friend bool operator < (const array_view<T>& lhs, const array_view<T>& rhs)
{
return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
//*************************************************************************
/// Greater-than for array views.
//*************************************************************************
friend bool operator > (const array_view<T>& lhs, const array_view<T>& rhs)
{
return rhs < lhs;
}
//*************************************************************************
/// Less-than-equal for array views.
//*************************************************************************
friend bool operator <= (const array_view<T>& lhs, const array_view<T>& rhs)
{
return !(lhs > rhs);
}
//*************************************************************************
/// Greater-than-equal for array views.
//*************************************************************************
friend bool operator >= (const array_view<T>& lhs, const array_view<T>& rhs)
{
return !(lhs < rhs);
}
private:
pointer mbegin;
pointer mend;
};
//*************************************************************************
/// Template deduction guides.
//*************************************************************************
#if ETL_USING_CPP17
template <typename TArray>
array_view(TArray& a)
-> array_view<typename TArray::value_type>;
template <typename TIterator>
array_view(const TIterator begin_, const TIterator end_)
-> array_view<etl::remove_pointer_t<TIterator>>;
template <typename TIterator,
typename TSize>
array_view(const TIterator begin_, const TSize size_)
-> array_view<etl::remove_pointer_t<TIterator>>;
#endif
//*************************************************************************
/// Hash function.
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <typename T>
struct hash<etl::array_view<T> >
{
size_t operator()(const etl::array_view<T>& view) const
{
return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(view.data()),
reinterpret_cast<const uint8_t*>(view.data() + view.size()));
}
};
#endif
}
//*************************************************************************
/// Swaps the values.
//*************************************************************************
template <typename T>
void swap(etl::array_view<T>& lhs, etl::array_view<T>& rhs) ETL_NOEXCEPT
{
lhs.swap(rhs);
}
#endif
|