-
Notifications
You must be signed in to change notification settings - Fork 34
Adds missing subscript operator to ArrayIteratorBase #1963
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // Copyright (c) Lawrence Livermore National Security, LLC and other | ||
| // Axom Project Contributors. See top-level LICENSE and COPYRIGHT | ||
| // files for dates and other details. | ||
| // | ||
| // SPDX-License-Identifier: (BSD-3-Clause) | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "axom/core/Array.hpp" | ||
| #include "axom/core/ArrayView.hpp" | ||
| #include "axom/core/ItemCollection.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <complex> | ||
| #include <functional> | ||
| #include <iterator> | ||
| #include <numeric> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| namespace | ||
| { | ||
| // Detects whether subscripting a const iterator is a valid expression. | ||
| template <typename Iter, typename = void> | ||
| struct has_subscript : std::false_type | ||
| { }; | ||
|
|
||
| template <typename Iter> | ||
| struct has_subscript<Iter, | ||
| std::void_t<decltype(std::declval<const Iter&>()[std::declval< | ||
| typename std::iterator_traits<Iter>::difference_type>()])>> : std::true_type | ||
| { }; | ||
| } // namespace | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| TEST(core_array_iterator, random_access_contract_is_static) | ||
| { | ||
| using ArrayIter = axom::Array<double>::ArrayIterator; | ||
| using ConstArrayIter = axom::Array<double>::ConstArrayIterator; | ||
| using ViewIter = decltype(std::declval<axom::ArrayView<double>&>().begin()); | ||
|
|
||
| static_assert(std::is_same<typename std::iterator_traits<ArrayIter>::iterator_category, | ||
| std::random_access_iterator_tag>::value, | ||
| "Array's iterator advertises random access"); | ||
| static_assert(std::is_same<typename std::iterator_traits<ConstArrayIter>::iterator_category, | ||
| std::random_access_iterator_tag>::value, | ||
| "Array's const iterator advertises random access"); | ||
| static_assert(std::is_same<typename std::iterator_traits<ViewIter>::iterator_category, | ||
| std::random_access_iterator_tag>::value, | ||
| "ArrayView's iterator advertises random access"); | ||
|
|
||
| static_assert(has_subscript<ArrayIter>::value, "Array iterator needs i[n]"); | ||
| static_assert(has_subscript<ConstArrayIter>::value, "Array const_iterator needs i[n]"); | ||
| static_assert(has_subscript<ViewIter>::value, "ArrayView iterator needs i[n]"); | ||
|
|
||
| static_assert(std::is_same<decltype(std::declval<const ArrayIter&>()[0]), double&>::value, | ||
| "Array iterator subscripting must preserve mutability"); | ||
| static_assert(std::is_same<decltype(std::declval<const ConstArrayIter&>()[0]), const double&>::value, | ||
| "Array const_iterator subscripting must return a const reference"); | ||
|
|
||
| // Note: The subscript is defined on ArrayIteratorBase, not on the common IteratorBase | ||
| // used by derived forward-only iterators (like ItemCollection) | ||
| using ForwardIter = axom::ItemCollection<double>::iterator; | ||
| static_assert(!has_subscript<ForwardIter>::value, | ||
| "ItemCollection's forward iterator must not acquire random access"); | ||
|
|
||
| SUCCEED(); | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| TEST(core_array_iterator, subscript_matches_offset_dereference) | ||
| { | ||
| axom::Array<int> arr(5); | ||
| std::iota(arr.begin(), arr.end(), 10); | ||
|
|
||
| auto it = arr.begin(); | ||
| for(axom::IndexType n = 0; n < arr.size(); ++n) | ||
| { | ||
| EXPECT_EQ(it[n], *(it + n)); | ||
| EXPECT_EQ(it[n], arr[n]); | ||
| } | ||
|
|
||
| // Subscript is relative to the iterator's position, not to begin(). | ||
| auto mid = arr.begin() + 2; | ||
| EXPECT_EQ(mid[0], arr[2]); | ||
| EXPECT_EQ(mid[2], arr[4]); | ||
| EXPECT_EQ(mid[-2], arr[0]); | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| TEST(core_array_iterator, subscript_is_a_mutable_reference) | ||
| { | ||
| axom::Array<int> arr(3); | ||
| arr.fill(0); | ||
|
|
||
| const auto it = arr.begin(); | ||
| it[1] = 42; | ||
| EXPECT_EQ(arr[1], 42); | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Regression: libc++ 22 rewrote std::__sift_down to index the iterator rather | ||
| // than dereference it, so std::sort over an axom::Array failed to compile. | ||
| // See numerics::solve_polynomial_durand_kerner, which sorts an | ||
| // axom::Array<std::complex<double>>. | ||
| //------------------------------------------------------------------------------ | ||
| TEST(core_array_iterator, sort_over_array_of_complex) | ||
| { | ||
| using Complex = std::complex<double>; | ||
|
|
||
| axom::Array<Complex> roots; | ||
| roots.push_back(Complex {3.0, 0.0}); | ||
| roots.push_back(Complex {1.0, 2.0}); | ||
| roots.push_back(Complex {1.0, -2.0}); | ||
| roots.push_back(Complex {2.0, 0.0}); | ||
|
|
||
| std::sort(roots.begin(), roots.end(), [](const Complex& lhs, const Complex& rhs) { | ||
| if(lhs.real() != rhs.real()) | ||
| { | ||
| return lhs.real() < rhs.real(); | ||
| } | ||
| return lhs.imag() < rhs.imag(); | ||
| }); | ||
|
|
||
| EXPECT_EQ(roots[0], Complex(1.0, -2.0)); | ||
| EXPECT_EQ(roots[1], Complex(1.0, 2.0)); | ||
| EXPECT_EQ(roots[2], Complex(2.0, 0.0)); | ||
| EXPECT_EQ(roots[3], Complex(3.0, 0.0)); | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| TEST(core_array_iterator, heap_algorithms_over_array_view) | ||
| { | ||
| axom::Array<int> arr(6); | ||
| const int values[6] = {5, 1, 4, 2, 6, 3}; | ||
| for(axom::IndexType i = 0; i < arr.size(); ++i) | ||
| { | ||
| arr[i] = values[i]; | ||
| } | ||
|
|
||
| axom::ArrayView<int> view(arr); | ||
|
|
||
| // make_heap/sort_heap route through __sift_down, which requires the subscript operator. | ||
| std::make_heap(view.begin(), view.end()); | ||
| EXPECT_TRUE(std::is_heap(view.begin(), view.end())); | ||
|
|
||
| std::sort_heap(view.begin(), view.end()); | ||
| EXPECT_TRUE(std::is_sorted(view.begin(), view.end())); | ||
| EXPECT_EQ(view[0], 1); | ||
| EXPECT_EQ(view[5], 6); | ||
|
|
||
| // partial_sort also reaches __sift_down. | ||
| std::partial_sort(view.begin(), view.begin() + 3, view.end(), std::greater<int> {}); | ||
| EXPECT_EQ(view[0], 6); | ||
| EXPECT_EQ(view[1], 5); | ||
| EXPECT_EQ(view[2], 4); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the bugfix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to put this in
IteratorBase?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, but I don't think so since other iterators that are not random access iterators derive from
IteratorBase.E.g.
ItemCollectionis a forward iterator, and it's advance would give the wrong answer foroperator[](-1)See:
axom/src/axom/core/ItemCollection.hpp
Lines 207 to 214 in 97f1e49