NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
digitize.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <algorithm>
31 #include <cstdint>
32 #include <iterator>
33 
35 #include "NumCpp/NdArray.hpp"
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
50  template<typename dtype1, typename dtype2>
52  {
53  const auto uniqueBins = unique(bins);
54  auto result = NdArray<uint32>(1, x.size());
55  result.fill(0);
56 
57  typename decltype(result)::size_type idx{ 0 };
58  std::for_each(x.begin(),
59  x.end(),
60  [&uniqueBins, &result, &idx](const auto value)
61  {
62  const auto upperBin = std::upper_bound(uniqueBins.begin(), uniqueBins.end(), value);
63  result[idx++] = std::distance(uniqueBins.begin(), upperBin);
64  });
65 
66  return result;
67  }
68 } // namespace nc
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4105
iterator end() noexcept
Definition: NdArrayCore.hpp:1479
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45
NdArray< uint32 > digitize(const NdArray< dtype1 > &x, const NdArray< dtype2 > &bins)
Definition: digitize.hpp:51
NdArray< dtype > unique(const NdArray< dtype > &inArray)
Definition: unique.hpp:54