NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
vsplit.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <vector>
31 
33 #include "NumCpp/NdArray.hpp"
34 
35 namespace nc
36 {
37  //============================================================================
38  // Method Description:
48  template<typename dtype>
49  std::vector<NdArray<dtype>> vsplit(const NdArray<dtype>& inArray, const NdArray<int32>& indices)
50  {
51  const auto numRows = inArray.numRows();
52  auto uniqueIndices = unique(indices);
53  for (auto& index : uniqueIndices)
54  {
55  if (index < 0)
56  {
57  index += numRows;
58  }
59  }
60  uniqueIndices = unique(uniqueIndices);
61 
62  std::vector<NdArray<dtype>> splits{};
63  splits.reserve(uniqueIndices.size() + 1);
64 
65  const auto cSlice = inArray.cSlice();
66  int32 lowerIdx = 0;
67  for (const auto index : uniqueIndices)
68  {
69  if (static_cast<uint32>(index) > numRows)
70  {
71  break;
72  }
73 
74  splits.push_back(inArray({ lowerIdx, index }, cSlice));
75  lowerIdx = index;
76  }
77 
78  if (static_cast<uint32>(lowerIdx) < numRows)
79  {
80  splits.push_back(inArray({ lowerIdx, static_cast<int32>(numRows) }, cSlice));
81  }
82 
83  return splits;
84  }
85 } // namespace nc
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
Slice cSlice(int32 inStartIdx=0, uint32 inStepSize=1) const noexcept
Definition: NdArrayCore.hpp:969
uint32 numRows() const noexcept
Definition: NdArrayCore.hpp:3264
Definition: Coordinate.hpp:45
std::vector< NdArray< dtype > > vsplit(const NdArray< dtype > &inArray, const NdArray< int32 > &indices)
Definition: vsplit.hpp:49
std::int32_t int32
Definition: Types.hpp:36
NdArray< dtype > unique(const NdArray< dtype > &inArray)
Definition: unique.hpp:54
std::uint32_t uint32
Definition: Types.hpp:40