PR for constexpr for etl::array

This commit is contained in:
John Wellbelove 2023-06-28 12:51:06 +01:00
parent 2d41441f02
commit 197e0815e6
2 changed files with 187 additions and 12 deletions

View File

@ -114,6 +114,7 @@ namespace etl
/// Returns a reference to the value at index 'i'.
///\param i The index of the element to access.
//*************************************************************************
ETL_NODISCARD
reference at(size_t i)
{
ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));
@ -125,6 +126,12 @@ namespace etl
/// Returns a const reference to the value at index 'i'.
///\param i The index of the element to access.
//*************************************************************************
ETL_NODISCARD
#if defined(ETL_THROW_EXCEPTIONS)
ETL_CONSTEXPR14
#else
ETL_CONSTEXPR
#endif
const_reference at(size_t i) const
{
ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));
@ -137,7 +144,8 @@ namespace etl
/// Returns a reference to the value at index 'i'.
///\param i The index of the element to access.
//*************************************************************************
ETL_CONSTEXPR14 reference operator[](size_t i)
ETL_NODISCARD
reference operator[](size_t i)
{
return _buffer[i];
}
@ -147,6 +155,7 @@ namespace etl
/// Returns a const reference to the value at index 'i'.
///\param i The index of the element to access.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reference operator[](size_t i) const
{
return _buffer[i];
@ -155,7 +164,8 @@ namespace etl
//*************************************************************************
/// Returns a reference to the first element.
//*************************************************************************
ETL_CONSTEXPR14 reference front()
ETL_NODISCARD
reference front()
{
return _buffer[0];
}
@ -163,6 +173,7 @@ namespace etl
//*************************************************************************
/// Returns a const reference to the first element.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reference front() const
{
return _buffer[0];
@ -171,7 +182,8 @@ namespace etl
//*************************************************************************
/// Returns a reference to the last element.
//*************************************************************************
ETL_CONSTEXPR14 reference back()
ETL_NODISCARD
reference back()
{
return _buffer[SIZE - 1];
}
@ -179,6 +191,7 @@ namespace etl
//*************************************************************************
/// Returns a const reference to the last element.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reference back() const
{
return _buffer[SIZE - 1];
@ -187,7 +200,8 @@ namespace etl
//*************************************************************************
/// Returns a pointer to the first element of the internal buffer.
//*************************************************************************
ETL_CONSTEXPR14 pointer data() ETL_NOEXCEPT
ETL_NODISCARD
pointer data() ETL_NOEXCEPT
{
return &_buffer[0];
}
@ -195,6 +209,7 @@ namespace etl
//*************************************************************************
/// Returns a const pointer to the first element of the internal buffer.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
{
return &_buffer[0];
@ -207,7 +222,8 @@ namespace etl
//*************************************************************************
/// Returns an iterator to the beginning of the array.
//*************************************************************************
ETL_CONSTEXPR14 iterator begin() ETL_NOEXCEPT
ETL_NODISCARD
iterator begin() ETL_NOEXCEPT
{
return &_buffer[0];
}
@ -215,6 +231,7 @@ namespace etl
//*************************************************************************
/// Returns a const iterator to the beginning of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
{
return &_buffer[0];
@ -223,6 +240,7 @@ namespace etl
//*************************************************************************
/// Returns a const iterator to the beginning of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
{
return begin();
@ -231,7 +249,8 @@ namespace etl
//*************************************************************************
/// Returns an iterator to the end of the array.
//*************************************************************************
ETL_CONSTEXPR14 iterator end() ETL_NOEXCEPT
ETL_NODISCARD
iterator end() ETL_NOEXCEPT
{
return _buffer + SIZE;
}
@ -239,6 +258,7 @@ namespace etl
//*************************************************************************
/// Returns a const iterator to the end of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
{
return _buffer + SIZE;
@ -247,6 +267,7 @@ namespace etl
//*************************************************************************
// Returns a const iterator to the end of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT
{
return _buffer + SIZE;
@ -255,7 +276,8 @@ namespace etl
//*************************************************************************
// Returns an reverse iterator to the reverse beginning of the array.
//*************************************************************************
ETL_CONSTEXPR14 reverse_iterator rbegin() ETL_NOEXCEPT
ETL_NODISCARD
reverse_iterator rbegin() ETL_NOEXCEPT
{
return reverse_iterator(end());
}
@ -263,6 +285,7 @@ namespace etl
//*************************************************************************
/// Returns a const reverse iterator to the reverse beginning of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
{
return const_reverse_iterator(end());
@ -271,6 +294,7 @@ namespace etl
//*************************************************************************
/// Returns a const reverse iterator to the reverse beginning of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
{
return const_reverse_iterator(end());
@ -279,7 +303,8 @@ namespace etl
//*************************************************************************
/// Returns a reverse iterator to the end of the array.
//*************************************************************************
ETL_CONSTEXPR14 reverse_iterator rend() ETL_NOEXCEPT
ETL_NODISCARD
reverse_iterator rend() ETL_NOEXCEPT
{
return reverse_iterator(begin());
}
@ -287,6 +312,7 @@ namespace etl
//*************************************************************************
/// Returns a const reverse iterator to the end of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
{
return const_reverse_iterator(begin());
@ -295,6 +321,7 @@ namespace etl
//*************************************************************************
/// Returns a const reverse iterator to the end of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
{
return const_reverse_iterator(begin());
@ -307,6 +334,7 @@ namespace etl
//*************************************************************************
/// Returns <b>true</b> if the array size is zero.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
{
return (SIZE == 0);
@ -315,6 +343,7 @@ namespace etl
//*************************************************************************
/// Returns the size of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
{
return SIZE;
@ -323,6 +352,7 @@ namespace etl
//*************************************************************************
/// Returns the maximum possible size of the array.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
{
return SIZE;
@ -338,14 +368,14 @@ namespace etl
//*************************************************************************
ETL_CONSTEXPR14 void fill(parameter_t value)
{
etl::fill(begin(), end(), value);
etl::fill(_buffer, _buffer + SIZE, value);
}
//*************************************************************************
/// Swaps the contents of this array and another.
///\param other A reference to the other array.
//*************************************************************************
void swap(array& other) ETL_NOEXCEPT
ETL_CONSTEXPR14 void swap(array& other) ETL_NOEXCEPT
{
using ETL_OR_STD::swap; // Allow ADL

View File

@ -97,7 +97,7 @@ namespace
CHECK_EQUAL(data.at(i), compare_data.at(i));
}
CHECK_THROW(data.at(data.size()), etl::array_out_of_range);
CHECK_THROW({ int d = data.at(data.size()); (void)d; }, etl::array_out_of_range);
}
//*************************************************************************
@ -110,7 +110,7 @@ namespace
CHECK_EQUAL(data.at(i), compare_data.at(i));
}
CHECK_THROW(data.at(data.size()), etl::array_out_of_range);
CHECK_THROW({ int d = data.at(data.size()); (void)d; }, etl::array_out_of_range);
}
//*************************************************************************
@ -737,5 +737,150 @@ namespace
CHECK_EQUAL(Moveable(9), data[9]);
}
#endif
#if ETL_USING_CPP14
//*************************************************************************
using Array = etl::array<int, 10U>;
//*********************************
constexpr int BeginEnd(const Array& data) noexcept
{
return *(data.begin() + 5);
}
//*********************************
constexpr int CBeginCEnd(const Array& data) noexcept
{
return *(data.cbegin() + 5);
}
#if ETL_USING_CPP20 && ETL_USING_STL
//*********************************
constexpr int RBeginREnd(const Array& data) noexcept
{
return *(data.rbegin() + 5);
}
//*********************************
constexpr int CRBeginCREnd(const Array& data) noexcept
{
return *(data.crbegin() + 5);
}
#endif
//*********************************
constexpr int DataSize(const Array& data) noexcept
{
return *(data.data() + 5);
}
//*********************************
constexpr Array Fill(int i) noexcept
{
Array a{};
a.fill(i);
return a;
}
//*********************************
#if ETL_USING_CPP20 && ETL_USING_STL
constexpr Array Swap(Array data1, Array data2) noexcept
{
data1.swap(data2);
return data1;
}
#endif
TEST(test_cpp14_constexpr)
{
constexpr Array data{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// [] operator
constexpr int i0 = data[0];
constexpr int i1 = data[1];
constexpr int i2 = data[2];
constexpr int i3 = data[3];
constexpr int i4 = data[4];
constexpr int i5 = data[5];
constexpr int i6 = data[6];
constexpr int i7 = data[7];
constexpr int i8 = data[8];
constexpr int i9 = data[9];
CHECK_EQUAL(data[0], i0);
CHECK_EQUAL(data[1], i1);
CHECK_EQUAL(data[2], i2);
CHECK_EQUAL(data[3], i3);
CHECK_EQUAL(data[4], i4);
CHECK_EQUAL(data[5], i5);
CHECK_EQUAL(data[6], i6);
CHECK_EQUAL(data[7], i7);
CHECK_EQUAL(data[8], i8);
CHECK_EQUAL(data[9], i9);
// front & back
constexpr int f0 = data.front();
constexpr int b9 = data.back();
CHECK_EQUAL(data[0], f0);
CHECK_EQUAL(data[9], b9);
// begin & end
constexpr int b5 = BeginEnd(data);
CHECK_EQUAL(data[5], b5);
// cbegin & cend
constexpr int cb5 = CBeginCEnd(data);
CHECK_EQUAL(data[5], cb5);
#if ETL_USING_CPP20 && ETL_USING_STL
// rbegin & rend
constexpr int rb5 = RBeginREnd(data);
CHECK_EQUAL(data[4], rb5);
// crbegin & crend
constexpr int crb5 = CRBeginCREnd(data);
CHECK_EQUAL(data[4], crb5);
#endif
// data
constexpr int d5 = DataSize(data);
CHECK_EQUAL(data[5], d5);
// empty
constexpr bool e = data.empty();
CHECK_FALSE(e);
// size
constexpr size_t s = data.size();
CHECK_EQUAL(data.size(), s);
// max_size
constexpr size_t ms = data.max_size();
CHECK_EQUAL(data.max_size(), ms);
// fill
constexpr Array a = Fill(5);
CHECK_EQUAL(5, a[0]);
CHECK_EQUAL(5, a[1]);
CHECK_EQUAL(5, a[2]);
CHECK_EQUAL(5, a[3]);
CHECK_EQUAL(5, a[4]);
CHECK_EQUAL(5, a[5]);
CHECK_EQUAL(5, a[6]);
CHECK_EQUAL(5, a[7]);
CHECK_EQUAL(5, a[8]);
CHECK_EQUAL(5, a[9]);
#if ETL_USING_CPP20 && ETL_USING_STL
// swap
constexpr Array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
constexpr Array data2{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
constexpr Array data3 = Swap(data1, data2);
CHECK_ARRAY_EQUAL(data2.data(), data3.data(), data2.size());
#endif
}
#endif
};
}