NumCpp  1.0
A C++ implementation of the Python Numpy library
Slice.hpp
Go to the documentation of this file.
1 
30 #pragma once
31 
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/Utils/num2str.hpp"
35 
36 #include <algorithm>
37 #include <iostream>
38 #include <string>
39 
40 namespace nc
41 {
42  //================================================================================
44  class Slice
45  {
46  public:
47  //====================================Attributes==============================
48  int32 start{ 0 };
49  int32 stop{ 1 };
50  int32 step{ 1 };
51 
52  //============================================================================
55  constexpr Slice() = default;
56 
57  //============================================================================
62  constexpr explicit Slice(int32 inStop) noexcept :
63  stop(inStop)
64  {}
65 
66  //============================================================================
72  constexpr Slice(int32 inStart, int32 inStop) noexcept :
73  start(inStart),
74  stop(inStop)
75  {}
76 
77  //============================================================================
84  constexpr Slice(int32 inStart, int32 inStop, int32 inStep) noexcept :
85  start(inStart),
86  stop(inStop),
87  step(inStep)
88  {}
89 
90  //============================================================================
95  std::string str() const
96  {
97  std::string out = "[" + utils::num2str(start) + ":" + utils::num2str(stop) + ":" + utils::num2str(step) + "]\n";
98  return out;
99  }
100 
101  //============================================================================
104  void print() const
105  {
106  std::cout << *this;
107  }
108 
109  //============================================================================
114  void makePositiveAndValidate(uint32 inArraySize)
115  {
117  if (start < 0)
118  {
119  start += inArraySize;
120  }
121  if (start > static_cast<int32>(inArraySize - 1))
122  {
123  THROW_INVALID_ARGUMENT_ERROR("Invalid start value for array of size " + utils::num2str(inArraySize) + ".");
124  }
125 
127  if (stop < 0)
128  {
129  stop += inArraySize;
130  }
131  if (stop > static_cast<int32>(inArraySize))
132  {
133  THROW_INVALID_ARGUMENT_ERROR("Invalid stop value for array of size " + utils::num2str(inArraySize) + ".");
134  }
135 
137  if (start < stop)
138  {
139  if (step < 0)
140  {
141  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values.");
142  }
143  }
144 
145  if (stop < start)
146  {
147  if (step > 0)
148  {
149  THROW_INVALID_ARGUMENT_ERROR("Invalid slice values.");
150  }
151 
153  std::swap(start, stop);
154  step *= -1;
155  }
156  }
157 
158  //============================================================================
165  uint32 numElements(uint32 inArraySize)
166  {
167  makePositiveAndValidate(inArraySize);
168 
169  uint32 num = 0;
170  for (int32 i = start; i < stop; i += step)
171  {
172  ++num;
173  }
174  return num;
175  }
176 
177  //============================================================================
185  friend std::ostream& operator<<(std::ostream& inOStream, const Slice& inSlice)
186  {
187  inOStream << inSlice.str();
188  return inOStream;
189  }
190  };
191 }
nc::Slice::stop
int32 stop
Definition: Slice.hpp:49
nc::int32
std::int32_t int32
Definition: Types.hpp:37
nc::Slice::makePositiveAndValidate
void makePositiveAndValidate(uint32 inArraySize)
Definition: Slice.hpp:114
Error.hpp
nc::Slice::print
void print() const
Definition: Slice.hpp:104
nc::Slice::Slice
constexpr Slice(int32 inStop) noexcept
Definition: Slice.hpp:62
nc::Slice::Slice
constexpr Slice(int32 inStart, int32 inStop) noexcept
Definition: Slice.hpp:72
nc::utils::num2str
std::string num2str(dtype inNumber)
Definition: num2str.hpp:47
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::Slice::operator<<
friend std::ostream & operator<<(std::ostream &inOStream, const Slice &inSlice)
Definition: Slice.hpp:185
num2str.hpp
nc::Slice::start
int32 start
Definition: Slice.hpp:48
nc::Slice::str
std::string str() const
Definition: Slice.hpp:95
nc::Slice::numElements
uint32 numElements(uint32 inArraySize)
Definition: Slice.hpp:165
nc::Slice::step
int32 step
Definition: Slice.hpp:50
nc
Definition: Coordinate.hpp:45
nc::swap
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:43
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
nc::Slice::Slice
constexpr Slice(int32 inStart, int32 inStop, int32 inStep) noexcept
Definition: Slice.hpp:84
Types.hpp
nc::Slice::Slice
constexpr Slice()=default
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:44