CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// -*- C++ -*-
12#ifndef __cxz_utils_h__
13#define __cxz_utils_h__
14
15#include <cstdlib>
16#include <algorithm>
17#include "types.h"
18
19namespace CNORXZ
20{
21 template <typename T, SizeT N>
23 {
24 return iter<0,N>( [&](auto i) { return std::get<i>(a); },
25 [](const auto&... e) { return Vector<T> { e... }; } );
26 }
27
28 template <typename T>
30 {
31 return a;
32 }
33
34 template <typename T>
35 Vector<T> toVec(const T& a)
36 {
37 return Vector<T> { a };
38 }
39
40 template <typename T, SizeT N1, SizeT N2>
41 constexpr Arr<T,N1+N2> cat2(const Arr<T,N1>& a1, const Arr<T,N2>& a2)
42 {
43 return iter<0,N1+N2>
44 ( [&](auto i) { if constexpr(i < N1) { return std::get<i>(a1); } else { return std::get<i-N1>(a2); } },
45 [](const auto&... e) { return Arr<T,N1+N2> { e... }; } );
46 }
47
48 template <typename T, SizeT N1>
49 constexpr Arr<T,N1+1> cat2(const Arr<T,N1>& a1, const T& a2)
50 {
51 return iter<0,N1>
52 ( [&](auto i) { return std::get<i>(a1); },
53 [&](const auto&... e) { return Arr<T,N1+1> { e..., a2 }; } );
54 }
55
56 template <typename T, SizeT N1>
57 constexpr Arr<T,N1+1> cat2(const T& a1, const Arr<T,N1>& a2)
58 {
59 return iter<0,N1>
60 ( [&](auto i) { return std::get<i>(a2); },
61 [&](const auto&... e) { return Arr<T,N1+1> { a1, e... }; } );
62 }
63
64 template <typename T>
65 constexpr Arr<T,2> cat2(const T& a1, const T& a2)
66 {
67 return Arr<T,2> { a1, a2 };
68 }
69
70 template <typename T, SizeT N2>
72 {
73 Vector<T> o(a1.size()+N2);
74 std::copy(a1.begin(), a1.end(), o.begin());
75 std::copy(a2.begin(), a2.end(), o.begin()+a1.size());
76 return o;
77 }
78
79 template <typename T, SizeT N1>
81 {
82 Vector<T> o(N1+a2.size());
83 std::copy(a1.begin(), a1.end(), o.begin());
84 std::copy(a2.begin(), a2.end(), o.begin()+N1);
85 return o;
86 }
87
88 template <typename T>
90 {
91 Vector<T> o(a1.size()+a2.size());
92 std::copy(a1.begin(), a1.end(), o.begin());
93 std::copy(a2.begin(), a2.end(), o.begin()+a1.size());
94 return o;
95 }
96
97 template <typename T>
98 Vector<T> cat2(const Vector<T>& a1, const T& a2)
99 {
100 Vector<T> o(a1);
101 o.push_back(a2);
102 return o;
103 }
104
105 template <typename T, SizeT N1>
106 Vector<T> cat2(const T& a1, const Vector<T>& a2)
107 {
108 Vector<T> o { a1 };
109 o.insert(o.end(), a2.begin(), a2.end());
110 return o;
111 }
112
113 template <typename T1, typename T2, typename... Ts>
114 decltype(auto) concat(const T1& a1, const T2& a2, const Ts&... as)
115 {
116 if constexpr(sizeof...(Ts) != 0){
117 return cat2(a1, concat(a2, as...));
118 }
119 else {
120 return cat2(a1, a2);
121 }
122 }
123
124 template <typename T, SizeT N>
125 constexpr Arr<T,N> mul(const Arr<T,N>& a, const T& b)
126 {
127 return iter<0,N>( [&](auto i) { return std::get<i>(a) * b; },
128 [](const auto&... e) { return Arr<T,N> { e... }; } );
129 }
130
131 template <typename T>
132 Vector<T> mul(const Vector<T>& a, const T& b)
133 {
134 Vector<T> o(a.size());
135 std::transform(a.begin(), a.end(), o.begin(), [&](const auto& x) { return x*b; } );
136 return o;
137 }
138
139 template <typename T>
140 constexpr T mul(const T& a, const T& b)
141 {
142 return a*b;
143 }
144
145}
146
147#endif
decltype(auto) concat(const T1 &a1, const T2 &a2, const Ts &... as)
Definition utils.h:114
constexpr Arr< T, N1+N2 > cat2(const Arr< T, N1 > &a1, const Arr< T, N2 > &a2)
Definition utils.h:41
std::vector< T, Allocator< T > > Vector
Definition types.h:310
Sptr< Range > rangeCast(const RangePtr r)
std::array< T, N > Arr
Definition types.h:45
constexpr Arr< T, N > mul(const Arr< T, N > &a, const T &b)
Definition utils.h:125
Vector< T > toVec(const Arr< T, N > &a)
Definition utils.h:22
Declaration of all library types.