NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
deleteIndices.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <vector>
31
34#include "NumCpp/Core/Slice.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 namespace detail
41 {
42 //============================================================================
43 // Method Description:
50 template<typename dtype, typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
51 NdArray<dtype> deleteFlatIndices(const NdArray<dtype>& inArray, Indices inIndices)
52 {
54 {
55 const auto arraySize = inArray.size();
56 stl_algorithms::for_each(inIndices.begin(),
57 inIndices.end(),
58 [arraySize](auto& value)
59 {
60 if (value < 0)
61 {
62 value += arraySize;
63 }
64 });
65 }
66
67 auto indices = unique(inIndices);
68
69 std::vector<dtype> values;
70 values.reserve(indices.size());
71 for (int32 i = 0; i < static_cast<int32>(inArray.size()); ++i)
72 {
73 if (std::binary_search(indices.begin(), indices.end(), i))
74 {
75 continue;
76 }
77
78 values.push_back(inArray[i]);
79 }
80
81 return NdArray<dtype>(values);
82 }
83
84 //============================================================================
85 // Method Description:
92 template<typename dtype, typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
93 NdArray<dtype> deleteRowIndices(const NdArray<dtype>& inArray, Indices inIndices)
94 {
95 const auto arrayRows = static_cast<int32>(inArray.numRows());
97 {
98 stl_algorithms::for_each(inIndices.begin(),
99 inIndices.end(),
100 [arrayRows](auto& value)
101 {
102 if (value < 0)
103 {
104 value += arrayRows;
105 }
106 });
107 }
108
109 auto indices = unique(inIndices);
110
111 uint32 indicesSize = 0;
112 std::for_each(indices.begin(),
113 indices.end(),
114 [arrayRows, &indicesSize](const auto& value)
115 {
116 if constexpr (std::is_signed_v<decltype(value)>)
117 {
118 if (value >= 0 && value < arrayRows)
119 {
120 ++indicesSize;
121 }
122 }
123 else
124 {
125 if (value < arrayRows)
126 {
127 ++indicesSize;
128 }
129 }
130 });
131
132 const auto arrayCols = static_cast<int32>(inArray.numCols());
133 NdArray<dtype> returnArray(arrayRows - indicesSize, arrayCols);
134
135 uint32 rowCounter = 0;
136 for (int32 row = 0; row < arrayRows; ++row)
137 {
138 if (std::binary_search(indices.begin(), indices.end(), row))
139 {
140 continue;
141 }
142
143 for (int32 col = 0; col < arrayCols; ++col)
144 {
145 returnArray(rowCounter, col) = inArray(row, col);
146 }
147
148 ++rowCounter;
149 }
150
151 return returnArray;
152 }
153
154 //============================================================================
155 // Method Description:
162 template<typename dtype, typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
163 NdArray<dtype> deleteColumnIndices(const NdArray<dtype>& inArray, Indices inIndices)
164 {
165 const auto arrayCols = static_cast<int32>(inArray.numCols());
167 {
168 stl_algorithms::for_each(inIndices.begin(),
169 inIndices.end(),
170 [arrayCols](auto& value)
171 {
172 if (value < 0)
173 {
174 value += arrayCols;
175 }
176 });
177 }
178
179 auto indices = unique(inIndices);
180
181 uint32 indicesSize = 0;
182 std::for_each(indices.begin(),
183 indices.end(),
184 [arrayCols, &indicesSize](const auto& value)
185 {
186 if constexpr (std::is_signed_v<decltype(value)>)
187 {
188 if (value >= 0 && value < arrayCols)
189 {
190 ++indicesSize;
191 }
192 }
193 else
194 {
195 if (value < arrayCols)
196 {
197 ++indicesSize;
198 }
199 }
200 });
201
202 const auto arrayRows = static_cast<int32>(inArray.numRows());
203 NdArray<dtype> returnArray(arrayRows, arrayCols - indicesSize);
204
205 uint32 colCounter = 0;
206 for (int32 col = 0; col < arrayCols; ++col)
207 {
208 if (std::binary_search(indices.begin(), indices.end(), col))
209 {
210 continue;
211 }
212
213 for (int32 row = 0; row < arrayRows; ++row)
214 {
215 returnArray(row, colCounter) = inArray(row, col);
216 }
217
218 ++colCounter;
219 }
220
221 return returnArray;
222 }
223 } // namespace detail
224
225 //============================================================================
226 // Method Description:
234 template<typename dtype, typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
235 NdArray<dtype> deleteIndices(const NdArray<dtype>& inArray, const Indices& inIndices, Axis inAxis = Axis::NONE)
236 {
237 switch (inAxis)
238 {
239 case Axis::NONE:
240 {
241 return detail::deleteFlatIndices(inArray, inIndices);
242 }
243 case Axis::ROW:
244 {
245 return detail::deleteRowIndices(inArray, inIndices);
246 }
247 case Axis::COL:
248 {
249 return detail::deleteColumnIndices(inArray, inIndices);
250 }
251 default:
252 {
253 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
254 return {}; // get rid of compiler warning
255 }
256 }
257 }
258
259 //============================================================================
260 // Method Description:
268 template<typename dtype>
269 NdArray<dtype> deleteIndices(const NdArray<dtype>& inArray, Slice inIndicesSlice, Axis inAxis = Axis::NONE)
270 {
271 switch (inAxis)
272 {
273 case Axis::NONE:
274 {
275 inIndicesSlice.makePositiveAndValidate(inArray.size());
276 break;
277 }
278 case Axis::ROW:
279 {
280 inIndicesSlice.makePositiveAndValidate(inArray.numRows());
281 break;
282 }
283 case Axis::COL:
284 {
285 inIndicesSlice.makePositiveAndValidate(inArray.numCols());
286 break;
287 }
288 }
289
290 std::vector<int32> indices;
291 for (auto i = inIndicesSlice.start; i < inIndicesSlice.stop; i += inIndicesSlice.step)
292 {
293 indices.push_back(i);
294 }
295
296 return deleteIndices(inArray, NdArray<int32>(indices.data(), indices.size(), PointerPolicy::SHELL), inAxis);
297 }
298
299 //============================================================================
300 // Method Description:
308 template<typename dtype>
310 {
311 NdArray<int32> inIndices = { inIndex };
312 return deleteIndices(inArray, inIndices, inAxis);
313 }
314} // 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:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
size_type numCols() const noexcept
Definition NdArrayCore.hpp:3541
size_type numRows() const noexcept
Definition NdArrayCore.hpp:3553
A Class for slicing into NdArrays.
Definition Slice.hpp:45
int32 step
Definition Slice.hpp:50
int32 start
Definition Slice.hpp:48
void makePositiveAndValidate(uint32 inArraySize)
Definition Slice.hpp:142
int32 stop
Definition Slice.hpp:49
Definition column_stack.hpp:40
NdArray< dtype > deleteColumnIndices(const NdArray< dtype > &inArray, Indices inIndices)
Definition deleteIndices.hpp:163
NdArray< dtype > deleteRowIndices(const NdArray< dtype > &inArray, Indices inIndices)
Definition deleteIndices.hpp:93
NdArray< dtype > deleteFlatIndices(const NdArray< dtype > &inArray, Indices inIndices)
Definition deleteIndices.hpp:51
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition StlAlgorithms.hpp:226
constexpr bool is_ndarray_signed_int_v
Definition NdArrayCore.hpp:120
Definition Cartesian.hpp:39
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
NdArray< dtype > unique(const NdArray< dtype > &inArray)
Definition unique.hpp:52
std::int32_t int32
Definition Types.hpp:36
NdArray< dtype > deleteIndices(const NdArray< dtype > &inArray, const Indices &inIndices, Axis inAxis=Axis::NONE)
Definition deleteIndices.hpp:235
@ SHELL
Definition Enums.hpp:58
std::uint32_t uint32
Definition Types.hpp:40