aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpavook <pavook@yandex-team.com>2024-07-31 12:03:17 +0300
committerpavook <pavook@yandex-team.com>2024-07-31 12:17:03 +0300
commitc4fb2cdf41179910a4e9632de119ab3c94b741bc (patch)
treebed136492bcce6a53acb13c6145f0afd06843677
parent27b7ec6ec161019a7cb8f731166e892c60e863e1 (diff)
downloadydb-c4fb2cdf41179910a4e9632de119ab3c94b741bc.tar.gz
Fix TEnumerator::TIterator
082782faf0e6e05d3aa1c56e096d33ea69282f57
-rw-r--r--library/cpp/iterator/enumerate.h22
-rw-r--r--library/cpp/iterator/ut/functools_ut.cpp14
2 files changed, 28 insertions, 8 deletions
diff --git a/library/cpp/iterator/enumerate.h b/library/cpp/iterator/enumerate.h
index 2c83fb41bf..6ebc12a0e7 100644
--- a/library/cpp/iterator/enumerate.h
+++ b/library/cpp/iterator/enumerate.h
@@ -27,23 +27,30 @@ namespace NPrivate {
struct TIterator {
using difference_type = std::ptrdiff_t;
using value_type = TValue;
- using pointer = TValue*;
- using reference = TValue&;
+ using pointer = void;
+ using reference = value_type;
using iterator_category = std::input_iterator_tag;
- TValue operator*() {
+ reference operator*() const {
return {Index_, *Iterator_};
}
- TValue operator*() const {
- return {Index_, *Iterator_};
- }
- void operator++() {
+
+ TIterator& operator++() {
++Index_;
++Iterator_;
+ return *this;
+ }
+
+ TIterator operator++(int) {
+ TIterator result = *this;
+ ++(*this);
+ return result;
}
+
bool operator!=(const TSentinel& other) const {
return Iterator_ != other.Iterator_;
}
+
bool operator==(const TSentinel& other) const {
return Iterator_ == other.Iterator_;
}
@@ -51,6 +58,7 @@ namespace NPrivate {
std::size_t Index_;
TIteratorState Iterator_;
};
+
public:
using iterator = TIterator;
using const_iterator = TIterator;
diff --git a/library/cpp/iterator/ut/functools_ut.cpp b/library/cpp/iterator/ut/functools_ut.cpp
index 2dee9a55c8..b11a715e0c 100644
--- a/library/cpp/iterator/ut/functools_ut.cpp
+++ b/library/cpp/iterator/ut/functools_ut.cpp
@@ -99,7 +99,7 @@ using namespace NFuncTools;
struct TTestSentinel {};
struct TTestIterator {
- int operator*() {
+ int operator*() const {
return X;
}
void operator++() {
@@ -139,6 +139,18 @@ using namespace NFuncTools;
EXPECT_EQ(j, v.size());
}
+ // Test correctness of iterator traits.
+ auto enumerated = Enumerate(a);
+ static_assert(std::ranges::input_range<decltype(enumerated)>);
+ static_assert(
+ std::is_same_v<decltype(enumerated.begin())::pointer,
+ std::iterator_traits<decltype(enumerated.begin())>::pointer>);
+
+ // Post-increment test.
+ auto it = enumerated.begin();
+ EXPECT_EQ(*(it++), (std::tuple{0, 1}));
+ EXPECT_EQ(*it, (std::tuple{1, 2}));
+
TVector<size_t> d = {0, 0, 0};
FOR_DISPATCH_2(i, x, Enumerate(d)) {
x = i;