aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-01-17 22:45:31 +0100
committerAdrian Kummerlaender2015-01-17 22:45:31 +0100
commit28dd281dd3e7c16185ecdb06b2a83f15bb074717 (patch)
tree30f1e6d34081434bba530dd2aaac1f72448e5c89
parentec52d63d3caca690e822e1a07ba9c2f47710b4d5 (diff)
downloadTypeAsValue-28dd281dd3e7c16185ecdb06b2a83f15bb074717.tar
TypeAsValue-28dd281dd3e7c16185ecdb06b2a83f15bb074717.tar.gz
TypeAsValue-28dd281dd3e7c16185ecdb06b2a83f15bb074717.tar.bz2
TypeAsValue-28dd281dd3e7c16185ecdb06b2a83f15bb074717.tar.lz
TypeAsValue-28dd281dd3e7c16185ecdb06b2a83f15bb074717.tar.xz
TypeAsValue-28dd281dd3e7c16185ecdb06b2a83f15bb074717.tar.zst
TypeAsValue-28dd281dd3e7c16185ecdb06b2a83f15bb074717.zip
Implemented `Take` function
* as its name implies this function _takes_ a maximum of _Count_ elements of a list * added appropriate test case
-rw-r--r--src/list/operation/basic.h35
-rw-r--r--test.cc9
2 files changed, 43 insertions, 1 deletions
diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h
index cee4cbc..dbd080e 100644
--- a/src/list/operation/basic.h
+++ b/src/list/operation/basic.h
@@ -2,6 +2,7 @@
#define TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_
#include "operation/math.h"
+#include "conditional/if.h"
namespace tav {
@@ -26,7 +27,10 @@ struct Nth {
typedef If<
equal_value<Index, Size<0>>::value,
Head<Cons>,
- typename Nth<Substract<Index, Size<1>>, Tail<Cons>>::type
+ typename Nth<
+ Substract<Index, Size<1>>,
+ Tail<Cons>
+ >::type
> type;
};
@@ -35,6 +39,35 @@ struct Nth<Index, void> {
typedef void type;
};
+template <
+ typename Count,
+ typename Current
+>
+struct Take {
+ typedef Cons<
+ Head<Current>,
+ typename Take<
+ Substract<Count, Size<1>>,
+ Tail<Current>
+ >::type
+ > type;
+};
+
+template <typename Current>
+struct Take<Size<0>, Current> {
+ typedef void type;
+};
+
+template <typename Count>
+struct Take<Count, void> {
+ typedef void type;
+};
+
+template <>
+struct Take<Size<0>, void> {
+ typedef void type;
+};
+
}
#endif // TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_
diff --git a/test.cc b/test.cc
index e8f7c47..f60adbd 100644
--- a/test.cc
+++ b/test.cc
@@ -62,6 +62,15 @@ TEST_F(TypeAsValueTest, ListNth) {
EXPECT_EQ(2, ( tav::Nth<tav::Size<1>, tav::List<tav::Int<1>, tav::Int<2>>::type>::type::value ));
}
+TEST_F(TypeAsValueTest, ListTake) {
+ // (length (take 1 (list 1 2)))
+ EXPECT_EQ(1, ( tav::Length<tav::Take<tav::Size<1>, tav::List<tav::Int<1>, tav::Int<2>>::type>::type>::type::value ));
+ // (length (take 2 (list 1 2)))
+ EXPECT_EQ(2, ( tav::Length<tav::Take<tav::Size<2>, tav::List<tav::Int<1>, tav::Int<2>>::type>::type>::type::value ));
+ // (length (take 3 (list 1 2)))
+ EXPECT_EQ(2, ( tav::Length<tav::Take<tav::Size<3>, tav::List<tav::Int<1>, tav::Int<2>>::type>::type>::type::value ));
+}
+
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 ));