codac 2.0.0
Loading...
Searching...
No Matches
codac2_template_tools.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <memory>
13#include "codac2_Matrix.h"
14#include "codac2_Vector.h"
15
16namespace codac2
17{
18 template<class T>
19 concept IsRealType = (std::is_same_v<double,T>
20 || std::is_same_v<Vector,T>
21 || std::is_same_v<Matrix,T>);
22
23 inline Index size_of([[maybe_unused]] int x)
24 {
25 return 1;
26 }
27
28 inline Index size_of([[maybe_unused]] double x)
29 {
30 return 1;
31 }
32
33 template<typename T>
34 inline Index size_of(const T& x)
35 {
36 return x.size();
37 }
38
39 template<typename C>
40 requires is_ctc_v<C>
41 inline Index size_of(const std::shared_ptr<C>& x)
42 {
43 return x->size();
44 }
45
46 template<typename S>
47 requires is_sep_v<S>
48 inline Index size_of(const std::shared_ptr<S>& x)
49 {
50 return x->size();
51 }
52
53 template<typename T1, typename T2>
54 inline bool same_size(const T1& x1, const T2& x2)
55 {
56 return size_of(x1) == size_of(x2);
57 }
58
59 template<class... T>
60 inline bool all_same_size(const T&... x)
61 {
62 return (... && (same_size(std::get<0>(std::make_tuple(x...)), x)));
63 }
64
65 template<class... T>
66 inline Index size_first_item(const T&... x)
67 {
68 return size_of(std::get<0>(std::make_tuple(x...)));
69 }
70
71 // Removes duplicates when no comparison operator is available
72 template<typename T>
73 inline void remove_duplicates_from_list(std::list<T>& l)
74 {
75 typename std::list<T>::iterator it = l.begin();
76 while(it != l.end())
77 {
78 if(std::count(l.begin(), l.end(), *it) > 1)
79 l.erase(it++);
80 else
81 ++it;
82 }
83 }
84
85 template<int R,int C>
86 inline auto vectorVector_to_vectorIntervalVector(const std::vector<Mat<double,R,C>>& x)
87 {
88 std::vector<Mat<Interval,R,C>> v(x.size());
89 for(size_t i = 0 ; i < x.size() ; i++)
90 v[i] = x[i].template cast<Interval>();
91 return v;
92 }
93
94 template <std::size_t N>
95 static inline std::array<float, N> to_array(std::initializer_list<float> list)
96 {
97 assert(list.size() == N);
98 std::array<float, N> arr;
99 std::copy(list.begin(), list.end(), arr.begin());
100 return arr;
101 }
102
103 template<int N>
104 inline Vector to_vector(const std::array<int,N>& x)
105 {
106 Vector s(x.size());
107 for(size_t i = 0 ; i < N ; i++)
108 s[i] = x[i];
109 return s;
110 }
111}
Definition codac2_OctaSym.h:21
Eigen::Matrix< double,-1, 1 > Vector
Alias for a dynamically-sized column vector of doubles.
Definition codac2_Vector.h:24