Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add at method to span #975

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The library is intended for any compiler that supports C++98/03/11/14/17/20.
- Checksums & hash functions
- Variants (a type that can store many types in a type-safe interface)
- Choice of asserts, exceptions, error handler or no checks on errors
- Unit tested (currently over 6480 tests), using VS2019, GCC 8.1.0, , GCC 9.3.0, Clang 9.0.0 & 10.0.0
- Unit tested (currently over 9400 tests), using VS2022, GCC 12, Clang 14.
- Many utilities for template support.
- Easy to read and documented source.
- Free support via email, GitHub and Slack
Expand Down
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
Loading