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 <type_traits>
15
16#include <xtl/xsequence.hpp>
17
18#include "../containers/xscalar.hpp"
19#include "../core/xfunction.hpp"
20#include "../core/xmath.hpp"
21#include "../core/xstrides.hpp"
22#include "../utils/xutils.hpp"
23#include "../views/xstrided_view.hpp"
24
25namespace xt
26{
31
32
33 namespace detail
34 {
35
36 template <bool lvalue>
37 struct lambda_isin
38 {
39 template <class E>
40 static auto make(E&& e)
41 {
42 return [&e](const auto& t)
43 {
44 return std::find(e.begin(), e.end(), t) != e.end();
45 };
46 }
47 };
48
49 template <>
50 struct lambda_isin<false>
51 {
52 template <class E>
53 static auto make(E&& e)
54 {
55 return [e](const auto& t)
56 {
57 return std::find(e.begin(), e.end(), t) != e.end();
58 };
59 }
60 };
61
62 }
63
74 template <class E, class T>
75 inline auto isin(E&& element, std::initializer_list<T> test_elements) noexcept
76 {
77 auto lambda = [test_elements](const auto& t)
78 {
79 return std::find(test_elements.begin(), test_elements.end(), t) != test_elements.end();
80 };
81 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
82 }
83
94 template <class E, class F>
95 inline auto isin(E&& element, F&& test_elements) noexcept
97 {
98 auto lambda = detail::lambda_isin<std::is_lvalue_reference<F>::value>::make(std::forward<F>(test_elements
99 ));
100 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
101 }
102
114 template <class E, iterator_concept I>
115 inline auto isin(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
116 {
117 auto lambda = [&test_elements_begin, &test_elements_end](const auto& t)
118 {
119 return std::find(test_elements_begin, test_elements_end, t) != test_elements_end;
120 };
121 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
122 }
123
134 template <class E, class T>
135 inline auto in1d(E&& element, std::initializer_list<T> test_elements) noexcept
136 {
137 XTENSOR_ASSERT(element.dimension() == 1ul);
138 return isin(std::forward<E>(element), std::forward<std::initializer_list<T>>(test_elements));
139 }
140
151 template <class E, class F>
152 inline auto in1d(E&& element, F&& test_elements) noexcept
154 {
155 XTENSOR_ASSERT(element.dimension() == 1ul);
156 XTENSOR_ASSERT(test_elements.dimension() == 1ul);
157 return isin(std::forward<E>(element), std::forward<F>(test_elements));
158 }
159
171 template <class E, iterator_concept I>
172 inline auto in1d(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
173 {
174 XTENSOR_ASSERT(element.dimension() == 1ul);
175 return isin(
176 std::forward<E>(element),
177 std::forward<I>(test_elements_begin),
178 std::forward<I>(test_elements_end)
179 );
180 }
181
191 template <class E1, class E2>
192 inline auto searchsorted(E1&& a, E2&& v, bool right = true)
193 {
194 XTENSOR_ASSERT(std::is_sorted(a.cbegin(), a.cend()));
195
196 auto out = xt::empty<size_t>(v.shape());
197
198 if (right)
199 {
200 for (size_t i = 0; i < v.size(); ++i)
201 {
202 out(i) = static_cast<std::size_t>(std::lower_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
203 }
204 }
205 else
206 {
207 for (size_t i = 0; i < v.size(); ++i)
208 {
209 out(i) = static_cast<std::size_t>(std::upper_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
210 }
211 }
212
213
214 return out;
215 }
216
217}
218
219#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
auto searchsorted(E1 &&a, E2 &&v, bool right=true)
Find indices where elements should be inserted to maintain order.
standard mathematical functions for xexpressions
auto make_lambda_xfunction(F &&lambda, E &&... args)
Create a xfunction from a lambda.
Definition xmath.hpp:1085
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