#ifndef _CHILON_TUPLE_ALGORITHM_
#define _CHILON_TUPLE_ALGORITHM_
#include <chilon/type/algorithm.hpp>
#include <tuple>
namespace chilon {
namespace type {
template <int i, class... Args>
struct at<i, std::tuple<Args...>> : at<i, Args...> {};
template <class... T>
struct size<std::tuple<T...>> : size<T...> {};
}
namespace tuple {
namespace detail {
template <class R, template <class> class Folder, class Iterator, int i, bool final, class... T>
struct fold {
static R exec(std::tuple<T...> const& tuple) {
enum { index = Iterator::template index<i>::value };
enum { next_index = Iterator::template index<i + 2>::value };
return Folder<typename std::tuple_element<index, std::tuple<T...>>::type>::exec(
std::get<index>(tuple)) +
fold<R, Folder, Iterator, i + 1, next_index < sizeof...(T), T...>::exec(tuple);
}
};
template <class R, template <class> class Folder, class Iterator, int i, class... T>
struct fold<R, Folder, Iterator, i, false, T...> {
static R exec(std::tuple<T...> const& tuple) {
enum { index = Iterator::template index<i>::value };
return Folder<typename std::tuple_element<index, std::tuple<T...>>::type>::exec(std::get<index>(tuple));
}
};
template <class R, class Iterator, int i, bool final, class... T>
struct join {
static R exec(std::tuple<T...> const& tuple, char const * const joinString) {
enum { index = Iterator::template index<i>::value };
enum { next_index = Iterator::template index<i + 2>::value };
return std::get<index>(tuple) + joinString +
join<R, Iterator, i + 1, next_index < sizeof...(T), T...>::exec(tuple, joinString);
}
};
template <class R, class Iterator, int i, class... T>
struct join<R, Iterator, i, false, T...> {
enum { index = Iterator::template index<i>::value };
static R exec(std::tuple<T...> const& tuple, char const * const joinString) {
return std::get<index>(tuple);
}
};
}
template <class R, template <class> class Folder, class Iterator = type::identity>
struct fold {
template <class... T>
static R exec(std::tuple<T...> const& tuple) {
enum { next_index = Iterator::template index<1>::value };
return detail::fold<R, Folder, Iterator, 0, next_index < sizeof...(T), T...>::exec(tuple);
}
};
template <class R, class Iterator = type::identity>
struct join {
template <class... T>
static R exec(std::tuple<T...> const& tuple, char const * const joinString = " ") {
enum { next_index = Iterator::template index<1>::value };
return detail::join<R, Iterator, 0, next_index < sizeof...(T), T...>::exec(tuple, joinString);
}
};
} }
#endif