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