Commit 06274a0d authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

Cut FOR_EACH_ENUMERATE

Summary: [Folly] Cut the `FOR_EACH_ENUMERATE` macro, which may be replaced by a combination of range-for, `ranges::view::enumerate`, and structured bindings.

Reviewed By: markisaa

Differential Revision: D21813019

fbshipit-source-id: fc9ac09a4e2f72f1433d0a518f03d5cd69a59c55
parent f6784e42
......@@ -159,23 +159,6 @@ constexpr decltype(auto) fetch(Sequence&& sequence, Index&& index);
for (auto&& _FE_ANON(s2_) = (c); !_FE_ANON(s1_); _FE_ANON(s1_) = true) \
for (auto i = _FE_ANON(s2_).rbegin(); i != _FE_ANON(s2_).rend(); ++i)
/*
* If you just want the element values, please use this construct:
*
* for (auto&& element : folly::enumerate(collection))
*
* If you need access to the iterators please write an explicit iterator loop
* and use a counter variable
*/
#define FOR_EACH_ENUMERATE(count, i, c) \
if (bool _FE_ANON(s1_) = false) { \
} else \
for (auto&& FOR_EACH_state2 = (c); !_FE_ANON(s1_); _FE_ANON(s1_) = true) \
if (size_t _FE_ANON(n1_) = 0) { \
} else if (const size_t& count = _FE_ANON(n1_)) { \
} else \
for (auto i = FOR_EACH_state2.begin(); i != FOR_EACH_state2.end(); \
++_FE_ANON(n1_), ++i)
/**
* If you just want the keys, please use this (ranges-v3) construct:
*
......
......@@ -393,52 +393,3 @@ TEST(Foreach, ForEachKvWithMultiMap) {
EXPECT_EQ(6, values);
EXPECT_EQ(3, numEntries);
}
TEST(Foreach, ForEachEnumerate) {
std::vector<int> vv;
int sumAA = 0;
int sumIter = 0;
int numIterations = 0;
FOR_EACH_ENUMERATE (aa, iter, vv) {
sumAA += aa;
sumIter += *iter;
++numIterations;
}
EXPECT_EQ(sumAA, 0);
EXPECT_EQ(sumIter, 0);
EXPECT_EQ(numIterations, 0);
vv.push_back(1);
vv.push_back(3);
vv.push_back(5);
FOR_EACH_ENUMERATE (aa, iter, vv) {
sumAA += aa;
sumIter += *iter;
++numIterations;
}
EXPECT_EQ(sumAA, 3); // 0 + 1 + 2
EXPECT_EQ(sumIter, 9); // 1 + 3 + 5
EXPECT_EQ(numIterations, 3);
}
TEST(Foreach, ForEachEnumerateBreak) {
std::vector<int> vv;
int sumAA = 0;
int sumIter = 0;
int numIterations = 0;
vv.push_back(1);
vv.push_back(2);
vv.push_back(4);
vv.push_back(8);
FOR_EACH_ENUMERATE (aa, iter, vv) {
sumAA += aa;
sumIter += *iter;
++numIterations;
if (aa == 1) {
break;
}
}
EXPECT_EQ(sumAA, 1); // 0 + 1
EXPECT_EQ(sumIter, 3); // 1 + 2
EXPECT_EQ(numIterations, 2);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment