-
Notifications
You must be signed in to change notification settings - Fork 10
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 Iterator to make the STL available to 1D Views #158
Merged
+163
−0
Merged
Changes from all commits
Commits
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 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,102 @@ | ||
#include "View.hpp" | ||
|
||
#include <boost/iterator/iterator_facade.hpp> | ||
//#include <boost/stl_interfaces/iterator_interface.hpp> | ||
|
||
namespace llama | ||
{ | ||
// requires Boost 1.74 which is quite new | ||
#if 0 | ||
template <typename View> | ||
struct Iterator | ||
: boost::stl_interfaces::proxy_iterator_interface< | ||
Iterator<View>, | ||
std::random_access_iterator_tag, | ||
decltype(std::declval<View>()(ArrayDomain<1>{}))> | ||
{ | ||
constexpr decltype(auto) operator*() const | ||
{ | ||
return (*view)(coord); | ||
} | ||
|
||
constexpr auto operator+=(std::ptrdiff_t n) -> Iterator& | ||
{ | ||
coord[0] += n; | ||
return *this; | ||
} | ||
|
||
friend constexpr auto operator-(const Iterator& a, const Iterator& b) | ||
{ | ||
return a.coord[0] - b.coord[0]; | ||
} | ||
|
||
friend constexpr bool operator==(const Iterator& a, const Iterator& b) | ||
{ | ||
return a.coord == b.coord; | ||
} | ||
|
||
ArrayDomain<1> coord; | ||
View* view; | ||
}; | ||
#endif | ||
|
||
template <typename View> | ||
struct Iterator | ||
: boost::iterators::iterator_facade< | ||
Iterator<View>, | ||
typename View::VirtualDatumType, | ||
std::random_access_iterator_tag, | ||
typename View::VirtualDatumType, | ||
std::ptrdiff_t> | ||
{ | ||
constexpr decltype(auto) dereference() const | ||
{ | ||
return (*view)(coord); | ||
} | ||
|
||
constexpr bool equal(const Iterator& other) const | ||
{ | ||
return coord == other.coord; | ||
} | ||
|
||
constexpr auto increment() -> Iterator& | ||
{ | ||
coord[0]++; | ||
return *this; | ||
} | ||
|
||
constexpr auto decrement() -> Iterator& | ||
{ | ||
coord[0]--; | ||
return *this; | ||
} | ||
|
||
constexpr auto advance(std::ptrdiff_t n) -> Iterator& | ||
{ | ||
coord[0] += n; | ||
return *this; | ||
} | ||
|
||
constexpr auto distance_to(const Iterator& other) const -> std::ptrdiff_t | ||
{ | ||
return static_cast<std::ptrdiff_t>(other.coord[0]) - static_cast<std::ptrdiff_t>(coord[0]); | ||
} | ||
|
||
ArrayDomain<1> coord; | ||
View* view; | ||
}; | ||
|
||
template <typename View> | ||
auto begin(View& view) -> Iterator<View> | ||
{ | ||
static_assert(View::ArrayDomain::rank == 1, "Iterators for non-1D views are not implemented"); | ||
return {{}, ArrayDomain<1>{}, &view}; | ||
} | ||
|
||
template <typename View> | ||
auto end(View& view) -> Iterator<View> | ||
{ | ||
static_assert(View::ArrayDomain::rank == 1, "Iterators for non-1D views are not implemented"); | ||
return {{}, view.mapping.arrayDomainSize, &view}; | ||
} | ||
} // namespace llama |
This file contains 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 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,60 @@ | ||
#include <algorithm> | ||
#include <catch2/catch.hpp> | ||
#include <llama/llama.hpp> | ||
#include <numeric> | ||
|
||
// clang-format off | ||
namespace tag { | ||
struct X {}; | ||
struct Y {}; | ||
struct Z {}; | ||
} | ||
|
||
using Position = llama::DS< | ||
llama::DE<tag::X, int>, | ||
llama::DE<tag::Y, int>, | ||
llama::DE<tag::Z, int> | ||
>; | ||
// clang-format on | ||
|
||
TEST_CASE("iterator") | ||
{ | ||
using ArrayDomain = llama::ArrayDomain<1>; | ||
constexpr auto arrayDomain = ArrayDomain{32}; | ||
constexpr auto mapping = llama::mapping::AoS<ArrayDomain, Position>{arrayDomain}; | ||
auto view = llama::allocView(mapping); | ||
|
||
for (auto vd : view) | ||
{ | ||
vd(tag::X{}) = 1; | ||
vd(tag::Y{}) = 2; | ||
vd(tag::Z{}) = 3; | ||
} | ||
std::transform(begin(view), end(view), begin(view), [](auto vd) { return vd * 2; }); | ||
const int sumY = std::accumulate(begin(view), end(view), 0, [](int acc, auto vd) { return acc + vd(tag::Y{}); }); | ||
CHECK(sumY == 128); | ||
} | ||
|
||
TEST_CASE("iterator.std_copy") | ||
{ | ||
using ArrayDomain = llama::ArrayDomain<1>; | ||
constexpr auto arrayDomain = ArrayDomain{32}; | ||
auto aosView = llama::allocView(llama::mapping::AoS<ArrayDomain, Position>{arrayDomain}); | ||
auto soaView = llama::allocView(llama::mapping::SoA<ArrayDomain, Position>{arrayDomain}); | ||
|
||
int i = 0; | ||
for (auto vd : aosView) | ||
{ | ||
vd(tag::X{}) = ++i; | ||
vd(tag::Y{}) = ++i; | ||
vd(tag::Z{}) = ++i; | ||
} | ||
std::copy(begin(aosView), end(aosView), begin(soaView)); | ||
i = 0; | ||
for (auto vd : soaView) | ||
{ | ||
CHECK(vd(tag::X{}) == ++i); | ||
CHECK(vd(tag::Y{}) == ++i); | ||
CHECK(vd(tag::Z{}) == ++i); | ||
} | ||
} |
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.
Having this line work is super crazy! This means we can use
std::copy
to copy between ANY LLAMA layouts. This will always do an element-wise copy, which is sometimes suboptimal. But it correctly shuffles the layouts through the iterators and virtual datums.