NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
concatenate.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"
38
39namespace nc
40{
41 namespace detail
42 {
43 //============================================================================
44 // Method Description:
54 template<typename dtype, typename Iterator>
55 NdArray<dtype> concatenate(Iterator begin, Iterator end, Axis inAxis = Axis::NONE)
56 {
57 switch (inAxis)
58 {
59 case Axis::NONE:
60 {
61 uint32 finalSize = 0;
62 auto iter = begin;
63 while (iter != end)
64 {
65 const auto& ndarray = *iter++;
66 finalSize += ndarray.size();
67 }
68
69 NdArray<dtype> returnArray(1, finalSize);
70 uint32 offset = 0;
71 iter = begin;
72 while (iter != end)
73 {
74 const auto& ndarray = *iter++;
75 stl_algorithms::copy(ndarray.cbegin(), ndarray.cend(), returnArray.begin() + offset);
76 offset += ndarray.size();
77 }
78
79 return returnArray;
80 }
81 case Axis::ROW:
82 {
83 return row_stack<dtype>(begin, end);
84 }
85 case Axis::COL:
86 {
87 return column_stack<dtype>(begin, end);
88 }
89 default:
90 {
91 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
92 return {}; // get rid of compiler warning
93 }
94 }
95 }
96 } // namespace detail
97
98 //============================================================================
99 // Method Description:
108 template<typename dtype>
109 NdArray<dtype> concatenate(const std::initializer_list<NdArray<dtype>>& inArrayList, Axis inAxis = Axis::NONE)
110 {
111 return detail::concatenate<dtype>(inArrayList.begin(), inArrayList.end(), inAxis);
112 }
113
114 //============================================================================
115 // Method Description:
124 template<typename dtype>
125 NdArray<dtype> concatenate(const std::vector<NdArray<dtype>>& inArrayList, Axis inAxis = Axis::NONE)
126 {
127 return detail::concatenate<dtype>(inArrayList.begin(), inArrayList.end(), inAxis);
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:139
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
NdArray< dtype > concatenate(Iterator begin, Iterator end, Axis inAxis=Axis::NONE)
Definition: concatenate.hpp:55
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:97
Definition: Cartesian.hpp:40
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
NdArray< dtype > concatenate(const std::initializer_list< NdArray< dtype > > &inArrayList, Axis inAxis=Axis::NONE)
Definition: concatenate.hpp:109
std::uint32_t uint32
Definition: Types.hpp:40