1bf215546Sopenharmony_ci// 2bf215546Sopenharmony_ci// Copyright 2013 Francisco Jerez 3bf215546Sopenharmony_ci// 4bf215546Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci// copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci// to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci// the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci// and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci// Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci// 11bf215546Sopenharmony_ci// The above copyright notice and this permission notice shall be included in 12bf215546Sopenharmony_ci// all copies or substantial portions of the Software. 13bf215546Sopenharmony_ci// 14bf215546Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15bf215546Sopenharmony_ci// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16bf215546Sopenharmony_ci// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17bf215546Sopenharmony_ci// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18bf215546Sopenharmony_ci// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19bf215546Sopenharmony_ci// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20bf215546Sopenharmony_ci// OTHER DEALINGS IN THE SOFTWARE. 21bf215546Sopenharmony_ci// 22bf215546Sopenharmony_ci 23bf215546Sopenharmony_ci#ifndef CLOVER_UTIL_TUPLE_HPP 24bf215546Sopenharmony_ci#define CLOVER_UTIL_TUPLE_HPP 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include <tuple> 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_cinamespace clover { 29bf215546Sopenharmony_ci namespace tuple { 30bf215546Sopenharmony_ci /// 31bf215546Sopenharmony_ci /// Static sequence of integers. 32bf215546Sopenharmony_ci /// 33bf215546Sopenharmony_ci template<int... Is> 34bf215546Sopenharmony_ci struct integral_sequence; 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci /// 37bf215546Sopenharmony_ci /// Static sequence containing all integers from 0 to N-1. 38bf215546Sopenharmony_ci /// 39bf215546Sopenharmony_ci template<int N, int... Is> 40bf215546Sopenharmony_ci struct enumerate { 41bf215546Sopenharmony_ci typedef typename enumerate<N-1, N-1, Is...>::type 42bf215546Sopenharmony_ci type; 43bf215546Sopenharmony_ci }; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci template<int... Is> 46bf215546Sopenharmony_ci struct enumerate<0, Is...> { 47bf215546Sopenharmony_ci typedef integral_sequence<Is...> type; 48bf215546Sopenharmony_ci }; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci namespace detail { 51bf215546Sopenharmony_ci template<typename F, typename T, 52bf215546Sopenharmony_ci typename E = typename enumerate<std::tuple_size< 53bf215546Sopenharmony_ci typename std::remove_reference<T>::type>::value 54bf215546Sopenharmony_ci >::type> 55bf215546Sopenharmony_ci struct _apply; 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci template<typename F, typename T, int... Is> 58bf215546Sopenharmony_ci struct _apply<F, T, integral_sequence<Is...>> { 59bf215546Sopenharmony_ci typedef typename std::remove_reference<F>::type func_type; 60bf215546Sopenharmony_ci typedef decltype( 61bf215546Sopenharmony_ci std::declval<func_type>()(std::get<Is>(std::declval<T &&>())...) 62bf215546Sopenharmony_ci ) value_type; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci static value_type 65bf215546Sopenharmony_ci eval(F &&f, T &&t) { 66bf215546Sopenharmony_ci return f(std::get<Is>(std::forward<T>(t))...); 67bf215546Sopenharmony_ci } 68bf215546Sopenharmony_ci }; 69bf215546Sopenharmony_ci } 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci /// 72bf215546Sopenharmony_ci /// Evaluate function \a f with the elements of tuple \a t 73bf215546Sopenharmony_ci /// expanded as arguments. 74bf215546Sopenharmony_ci /// 75bf215546Sopenharmony_ci template<typename F, typename T> 76bf215546Sopenharmony_ci typename detail::_apply<F, T>::value_type 77bf215546Sopenharmony_ci apply(F &&f, T &&t) { 78bf215546Sopenharmony_ci return detail::_apply<F, T>::eval(std::forward<F>(f), 79bf215546Sopenharmony_ci std::forward<T>(t)); 80bf215546Sopenharmony_ci } 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci namespace detail { 83bf215546Sopenharmony_ci template<typename F, typename T, 84bf215546Sopenharmony_ci typename E = typename enumerate<std::tuple_size< 85bf215546Sopenharmony_ci typename std::remove_reference<T>::type>::value 86bf215546Sopenharmony_ci >::type> 87bf215546Sopenharmony_ci struct _map; 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci template<typename F, typename T, int... Is> 90bf215546Sopenharmony_ci struct _map<F, T, integral_sequence<Is...>> { 91bf215546Sopenharmony_ci typedef typename std::remove_reference<F>::type func_type; 92bf215546Sopenharmony_ci typedef std::tuple< 93bf215546Sopenharmony_ci decltype(std::declval<func_type>()( 94bf215546Sopenharmony_ci std::get<Is>(std::declval<T &&>())))... 95bf215546Sopenharmony_ci > value_type; 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci static value_type 98bf215546Sopenharmony_ci eval(F &&f, T &&t) { 99bf215546Sopenharmony_ci return value_type(f(std::get<Is>(std::forward<T>(t)))...); 100bf215546Sopenharmony_ci } 101bf215546Sopenharmony_ci }; 102bf215546Sopenharmony_ci } 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci /// 105bf215546Sopenharmony_ci /// Evaluate function \a f on each element of the tuple \a t and 106bf215546Sopenharmony_ci /// return the resulting values as a new tuple. 107bf215546Sopenharmony_ci /// 108bf215546Sopenharmony_ci template<typename F, typename T> 109bf215546Sopenharmony_ci typename detail::_map<F, T>::value_type 110bf215546Sopenharmony_ci map(F &&f, T &&t) { 111bf215546Sopenharmony_ci return detail::_map<F, T>::eval(std::forward<F>(f), 112bf215546Sopenharmony_ci std::forward<T>(t)); 113bf215546Sopenharmony_ci } 114bf215546Sopenharmony_ci } 115bf215546Sopenharmony_ci} 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci#endif 118