Skip to content

Commit f5eab49

Browse files
Add at method to span (#975)
* Update README.md * Add at method to span --------- Co-authored-by: John Wellbelove <[email protected]>
1 parent 7f66536 commit f5eab49

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

include/etl/span.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,26 @@ namespace etl
272272
{
273273
pbegin = other.pbegin;
274274
return *this;
275+
}
276+
277+
//*************************************************************************
278+
/// Returns a reference to the value at index 'i'.
279+
//*************************************************************************
280+
ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i)
281+
{
282+
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
283+
284+
return pbegin[i];
285+
}
286+
287+
//*************************************************************************
288+
/// Returns a const reference to the value at index 'i'.
289+
//*************************************************************************
290+
ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const
291+
{
292+
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
293+
294+
return pbegin[i];
275295
}
276296

277297
//*************************************************************************
@@ -614,6 +634,26 @@ namespace etl
614634
return *this;
615635
}
616636

637+
//*************************************************************************
638+
/// Returns a reference to the value at index 'i'.
639+
//*************************************************************************
640+
ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i)
641+
{
642+
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
643+
644+
return pbegin[i];
645+
}
646+
647+
//*************************************************************************
648+
/// Returns a const reference to the value at index 'i'.
649+
//*************************************************************************
650+
ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const
651+
{
652+
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
653+
654+
return pbegin[i];
655+
}
656+
617657
//*************************************************************************
618658
/// Returns a reference to the indexed value.
619659
//*************************************************************************

test/test_span_dynamic_extent.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,22 @@ namespace
445445
CHECK_EQUAL(etldata.data(), cview.data());
446446
}
447447

448+
//*************************************************************************
449+
TEST(test_at)
450+
{
451+
View view(etldata.begin(), etldata.end());
452+
CView cview(etldata.begin(), etldata.end());
453+
454+
for (size_t i = 0UL; i < etldata.size(); ++i)
455+
{
456+
CHECK_EQUAL(etldata.at(i), view.at(i));
457+
CHECK_EQUAL(etldata.at(i), cview.at(i));
458+
}
459+
460+
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::array_out_of_range);
461+
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::array_out_of_range);
462+
}
463+
448464
//*************************************************************************
449465
TEST(test_index_operator)
450466
{

test/test_span_fixed_extent.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,22 @@ namespace
433433
CHECK_EQUAL(etldata.data(), cview.data());
434434
}
435435

436+
//*************************************************************************
437+
TEST(test_at)
438+
{
439+
View view(etldata.begin(), etldata.end());
440+
CView cview(etldata.begin(), etldata.end());
441+
442+
for (size_t i = 0UL; i < etldata.size(); ++i)
443+
{
444+
CHECK_EQUAL(etldata.at(i), view.at(i));
445+
CHECK_EQUAL(etldata.at(i), cview.at(i));
446+
}
447+
448+
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::array_out_of_range);
449+
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::array_out_of_range);
450+
}
451+
436452
//*************************************************************************
437453
TEST(test_index_operator)
438454
{

0 commit comments

Comments
 (0)