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 <iterator>
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{
32
33
34 namespace detail
35 {
36
37 template <bool lvalue>
38 struct lambda_isin
39 {
40 template <class E>
41 static auto make(E&& e)
42 {
43 return [&e](const auto& t)
44 {
45 return std::find(e.begin(), e.end(), t) != e.end();
46 };
47 }
48 };
49
50 template <>
51 struct lambda_isin<false>
52 {
53 template <class E>
54 static auto make(E&& e)
55 {
56 return [e](const auto& t)
57 {
58 return std::find(e.begin(), e.end(), t) != e.end();
59 };
60 }
61 };
62
63 }
64
75 template <class E, class T>
76 inline auto isin(E&& element, std::initializer_list<T> test_elements) noexcept
77 {
78 auto lambda = [test_elements](const auto& t)
79 {
80 return std::find(test_elements.begin(), test_elements.end(), t) != test_elements.end();
81 };
82 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
83 }
84
95 template <class E, class F>
96 inline auto isin(E&& element, F&& test_elements) noexcept
98 {
99 auto lambda = detail::lambda_isin<std::is_lvalue_reference<F>::value>::make(std::forward<F>(test_elements
100 ));
101 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
102 }
103
115 template <class E, std::forward_iterator I>
116 inline auto isin(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
117 {
118 auto lambda = [&test_elements_begin, &test_elements_end](const auto& t)
119 {
120 return std::find(test_elements_begin, test_elements_end, t) != test_elements_end;
121 };
122 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
123 }
124
135 template <class E, class T>
136 inline auto in1d(E&& element, std::initializer_list<T> test_elements) noexcept
137 {
138 XTENSOR_ASSERT(element.dimension() == 1ul);
139 return isin(std::forward<E>(element), std::forward<std::initializer_list<T>>(test_elements));
140 }
141
152 template <class E, class F>
153 inline auto in1d(E&& element, F&& test_elements) noexcept
154 requires(iterable_expression<F>)
155 {
156 XTENSOR_ASSERT(element.dimension() == 1ul);
157 XTENSOR_ASSERT(test_elements.dimension() == 1ul);
158 return isin(std::forward<E>(element), std::forward<F>(test_elements));
159 }
160
172 template <class E, std::forward_iterator I>
173 inline auto in1d(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
174 {
175 XTENSOR_ASSERT(element.dimension() == 1ul);
176 return isin(
177 std::forward<E>(element),
178 std::forward<I>(test_elements_begin),
179 std::forward<I>(test_elements_end)
180 );
181 }
182
192 template <class E1, class E2>
193 inline auto searchsorted(E1&& a, E2&& v, bool right = true)
194 {
195 XTENSOR_ASSERT(std::is_sorted(a.cbegin(), a.cend()));
196
197 auto out = xt::empty<size_t>(v.shape());
198
199 if (right)
200 {
201 for (size_t i = 0; i < v.size(); ++i)
202 {
203 out(i) = static_cast<std::size_t>(std::lower_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
204 }
205 }
206 else
207 {
208 for (size_t i = 0; i < v.size(); ++i)
209 {
210 out(i) = static_cast<std::size_t>(std::upper_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
211 }
212 }
213
214
215 return out;
216 }
217
218}
219
220#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