-
Notifications
You must be signed in to change notification settings - Fork 1
[utils] Implemented Enumerate iterator adapter (#28) #29
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
Open
derzhavin3016
wants to merge
5
commits into
master
Choose a base branch
from
enumerate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2ecd9a
[utils] Implemented Enumerate iterator adapter (#28)
derzhavin3016 e73eff3
[utils] Improved move semantics of enumerate
derzhavin3016 6d407c5
[utils] Added const to enumerate iterator's count
derzhavin3016 7d86497
[utils] Removed makeEnumerate
derzhavin3016 5c870d9
[utils] Added test + some fixes
derzhavin3016 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| #ifndef __INCLUDE_UTILS_UTILS_HH__ | ||
| #define __INCLUDE_UTILS_UTILS_HH__ | ||
|
|
||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <iterator> | ||
|
|
||
| namespace utils | ||
| { | ||
| template <class T> | ||
| concept ItContainer = requires(T cont) | ||
| { | ||
| { | ||
| std::begin(cont) | ||
| } -> std::forward_iterator; | ||
| { | ||
| std::end(cont) | ||
| } -> std::forward_iterator; | ||
| { | ||
| std::size(cont) | ||
| } -> std::convertible_to<std::size_t>; | ||
| }; | ||
|
|
||
| namespace detail | ||
| { | ||
| template <std::forward_iterator It> | ||
| class EnumerateIt final | ||
| { | ||
| private: | ||
| template <typename Ref> | ||
| struct ArrowProxy | ||
| { | ||
| Ref r; | ||
|
|
||
| Ref *operator->() | ||
| { | ||
| return &r; | ||
| } | ||
| }; | ||
|
|
||
| public: | ||
| using iterator_category = std::forward_iterator_tag; | ||
| using difference_type = | ||
| std::pair<std::make_signed_t<std::size_t>, typename std::iterator_traits<It>::difference_type>; | ||
| using value_type = std::pair<std::size_t, typename std::iterator_traits<It>::value_type>; | ||
| using reference = std::pair<std::size_t, typename std::iterator_traits<It>::reference>; | ||
| using pointer = ArrowProxy<reference>; | ||
|
|
||
| private: | ||
| std::size_t counter_; | ||
| It iter_; | ||
|
|
||
| public: | ||
| EnumerateIt(std::size_t i, It iter) : counter_(i), iter_(iter) | ||
| {} | ||
|
|
||
| reference operator*() const | ||
| { | ||
| return {counter_, *iter_}; | ||
| } | ||
|
|
||
| EnumerateIt &operator++() | ||
| { | ||
| ++counter_; | ||
| ++iter_; | ||
| return *this; | ||
| } | ||
|
|
||
| EnumerateIt operator++(int) | ||
| { | ||
| auto temp{*this}; | ||
| operator++(); | ||
| return temp; | ||
| } | ||
|
|
||
| bool equals(const EnumerateIt &rhs) const | ||
| { | ||
| return iter_ == rhs.iter_; | ||
| } | ||
|
|
||
| pointer operator->() const | ||
| { | ||
| return pointer{{counter_, *iter_}}; | ||
| } | ||
| }; | ||
|
|
||
| template <std::forward_iterator It> | ||
| bool operator==(const EnumerateIt<It> &lhs, const EnumerateIt<It> &rhs) | ||
| { | ||
| return lhs.equals(rhs); | ||
| } | ||
| } // namespace detail | ||
|
|
||
| template <ItContainer C> | ||
| class Enumerate final | ||
| { | ||
| private: | ||
| using NoRefC = std::remove_reference_t<C>; | ||
| using ContStorageType = std::conditional_t<std::is_rvalue_reference_v<C>, NoRefC, NoRefC &>; | ||
|
|
||
| ContStorageType cont_; | ||
| using EnumItType = detail::EnumerateIt<decltype(std::begin(cont_))>; | ||
|
|
||
| public: | ||
| Enumerate(C &&cont) : cont_(std::forward<C>(cont)) | ||
| {} | ||
|
|
||
| auto begin() | ||
| { | ||
| return EnumItType(0, std::begin(cont_)); | ||
| } | ||
| auto begin() const | ||
| { | ||
| return EnumItType(0, std::begin(cont_)); | ||
| } | ||
|
|
||
| auto end() | ||
| { | ||
| return EnumItType(std::size(cont_), std::end(cont_)); | ||
| } | ||
| auto end() const | ||
| { | ||
| return EnumItType(std::size(cont_), std::end(cont_)); | ||
| } | ||
|
|
||
| auto size() const | ||
| { | ||
| return std::size(cont_); | ||
| } | ||
| }; | ||
|
|
||
| template <ItContainer C> | ||
| Enumerate(C &&) -> Enumerate<C &&>; | ||
|
|
||
| } // namespace utils | ||
|
|
||
| #endif /* __INCLUDE_UTILS_UTILS_HH__ */ | ||
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,4 @@ | ||
| add_library(utils INTERFACE) | ||
| target_sources(utils | ||
| INTERFACE ${CMAKE_SOURCE_DIR}/include/utils/utils.hh | ||
| ) |
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,188 @@ | ||
| #include "utils/utils.hh" | ||
|
|
||
| #include "test_header.hh" | ||
| #include <algorithm> | ||
| #include <numeric> | ||
| #include <vector> | ||
|
|
||
| using namespace utils; | ||
|
|
||
| TEST(EnumerateTest, ordVecSimpleBind) | ||
| { | ||
| // Arrange | ||
| std::vector<int> vec; | ||
| vec.resize(10); | ||
| std::iota(vec.begin(), vec.end(), 0); | ||
|
|
||
| // Act && Asserts | ||
| for (auto val : Enumerate(vec)) | ||
| ASSERT_EQ(val.first, val.second); | ||
| } | ||
|
|
||
| TEST(EnumerateTest, ordVecStructBind) | ||
| { | ||
| // Arrange | ||
| std::vector<int> vec; | ||
| vec.resize(10); | ||
| std::iota(vec.begin(), vec.end(), 0); | ||
|
|
||
| // Act && Asserts | ||
| for (auto [i, val] : Enumerate(vec)) | ||
| ASSERT_EQ(i, val); | ||
| } | ||
|
|
||
| TEST(EnumerateTest, ordVecChangeValue) | ||
| { | ||
| // Arrange | ||
| std::vector<int> vec, expect; | ||
| vec.resize(10); | ||
| expect.resize(vec.size()); | ||
| std::iota(vec.begin(), vec.end(), 0); | ||
| std::iota(expect.rbegin(), expect.rend(), -expect.size() + 1); | ||
|
|
||
| // Act | ||
| for (auto [i, val] : Enumerate(vec)) | ||
| val = -static_cast<int>(i); | ||
|
|
||
| // Assert | ||
| EXPECT_EQ(vec, expect); | ||
| } | ||
|
|
||
| TEST(EnumerateTest, twiceDeref) | ||
| { | ||
| // Arrange | ||
| std::vector<int> vec = {1, 2, 3}; | ||
|
|
||
| // Act | ||
| auto enumerate = Enumerate(vec); | ||
| auto it1 = enumerate.begin(); | ||
| auto it2 = enumerate.begin(); | ||
|
|
||
| using value_type = | ||
| std::iterator_traits<detail::EnumerateIt<std::vector<int>::iterator>>::value_type; | ||
|
|
||
| value_type v1 = *it1++; | ||
| value_type v2 = *it1; | ||
derzhavin3016 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| auto r1 = *it2; | ||
| ++it2; | ||
| auto r2 = *it2; | ||
|
|
||
| // Assert | ||
| EXPECT_EQ(v1.first, 0); | ||
| EXPECT_EQ(v2.first, 1); | ||
|
|
||
| EXPECT_NE(r1, r2); | ||
| } | ||
|
|
||
| TEST(EnumerateTest, ordVecTemp) | ||
| { | ||
| // Arrange | ||
| std::vector<int> res, expect(10, 10); | ||
| res.resize(10); | ||
|
|
||
| // Act | ||
| for (auto [i, val] : Enumerate(std::vector<int>(expect.size(), 10))) | ||
| res[i] = val; | ||
|
|
||
| // Assert | ||
| EXPECT_EQ(res, expect); | ||
| } | ||
|
|
||
| TEST(EnumerateTest, arrowProxy) | ||
| { | ||
| // Arrange | ||
| struct Compl | ||
| { | ||
| int a; | ||
|
|
||
| Compl &operator=(int v) | ||
| { | ||
| a = v; | ||
| return *this; | ||
| } | ||
|
|
||
| bool operator==(const Compl &) const = default; | ||
|
|
||
| void inc() | ||
| { | ||
| a++; | ||
| } | ||
| }; | ||
|
|
||
| std::vector<Compl> vec, expect; | ||
| vec.resize(10); | ||
| expect.resize(vec.size()); | ||
| std::iota(vec.begin(), vec.end(), 0); | ||
| std::iota(expect.begin(), expect.end(), 1); | ||
|
|
||
| // Act | ||
| auto enumerate = Enumerate(vec); | ||
| for (auto it = enumerate.begin(); it != enumerate.end(); it++) | ||
| it->second.inc(); | ||
|
|
||
| // Assert | ||
| EXPECT_EQ(vec, expect); | ||
| } | ||
|
|
||
| TEST(EnumerateTest, constContainer) | ||
| { | ||
| // Arrange | ||
| const std::vector vec = {1, 2, 3, 4, 5}; | ||
| std::vector<int> vec2(vec.size()); | ||
|
|
||
| // Act | ||
| for (auto [idx, val] : Enumerate(vec)) | ||
| vec2[idx] = val; | ||
|
|
||
| // Assert | ||
| EXPECT_EQ(vec, vec2); | ||
| } | ||
|
|
||
| TEST(EnumerateTest, MoveSemantics) | ||
| { | ||
| // Arrange | ||
| struct WasMovedType | ||
| {}; | ||
| struct WasCopiedType | ||
| {}; | ||
| struct ToMove | ||
| { | ||
| private: | ||
| std::vector<int> dummy{}; | ||
|
|
||
| public: | ||
| ToMove() = default; | ||
|
|
||
| ToMove(const ToMove &that) : dummy(that.dummy) | ||
| { | ||
| throw WasCopiedType{}; | ||
| } | ||
| ToMove(ToMove &&that) : dummy(std::move(that.dummy)) | ||
| { | ||
| throw WasMovedType{}; | ||
| } | ||
|
|
||
| auto size() const | ||
| { | ||
| return dummy.size(); | ||
| } | ||
|
|
||
| auto begin() | ||
| { | ||
| return dummy.begin(); | ||
| } | ||
|
|
||
| auto end() | ||
| { | ||
| return dummy.end(); | ||
| } | ||
| }; | ||
|
|
||
| ToMove local; | ||
| // Act && Assert | ||
| EXPECT_THROW([[maybe_unused]] auto moved = Enumerate(ToMove{});, WasMovedType); | ||
| EXPECT_NO_THROW([[maybe_unused]] auto copied = Enumerate(local);); | ||
| } | ||
|
|
||
| #include "test_footer.hh" | ||
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.
Is it really should be a pair? Maybe just number is enough?