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