xtensor
 
Loading...
Searching...
No Matches
xset_operation.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_XSET_OPERATION_HPP
11#define XTENSOR_XSET_OPERATION_HPP
12
13#include <algorithm>
14#include <functional>
15#include <type_traits>
16
17#include <xtl/xsequence.hpp>
18
19#include "../containers/xscalar.hpp"
20#include "../core/xfunction.hpp"
21#include "../core/xmath.hpp"
22#include "../core/xstrides.hpp"
23#include "../utils/xutils.hpp"
24#include "../views/xstrided_view.hpp"
25
26namespace xt
27{
28
29 namespace detail
30 {
31
32 template <bool lvalue>
33 struct lambda_isin
34 {
35 template <class E>
36 static auto make(E&& e)
37 {
38 return [&e](const auto& t)
39 {
40 return std::find(e.begin(), e.end(), t) != e.end();
41 };
42 }
43 };
44
45 template <>
46 struct lambda_isin<false>
47 {
48 template <class E>
49 static auto make(E&& e)
50 {
51 return [e](const auto& t)
52 {
53 return std::find(e.begin(), e.end(), t) != e.end();
54 };
55 }
56 };
57
58 }
59
70 template <class E, class T>
71 inline auto isin(E&& element, std::initializer_list<T> test_elements) noexcept
72 {
73 auto lambda = [test_elements](const auto& t)
74 {
75 return std::find(test_elements.begin(), test_elements.end(), t) != test_elements.end();
76 };
77 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
78 }
79
90 template <class E, class F>
91 inline auto isin(E&& element, F&& test_elements) noexcept
93 {
94 auto lambda = detail::lambda_isin<std::is_lvalue_reference<F>::value>::make(std::forward<F>(test_elements
95 ));
96 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
97 }
98
110 template <class E, iterator_concept I>
111 inline auto isin(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
112 {
113 auto lambda = [&test_elements_begin, &test_elements_end](const auto& t)
114 {
115 return std::find(test_elements_begin, test_elements_end, t) != test_elements_end;
116 };
117 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
118 }
119
130 template <class E, class T>
131 inline auto in1d(E&& element, std::initializer_list<T> test_elements) noexcept
132 {
133 XTENSOR_ASSERT(element.dimension() == 1ul);
134 return isin(std::forward<E>(element), std::forward<std::initializer_list<T>>(test_elements));
135 }
136
147 template <class E, class F>
148 inline auto in1d(E&& element, F&& test_elements) noexcept
150 {
151 XTENSOR_ASSERT(element.dimension() == 1ul);
152 XTENSOR_ASSERT(test_elements.dimension() == 1ul);
153 return isin(std::forward<E>(element), std::forward<F>(test_elements));
154 }
155
167 template <class E, iterator_concept I>
168 inline auto in1d(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
169 {
170 XTENSOR_ASSERT(element.dimension() == 1ul);
171 return isin(
172 std::forward<E>(element),
173 std::forward<I>(test_elements_begin),
174 std::forward<I>(test_elements_end)
175 );
176 }
177
187 template <class E1, class E2>
188 inline auto searchsorted(E1&& a, E2&& v, bool right = true)
189 {
190 XTENSOR_ASSERT(std::is_sorted(a.cbegin(), a.cend()));
191
192 auto out = xt::empty<size_t>(v.shape());
193
194 if (right)
195 {
196 for (size_t i = 0; i < v.size(); ++i)
197 {
198 out(i) = static_cast<std::size_t>(std::lower_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
199 }
200 }
201 else
202 {
203 for (size_t i = 0; i < v.size(); ++i)
204 {
205 out(i) = static_cast<std::size_t>(std::upper_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
206 }
207 }
208
209
210 return out;
211 }
212
213}
214
215#endif
auto in1d(E &&element, std::initializer_list< T > test_elements) noexcept
in1d
auto isin(E &&element, std::initializer_list< T > test_elements) noexcept
isin
standard mathematical functions for xexpressions
auto make_lambda_xfunction(F &&lambda, E &&... args)
Create a xfunction from a lambda.
Definition xmath.hpp:1085
auto searchsorted(E1 &&a, E2 &&v, bool right=true)
Find indices where elements should be inserted to maintain order.
xarray< T, L > empty(const S &shape)
Create a xcontainer (xarray, xtensor or xtensor_fixed) with uninitialized values of with value_type T...
Definition xbuilder.hpp:89