NumCpp  2.3.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 
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  //============================================================================
94  std::string str() const
95  {
96  std::string out = "[" + utils::num2str(start) + ":" + utils::num2str(stop) + ":" + utils::num2str(step) + "]\n";
97  return out;
98  }
99 
100  //============================================================================
103  void print() const
104  {
105  std::cout << *this;
106  }
107 
108  //============================================================================
113  void makePositiveAndValidate(uint32 inArraySize)
114  {
116  if (start < 0)
117  {
118  start += inArraySize;
119  }
120  if (start > static_cast<int32>(inArraySize - 1))
121  {
122  THROW_INVALID_ARGUMENT_ERROR("Invalid start value for array of size " + utils::num2str(inArraySize) + ".");
123  }
124 
126  if (stop < 0)
127  {
128  stop += inArraySize;
129  }
130  if (stop > static_cast<int32>(inArraySize))
131  {
132  THROW_INVALID_ARGUMENT_ERROR("Invalid stop value for array of size " + utils::num2str(inArraySize) + ".");
133  }
134 
136  if (start < stop)
137  {
138  if (step < 0)
139  {
140  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values.");
141  }
142  }
143 
144  if (stop < start)
145  {
146  if (step > 0)
147  {
148  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values.");
149  }
150 
152  std::swap(start, stop);
153  step *= -1;
154  }
155  }
156 
157  //============================================================================
164  uint32 numElements(uint32 inArraySize)
165  {
166  makePositiveAndValidate(inArraySize);
167 
168  uint32 num = 0;
169  for (int32 i = start; i < stop; i += step)
170  {
171  ++num;
172  }
173  return num;
174  }
175 
176  //============================================================================
184  friend std::ostream& operator<<(std::ostream& inOStream, const Slice& inSlice)
185  {
186  inOStream << inSlice.str();
187  return inOStream;
188  }
189  };
190 } // namespace nc
nc::Slice::stop
int32 stop
Definition: Slice.hpp:48
nc::int32
std::int32_t int32
Definition: Types.hpp:36
nc::Slice::makePositiveAndValidate
void makePositiveAndValidate(uint32 inArraySize)
Definition: Slice.hpp:113
Error.hpp
nc::Slice::print
void print() const
Definition: Slice.hpp:103
nc::Slice::Slice
constexpr Slice(int32 inStop) noexcept
Definition: Slice.hpp:61
nc::Slice::Slice
constexpr Slice(int32 inStart, int32 inStop) noexcept
Definition: Slice.hpp:71
nc::utils::num2str
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
nc::Slice::operator<<
friend std::ostream & operator<<(std::ostream &inOStream, const Slice &inSlice)
Definition: Slice.hpp:184
num2str.hpp
nc::Slice::start
int32 start
Definition: Slice.hpp:47
nc::Slice::str
std::string str() const
Definition: Slice.hpp:94
nc::Slice::numElements
uint32 numElements(uint32 inArraySize)
Definition: Slice.hpp:164
nc::Slice::step
int32 step
Definition: Slice.hpp:49
nc
Definition: Coordinate.hpp:44
nc::swap
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::Slice::Slice
constexpr Slice(int32 inStart, int32 inStop, int32 inStep) noexcept
Definition: Slice.hpp:83
Types.hpp
nc::Slice::Slice
constexpr Slice()=default
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:43