Skip to content

Commit

Permalink
Add at method to span
Browse files Browse the repository at this point in the history
  • Loading branch information
mike919192 committed Nov 13, 2024
1 parent a88a48d commit f4c3420
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/etl/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

//*************************************************************************
Expand Down Expand Up @@ -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.
//*************************************************************************
Expand Down
16 changes: 16 additions & 0 deletions test/test_span_dynamic_extent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
16 changes: 16 additions & 0 deletions test/test_span_fixed_extent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit f4c3420

Please sign in to comment.