aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-01-17 17:28:02 +0100
committerAdrian Kummerlaender2015-01-17 17:28:02 +0100
commit385c51950644121d7b6e04718374b1af63209ac7 (patch)
tree4d386aadebbb4b1e28ac835f4e5e6736c05ed5dd
parentd975c2365730644a7eb15b88e02ec0e92b1fe7b1 (diff)
downloadTypeAsValue-385c51950644121d7b6e04718374b1af63209ac7.tar
TypeAsValue-385c51950644121d7b6e04718374b1af63209ac7.tar.gz
TypeAsValue-385c51950644121d7b6e04718374b1af63209ac7.tar.bz2
TypeAsValue-385c51950644121d7b6e04718374b1af63209ac7.tar.lz
TypeAsValue-385c51950644121d7b6e04718374b1af63209ac7.tar.xz
TypeAsValue-385c51950644121d7b6e04718374b1af63209ac7.tar.zst
TypeAsValue-385c51950644121d7b6e04718374b1af63209ac7.zip
Implemented `Length` function
* as its name implies this function returns the length of a given _Cons_ structure * result type is `Size<Length>` which wraps `std::size_t` to match the `sizeof` operator
-rw-r--r--src/conditional/if.h8
-rw-r--r--src/list/list.h10
-rw-r--r--src/type.h3
-rw-r--r--test.cc14
4 files changed, 29 insertions, 6 deletions
diff --git a/src/conditional/if.h b/src/conditional/if.h
index 29aa6f0..b9568f6 100644
--- a/src/conditional/if.h
+++ b/src/conditional/if.h
@@ -7,13 +7,13 @@ namespace tav {
template <
bool Condition,
- typename IfBranch,
- typename ElseBranch
+ typename TrueBranch,
+ typename FalseBranch
>
using If = typename std::conditional<
Condition,
- IfBranch,
- ElseBranch
+ TrueBranch,
+ FalseBranch
>::type;
}
diff --git a/src/list/list.h b/src/list/list.h
index edd46e8..f21d0fc 100644
--- a/src/list/list.h
+++ b/src/list/list.h
@@ -27,6 +27,16 @@ using Head = Car<Cons>;
template <typename Cons>
using Tail = Cdr<Cons>;
+template <typename Cons>
+struct Length {
+ typedef Add<Size<1>, typename Length<Cdr<Cons>>::type> type;
+};
+
+template <>
+struct Length<void> {
+ typedef Size<0> type;
+};
+
template <
typename Primary,
typename Secondary
diff --git a/src/type.h b/src/type.h
index 304bff4..74e0650 100644
--- a/src/type.h
+++ b/src/type.h
@@ -8,6 +8,9 @@ namespace tav {
template <int Value>
using Int = typename std::integral_constant<int, Value>::type;
+template <std::size_t Value>
+using Size = typename std::integral_constant<std::size_t, Value>::type;
+
template <bool Value>
using Boolean = typename std::integral_constant<bool, Value>::type;
diff --git a/test.cc b/test.cc
index 390804b..6ce3691 100644
--- a/test.cc
+++ b/test.cc
@@ -21,9 +21,9 @@ TEST_F(TypeAsValueTest, BasicMath) {
TEST_F(TypeAsValueTest, Conditional) {
// (if #t 1 2)
- EXPECT_EQ(1, ( tav::If<true, tav::Int<1>, tav::Int<2>>::value ));
+ EXPECT_EQ(1, ( tav::If<true, tav::Int<1>, tav::Int<2>>::type::value ));
// (if #f 1 2)
- EXPECT_EQ(2, ( tav::If<false, tav::Int<1>, tav::Int<2>>::value ));
+ EXPECT_EQ(2, ( tav::If<false, tav::Int<1>, tav::Int<2>>::type::value ));
}
TEST_F(TypeAsValueTest, Cons) {
@@ -38,6 +38,11 @@ TEST_F(TypeAsValueTest, Cons) {
}
TEST_F(TypeAsValueTest, List) {
+ // (length (list 1))
+ EXPECT_EQ(1, ( tav::Length<tav::List<tav::Int<1>>::type>::type::value ));
+ // (length (list 1 2))
+ EXPECT_EQ(2, ( tav::Length<tav::List<tav::Int<1>, tav::Int<2>>::type>::type::value ));
+
// (head (list 1))
EXPECT_EQ(1, ( tav::Head<tav::List<tav::Int<1>>::type>::value ));
// (head (list 1 2))
@@ -49,6 +54,11 @@ TEST_F(TypeAsValueTest, List) {
}
TEST_F(TypeAsValueTest, ListConcatenate) {
+ // (length (concatenate (list 1) (list 2)))
+ EXPECT_EQ(2, ( tav::Length<tav::Concatenate<tav::List<tav::Int<1>>::type, tav::List<tav::Int<2>>::type>::type>::type::value ));
+ // (length (concatenate (list 1 2) (list 3 4)))
+ EXPECT_EQ(4, ( tav::Length<tav::Concatenate<tav::List<tav::Int<1>, tav::Int<2>>::type, tav::List<tav::Int<3>, tav::Int<4>>::type>::type>::type::value ));
+
// (head (concatenate (list 1) (list 2)))
EXPECT_EQ(1, ( tav::Head<tav::Concatenate<tav::List<tav::Int<1>>::type, tav::List<tav::Int<2>>::type>::type>::value ));
// (head (tail (concatenate (list 1) (list 2))))