Skip to content

Commit 13c3ace

Browse files
committed
Converted '*' into the Cartesian product operator.
1 parent d77a66b commit 13c3ace

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

‎src/h/list.h

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,29 @@ namespace ctgl {
4747
template <typename... Ts>
4848
constexpr bool operator==(List<Ts...>, List<Ts...>) noexcept;
4949

50-
// Adds an element to the front of a List.
50+
// Prepends the given element to the provided List.
5151
template <typename T, typename... Ts>
5252
constexpr auto operator+(T, List<Ts...>) noexcept;
5353

54-
// Adds an element to the back of a List.
54+
// Appends the given element to the provided List.
5555
template <typename... Ts, typename T>
5656
constexpr auto operator+(List<Ts...>, T) noexcept;
5757

5858
// Concatenates two Lists together.
5959
template <typename... Ts, typename... Us>
6060
constexpr auto operator+(List<Ts...>, List<Us...>) noexcept;
6161

62-
// Adds the given element to each List in the provided List of Lists.
62+
// Prepends the given element to each List in the provided List of Lists.
6363
template <typename T, typename... Ts>
6464
constexpr auto operator*(T, List<Ts...>) noexcept;
65+
66+
// Appends the given element to each List in the provided List of Lists.
67+
template <typename... Ts, typename T>
68+
constexpr auto operator*(List<Ts...>, T) noexcept;
69+
70+
// Takes the Cartesian product of the given Lists of Lists.
71+
template <typename... Ts, typename... Us>
72+
constexpr auto operator*(List<Ts...>, List<Us...>) noexcept;
6573
}
6674

6775
// -------------------------------------------------------------------------
@@ -173,15 +181,37 @@ namespace ctgl {
173181
return List<Ts..., Us...>{};
174182
}
175183

176-
template <typename T>
177-
constexpr auto operator*(T, List<>) noexcept {
184+
template <typename T, typename... Ts>
185+
constexpr auto operator*(T, List<Ts...>) noexcept {
186+
return List<List<T>>{} * List<Ts...>{};
187+
}
188+
189+
template <typename... Ts, typename T>
190+
constexpr auto operator*(List<Ts...>, T) noexcept {
191+
return List<Ts...>{} * List<List<T>>{};
192+
}
193+
194+
template <typename... T>
195+
constexpr auto distribute(List<T...>, List<>) noexcept {
178196
return List<>{};
179197
}
180198

181-
template <typename T, typename... Us, typename... Ts>
182-
constexpr auto operator*(T, List<List<Us...>, Ts...>) noexcept {
183-
constexpr auto head = List<List<T, Us...>>{};
184-
constexpr auto tail = T{} * List<Ts...>{};
199+
template <typename... T, typename... U, typename... Us>
200+
constexpr auto distribute(List<T...>, List<List<U...>, Us...>) noexcept {
201+
constexpr auto head = List<List<T..., U...>>{};
202+
constexpr auto tail = distribute(List<T...>{}, List<Us...>{});
203+
return head + tail;
204+
}
205+
206+
template <typename... Us>
207+
constexpr auto operator*(List<>, List<Us...>) noexcept {
208+
return List<>{};
209+
}
210+
211+
template <typename... T, typename... Ts, typename... Us>
212+
constexpr auto operator*(List<List<T...>, Ts...>, List<Us...>) noexcept {
213+
constexpr auto head = distribute(List<T...>{}, List<Us...>{});
214+
constexpr auto tail = List<Ts...>{} * List<Us...>{};
185215
return head + tail;
186216
}
187217

‎src/test/list_test.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,23 @@ TEST(ListTest, Plus) {
135135
TEST(ListTest, Star) {
136136
// Empty
137137
EXPECT_EQ(int{} * List<>{}, List<>{});
138+
EXPECT_EQ(List<>{} * int{}, List<>{});
139+
EXPECT_EQ(List<>{} * List<>{}, List<>{});
138140

139141
// Single
140-
EXPECT_EQ(int{} * List<List<int>>{}, (List<List<int, int>>{}));
141142
EXPECT_EQ(int{} * List<List<bool>>{}, (List<List<int, bool>>{}));
143+
EXPECT_EQ(List<List<bool>>{} * int{}, (List<List<bool, int>>{}));
144+
EXPECT_EQ(List<List<int>>{} * List<List<bool>>{}, (List<List<int, bool>>{}));
142145

143146
// Multiple
144147
EXPECT_EQ(int{} * (List<List<float, double>>{}), (List<List<int, float, double>>{}));
145148
EXPECT_EQ(int{} * (List<List<bool>, List<long>>{}), (List<List<int, bool>, List<int, long>>{}));
149+
EXPECT_EQ((List<List<float, double>>{}) * int{}, (List<List<float, double, int>>{}));
150+
EXPECT_EQ((List<List<bool>, List<long>>{}) * int{}, (List<List<bool, int>, List<long, int>>{}));
151+
EXPECT_EQ((List<List<bool>, List<long>>{}) * (List<List<int>, List<char>>{}), (List<List<bool, int>,
152+
List<bool, char>,
153+
List<long, int>,
154+
List<long, char>>{}));
146155
}
147156

148157
// Unit tests for the ctgl::list::<< operator.

0 commit comments

Comments
 (0)