MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlContainerHelpers.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2021, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#pragma once
14
15#include "mlUtilsSystem.h"
16#include <algorithm>
17#include <iterator>
18
25template <class T> struct Is
26{
28 template <typename U> constexpr bool in(U a)
29 {
30 return a == d_;
31 }
32
33 template <class Arg, class... Args> constexpr bool in(Arg a, Args... args)
34 {
35 return in(a) || in(args...);
36 }
37};
38
39template <class T> constexpr Is<T> is(T d)
40{
41 return Is<T>{d};
42}
43
44
63template <class T, class R, typename I, I (T::*Count)() const, R* (T::*Access)(I) const> class ContainerProxy
64{
65 const T* _object;
66
67public:
68 using value_type = R*;
69
70 explicit ContainerProxy(const T& object)
71 : _object(&object)
72 {}
73
74 ContainerProxy(const ContainerProxy&) = default;
78
80 {
81 public:
82 using iterator_category = std::bidirectional_iterator_tag;
86 using const_pointer = const pointer;
89
91 : _index{index}
92 , _proxy{&proxy}
93 {}
94
95 iterator(const iterator&) = default;
96 iterator& operator=(const iterator&) = default;
97 iterator(iterator&&) = default;
99
100 I index() const
101 {
102 return _index;
103 }
104
106 {
107 return (_proxy->_object->*Access)(_index);
108 }
109
111 {
112 return operator*();
113 }
114
116 {
117 _index += 1;
118 return *this;
119 }
120
122 {
123 iterator tmp = *this;
124 ++*this;
125 return tmp;
126 }
127
129 {
130 _index -= 1;
131 return *this;
132 }
133
135 {
136 iterator tmp = *this;
137 --*this;
138 return tmp;
139 }
140
141 friend bool operator==(const iterator& x, const iterator& y)
142 {
143 return x._proxy == y._proxy && x._index == y._index;
144 }
145
146 friend bool operator!=(const iterator& x, const iterator& y)
147 {
148 return !(x == y);
149 }
150
151
152 private:
153 I _index;
154 const ContainerProxy* _proxy;
155 };
156
158
160 {
161 return iterator(*this, 0);
162 }
163
164 iterator end() const
165 {
166 return iterator(*this, (_object->*Count)());
167 }
168
170 {
171 return begin();
172 }
173
175 {
176 return end();
177 }
178};
179
180/*
181 * Checks whether the provided second range is a rotation of the first range. Hereby it uses the predicate
182 * \p p to compare the values of the two ranges.
183 * If it is a rotation, then it returns the rotation point of the second range.
184 * If it is not a rotation or both ranges are empty, then it returns the end of the second range.
185 * \range1 The first range
186 * \range2 The second range
187 * \p The predicate that is used to compare two values
188 */
189template <typename Range1, typename Range2, typename BinaryPredicate>
190constexpr typename Range2::const_iterator is_rotated(const Range1& range1, const Range2& range2, BinaryPredicate p)
191{
192 if (std::size(range1) != std::size(range2)) {
193 return std::end(range2);
194 }
195 if (std::size(range1) == 0) {
196 return std::end(range2);
197 }
198 const auto itFind2 =
199 std::find_if(std::begin(range2), std::end(range2), [p, &range1](const auto& v) { return p(*begin(range1), v); });
200
201 if (itFind2 == std::end(range2)) {
202 return std::end(range2);
203 }
204 auto resultIts = std::mismatch(itFind2, std::end(range2), std::begin(range1), std::end(range1), p);
205 if (resultIts.first != std::end(range2)) {
206 return std::end(range2);
207 }
208 resultIts = std::mismatch(std::begin(range2), itFind2, resultIts.second, std::end(range1), p);
209 if (resultIts.first != itFind2) {
210 return std::end(range2);
211 }
212 return itFind2;
213}
214
215/*
216 * Checks whether the provided second range is a rotation of the first range.
217 * If it is a rotation, then it returns the rotation point of the second range.
218 * If it is not a rotation or both ranges are empty, then it returns the end of the second range.
219 * \range1 The first range
220 * \range2 The second range
221 */
222template <typename Range1, typename Range2>
223constexpr typename Range2::const_iterator is_rotated(const Range1& range1, const Range2& range2)
224{
225 if (std::size(range1) != std::size(range2)) {
226 return std::end(range2);
227 }
228 if (std::size(range1) == 0) {
229 return std::end(range2);
230 }
231 const auto itFind2 = std::find(std::begin(range2), std::end(range2), *begin(range1));
232
233 if (itFind2 == std::end(range2)) {
234 return std::end(range2);
235 }
236 auto resultIts = std::mismatch(itFind2, std::end(range2), std::begin(range1), std::end(range1));
237 if (resultIts.first != std::end(range2)) {
238 return std::end(range2);
239 }
240 resultIts = std::mismatch(std::begin(range2), itFind2, resultIts.second, std::end(range1));
241 if (resultIts.first != itFind2) {
242 return std::end(range2);
243 }
244 return itFind2;
245}
246
247
@ R
@ T
@ U
@ I
friend bool operator==(const iterator &x, const iterator &y)
iterator & operator=(const iterator &)=default
iterator & operator=(iterator &&)=default
friend bool operator!=(const iterator &x, const iterator &y)
std::bidirectional_iterator_tag iterator_category
iterator(iterator &&)=default
iterator(const iterator &)=default
typename ContainerProxy::value_type value_type
iterator(const ContainerProxy &proxy, I index)
This template implements a proxy for containers that contain a collection of items normally accessed ...
ContainerProxy(const T &object)
iterator begin() const
const_iterator cend() const
ContainerProxy(ContainerProxy &&)=default
ContainerProxy & operator=(ContainerProxy &&)=default
const_iterator cbegin() const
ContainerProxy(const ContainerProxy &)=default
ContainerProxy & operator=(const ContainerProxy &)=default
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
constexpr Is< T > is(T d)
constexpr Range2::const_iterator is_rotated(const Range1 &range1, const Range2 &range2, BinaryPredicate p)
In combination with the in function, it returns true if a passed value is in the set of possible valu...
constexpr bool in(Arg a, Args... args)
constexpr bool in(U a)