C++ Library Extensions 2022.12.09
To help learn modern C++ programming
cpg_array_tuple_vector_operations.hpp
Go to the documentation of this file.
1/*
2 Author: Thomas Kim
3 First Edit: Dec. 03, 2021
4 Second Edit: Dec. 05, 2021
5 Third Edit: Dec. 09, 2021 - safe binary operation
6 Fourth Edit: Dec. 12, 2021 - std::array - std::vector operation
7 Fifth Edit: July 06, 2022 - std::array - std::vector - tuple operation
8*/
9
10#ifndef _CGP_ARRAY_TUPLE_VECTOR_OPERATIONS_HPP
11#define _CGP_ARRAY_TUPLE_VECTOR_OPERATIONS_HPP
12
13#ifndef NOMINMAX
14#define NOMINMAX
15#endif
16
19
20namespace std::inline stl_extensions
21{
22 namespace cgt = cpg::types;
23
24 template< cgt::tuple_c Left, cgt::tuple_c Right>
25 requires requires (Left l, Right r) { l + r; }
26 auto operator+(std::vector<Left> const& L, std::vector<Right> const& R)
27 {
28 assert(L.size() == R.size());
29
30 using common_t = decltype(Left{} + Right{});
31 using vctr_t = std::vector<common_t>;
32
33 vctr_t V(L.size());
34
35 for(std::size_t i{}; i < L.size(); ++i)
36 V[i] = L[i] + R[i];
37
38 return V;
39 }
40
41 template< cgt::tuple_c Left, cgt::tuple_c Right>
42 requires requires (Left l, Right r) { l + r; }
43 auto operator-(std::vector<Left> const& L, std::vector<Right> const& R)
44 {
45 assert(L.size() == R.size());
46
47 using common_t = decltype(Left{} + Right{});
48 using vctr_t = std::vector<common_t>;
49
50 vctr_t V(L.size());
51
52 for(std::size_t i{}; i < L.size(); ++i)
53 V[i] = L[i] - R[i];
54
55 return V;
56 }
57
58 template< cgt::tuple_c Left, cgt::tuple_c Right>
59 requires requires (Left l, Right r) { l + r; }
60 auto operator*(std::vector<Left> const& L, std::vector<Right> const& R)
61 {
62 assert(L.size() == R.size());
63
64 using common_t = decltype(Left{} + Right{});
65 using vctr_t = std::vector<common_t>;
66
67 vctr_t V(L.size());
68
69 for(std::size_t i{}; i < L.size(); ++i)
70 V[i] = L[i] * R[i];
71
72 return V;
73 }
74
75 template< cgt::tuple_c Left, cgt::tuple_c Right>
76 requires requires (Left l, Right r) { l + r; }
77 auto operator/(std::vector<Left> const& L, std::vector<Right> const& R)
78 {
79 assert(L.size() == R.size());
80
81 using common_t = decltype(Left{} + Right{});
82 using vctr_t = std::vector<common_t>;
83
84 vctr_t V(L.size());
85
86 for(std::size_t i{}; i < L.size(); ++i)
87 V[i] = L[i] / R[i];
88
89 return V;
90 }
91
92 template< cgt::vector_c Left, cgt::vector_c Right, std::size_t N>
93 requires requires (Left l, Right r) { l + r; }
94 auto operator+(std::array<Left, N> const& L, std::array<Right, N> const& R)
95 {
96 using vctr_t = decltype(Left{} + Right{});
97 using array_t = std::array<vctr_t, N>;
98
99 array_t A;
100
101 for(std::size_t i{}; i < L.size(); ++i)
102 A[i] = L[i] + R[i];
103
104 return A;
105 }
106
107 template< cgt::vector_c Left, cgt::vector_c Right, std::size_t N>
108 requires requires (Left l, Right r) { l + r; }
109 auto operator-(std::array<Left, N> const& L, std::array<Right, N> const& R)
110 {
111 using vctr_t = decltype(Left{} - Right{});
112 using array_t = std::array<vctr_t, N>;
113
114 array_t A;
115
116 for(std::size_t i{}; i < L.size(); ++i)
117 A[i] = L[i] - R[i];
118
119 return A;
120 }
121
122
123 template< cgt::vector_c Left, cgt::vector_c Right, std::size_t N>
124 requires requires (Left l, Right r) { l + r; }
125 auto operator*(std::array<Left, N> const& L, std::array<Right, N> const& R)
126 {
127 using vctr_t = decltype(Left{} * Right{});
128 using array_t = std::array<vctr_t, N>;
129
130 array_t A;
131
132 for(std::size_t i{}; i < L.size(); ++i)
133 A[i] = L[i] * R[i];
134
135 return A;
136 }
137
138 template< cgt::vector_c Left, cgt::vector_c Right, std::size_t N>
139 requires requires (Left l, Right r) { l + r; }
140 auto operator/(std::array<Left, N> const& L, std::array<Right, N> const& R)
141 {
142 using vctr_t = decltype(Left{} / Right{});
143 using array_t = std::array<vctr_t, N>;
144
145 array_t A;
146
147 for(std::size_t i{}; i < L.size(); ++i)
148 A[i] = L[i] / R[i];
149
150 return A;
151 }
152
153 namespace hidden
154 {
155 template<typename S, typename T>
156 auto fn_common_container_type(S s, T t) requires requires { s + t; }
157 {
158 return s + t;
159 }
160 }
161
162 template<typename S, typename T>
165 std::declval<std::remove_cvref_t<S>>(), std::declval<std::remove_cvref_t<T>>()));
166}
167// end of namespace std
168
169#endif
170// end of file
std::common_type_t< S, T > common_t
Definition: cpg_bitwise.hpp:79
auto operator-(const std::tuple< ArgTypes1... > &tuple_a, const std::tuple< ArgTypes2... > &tuple_b)
auto operator+(const std::tuple< ArgTypes1... > &tuple_a, const std::tuple< ArgTypes2... > &tuple_b)
auto operator/(const std::tuple< ArgTypes1... > &tuple_a, const std::tuple< ArgTypes2... > &tuple_b)
auto operator*(const std::tuple< ArgTypes1... > &tuple_a, const std::tuple< ArgTypes2... > &tuple_b)
decltype(hidden::fn_common_container_type(std::declval< std::remove_cvref_t< S > >(), std::declval< std::remove_cvref_t< T > >())) common_container_t