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