NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
vsplit.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <vector>
31
33#include "NumCpp/NdArray.hpp"
34
35namespace nc
36{
37 //============================================================================
38 // Method Description:
48 template<typename dtype, typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
49 std::vector<NdArray<dtype>> vsplit(const NdArray<dtype>& inArray, const Indices& indices)
50 {
51 const auto numRows = static_cast<int32>(inArray.numRows());
52 NdArray<int32> uniqueIndices(1, indices.size());
53 stl_algorithms::transform(indices.begin(),
54 indices.end(),
55 uniqueIndices.begin(),
56 [numRows](auto index) noexcept -> int32
57 {
58 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
59 {
60 if (index < 0)
61 {
62 index = std::max(index + numRows, int32{ 0 });
63 }
64 }
65 if (index > numRows - 1)
66 {
67 index = numRows - 1;
68 }
69
70 return static_cast<int32>(index);
71 });
72 uniqueIndices = unique(uniqueIndices);
73
74 std::vector<NdArray<dtype>> splits{};
75 splits.reserve(uniqueIndices.size() + 1);
76
77 const auto cSlice = inArray.cSlice();
78 int32 lowerIdx = 0;
79 for (const auto index : uniqueIndices)
80 {
81 if (index == 0)
82 {
83 splits.push_back(NdArray<dtype>(Shape(0, inArray.numCols())));
84 continue;
85 }
86 else
87 {
88 splits.push_back(inArray(Slice(lowerIdx, index), cSlice));
89 }
90
91 lowerIdx = index;
92 }
93
94 if (lowerIdx < numRows - 1)
95 {
96 splits.push_back(inArray(Slice(lowerIdx, numRows), cSlice));
97 }
98 else
99 {
100 splits.push_back(inArray(-1, cSlice));
101 }
102
103 return splits;
104 }
105} // namespace nc
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3477
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:775
Definition: Cartesian.hpp:40
NdArray< dtype > unique(const NdArray< dtype > &inArray)
Definition: unique.hpp:53
std::int32_t int32
Definition: Types.hpp:36
std::vector< NdArray< dtype > > vsplit(const NdArray< dtype > &inArray, const Indices &indices)
Definition: vsplit.hpp:49