NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
row_stack.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <initializer_list>
31#include <vector>
32
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 namespace detail
41 {
42 //============================================================================
43 // Method Description:
51 template<typename dtype, typename Iterator>
52 NdArray<dtype> row_stack(Iterator begin, Iterator end)
53 {
54 // first loop through to calculate the final size of the array
55 Shape finalShape;
56 auto iter = begin;
57 while (iter != end)
58 {
59 const auto& ndarray = *iter++;
60 if (ndarray.shape().isnull())
61 {
62 continue;
63 }
64
65 if (finalShape.isnull())
66 {
67 finalShape = ndarray.shape();
68 }
69 else if (ndarray.shape().cols != finalShape.cols)
70 {
71 THROW_INVALID_ARGUMENT_ERROR("input arrays must have the same number of columns.");
72 }
73 else
74 {
75 finalShape.rows += ndarray.shape().rows;
76 }
77 }
78
79 // now that we know the final size, contruct the output array
80 NdArray<dtype> returnArray(finalShape);
81 uint32 rowStart = 0;
82 iter = begin;
83 while (iter != end)
84 {
85 const auto& ndarray = *iter++;
86 const Shape theShape = ndarray.shape();
87 for (uint32 row = 0; row < theShape.rows; ++row)
88 {
89 for (uint32 col = 0; col < theShape.cols; ++col)
90 {
91 returnArray(rowStart + row, col) = ndarray(row, col);
92 }
93 }
94 rowStart += theShape.rows;
95 }
96
97 return returnArray;
98 }
99 } // namespace detail
100
101 //============================================================================
102 // Method Description:
109 template<typename dtype>
110 NdArray<dtype> row_stack(const std::initializer_list<NdArray<dtype>>& inArrayList)
111 {
112 return detail::row_stack<dtype>(inArrayList.begin(), inArrayList.end());
113 }
114
115 //============================================================================
116 // Method Description:
123 template<typename dtype>
124 NdArray<dtype> row_stack(const std::vector<NdArray<dtype>>& inArrayList)
125 {
126 return detail::row_stack<dtype>(inArrayList.begin(), inArrayList.end());
127 }
128} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
bool isnull() const noexcept
Definition Core/shape.hpp:115
uint32 rows
Definition Core/shape.hpp:44
uint32 cols
Definition Core/shape.hpp:45
Definition column_stack.hpp:40
NdArray< dtype > row_stack(Iterator begin, Iterator end)
Definition row_stack.hpp:52
Definition Cartesian.hpp:39
NdArray< dtype > row_stack(const std::initializer_list< NdArray< dtype > > &inArrayList)
Definition row_stack.hpp:110
std::uint32_t uint32
Definition Types.hpp:40