NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Slice.hpp
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <algorithm>
32 #include <iostream>
33 #include <string>
34 
36 #include "NumCpp/Core/Types.hpp"
37 #include "NumCpp/Utils/num2str.hpp"
38 
39 namespace nc
40 {
41  //================================================================================
43  class Slice
44  {
45  public:
46  //====================================Attributes==============================
47  int32 start{ 0 };
48  int32 stop{ 1 };
49  int32 step{ 1 };
50 
51  //============================================================================
54  constexpr Slice() = default;
55 
56  //============================================================================
61  constexpr explicit Slice(int32 inStop) noexcept :
62  stop(inStop)
63  {
64  }
65 
66  //============================================================================
72  constexpr Slice(int32 inStart, int32 inStop) noexcept :
73  start(inStart),
74  stop(inStop)
75  {
76  }
77 
78  //============================================================================
85  constexpr Slice(int32 inStart, int32 inStop, int32 inStep) noexcept :
86  start(inStart),
87  stop(inStop),
88  step(inStep)
89  {
90  }
91 
92  //============================================================================
99  bool operator==(const Slice& inOtherSlice) const noexcept
100  {
101  return start == inOtherSlice.start && stop == inOtherSlice.stop && step == inOtherSlice.step;
102  }
103 
104  //============================================================================
111  bool operator!=(const Slice& inOtherSlice) const noexcept
112  {
113  return !(*this == inOtherSlice);
114  }
115 
116  //============================================================================
121  std::string str() const
122  {
123  std::string out =
124  "[" + utils::num2str(start) + ":" + utils::num2str(stop) + ":" + utils::num2str(step) + "]\n";
125  return out;
126  }
127 
128  //============================================================================
131  void print() const
132  {
133  std::cout << *this;
134  }
135 
136  //============================================================================
141  void makePositiveAndValidate(uint32 inArraySize)
142  {
144  if (start < 0)
145  {
146  start += inArraySize;
147  }
148  if (start > static_cast<int32>(inArraySize - 1))
149  {
150  THROW_INVALID_ARGUMENT_ERROR("Invalid start value for array of size " + utils::num2str(inArraySize) +
151  ".");
152  }
153 
155  if (stop < 0)
156  {
157  stop += inArraySize;
158  }
159  if (stop > static_cast<int32>(inArraySize))
160  {
161  THROW_INVALID_ARGUMENT_ERROR("Invalid stop value for array of size " + utils::num2str(inArraySize) +
162  ".");
163  }
164 
166  if (start < stop)
167  {
168  if (step < 0)
169  {
170  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values [" + utils::num2str(start) + ", " +
171  utils::num2str(stop) + ", " + utils::num2str(step) + ']');
172  }
173  }
174 
175  if (stop < start)
176  {
177  if (step > 0)
178  {
179  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values [" + utils::num2str(start) + ", " +
180  utils::num2str(stop) + ", " + utils::num2str(step) + ']');
181  }
182 
184  std::swap(start, stop);
185  step *= -1;
186  }
187  }
188 
189  //============================================================================
196  uint32 numElements(uint32 inArraySize)
197  {
198  makePositiveAndValidate(inArraySize);
199 
200  uint32 num = 0;
201  for (int32 i = start; i < stop; i += step)
202  {
203  ++num;
204  }
205  return num;
206  }
207 
208  //============================================================================
216  friend std::ostream& operator<<(std::ostream& inOStream, const Slice& inSlice)
217  {
218  inOStream << inSlice.str();
219  return inOStream;
220  }
221  };
222 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
int32 step
Definition: Slice.hpp:49
void print() const
Definition: Slice.hpp:131
int32 start
Definition: Slice.hpp:47
void makePositiveAndValidate(uint32 inArraySize)
Definition: Slice.hpp:141
bool operator==(const Slice &inOtherSlice) const noexcept
Definition: Slice.hpp:99
constexpr Slice(int32 inStart, int32 inStop, int32 inStep) noexcept
Definition: Slice.hpp:85
constexpr Slice(int32 inStop) noexcept
Definition: Slice.hpp:61
uint32 numElements(uint32 inArraySize)
Definition: Slice.hpp:196
constexpr Slice(int32 inStart, int32 inStop) noexcept
Definition: Slice.hpp:72
int32 stop
Definition: Slice.hpp:48
friend std::ostream & operator<<(std::ostream &inOStream, const Slice &inSlice)
Definition: Slice.hpp:216
constexpr Slice()=default
std::string str() const
Definition: Slice.hpp:121
bool operator!=(const Slice &inOtherSlice) const noexcept
Definition: Slice.hpp:111
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
Definition: Coordinate.hpp:45
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
std::int32_t int32
Definition: Types.hpp:36
std::uint32_t uint32
Definition: Types.hpp:40