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

Cut FOR_EACH_KV

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

Differential Revision: D21826182

fbshipit-source-id: ce4712afd3d0d7806eb1fca8c97009da117f982e
parent 58171cb5
......@@ -159,35 +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 keys, please use this (ranges-v3) construct:
*
* for (auto&& element : collection | views::keys)
*
* If you just want the values, please use this (ranges-v3) construct:
*
* for (auto&& element : collection | views::values)
*
* If you need to see both, use:
*
* for (auto&& element : collection) {
* auto const& key = element.first;
* auto& value = element.second;
* ......
* }
*
*/
#define FOR_EACH_KV(k, v, c) \
if (unsigned int _FE_ANON(s1_) = 0) { \
} else \
for (auto&& _FE_ANON(s2_) = (c); !_FE_ANON(s1_); _FE_ANON(s1_) = 1) \
for (auto _FE_ANON(s3_) = _FE_ANON(s2_).begin(); \
_FE_ANON(s3_) != _FE_ANON(s2_).end(); \
_FE_ANON(s1_) == 2 ? ((_FE_ANON(s1_) = 0), ++_FE_ANON(s3_)) \
: (_FE_ANON(s3_) = _FE_ANON(s2_).end())) \
for (auto& k = _FE_ANON(s3_)->first; !_FE_ANON(s1_); ++_FE_ANON(s1_)) \
for (auto& v = _FE_ANON(s3_)->second; !_FE_ANON(s1_); ++_FE_ANON(s1_))
namespace folly {
namespace detail {
......
......@@ -30,7 +30,6 @@ using namespace folly;
// iter->first and iter->second to local vars inside the FOR_EACH loop.
// 2. Benchmark iterating through the man with FOR_EACH, but use iter->first and
// iter->second as is, without assigning to local variables.
// 3. Use FOR_EACH_KV loop to iterate through the map.
// For use in benchmarks below.
std::map<int, std::string> bmMap;
......@@ -305,20 +304,6 @@ BENCHMARK(ForEachKVNoMacroNoAssign, iters) {
}
}
BENCHMARK(ForEachKVMacro, iters) {
int sumKeys = 0;
std::string sumValues;
BENCHMARK_SUSPEND {
setupBenchmark(iters);
}
FOR_EACH_KV (k, v, bmMap) {
sumKeys += k;
sumValues += v;
}
}
BENCHMARK(ForEachManual, iters) {
int sum = 1;
for (size_t i = 1; i < iters; ++i) {
......
......@@ -340,56 +340,3 @@ TEST(Foreach, ForEachNested) {
auto len = hello.size();
EXPECT_EQ(len * len, n);
}
TEST(Foreach, ForEachKV) {
std::map<std::string, int> testMap;
testMap["abc"] = 1;
testMap["def"] = 2;
std::string keys = "";
int values = 0;
int numEntries = 0;
FOR_EACH_KV (key, value, testMap) {
keys += key;
values += value;
++numEntries;
}
EXPECT_EQ("abcdef", keys);
EXPECT_EQ(3, values);
EXPECT_EQ(2, numEntries);
}
TEST(Foreach, ForEachKVBreak) {
std::map<std::string, int> testMap;
testMap["abc"] = 1;
testMap["def"] = 2;
std::string keys = "";
int values = 0;
int numEntries = 0;
FOR_EACH_KV (key, value, testMap) {
keys += key;
values += value;
++numEntries;
break;
}
EXPECT_EQ("abc", keys);
EXPECT_EQ(1, values);
EXPECT_EQ(1, numEntries);
}
TEST(Foreach, ForEachKvWithMultiMap) {
std::multimap<std::string, int> testMap;
testMap.insert(std::make_pair("abc", 1));
testMap.insert(std::make_pair("abc", 2));
testMap.insert(std::make_pair("def", 3));
std::string keys = "";
int values = 0;
int numEntries = 0;
FOR_EACH_KV (key, value, testMap) {
keys += key;
values += value;
++numEntries;
}
EXPECT_EQ("abcabcdef", keys);
EXPECT_EQ(6, values);
EXPECT_EQ(3, numEntries);
}
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