NumCpp  1.0
A C++ implementation of the Python Numpy library
trim_zeros.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Types.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  else
68  {
69  ++place;
70  }
71  }
72 
73  if (place == inArray.size())
74  {
75  return NdArray<dtype>(0);
76  }
77 
78  NdArray<dtype> returnArray(1, inArray.size() - place);
79  stl_algorithms::copy(inArray.cbegin() + place, inArray.cend(), returnArray.begin());
80 
81  return returnArray;
82  }
83  else if (inTrim == "b")
84  {
85  uint32 place = inArray.size();
86  for (uint32 i = inArray.size() - 1; i > 0; --i)
87  {
88  if (inArray[i] != dtype{ 0 })
89  {
90  break;
91  }
92  else
93  {
94  --place;
95  }
96  }
97 
98  if (place == 0 || (place == 1 && inArray[0] == dtype{ 0 }))
99  {
100  return NdArray<dtype>(0);
101  }
102 
103  NdArray<dtype> returnArray(1, place);
104  stl_algorithms::copy(inArray.cbegin(), inArray.cbegin() + place, returnArray.begin());
105 
106  return returnArray;
107  }
108  else if (inTrim == "fb")
109  {
110  uint32 placeBegin = 0;
111  for (auto value : inArray)
112  {
113  if (value != dtype{ 0 })
114  {
115  break;
116  }
117  else
118  {
119  ++placeBegin;
120  }
121  }
122 
123  if (placeBegin == inArray.size())
124  {
125  return NdArray<dtype>(0);
126  }
127 
128  uint32 placeEnd = inArray.size();
129  for (uint32 i = inArray.size() - 1; i > 0; --i)
130  {
131  if (inArray[i] != dtype{ 0 })
132  {
133  break;
134  }
135  else
136  {
137  --placeEnd;
138  }
139  }
140 
141  if (placeEnd == 0 || (placeEnd == 1 && inArray[0] == dtype{ 0 }))
142  {
143  return NdArray<dtype>(0);
144  }
145 
146  NdArray<dtype> returnArray(1, placeEnd - placeBegin);
147  stl_algorithms::copy(inArray.cbegin() + placeBegin, inArray.cbegin() + placeEnd, returnArray.begin());
148 
149  return returnArray;
150  }
151  else
152  {
153  THROW_INVALID_ARGUMENT_ERROR("trim options are 'f' = front, 'b' = back, 'fb' = front and back.");
154  }
155 
156  return NdArray<dtype>(); // getting rid of compiler warning
157  }
158 }
StaticAsserts.hpp
nc::trim_zeros
NdArray< dtype > trim_zeros(const NdArray< dtype > &inArray, const std::string inTrim="fb")
Definition: trim_zeros.hpp:54
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:4310
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