mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Added array_view
This commit is contained in:
parent
8045ae55c4
commit
835a5cbad0
679
src/array_view.h
Normal file
679
src/array_view.h
Normal file
@ -0,0 +1,679 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 jwellbelove
|
||||
|
||||
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__
|
||||
#define __ETL_ARRAY_VIEW__
|
||||
|
||||
#include "platform.h"
|
||||
#include "memory.h"
|
||||
#include "iterator.h"
|
||||
#include "error_handler.h"
|
||||
#include "exception.h"
|
||||
#include "hash.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
///\defgroup array array
|
||||
/// A wrapper for arrays
|
||||
///\ingroup containers
|
||||
|
||||
#undef ETL_FILE
|
||||
#define ETL_FILE "41"
|
||||
|
||||
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 stack is full.
|
||||
//***************************************************************************
|
||||
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_FILE"A"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Array view.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
class array_view
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T* iterator;
|
||||
typedef const T* const_iterator;
|
||||
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from std::array or etl::array or other type that supports
|
||||
/// data() and size() member functions.
|
||||
//*************************************************************************
|
||||
template <typename TArray,
|
||||
typename TDummy = typename etl::enable_if<!etl::is_same<T, TArray>::value, void>::type>
|
||||
array_view(TArray& a)
|
||||
: mbegin(a.data()),
|
||||
mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from iterators
|
||||
//*************************************************************************
|
||||
template <typename TIterator,
|
||||
typename TDummy = typename etl::enable_if<etl::is_random_iterator<TIterator>::value, void>::type>
|
||||
array_view(TIterator begin_, TIterator end_)
|
||||
: mbegin(etl::addressof(*begin_)),
|
||||
mend(etl::addressof(*begin_) + std::distance(begin_, end_))
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from C array
|
||||
//*************************************************************************
|
||||
template <typename TIterator,
|
||||
typename TSize,
|
||||
typename TDummy = typename etl::enable_if<etl::is_random_iterator<TIterator>::value, void>::type>
|
||||
array_view(TIterator begin_, TSize size_)
|
||||
: mbegin(etl::addressof(*begin_)),
|
||||
mend(etl::addressof(*begin_) + size_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from C array
|
||||
//*************************************************************************
|
||||
template<const size_t ARRAY_SIZE>
|
||||
array_view(T(&begin_)[ARRAY_SIZE])
|
||||
: mbegin(begin_),
|
||||
mend(begin_ + ARRAY_SIZE)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor
|
||||
//*************************************************************************
|
||||
array_view(const array_view& other)
|
||||
: 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()
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const pointer to the first element of the internal storage.
|
||||
//*************************************************************************
|
||||
const_pointer data() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
iterator begin()
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
const_iterator begin() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
const_iterator cbegin() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
iterator end()
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_iterator end() const
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_iterator cend() const
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns an reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
reverse_iterator rbegin()
|
||||
{
|
||||
return reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator rbegin() const
|
||||
{
|
||||
return const_reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator crbegin() const
|
||||
{
|
||||
return const_reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
reverse_iterator rend()
|
||||
{
|
||||
return reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator rend() const
|
||||
{
|
||||
return const_reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator crend() const
|
||||
{
|
||||
return const_reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Capacity
|
||||
//*************************************************************************
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns <b>true</b> if the array size is zero.
|
||||
//*************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return (mbegin == mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the size of the array.
|
||||
//*************************************************************************
|
||||
size_t size() const
|
||||
{
|
||||
return (mend - mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum possible size of the array.
|
||||
//*************************************************************************
|
||||
size_t max_size() const
|
||||
{
|
||||
return size();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the indexed value.
|
||||
//*************************************************************************
|
||||
reference operator[](size_t i)
|
||||
{
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
const_reference operator[](size_t i) const
|
||||
{
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the indexed value.
|
||||
//*************************************************************************
|
||||
reference at(size_t i)
|
||||
{
|
||||
ETL_ASSERT(i < size(), ETL_ERROR(array_view_bounds));
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
const_reference at(size_t i) const
|
||||
{
|
||||
ETL_ASSERT(i < size(), ETL_ERROR(array_view_bounds));
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Equality for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator == (const array_view<T>& lhs, const array_view<T>& rhs)
|
||||
{
|
||||
return (lhs.size() == rhs.size()) &&
|
||||
std::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 std::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:
|
||||
|
||||
// Disabled.
|
||||
array_view();
|
||||
|
||||
T* mbegin;
|
||||
T* mend;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Constant array view.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
class const_array_view
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef const T& const_reference;
|
||||
typedef const T* const_pointer;
|
||||
typedef const T* const_iterator;
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from std::array or etl::array or other type that supports
|
||||
/// data() and size() member functions.
|
||||
//*************************************************************************
|
||||
template <typename TArray,
|
||||
typename TDummy = typename etl::enable_if<!etl::is_same<T, TArray>::value, void>::type>
|
||||
const_array_view(TArray& a)
|
||||
: mbegin(a.data()),
|
||||
mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from iterators
|
||||
//*************************************************************************
|
||||
template <typename TIterator,
|
||||
typename TDummy = typename etl::enable_if<etl::is_random_iterator<TIterator>::value, void>::type>
|
||||
const_array_view(TIterator begin_, TIterator end_)
|
||||
: mbegin(etl::addressof(*begin_)),
|
||||
mend(etl::addressof(*begin_) + std::distance(begin_, end_))
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from C array
|
||||
//*************************************************************************
|
||||
template <typename TIterator,
|
||||
typename TSize, typename TDummy = typename etl::enable_if<etl::is_random_iterator<TIterator>::value, void>::type>
|
||||
const_array_view(TIterator begin_, TSize size_)
|
||||
: mbegin(etl::addressof(*begin_)),
|
||||
mend(etl::addressof(*begin_) + size_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from C array
|
||||
//*************************************************************************
|
||||
template<const size_t ARRAY_SIZE>
|
||||
const_array_view(const T(&begin_)[ARRAY_SIZE])
|
||||
: mbegin(begin_),
|
||||
mend(begin_ + ARRAY_SIZE)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor
|
||||
//*************************************************************************
|
||||
const_array_view(const const_array_view& other)
|
||||
: mbegin(other.mbegin),
|
||||
mend(other.mend)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the first element.
|
||||
//*************************************************************************
|
||||
const_reference front() const
|
||||
{
|
||||
return *mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the last element.
|
||||
//*************************************************************************
|
||||
const_reference back() const
|
||||
{
|
||||
return *(mend - 1);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const pointer to the first element of the internal storage.
|
||||
//*************************************************************************
|
||||
const_pointer data() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
const_iterator begin() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
const_iterator cbegin() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_iterator end() const
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_iterator cend() const
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator rbegin() const
|
||||
{
|
||||
return const_reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator crbegin() const
|
||||
{
|
||||
return const_reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator rend() const
|
||||
{
|
||||
return const_reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator crend() const
|
||||
{
|
||||
return const_reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Capacity
|
||||
//*************************************************************************
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns <b>true</b> if the array size is zero.
|
||||
//*************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return (mbegin == mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the size of the array.
|
||||
//*************************************************************************
|
||||
size_t size() const
|
||||
{
|
||||
return (mend - mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum possible size of the array.
|
||||
//*************************************************************************
|
||||
size_t max_size() const
|
||||
{
|
||||
return size();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
const_reference operator[](size_t i) const
|
||||
{
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
const_reference at(size_t i) const
|
||||
{
|
||||
ETL_ASSERT(i < size(), ETL_ERROR(array_view_bounds));
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Equality for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator == (const const_array_view<T>& lhs, const const_array_view<T>& rhs)
|
||||
{
|
||||
return (lhs.size() == rhs.size()) &&
|
||||
std::equal(lhs.begin(), lhs.end(), rhs.begin());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Inequality for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator != (const const_array_view<T>& lhs, const const_array_view<T>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less-than for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator < (const const_array_view<T>& lhs, const const_array_view<T>& rhs)
|
||||
{
|
||||
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater-than for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator > (const const_array_view<T>& lhs, const const_array_view<T>& rhs)
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less-than-equal for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator <= (const const_array_view<T>& lhs, const const_array_view<T>& rhs)
|
||||
{
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater-than-equal for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator >= (const const_array_view<T>& lhs, const const_array_view<T>& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// Disabled.
|
||||
const_array_view();
|
||||
|
||||
const T* mbegin;
|
||||
const T* mend;
|
||||
};
|
||||
}
|
||||
|
||||
#undef ETL_FILE
|
||||
|
||||
#endif
|
||||
|
||||
@ -38,3 +38,4 @@
|
||||
38 message
|
||||
39 message_bus
|
||||
40 factory / variant_pool
|
||||
41 array_view
|
||||
|
||||
458
test/test_array_view.cpp
Normal file
458
test/test_array_view.cpp
Normal file
@ -0,0 +1,458 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 jwellbelove
|
||||
|
||||
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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "UnitTest++.h"
|
||||
|
||||
#include "array_view.h"
|
||||
#include "array.h"
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
namespace
|
||||
{
|
||||
SUITE(test_array_view)
|
||||
{
|
||||
static const size_t SIZE = 10;
|
||||
|
||||
typedef etl::array<int, SIZE> EtlData;
|
||||
typedef std::array<int, SIZE> StlData;
|
||||
typedef std::vector<int> StlVData;
|
||||
|
||||
typedef etl::array_view<int> View;
|
||||
typedef etl::const_array_view<int> CView;
|
||||
|
||||
EtlData etldata = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
StlData stldata = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
StlVData stlvdata = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
EtlData etldatasmaller = { 0, 1, 2, 3, 4, 5, 5, 7, 8, 9 };
|
||||
EtlData etldatashorter = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
|
||||
|
||||
EtlData etloriginal = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
EtlData etlmodified = { 0, 1, 10, 10, 10, 10, 10, 10, 8, 9 };
|
||||
|
||||
const EtlData cetldata = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
const StlData cstldata = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
const StlVData cstlvdata = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
int cdata[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
int* pcdata = cdata;
|
||||
|
||||
const int ccdata[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
const int* pccdata = ccdata;
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_etl_array_1)
|
||||
{
|
||||
View view(etldata);
|
||||
|
||||
CHECK_EQUAL(etldata.size(), view.size());
|
||||
CHECK_EQUAL(etldata.max_size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), etldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_etl_array_1_const)
|
||||
{
|
||||
CView view(cetldata);
|
||||
|
||||
CHECK_EQUAL(cetldata.size(), view.size());
|
||||
CHECK_EQUAL(cetldata.max_size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), cetldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_etl_array_2)
|
||||
{
|
||||
View view(etldata.begin(), etldata.end());
|
||||
|
||||
CHECK_EQUAL(SIZE, view.size());
|
||||
CHECK_EQUAL(SIZE, view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), etldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_etl_array_2_const)
|
||||
{
|
||||
CView view(cetldata.begin(), cetldata.end());
|
||||
|
||||
CHECK_EQUAL(SIZE, view.size());
|
||||
CHECK_EQUAL(SIZE, view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), cetldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_stl_array_1)
|
||||
{
|
||||
View view(stldata);
|
||||
|
||||
CHECK_EQUAL(stldata.size(), view.size());
|
||||
CHECK_EQUAL(stldata.max_size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), stldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_stl_array_1_const)
|
||||
{
|
||||
CView view(cstldata);
|
||||
|
||||
CHECK_EQUAL(cstldata.size(), view.size());
|
||||
CHECK_EQUAL(cstldata.max_size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), cstldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_stl_array_2)
|
||||
{
|
||||
View view(stldata.begin(), stldata.end());
|
||||
|
||||
CHECK_EQUAL(stldata.size(), view.size());
|
||||
CHECK_EQUAL(stldata.max_size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), stldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_stl_array_2_const)
|
||||
{
|
||||
CView view(cstldata.begin(), cstldata.end());
|
||||
|
||||
CHECK_EQUAL(cstldata.size(), view.size());
|
||||
CHECK_EQUAL(cstldata.max_size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), cstldata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_stl_vector_1)
|
||||
{
|
||||
View view(stlvdata);
|
||||
|
||||
CHECK_EQUAL(stlvdata.size(), view.size());
|
||||
CHECK_EQUAL(stlvdata.size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), stlvdata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_stl_vector_1_const)
|
||||
{
|
||||
CView view(cstlvdata);
|
||||
|
||||
CHECK_EQUAL(cstlvdata.size(), view.size());
|
||||
CHECK_EQUAL(cstlvdata.size(), view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), cstlvdata.begin());
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_c_array_1)
|
||||
{
|
||||
View view(pcdata, SIZE);
|
||||
|
||||
CHECK_EQUAL(SIZE, view.size());
|
||||
CHECK_EQUAL(SIZE, view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), pcdata);
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_c_array_1_const)
|
||||
{
|
||||
CView view(pccdata, SIZE);
|
||||
|
||||
CHECK_EQUAL(SIZE, view.size());
|
||||
CHECK_EQUAL(SIZE, view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), pccdata);
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_c_array_2)
|
||||
{
|
||||
View view(cdata);
|
||||
|
||||
CHECK_EQUAL(SIZE, view.size());
|
||||
CHECK_EQUAL(SIZE, view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), cdata);
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_c_array_2_const)
|
||||
{
|
||||
CView view(ccdata);
|
||||
|
||||
CHECK_EQUAL(SIZE, view.size());
|
||||
CHECK_EQUAL(SIZE, view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), ccdata);
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_range)
|
||||
{
|
||||
View view(etldata.begin() + 2, etldata.end() - 2);
|
||||
|
||||
CHECK_EQUAL(etldata.size() - 4, view.size());
|
||||
CHECK_EQUAL(etldata.size() - 4, view.max_size());
|
||||
|
||||
bool isEqual = std::equal(view.begin(), view.end(), etldata.begin() + 2);
|
||||
CHECK(isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modify_range)
|
||||
{
|
||||
View view(etloriginal.begin() + 2, etloriginal.end() - 2);
|
||||
|
||||
CHECK_EQUAL(etloriginal.size() - 4, view.size());
|
||||
CHECK_EQUAL(etloriginal.size() - 4, view.max_size());
|
||||
|
||||
std::fill(view.begin(), view.end(), 10);
|
||||
|
||||
bool isEqual;
|
||||
|
||||
isEqual = std::equal(etloriginal.begin(), etloriginal.end(), etlmodified.begin());
|
||||
CHECK(isEqual);
|
||||
|
||||
isEqual = std::equal(etldata.begin(), etldata.end(), etlmodified.begin());
|
||||
CHECK(!isEqual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_begin_end)
|
||||
{
|
||||
View view(etldata.begin(), etldata.begin());
|
||||
CView cview(etldata.begin(), etldata.begin());
|
||||
|
||||
CHECK_EQUAL(view.begin(), view.cbegin());
|
||||
CHECK_EQUAL(cview.begin(), cview.cbegin());
|
||||
|
||||
CHECK_EQUAL(view.rbegin().base(), view.crbegin().base());
|
||||
CHECK_EQUAL(cview.rbegin().base(), cview.crbegin().base());
|
||||
|
||||
CHECK_EQUAL(view.end(), view.cend());
|
||||
CHECK_EQUAL(cview.end(), cview.cend());
|
||||
|
||||
CHECK_EQUAL(view.rend().base(), view.crend().base());
|
||||
CHECK_EQUAL(cview.rend().base(), cview.crend().base());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_front_back)
|
||||
{
|
||||
View view(etldata.begin(), etldata.end());
|
||||
CView cview(etldata.begin(), etldata.end());
|
||||
|
||||
CHECK_EQUAL(etldata.front(), view.front());
|
||||
CHECK_EQUAL(etldata.front(), cview.front());
|
||||
|
||||
CHECK_EQUAL(etldata.back(), view.back());
|
||||
CHECK_EQUAL(etldata.back(), cview.back());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_data)
|
||||
{
|
||||
View view(etldata.begin(), etldata.end());
|
||||
CView cview(etldata.begin(), etldata.end());
|
||||
|
||||
CHECK_EQUAL(etldata.data(), view.data());
|
||||
CHECK_EQUAL(etldata.data(), cview.data());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_index_operator)
|
||||
{
|
||||
View view(etldata.begin(), etldata.end());
|
||||
CView cview(etldata.begin(), etldata.end());
|
||||
|
||||
for (size_t i = 0; i < etldata.size(); ++i)
|
||||
{
|
||||
CHECK_EQUAL(etldata[i], view[i]);
|
||||
CHECK_EQUAL(etldata[i], cview[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_at)
|
||||
{
|
||||
View view(etldata.begin(), etldata.end());
|
||||
CView cview(etldata.begin(), etldata.end());
|
||||
|
||||
for (size_t i = 0; i < etldata.size(); ++i)
|
||||
{
|
||||
CHECK_EQUAL(etldata[i], view.at(i));
|
||||
CHECK_EQUAL(etldata[i], cview.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_at_exception)
|
||||
{
|
||||
View view(etldata.begin(), etldata.end());
|
||||
CView cview(etldata.begin(), etldata.end());
|
||||
|
||||
CHECK_THROW(view.at(view.size()), etl::array_view_bounds);
|
||||
CHECK_THROW(cview.at(cview.size()), etl::array_view_bounds);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_non_member_same)
|
||||
{
|
||||
View view1(etldata.begin(), etldata.end());
|
||||
View view2(etldata.begin(), etldata.end());
|
||||
|
||||
CView cview1(etldata.begin(), etldata.end());
|
||||
CView cview2(etldata.begin(), etldata.end());
|
||||
|
||||
CHECK(view1 == view2);
|
||||
CHECK(cview1 == cview2);
|
||||
|
||||
CHECK(view1 <= view2);
|
||||
CHECK(cview1 <= cview2);
|
||||
|
||||
CHECK(view1 >= view2);
|
||||
CHECK(cview1 >= cview2);
|
||||
|
||||
CHECK(!(view1 > view2));
|
||||
CHECK(!(cview1 > cview2));
|
||||
|
||||
CHECK(!(view1 < view2));
|
||||
CHECK(!(cview1 < cview2));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_non_member_smaller)
|
||||
{
|
||||
View view1(etldata.begin(), etldata.end());
|
||||
View view2(etldatasmaller.begin(), etldatasmaller.end());
|
||||
|
||||
CView cview1(etldata.begin(), etldata.end());
|
||||
CView cview2(etldatasmaller.begin(), etldatasmaller.end());
|
||||
|
||||
CHECK(!(view1 == view2));
|
||||
CHECK(!(cview1 == cview2));
|
||||
|
||||
CHECK(!(view1 <= view2));
|
||||
CHECK(!(cview1 <= cview2));
|
||||
|
||||
CHECK(view2 <= view1);
|
||||
CHECK(cview2 <= cview1);
|
||||
|
||||
CHECK(view1 >= view2);
|
||||
CHECK(cview1 >= cview2);
|
||||
|
||||
CHECK(!(view2 >= view1));
|
||||
CHECK(!(cview2 >= cview1));
|
||||
|
||||
CHECK(view1 > view2);
|
||||
CHECK(cview1 > cview2);
|
||||
|
||||
CHECK(!(view2 > view1));
|
||||
CHECK(!(cview2 > cview1));
|
||||
|
||||
CHECK(!(view1 < view2));
|
||||
CHECK(!(cview1 < cview2));
|
||||
|
||||
CHECK(view2 < view1);
|
||||
CHECK(cview2 < cview1);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_non_member_shorter)
|
||||
{
|
||||
View view1(etldata.begin(), etldata.end());
|
||||
View view2(etldatashorter.begin(), etldatashorter.end());
|
||||
|
||||
CView cview1(etldata.begin(), etldata.end());
|
||||
CView cview2(etldatashorter.begin(), etldatashorter.end());
|
||||
|
||||
CHECK(!(view1 == view2));
|
||||
CHECK(!(cview1 == cview2));
|
||||
|
||||
CHECK(!(view1 <= view2));
|
||||
CHECK(!(cview1 <= cview2));
|
||||
|
||||
CHECK(view2 <= view1);
|
||||
CHECK(cview2 <= cview1);
|
||||
|
||||
CHECK(view1 >= view2);
|
||||
CHECK(cview1 >= cview2);
|
||||
|
||||
CHECK(!(view2 >= view1));
|
||||
CHECK(!(cview2 >= cview1));
|
||||
|
||||
CHECK(view1 > view2);
|
||||
CHECK(cview1 > cview2);
|
||||
|
||||
CHECK(!(view2 > view1));
|
||||
CHECK(!(cview2 > cview1));
|
||||
|
||||
CHECK(!(view1 < view2));
|
||||
CHECK(!(cview1 < cview2));
|
||||
|
||||
CHECK(view2 < view1);
|
||||
CHECK(cview2 < cview1);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_empty)
|
||||
{
|
||||
View view1(etldata.begin(), etldata.begin());
|
||||
CHECK(view1.empty());
|
||||
|
||||
View view2(etldata.begin(), etldata.begin() + 1);
|
||||
CHECK(!view2.empty());
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -130,6 +130,7 @@
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\src\array_view.h" />
|
||||
<ClInclude Include="..\..\src\atomic.h" />
|
||||
<ClInclude Include="..\..\src\atomic\atomic_arm.h" />
|
||||
<ClInclude Include="..\..\src\atomic\atomic_gcc.h" />
|
||||
@ -337,6 +338,7 @@
|
||||
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">../../../unittest-cpp</AdditionalIncludeDirectories>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_array_view.cpp" />
|
||||
<ClCompile Include="..\test_binary.cpp" />
|
||||
<ClCompile Include="..\test_bitset.cpp" />
|
||||
<ClCompile Include="..\test_bloom_filter.cpp" />
|
||||
|
||||
@ -561,6 +561,9 @@
|
||||
<ClInclude Include="..\..\src\variant_pool.h">
|
||||
<Filter>ETL\Containers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\array_view.h">
|
||||
<Filter>ETL\Containers</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\main.cpp">
|
||||
@ -935,6 +938,9 @@
|
||||
<ClCompile Include="..\test_variant_pool.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_array_view.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\Doxyfile">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user