From 87e4728f9c98d7975a11c15c35f6266729403c04 Mon Sep 17 00:00:00 2001 From: namark <namark@disroot.org> Date: Sat, 23 Oct 2021 23:22:48 +0400 Subject: [PATCH] Concatenation and offsetting for integer sequences. --- source/simple/support/carcdr.hpp | 28 +++++++++++++++++++++++++++- unit_tests/carcdr.cpp | 12 ++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/source/simple/support/carcdr.hpp b/source/simple/support/carcdr.hpp index 875dc25..37804b0 100644 --- a/source/simple/support/carcdr.hpp +++ b/source/simple/support/carcdr.hpp @@ -6,7 +6,7 @@ namespace simple::support { - using std::size_t; + using std::size_t; // TODO: hmm... template <typename Int, Int... Values> using lisp_list = std::integer_sequence<Int, Values...>; @@ -57,6 +57,32 @@ namespace simple::support return carcdr<List>::car; }(); + // TODO: not car cdr anymore, move + template <typename IntSec, typename OtherIntSec> + struct concat_seq; + + template <typename Int, Int... ones, Int... others> + struct concat_seq<std::integer_sequence<Int, ones...>, std::integer_sequence<Int,others...>> + { + using type = std::integer_sequence<Int, ones..., others...>; + }; + + template <typename A, typename B> + using concat_seq_t = typename concat_seq<A,B>::type; + + + template <typename Int, Int, typename Seq> + struct offset_seq; + + template <typename Int, Int offset, Int... Is> + struct offset_seq<Int, offset, std::integer_sequence<Int, Is...>> + { + using type = std::integer_sequence<Int, (Is + offset)...>; + }; + + template <typename Int, Int offset, typename Seq> + using offset_seq_t = typename offset_seq<Int, offset, Seq>::type; + } // namespace simple::support #endif /* end of include guard */ diff --git a/unit_tests/carcdr.cpp b/unit_tests/carcdr.cpp index 4ac8f2a..1b60721 100644 --- a/unit_tests/carcdr.cpp +++ b/unit_tests/carcdr.cpp @@ -2,6 +2,9 @@ using namespace simple::support; +template <int... Is> +using iseq = std::integer_sequence<int, Is...>; + int main() { using ints = lisp_list<int, -1, 10, 13, 999>; @@ -14,5 +17,14 @@ int main() static_assert(std::is_same_v<cdr<cdr<cdr<cdr<ints>>>>, lisp_list<int>>); + + static_assert(std::is_same_v< + concat_seq_t<iseq<1,2>, iseq<3,4,5>>, + iseq<1,2,3,4,5>>); + + static_assert(std::is_same_v< + offset_seq_t<int, -3, iseq<1,2,3>>, + iseq<-2,-1,0>>); + return 0; } -- GitLab