forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/*************************************************************************** | ||
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht * | ||
* Copyright (c) QuantStack * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
****************************************************************************/ | ||
|
||
#ifndef XTENSOR_XSET_OPERATION_HPP | ||
#define XTENSOR_XSET_OPERATION_HPP | ||
|
||
#include <algorithm> | ||
#include <functional> | ||
#include <type_traits> | ||
|
||
#include <xtl/xsequence.hpp> | ||
|
||
#include "xfunction.hpp" | ||
#include "xutils.hpp" | ||
#include "xscalar.hpp" | ||
#include "xstrides.hpp" | ||
#include "xstrided_view.hpp" | ||
#include "xmath.hpp" | ||
|
||
namespace xt | ||
{ | ||
|
||
/** | ||
* @ingroup logical_operators | ||
* @brief isin | ||
* | ||
* Returns a boolean array of the same shape as ``element`` that is ``true`` where an element of | ||
* ``element`` is in ``test_elements`` and ``False`` otherwise. | ||
* @param element an \ref xexpression | ||
* @param test_elements an array | ||
* @return a boolean array | ||
*/ | ||
template <class E, class T> | ||
inline auto isin(E&& element, std::initializer_list<T> test_elements) noexcept | ||
{ | ||
auto lambda = [test_elements](const auto& t) { | ||
return std::find(test_elements.begin(), test_elements.end(), t) != test_elements.end(); }; | ||
return make_lambda_xfunction(std::move(lambda), std::forward<E>(element)); | ||
} | ||
|
||
/** | ||
* @ingroup logical_operators | ||
* @brief isin | ||
* | ||
* Returns a boolean array of the same shape as ``element`` that is ``true`` where an element of | ||
* ``element`` is in ``test_elements`` and ``False`` otherwise. | ||
* @param element an \ref xexpression | ||
* @param test_elements an array | ||
* @return a boolean array | ||
*/ | ||
template <class E, class F, class = typename std::enable_if_t<has_iterator_interface<F>::value>> | ||
inline auto isin(E&& element, F&& test_elements) noexcept | ||
{ | ||
auto lambda = [&test_elements](const auto& t) { | ||
return std::find(test_elements.begin(), test_elements.end(), t) != test_elements.end(); }; | ||
return make_lambda_xfunction(std::move(lambda), std::forward<E>(element)); | ||
} | ||
|
||
/** | ||
* @ingroup logical_operators | ||
* @brief isin | ||
* | ||
* Returns a boolean array of the same shape as ``element`` that is ``true`` where an element of | ||
* ``element`` is in ``test_elements`` and ``False`` otherwise. | ||
* @param element an \ref xexpression | ||
* @param test_elements_begin iterator to the beginning of an array | ||
* @param test_elements_end iterator to the end of an array | ||
* @return a boolean array | ||
*/ | ||
template <class E, class I, class = typename std::enable_if_t<is_iterator<I>::value>> | ||
inline auto isin(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept | ||
{ | ||
auto lambda = [&test_elements_begin, &test_elements_end](const auto& t) { | ||
return std::find(test_elements_begin, test_elements_end, t) != test_elements_end; }; | ||
return make_lambda_xfunction(std::move(lambda), std::forward<E>(element)); | ||
} | ||
|
||
/** | ||
* @ingroup logical_operators | ||
* @brief in1d | ||
* | ||
* Returns a boolean array of the same shape as ``element`` that is ``true`` where an element of | ||
* ``element`` is in ``test_elements`` and ``False`` otherwise. | ||
* @param element an \ref xexpression | ||
* @param test_elements an array | ||
* @return a boolean array | ||
*/ | ||
template <class E, class T> | ||
inline auto in1d(E&& element, std::initializer_list<T> test_elements) noexcept | ||
{ | ||
XTENSOR_ASSERT(element.dimension() == 1ul); | ||
return isin(std::forward<E>(element), std::forward<std::initializer_list<T>>(test_elements)); | ||
} | ||
|
||
/** | ||
* @ingroup logical_operators | ||
* @brief in1d | ||
* | ||
* Returns a boolean array of the same shape as ``element`` that is ``true`` where an element of | ||
* ``element`` is in ``test_elements`` and ``False`` otherwise. | ||
* @param element an \ref xexpression | ||
* @param test_elements an array | ||
* @return a boolean array | ||
*/ | ||
template <class E, class F, class = typename std::enable_if_t<has_iterator_interface<F>::value>> | ||
inline auto in1d(E&& element, F&& test_elements) noexcept | ||
{ | ||
XTENSOR_ASSERT(element.dimension() == 1ul); | ||
XTENSOR_ASSERT(test_elements.dimension() == 1ul); | ||
return isin(std::forward<E>(element), std::forward<F>(test_elements)); | ||
} | ||
|
||
/** | ||
* @ingroup logical_operators | ||
* @brief in1d | ||
* | ||
* Returns a boolean array of the same shape as ``element`` that is ``true`` where an element of | ||
* ``element`` is in ``test_elements`` and ``False`` otherwise. | ||
* @param element an \ref xexpression | ||
* @param test_elements_begin iterator to the beginning of an array | ||
* @param test_elements_end iterator to the end of an array | ||
* @return a boolean array | ||
*/ | ||
template <class E, class I, class = typename std::enable_if_t<is_iterator<I>::value>> | ||
inline auto in1d(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept | ||
{ | ||
XTENSOR_ASSERT(element.dimension() == 1ul); | ||
XTENSOR_ASSERT(test_elements.dimension() == 1ul); | ||
return isin(std::forward<E>(element), std::forward<I>(test_elements_begin), std::forward<I>(test_elements_end)); | ||
} | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -839,4 +839,5 @@ namespace xt | |
EXPECT_EQ(expected1, res3); | ||
EXPECT_EQ(expected2, res4); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/*************************************************************************** | ||
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht * | ||
* Copyright (c) QuantStack * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
****************************************************************************/ | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include <cstddef> | ||
|
||
#include "xtensor/xarray.hpp" | ||
#include "xtensor/xtensor.hpp" | ||
#include "xtensor/xset_operation.hpp" | ||
|
||
namespace xt | ||
{ | ||
TEST(xset_operation, isin) | ||
{ | ||
xt::xtensor<int,2> a = {{1, 2, 1}, {0, 3, 1}}; | ||
xt::xtensor<int,1> b = {1, 2}; | ||
xt::xtensor<bool,2> res = {{true, true, true}, {false, false, true}}; | ||
EXPECT_EQ(xt::isin(a, b), res); | ||
EXPECT_EQ(xt::isin(a, b.begin(), b.end()), res); | ||
EXPECT_EQ(xt::isin(a, {1, 2}), res); | ||
} | ||
|
||
TEST(xset_operation, in1d) | ||
{ | ||
xt::xtensor<int,1> a = {1, 2, 1, 0, 3, 5, 1}; | ||
xt::xtensor<int,1> b = {1, 2}; | ||
xt::xtensor<bool,1> res = {true, true, true, false, false, false, true}; | ||
EXPECT_EQ(xt::in1d(a, b), res); | ||
EXPECT_EQ(xt::in1d(a, b.begin(), b.end()), res); | ||
EXPECT_EQ(xt::in1d(a, {1, 2}), res); | ||
} | ||
} |