Add at method to span (#975)

* Update README.md

* Add at method to span

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
mike919192 2024-11-13 16:26:04 -05:00 committed by GitHub
parent 7f66536183
commit f5eab49208
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 72 additions and 0 deletions

View File

@ -272,6 +272,26 @@ namespace etl
{
pbegin = other.pbegin;
return *this;
}
//*************************************************************************
/// Returns a reference to the value at index 'i'.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i)
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
return pbegin[i];
}
//*************************************************************************
/// Returns a const reference to the value at index 'i'.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
return pbegin[i];
}
//*************************************************************************
@ -614,6 +634,26 @@ namespace etl
return *this;
}
//*************************************************************************
/// Returns a reference to the value at index 'i'.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i)
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
return pbegin[i];
}
//*************************************************************************
/// Returns a const reference to the value at index 'i'.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
return pbegin[i];
}
//*************************************************************************
/// Returns a reference to the indexed value.
//*************************************************************************

View File

@ -445,6 +445,22 @@ namespace
CHECK_EQUAL(etldata.data(), cview.data());
}
//*************************************************************************
TEST(test_at)
{
View view(etldata.begin(), etldata.end());
CView cview(etldata.begin(), etldata.end());
for (size_t i = 0UL; i < etldata.size(); ++i)
{
CHECK_EQUAL(etldata.at(i), view.at(i));
CHECK_EQUAL(etldata.at(i), cview.at(i));
}
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::array_out_of_range);
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::array_out_of_range);
}
//*************************************************************************
TEST(test_index_operator)
{

View File

@ -433,6 +433,22 @@ namespace
CHECK_EQUAL(etldata.data(), cview.data());
}
//*************************************************************************
TEST(test_at)
{
View view(etldata.begin(), etldata.end());
CView cview(etldata.begin(), etldata.end());
for (size_t i = 0UL; i < etldata.size(); ++i)
{
CHECK_EQUAL(etldata.at(i), view.at(i));
CHECK_EQUAL(etldata.at(i), cview.at(i));
}
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::array_out_of_range);
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::array_out_of_range);
}
//*************************************************************************
TEST(test_index_operator)
{