summaryrefslogtreecommitdiff
path: root/lib/hj/zip.h
blob: 0cc7ee2393a07c954e9b51738e2a4b3d4e8f5389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include <algorithm>
#include <any>
#include <iostream>
#include <vector>
#include <tuple>

template<class TupType, size_t... I>
void print_tuple(const TupType& _tup, std::index_sequence<I...>)
{
    std::cout << "(";
    (..., (std::cout << (I == 0? "" : ", ") << std::get<I>(_tup)));
    std::cout << ")\n";
}

template<class... T>
void print_tuple(const std::tuple<T...>& _tup)
{
    print_tuple(_tup, std::make_index_sequence<sizeof...(T)>());
}

template<typename T>
void variatic_min(std::vector<T> v, int &m) {
    if (m > v.size()) m = v.size();
}

template<int N, typename... Ts>
using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type;

template <typename... Result, std::size_t... Indices>
auto vec_to_tup_helper(const std::vector<std::any>& v, std::index_sequence<Indices...>) {
    return std::make_tuple(
	    std::any_cast<NthTypeOf<Indices, Result...>>(v[Indices])...
    );
}

template <typename ...Result>
std::tuple<Result...> vec_to_tup(std::vector<std::any>& values) {
    return vec_to_tup_helper<Result...>(values, std::make_index_sequence<sizeof...(Result)>());
}

template <typename T>
void celem_at(std::vector<T> v, int i, std::vector<std::any> &r) {
    r.push_back(v[i]);
}

template<typename... Types>
std::vector<std::tuple<Types...>> zip(std::vector<Types>... args) {
    int m = 0xFFFFFFFF;
    std::vector<std::tuple<Types...>> res;

    (variatic_min(args, m), ...);

    for (int i = 0; i < m; i++) {
        std::vector<std::any> vaux;
        (celem_at(args, i, vaux), ...);
        res.push_back(vec_to_tup<Types...>(vaux));
    }
    return res;
}