-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.hpp
102 lines (90 loc) · 3.63 KB
/
apply.hpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// algocpp/array/apply.hpp
//
// This file is part of algocpp and is copyrighted by algocpp.
// If used, it must comply with the MIT License.
#ifndef ALGOCPP_ARRAY_APPLY
#define ALGOCPP_ARRAY_APPLY
#include <vector>
#include <array>
#include <list>
#include <functional>
#include <stdexcept>
#include <iostream>
#include <cxxabi.h>
namespace algocpp
{
namespace array
{
/// @brief Passing two arrays to a function
/// The values of the two arrays are passed one by one to the function, and the result is returned as a new array.
/// The computational complexity is @c O(N) , where @c N is the length of the array.
/// If the array lengths are not equal, an exception is thrown.
/// The behavior when the function is other than T(T,T) is undefined.
/// @tparam T Array value type
/// @tparam funcs function type
/// @param a Array to be the first argument of the function
/// @param b Array to be the second argument of the function
/// @param func Function to which a value is entered
/// @return Array of results of inputting arguments a and b to function func, respectively
/// @exception @c std::invaid_argument
template <typename T, typename funcs>
inline std::vector<T> apply(std::vector<T> a, std::vector<T> b, funcs func)
{
if (a.size() != b.size())
{
throw std::invalid_argument("Arrays must be of equal length.");
}
std::vector<T> result(a.size());
for (unsigned long long i = 0; i < a.size(); ++i)
{
result[i] = func(a[i], b[i]);
}
return result;
}
/// @brief Passing two arrays to a function
/// The values of the two arrays are passed one by one to the function, and the result is returned as a new array.
/// The computational complexity is @c O(N) , where @c N is the length of the array.
/// The behavior when the function is other than T(T,T) is undefined.
/// @tparam T Array value type
/// @tparam N Array length
/// @tparam funcs function type
/// @param a Array to be the first argument of the function
/// @param b Array to be the second argument of the function
/// @param func Function to which a value is entered
/// @return Array of results of inputting arguments a and b to function func, respectively
template <typename T, std::size_t N, typename funcs>
inline std::array<T, N> apply(std::array<T, N> a, std::array<T, N> b, funcs func) noexcept
{
std::array<T, N> result;
for (unsigned long long i = 0; i < a.size(); ++i)
{
result[i] = func(a[i], b[i]);
}
return result;
}
/// @brief Passing two arrays to a function
/// The values of the two arrays are passed one by one to the function, and the result is returned as a new array.
/// The computational complexity is @c O(N) , where @c N is the length of the array.
/// If the array lengths are not equal, an exception is thrown.
/// The behavior when the function is other than T(T,T) is undefined.
/// @tparam T Array value type
/// @tparam funcs function type
/// @param a Array to be the first argument of the function
/// @param b Array to be the second argument of the function
/// @param func Function to which a value is entered
/// @return Array of results of inputting arguments a and b to function func, respectively
/// @exception @c std::invaid_argument
template <typename T, typename funcs>
inline std::list<T> apply(std::list<T> a, std::list<T> b, funcs func)
{
if (a.size() != b.size())
{
throw std::invalid_argument("Arrays must be of equal length.");
}
std::vector<T> A(a.begin(), a.end()), B(b.begin(), b.end());
std::vector<T> result = apply(A, B, func);
return std::list<T>(result.begin(), result.end());
}
}
}
#endif // ALGOCPP_ARRAY_APPLY