Commit ec7690d7 authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

Always use folly::index_sequence

Summary:
Continuing from D7482813, ensure everywhere in folly is using the folly versions of integer and index sequences.
Also implement index_sequence_for and use it.

Reviewed By: aary

Differential Revision: D7490483

fbshipit-source-id: 082209dd6c6ca11f67b38dbe4529d46e9dce57ce
parent 92320e04
......@@ -183,6 +183,8 @@ using make_integer_sequence = typename utility_detail::make_seq<
template <std::size_t Size>
using make_index_sequence = make_integer_sequence<std::size_t, Size>;
template <class... T>
using index_sequence_for = make_index_sequence<sizeof...(T)>;
/**
* Backports from C++17 of:
......
......@@ -24,6 +24,7 @@
#include <folly/Portability.h>
#include <folly/Traits.h>
#include <folly/Utility.h>
namespace folly {
......@@ -256,8 +257,7 @@ void for_each_range_impl(Sequence&& range, Func& func) {
template <typename Seq, typename F, typename = void>
struct ForEachTupleImpl {
template <typename Sequence, typename Func, std::size_t... Indices>
static void
impl(Sequence&& seq, Func& func, std::index_sequence<Indices...>) {
static void impl(Sequence&& seq, Func& func, index_sequence<Indices...>) {
// unroll the loop in an initializer list construction parameter expansion
// pack
static_cast<void>(std::initializer_list<int>{
......@@ -270,8 +270,7 @@ struct ForEachTupleImpl {
template <typename Seq, typename F>
struct ForEachTupleImpl<Seq, F, EnableIfBreaksTuple<Seq, F>> {
template <typename Sequence, typename Func, std::size_t... Indices>
static void
impl(Sequence&& seq, Func& func, std::index_sequence<Indices...>) {
static void impl(Sequence&& seq, Func& func, index_sequence<Indices...>) {
// unroll the loop in an initializer list construction parameter expansion
// pack
LoopControl break_or_not = LoopControl::CONTINUE;
......@@ -307,7 +306,7 @@ void for_each_tuple_impl(Sequence&& seq, Func& func) {
constexpr auto length =
std::tuple_size<typename std::decay<Sequence>::type>::value;
ForEachTupleImpl<Sequence, Func>::impl(
std::forward<Sequence>(seq), func, std::make_index_sequence<length>{});
std::forward<Sequence>(seq), func, make_index_sequence<length>{});
}
template <
typename Sequence,
......
......@@ -23,6 +23,7 @@
#include <type_traits>
#include <utility>
#include <folly/Utility.h>
#include <folly/lang/RValueReferenceWrapper.h>
namespace folly {
......@@ -251,16 +252,15 @@ class emplace_iterator_base<Derived, EmplaceImpl, false>
*/
template <typename... Args>
Derived& operator=(emplace_args<Args...>& args) {
return unpackAndEmplace(args, std::index_sequence_for<Args...>{});
return unpackAndEmplace(args, index_sequence_for<Args...>{});
}
template <typename... Args>
Derived& operator=(const emplace_args<Args...>& args) {
return unpackAndEmplace(args, std::index_sequence_for<Args...>{});
return unpackAndEmplace(args, index_sequence_for<Args...>{});
}
template <typename... Args>
Derived& operator=(emplace_args<Args...>&& args) {
return unpackAndEmplace(
std::move(args), std::index_sequence_for<Args...>{});
return unpackAndEmplace(std::move(args), index_sequence_for<Args...>{});
}
// No-ops.
......@@ -284,17 +284,17 @@ class emplace_iterator_base<Derived, EmplaceImpl, false>
protected:
template <typename Args, std::size_t... I>
Derived& unpackAndEmplace(Args& args, std::index_sequence<I...>) {
Derived& unpackAndEmplace(Args& args, index_sequence<I...>) {
this->emplace(get_emplace_arg<I>(args)...);
return static_cast<Derived&>(*this);
}
template <typename Args, std::size_t... I>
Derived& unpackAndEmplace(const Args& args, std::index_sequence<I...>) {
Derived& unpackAndEmplace(const Args& args, index_sequence<I...>) {
this->emplace(get_emplace_arg<I>(args)...);
return static_cast<Derived&>(*this);
}
template <typename Args, std::size_t... I>
Derived& unpackAndEmplace(Args&& args, std::index_sequence<I...>) {
Derived& unpackAndEmplace(Args&& args, index_sequence<I...>) {
this->emplace(get_emplace_arg<I>(std::move(args))...);
return static_cast<Derived&>(*this);
}
......@@ -323,16 +323,16 @@ class emplace_iterator_base<Derived, EmplaceImpl, true>
*/
template <typename... Args>
Derived& operator=(std::pair<Args...>& args) {
return this->unpackAndEmplace(args, std::index_sequence_for<Args...>{});
return this->unpackAndEmplace(args, index_sequence_for<Args...>{});
}
template <typename... Args>
Derived& operator=(const std::pair<Args...>& args) {
return this->unpackAndEmplace(args, std::index_sequence_for<Args...>{});
return this->unpackAndEmplace(args, index_sequence_for<Args...>{});
}
template <typename... Args>
Derived& operator=(std::pair<Args...>&& args) {
return this->unpackAndEmplace(
std::move(args), std::index_sequence_for<Args...>{});
std::move(args), index_sequence_for<Args...>{});
}
/**
......@@ -341,16 +341,16 @@ class emplace_iterator_base<Derived, EmplaceImpl, true>
*/
template <typename... Args>
Derived& operator=(std::tuple<Args...>& args) {
return this->unpackAndEmplace(args, std::index_sequence_for<Args...>{});
return this->unpackAndEmplace(args, index_sequence_for<Args...>{});
}
template <typename... Args>
Derived& operator=(const std::tuple<Args...>& args) {
return this->unpackAndEmplace(args, std::index_sequence_for<Args...>{});
return this->unpackAndEmplace(args, index_sequence_for<Args...>{});
}
template <typename... Args>
Derived& operator=(std::tuple<Args...>&& args) {
return this->unpackAndEmplace(
std::move(args), std::index_sequence_for<Args...>{});
std::move(args), index_sequence_for<Args...>{});
}
// We need all of these explicit defaults because the custom operator=
......
......@@ -20,6 +20,7 @@
#include <folly/Benchmark.h>
#include <folly/FBVector.h>
#include <folly/Utility.h>
#include <folly/dynamic.h>
#include <folly/init/Init.h>
#include <folly/json.h>
......@@ -99,7 +100,7 @@ BENCHMARK_RELATIVE(intAppend_format) {
BENCHMARK_DRAW_LINE();
template <size_t... Indexes>
int snprintf20Numbers(int i, std::index_sequence<Indexes...>) {
int snprintf20Numbers(int i, index_sequence<Indexes...>) {
static_assert(20 == sizeof...(Indexes), "Must have exactly 20 indexes");
return snprintf(
bigBuf.data(),
......@@ -114,13 +115,13 @@ int snprintf20Numbers(int i, std::index_sequence<Indexes...>) {
BENCHMARK(bigFormat_snprintf, iters) {
while (iters--) {
for (int i = -100; i < 100; i++) {
snprintf20Numbers(i, std::make_index_sequence<20>());
snprintf20Numbers(i, make_index_sequence<20>());
}
}
}
template <size_t... Indexes>
decltype(auto) format20Numbers(int i, std::index_sequence<Indexes...>) {
decltype(auto) format20Numbers(int i, index_sequence<Indexes...>) {
static_assert(20 == sizeof...(Indexes), "Must have exactly 20 indexes");
return format(
"{} {} {} {} {}"
......@@ -141,9 +142,8 @@ BENCHMARK_RELATIVE(bigFormat_format, iters) {
while (iters--) {
for (int i = -100; i < 100; i++) {
p = bigBuf.data();
suspender.dismissing([&] {
format20Numbers(i, std::make_index_sequence<20>())(writeToBuf);
});
suspender.dismissing(
[&] { format20Numbers(i, make_index_sequence<20>())(writeToBuf); });
}
}
}
......
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