NumCpp  2.14.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
trim_zeros.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <string>
31
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 //============================================================================
41 // Method Description:
51 template<typename dtype>
52 NdArray<dtype> trim_zeros(const NdArray<dtype>& inArray, const std::string& inTrim = "fb")
53 {
55
56 if (inTrim == "f")
57 {
58 uint32 place = 0;
59 for (auto value : inArray)
60 {
61 if (!utils::essentiallyEqual(value, dtype{ 0 }))
62 {
63 break;
64 }
65
66 ++place;
67 }
68
69 if (place == inArray.size())
70 {
71 return NdArray<dtype>(0);
72 }
73
75 stl_algorithms::copy(inArray.cbegin() + place, inArray.cend(), returnArray.begin());
76
77 return returnArray;
78 }
79
80 if (inTrim == "b")
81 {
82 uint32 place = inArray.size();
83 for (uint32 i = inArray.size() - 1; i > 0; --i)
84 {
86 {
87 break;
88 }
89
90 --place;
91 }
92
93 if (place == 0 || (place == 1 && utils::essentiallyEqual(inArray[0], dtype{ 0 })))
94 {
95 return NdArray<dtype>(0);
96 }
97
99 stl_algorithms::copy(inArray.cbegin(), inArray.cbegin() + place, returnArray.begin());
100
101 return returnArray;
102 }
103
104 if (inTrim == "fb")
105 {
106 uint32 placeBegin = 0;
107 for (auto value : inArray)
108 {
109 if (!utils::essentiallyEqual(value, dtype{ 0 }))
110 {
111 break;
112 }
113
114 ++placeBegin;
115 }
116
117 if (placeBegin == inArray.size())
118 {
119 return NdArray<dtype>(0);
120 }
121
122 uint32 placeEnd = inArray.size();
123 for (uint32 i = inArray.size() - 1; i > 0; --i)
124 {
126 {
127 break;
128 }
129
130 --placeEnd;
131 }
132
133 if (placeEnd == 0 || (placeEnd == 1 && utils::essentiallyEqual(inArray[0], dtype{ 0 })))
134 {
135 return NdArray<dtype>(0);
136 }
137
139 stl_algorithms::copy(inArray.cbegin() + placeBegin, inArray.cbegin() + placeEnd, returnArray.begin());
140
141 return returnArray;
142 }
143
144 THROW_INVALID_ARGUMENT_ERROR("trim options are 'f' = front, 'b' = back, 'fb' = front and back.");
145 return {};
146 }
147} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition StaticAsserts.hpp:56
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition StlAlgorithms.hpp:97
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:49
Definition Cartesian.hpp:40
void place(NdArray< dtype > &arr, const NdArray< bool > &mask, const NdArray< dtype > &vals)
Definition place.hpp:47
NdArray< dtype > trim_zeros(const NdArray< dtype > &inArray, const std::string &inTrim="fb")
Definition trim_zeros.hpp:52
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
std::uint32_t uint32
Definition Types.hpp:40