Sleipnir C++ API
Loading...
Searching...
No Matches
slice.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <concepts>
6#include <limits>
7#include <utility>
8
9#include "sleipnir/util/assert.hpp"
10#include "sleipnir/util/symbol_exports.hpp"
11
12namespace slp {
13
14namespace slicing {
15
19struct none_t {};
20
24static inline constexpr none_t _;
25
26} // namespace slicing
27
31class SLEIPNIR_DLLEXPORT Slice {
32 public:
34 int start = 0;
35
37 int stop = 0;
38
40 int step = 1;
41
45 constexpr Slice() = default;
46
50 constexpr Slice(slicing::none_t) // NOLINT
51 : Slice(0, std::numeric_limits<int>::max(), 1) {}
52
58 constexpr Slice(int start) { // NOLINT
59 this->start = start;
60 this->stop = (start == -1) ? std::numeric_limits<int>::max() : start + 1;
61 this->step = 1;
62 }
63
70 template <typename Start, typename Stop>
71 requires(std::same_as<Start, slicing::none_t> ||
72 std::convertible_to<Start, int>) &&
73 (std::same_as<Stop, slicing::none_t> ||
74 std::convertible_to<Stop, int>)
75 constexpr Slice(Start start, Stop stop)
76 : Slice(std::move(start), std::move(stop), 1) {}
77
85 template <typename Start, typename Stop, typename Step>
86 requires(std::same_as<Start, slicing::none_t> ||
87 std::convertible_to<Start, int>) &&
88 (std::same_as<Stop, slicing::none_t> ||
89 std::convertible_to<Stop, int>) &&
90 (std::same_as<Step, slicing::none_t> ||
91 std::convertible_to<Step, int>)
92 constexpr Slice(Start start, Stop stop, Step step) {
93 if constexpr (std::same_as<Step, slicing::none_t>) {
94 this->step = 1;
95 } else {
96 slp_assert(step != 0);
97
98 this->step = step;
99 }
100
101 // Avoid UB for step = -step if step is INT_MIN
102 if (this->step == std::numeric_limits<int>::min()) {
103 this->step = -std::numeric_limits<int>::max();
104 }
105
106 if constexpr (std::same_as<Start, slicing::none_t>) {
107 if (this->step < 0) {
108 this->start = std::numeric_limits<int>::max();
109 } else {
110 this->start = 0;
111 }
112 } else {
113 this->start = start;
114 }
115
116 if constexpr (std::same_as<Stop, slicing::none_t>) {
117 if (this->step < 0) {
118 this->stop = std::numeric_limits<int>::min();
119 } else {
120 this->stop = std::numeric_limits<int>::max();
121 }
122 } else {
123 this->stop = stop;
124 }
125 }
126
134 constexpr int adjust(int length) {
135 slp_assert(step != 0);
136 slp_assert(step >= -std::numeric_limits<int>::max());
137
138 if (start < 0) {
139 start += length;
140
141 if (start < 0) {
142 start = (step < 0) ? -1 : 0;
143 }
144 } else if (start >= length) {
145 start = (step < 0) ? length - 1 : length;
146 }
147
148 if (stop < 0) {
149 stop += length;
150
151 if (stop < 0) {
152 stop = (step < 0) ? -1 : 0;
153 }
154 } else if (stop >= length) {
155 stop = (step < 0) ? length - 1 : length;
156 }
157
158 if (step < 0) {
159 if (stop < start) {
160 return (start - stop - 1) / -step + 1;
161 } else {
162 return 0;
163 }
164 } else {
165 if (start < stop) {
166 return (stop - start - 1) / step + 1;
167 } else {
168 return 0;
169 }
170 }
171 }
172};
173
174} // namespace slp
Definition slice.hpp:31
constexpr Slice()=default
constexpr Slice(Start start, Stop stop)
Definition slice.hpp:75
constexpr Slice(slicing::none_t)
Definition slice.hpp:50
constexpr int adjust(int length)
Definition slice.hpp:134
constexpr Slice(Start start, Stop stop, Step step)
Definition slice.hpp:92
constexpr Slice(int start)
Definition slice.hpp:58
Definition slice.hpp:19