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 "xfunction.hpp"
20#include "xmath.hpp"
21#include "xscalar.hpp"
22#include "xstrided_view.hpp"
23#include "xstrides.hpp"
24#include "xutils.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, class = typename std::enable_if_t<has_iterator_interface<F>::value>>
91 inline auto isin(E&& element, F&& test_elements) noexcept
92 {
93 auto lambda = detail::lambda_isin<std::is_lvalue_reference<F>::value>::make(std::forward<F>(test_elements
94 ));
95 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
96 }
97
109 template <class E, class I, class = typename std::enable_if_t<is_iterator<I>::value>>
110 inline auto isin(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
111 {
112 auto lambda = [&test_elements_begin, &test_elements_end](const auto& t)
113 {
115 };
116 return make_lambda_xfunction(std::move(lambda), std::forward<E>(element));
117 }
118
129 template <class E, class T>
130 inline auto in1d(E&& element, std::initializer_list<T> test_elements) noexcept
131 {
132 XTENSOR_ASSERT(element.dimension() == 1ul);
133 return isin(std::forward<E>(element), std::forward<std::initializer_list<T>>(test_elements));
134 }
135
146 template <class E, class F, class = typename std::enable_if_t<has_iterator_interface<F>::value>>
147 inline auto in1d(E&& element, F&& test_elements) noexcept
148 {
149 XTENSOR_ASSERT(element.dimension() == 1ul);
150 XTENSOR_ASSERT(test_elements.dimension() == 1ul);
151 return isin(std::forward<E>(element), std::forward<F>(test_elements));
152 }
153
165 template <class E, class I, class = typename std::enable_if_t<is_iterator<I>::value>>
166 inline auto in1d(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
167 {
168 XTENSOR_ASSERT(element.dimension() == 1ul);
169 return isin(
170 std::forward<E>(element),
171 std::forward<I>(test_elements_begin),
172 std::forward<I>(test_elements_end)
173 );
174 }
175
185 template <class E1, class E2>
186 inline auto searchsorted(E1&& a, E2&& v, bool right = true)
187 {
188 XTENSOR_ASSERT(std::is_sorted(a.cbegin(), a.cend()));
189
190 auto out = xt::empty<size_t>(v.shape());
191
192 if (right)
193 {
194 for (size_t i = 0; i < v.size(); ++i)
195 {
196 out(i) = static_cast<std::size_t>(std::lower_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
197 }
198 }
199 else
200 {
201 for (size_t i = 0; i < v.size(); ++i)
202 {
203 out(i) = static_cast<std::size_t>(std::upper_bound(a.cbegin(), a.cend(), v(i)) - a.cbegin());
204 }
205 }
206
207
208 return out;
209 }
210
211}
212
213#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.