2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <type_traits>
23 // tupleRange<start, n>(tuple): select n elements starting at index start
25 // tupleRange<start>(tuple): select all elements starting at index start
26 // until the end of the given tuple
27 // tuplePrepend(x, tuple): return a tuple obtained by prepending x to the
30 // In Lisp lingo, std::get<0> is car, tupleRange<1> is cdr, and tuplePrepend
35 // TemplateSeq<T, ...> is a type parametrized by sizeof...(Xs) values of type
36 // T. Used to destructure the values into a template parameter pack;
37 // see the example in TupleSelect, below.
38 template <class T, T... xs>
41 using Prepend = TemplateSeq<T, x, xs...>;
44 // TemplateRange<T, start, n>::type is
45 // TemplateSeq<T, start+1, start+2, ..., start+n-1>
46 template <class T, T start, T n, class Enable=void> struct TemplateRange;
48 template <class T, T start, T n>
51 typename std::enable_if<(n > 0)>::type> {
53 typename TemplateRange<T, start+1, n-1>::type::template Prepend<start>;
56 template <class T, T start, T n>
59 typename std::enable_if<(n <= 0)>::type> {
60 using type = TemplateSeq<T>;
63 // Similar to TemplateRange, given a tuple T,
64 // TemplateTupleRange<T, start, n>::type is
65 // TemplateSeq<size_t, start, start+1, ..., start+k-1>
66 // where k = min(tuple_size<T>::value - start, n)
67 // (that is, it's a TemplateSeq of at most n elements, but won't extend
68 // past the end of the given tuple)
71 std::size_t start = 0,
72 std::size_t n = std::numeric_limits<std::size_t>::max(),
74 std::tuple_size<typename std::remove_reference<T>::type>::value,
75 class Enable = typename std::enable_if<(start <= size)>::type>
76 struct TemplateTupleRange {
77 using type = typename TemplateRange<
80 (n <= size - start ? n : size - start)>::type;
85 // Helper class to select a subset of a tuple
86 template <class S> struct TupleSelect;
87 template <std::size_t... Ns>
88 struct TupleSelect<TemplateSeq<std::size_t, Ns...>> {
90 static auto select(T&& v)
91 -> decltype(std::make_tuple(std::get<Ns>(std::forward<T>(v))...)) {
92 return std::make_tuple(std::get<Ns>(std::forward<T>(v))...);
98 // Return a tuple consisting of the elements at a range of indices.
100 // Use as tupleRange<start, n>(t) to return a tuple of (at most) n
101 // elements starting at index start in tuple t.
102 // If only start is specified (tupleRange<start>(t)), returns all elements
103 // starting at index start until the end of the tuple t.
104 // Won't compile if start > size of t.
105 // Will return fewer elements (size - start) if start + n > size of t.
107 std::size_t start = 0,
108 std::size_t n = std::numeric_limits<std::size_t>::max(),
110 class Seq = typename TemplateTupleRange<T, start, n>::type>
111 auto tupleRange(T&& v)
112 -> decltype(detail::TupleSelect<Seq>::select(std::forward<T>(v))) {
113 return detail::TupleSelect<Seq>::select(std::forward<T>(v));
116 // Return a tuple obtained by prepending car to the tuple cdr.
117 template <class T, class U>
118 auto tuplePrepend(T&& car, U&& cdr)
119 -> decltype(std::tuple_cat(std::make_tuple(std::forward<T>(car)),
120 std::forward<U>(cdr))) {
121 return std::tuple_cat(std::make_tuple(std::forward<T>(car)),
122 std::forward<U>(cdr));