aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-02-20 22:03:19 +0100
committerAdrian Kummerlaender2015-02-20 22:03:19 +0100
commitde1f1dc67a1dabcddac2e89a5d703e98a92b7f11 (patch)
tree3bea402b6a699c00f9d2f820d5583f28902d4110
parent397264a5e0046d9c84d8de41b9683ae695c516e0 (diff)
Added `ReplaceNth` list operation
* as its name implies this function replaces the _nth_ element of a given list * added appropriate test case
-rw-r--r--src/list/operation/delete.h4
-rw-r--r--src/list/operation/delete_nth.h6
-rw-r--r--src/list/operation/replace_nth.h23
-rw-r--r--test.cc15
4 files changed, 43 insertions, 5 deletions
diff --git a/src/list/operation/delete.h b/src/list/operation/delete.h
index 48f1137..5ac620a 100644
--- a/src/list/operation/delete.h
+++ b/src/list/operation/delete.h
@@ -9,11 +9,11 @@ namespace tav {
template <
typename Element,
- typename List
+ typename Sequence
>
using Delete = Remove<
Apply<IsEqualValue, _0, Element>::template function,
- List
+ Sequence
>;
}
diff --git a/src/list/operation/delete_nth.h b/src/list/operation/delete_nth.h
index 18e52bf..a834150 100644
--- a/src/list/operation/delete_nth.h
+++ b/src/list/operation/delete_nth.h
@@ -9,11 +9,11 @@ namespace tav {
template <
typename Index,
- typename List
+ typename Sequence
>
using DeleteNth = Append<
- Take<Index, List>,
- Drop<Add<Index, Size<1>>, List>
+ Take<Index, Sequence>,
+ Drop<Add<Index, Size<1>>, Sequence>
>;
}
diff --git a/src/list/operation/replace_nth.h b/src/list/operation/replace_nth.h
new file mode 100644
index 0000000..519a040
--- /dev/null
+++ b/src/list/operation/replace_nth.h
@@ -0,0 +1,23 @@
+#ifndef TYPEASVALUE_SRC_LIST_OPERATION_REPLACE_NTH_H_
+#define TYPEASVALUE_SRC_LIST_OPERATION_REPLACE_NTH_H_
+
+#include "list/operation/concatenate.h"
+#include "list/operation/take.h"
+#include "list/operation/drop.h"
+
+namespace tav {
+
+template <
+ typename Index,
+ typename Value,
+ typename Sequence
+>
+using ReplaceNth = Concatenate<List<
+ Take<Index, Sequence>,
+ List<Value>,
+ Drop<Add<Index, Size<1>>, Sequence>
+>>;
+
+}
+
+#endif // TYPEASVALUE_SRC_LIST_OPERATION_REPLACE_NTH_H_
diff --git a/test.cc b/test.cc
index 7a2e963..774f9be 100644
--- a/test.cc
+++ b/test.cc
@@ -22,6 +22,7 @@
#include "list/generator/higher/list_tabulate.h"
#include "list/operation/delete.h"
#include "list/operation/delete_nth.h"
+#include "list/operation/replace_nth.h"
#include "function/apply.h"
@@ -666,6 +667,20 @@ static_assert(
"(delete-nth 1 (list 1 2 3)) != (list 1 3)"
);
+// list replace nth
+
+static_assert(
+ std::is_same<
+ tav::List<tav::Int<1>, tav::Int<42>, tav::Int<3>>,
+ tav::ReplaceNth<
+ tav::Size<1>,
+ tav::Int<42>,
+ tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>
+ >
+ >::value,
+ "(replace-nth 1 42 (list 1 2 3)) != (list 1 42 3)"
+);
+
// list partition
static_assert(