Skip to content

Commit 889a059

Browse files
committed
Implemented the path::shortest() function which selects the shorter of two given Paths.
1 parent 13c3ace commit 889a059

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

‎src/h/path.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ namespace ctgl {
3030

3131
template <typename T>
3232
constexpr auto dropPrefix(T, Path<>) noexcept;
33+
34+
// Chooses the "shorter" of the given Paths using the following tabular expression:
35+
// +--------------------------------------------------+ +----+
36+
// | p1 == DNE | --> | p2 |
37+
// +-----------+--------------------------------------+ +----+
38+
// | p1 != DNE | p2 == DNE | --> | p1 |
39+
// | +-----------+--------------------------+ +----+
40+
// | | p2 != DNE | length(p1) < length(p2) | --> | p1 |
41+
// | | +--------------------------+ +----+
42+
// | | | length(p1) >= length(p2) | --> | p2 |
43+
// +-----------------------+--------------------------+ +----+
44+
template <typename... Ts, typename... Us>
45+
constexpr auto shortest(Path<Ts...> p1, Path<Us...> p2) noexcept;
3346
}
3447

3548
// Definitions
@@ -70,6 +83,19 @@ namespace ctgl {
7083
constexpr auto dropPrefix(T, Path<>) noexcept {
7184
return Path<>{};
7285
}
86+
87+
template <typename... Ts, typename... Us>
88+
constexpr auto shortest(Path<Ts...> p1, Path<Us...> p2) noexcept {
89+
if constexpr (p1 == DNE) {
90+
return p2;
91+
} else if constexpr (p2 == DNE) {
92+
return p1;
93+
} else if constexpr (length(p1) < length(p2)) {
94+
return p1;
95+
} else {
96+
return p2;
97+
}
98+
}
7399
}
74100

75101
// Convenient Type Definitions

‎src/test/path_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using namespace ctgl;
77
using namespace forge;
88

9+
910
// Unit tests for the ctgl::path::length() function.
1011
TEST(PathTest, Length) {
1112
// Empty
@@ -52,4 +53,19 @@ TEST(PathTest, DropPrefix) {
5253
EXPECT_EQ(path::dropPrefix(N2{}, Path<E12, E23>{}), Path<E23>{});
5354
EXPECT_EQ(path::dropPrefix(N3{}, Path<E34, E41>{}), (Path<E34, E41>{}));
5455
EXPECT_EQ(path::dropPrefix(N4{}, Path<E34, E41, E23>{}), (Path<E41, E23>{}));
56+
}
57+
58+
// Unit tests for the ctgl::path::shortest() function.
59+
TEST(PathTest, Shortest) {
60+
// DNE
61+
EXPECT_EQ(path::shortest(path::DNE, path::DNE), path::DNE);
62+
EXPECT_EQ(path::shortest(Path<E11>{}, path::DNE), Path<E11>{});
63+
EXPECT_EQ(path::shortest(path::DNE, Path<E11>{}), Path<E11>{});
64+
65+
// Exists
66+
EXPECT_EQ(path::shortest(Path<E12>{}, Path<E12>{}), Path<E12>{});
67+
EXPECT_EQ(path::shortest(Path<E12>{}, Path<E13>{}), Path<E12>{});
68+
EXPECT_EQ(path::shortest(Path<E13>{}, Path<E12>{}), Path<E12>{});
69+
EXPECT_EQ(path::shortest(Path<E11, E11>{}, Path<E14>{}), (Path<E11, E11>{}));
70+
EXPECT_EQ(path::shortest(Path<E14>{}, Path<E11, E11>{}), (Path<E11, E11>{}));
5571
}

0 commit comments

Comments
 (0)