NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
searchsorted.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <iterator>
32
33#include "NumCpp/Core/Enums.hpp"
35#include "NumCpp/NdArray.hpp"
36
37namespace nc
38{
39 //============================================================================
40 // Method Description:
57 template<typename dtype>
59 searchsorted(const NdArray<dtype>& inArray, dtype inValue, Side side = Side::LEFT)
60 {
62
63 switch (side)
64 {
65 case Side::LEFT:
66 {
67 return std::distance(inArray.begin(), std::lower_bound(inArray.begin(), inArray.end(), inValue));
68 }
69 case Side::RIGHT:
70 {
71 return std::distance(inArray.begin(), std::upper_bound(inArray.begin(), inArray.end(), inValue));
72 }
73 }
74
75 // get rid of compiler warning
76 return {};
77 }
78
79 //============================================================================
80 // Method Description:
97 template<typename dtype>
99 searchsorted(const NdArray<dtype>& inArray, const NdArray<dtype>& inValues, Side side = Side::LEFT)
100 {
102 std::transform(inValues.begin(),
103 inValues.end(),
104 indices.begin(),
105 [&inArray, side](const auto& value) { return searchsorted(inArray, value, side); });
106 return indices;
107 }
108} // namespace nc
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:56
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
int32 index_type
Definition: NdArrayCore.hpp:157
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:775
Definition: Cartesian.hpp:40
Side
Definition: Enums.hpp:129
NdArray< dtype >::index_type searchsorted(const NdArray< dtype > &inArray, dtype inValue, Side side=Side::LEFT)
Definition: searchsorted.hpp:59