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