NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
trim_zeros.hpp
Go to the documentation of this file.
1 #pragma once
29 
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <string>
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
52  template<typename dtype>
53  NdArray<dtype> trim_zeros(const NdArray<dtype>& inArray, const std::string& inTrim = "fb")
54  {
56 
57  if (inTrim == "f")
58  {
59  uint32 place = 0;
60  for (auto value : inArray)
61  {
62  if (value != dtype{ 0 })
63  {
64  break;
65  }
66 
67  ++place;
68  }
69 
70  if (place == inArray.size())
71  {
72  return NdArray<dtype>(0);
73  }
74 
75  NdArray<dtype> returnArray(1, inArray.size() - place);
76  stl_algorithms::copy(inArray.cbegin() + place, inArray.cend(), returnArray.begin());
77 
78  return returnArray;
79  }
80 
81  if (inTrim == "b")
82  {
83  uint32 place = inArray.size();
84  for (uint32 i = inArray.size() - 1; i > 0; --i)
85  {
86  if (inArray[i] != dtype{ 0 })
87  {
88  break;
89  }
90 
91  --place;
92  }
93 
94  if (place == 0 || (place == 1 && inArray[0] == dtype{ 0 }))
95  {
96  return NdArray<dtype>(0);
97  }
98 
99  NdArray<dtype> returnArray(1, place);
100  stl_algorithms::copy(inArray.cbegin(), inArray.cbegin() + place, returnArray.begin());
101 
102  return returnArray;
103  }
104 
105  if (inTrim == "fb")
106  {
107  uint32 placeBegin = 0;
108  for (auto value : inArray)
109  {
110  if (value != dtype{ 0 })
111  {
112  break;
113  }
114 
115  ++placeBegin;
116  }
117 
118  if (placeBegin == inArray.size())
119  {
120  return NdArray<dtype>(0);
121  }
122 
123  uint32 placeEnd = inArray.size();
124  for (uint32 i = inArray.size() - 1; i > 0; --i)
125  {
126  if (inArray[i] != dtype{ 0 })
127  {
128  break;
129  }
130 
131  --placeEnd;
132  }
133 
134  if (placeEnd == 0 || (placeEnd == 1 && inArray[0] == dtype{ 0 }))
135  {
136  return NdArray<dtype>(0);
137  }
138 
139  NdArray<dtype> returnArray(1, placeEnd - placeBegin);
140  stl_algorithms::copy(inArray.cbegin() + placeBegin, inArray.cbegin() + placeEnd, returnArray.begin());
141 
142  return returnArray;
143  }
144 
145  THROW_INVALID_ARGUMENT_ERROR("trim options are 'f' = front, 'b' = back, 'fb' = front and back.");
146  return {};
147  }
148 } // namespace nc
StaticAsserts.hpp
Error.hpp
STATIC_ASSERT_ARITHMETIC_OR_COMPLEX
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:50
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::stl_algorithms::copy
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:95
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
nc
Definition: Coordinate.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1143
StlAlgorithms.hpp
Types.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1087
nc::trim_zeros
NdArray< dtype > trim_zeros(const NdArray< dtype > &inArray, const std::string &inTrim="fb")
Definition: trim_zeros.hpp:53