NumCpp  2.5.1
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 
32 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/Utils/num2str.hpp"
34 
35 #include <algorithm>
36 #include <iostream>
37 #include <string>
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  //============================================================================
71  constexpr Slice(int32 inStart, int32 inStop) noexcept :
72  start(inStart),
73  stop(inStop)
74  {}
75 
76  //============================================================================
83  constexpr Slice(int32 inStart, int32 inStop, int32 inStep) noexcept :
84  start(inStart),
85  stop(inStop),
86  step(inStep)
87  {}
88 
89  //============================================================================
96  bool operator==(const Slice& inOtherSlice) const noexcept
97  {
98  return start == inOtherSlice.start && stop == inOtherSlice.stop && step == inOtherSlice.step;
99  }
100 
101  //============================================================================
108  bool operator!=(const Slice& inOtherSlice) const noexcept
109  {
110  return !(*this == inOtherSlice);
111  }
112 
113  //============================================================================
118  std::string str() const
119  {
120  std::string out = "[" + utils::num2str(start) + ":" + utils::num2str(stop) + ":" + utils::num2str(step) + "]\n";
121  return out;
122  }
123 
124  //============================================================================
127  void print() const
128  {
129  std::cout << *this;
130  }
131 
132  //============================================================================
137  void makePositiveAndValidate(uint32 inArraySize)
138  {
140  if (start < 0)
141  {
142  start += inArraySize;
143  }
144  if (start > static_cast<int32>(inArraySize - 1))
145  {
146  THROW_INVALID_ARGUMENT_ERROR("Invalid start value for array of size " + utils::num2str(inArraySize) + ".");
147  }
148 
150  if (stop < 0)
151  {
152  stop += inArraySize;
153  }
154  if (stop > static_cast<int32>(inArraySize))
155  {
156  THROW_INVALID_ARGUMENT_ERROR("Invalid stop value for array of size " + utils::num2str(inArraySize) + ".");
157  }
158 
160  if (start < stop)
161  {
162  if (step < 0)
163  {
164  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values.");
165  }
166  }
167 
168  if (stop < start)
169  {
170  if (step > 0)
171  {
172  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values.");
173  }
174 
176  std::swap(start, stop);
177  step *= -1;
178  }
179  }
180 
181  //============================================================================
188  uint32 numElements(uint32 inArraySize)
189  {
190  makePositiveAndValidate(inArraySize);
191 
192  uint32 num = 0;
193  for (int32 i = start; i < stop; i += step)
194  {
195  ++num;
196  }
197  return num;
198  }
199 
200  //============================================================================
208  friend std::ostream& operator<<(std::ostream& inOStream, const Slice& inSlice)
209  {
210  inOStream << inSlice.str();
211  return inOStream;
212  }
213  };
214 } // 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:127
int32 start
Definition: Slice.hpp:47
void makePositiveAndValidate(uint32 inArraySize)
Definition: Slice.hpp:137
bool operator==(const Slice &inOtherSlice) const noexcept
Definition: Slice.hpp:96
constexpr Slice(int32 inStart, int32 inStop, int32 inStep) noexcept
Definition: Slice.hpp:83
constexpr Slice(int32 inStop) noexcept
Definition: Slice.hpp:61
uint32 numElements(uint32 inArraySize)
Definition: Slice.hpp:188
constexpr Slice(int32 inStart, int32 inStop) noexcept
Definition: Slice.hpp:71
int32 stop
Definition: Slice.hpp:48
friend std::ostream & operator<<(std::ostream &inOStream, const Slice &inSlice)
Definition: Slice.hpp:208
constexpr Slice()=default
std::string str() const
Definition: Slice.hpp:118
bool operator!=(const Slice &inOtherSlice) const noexcept
Definition: Slice.hpp:108
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