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