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
17struct none_t {};
18
20static inline constexpr none_t _;
21
22} // namespace slicing
23
25class SLEIPNIR_DLLEXPORT Slice {
26 public:
28 int start = 0;
29
31 int stop = 0;
32
34 int step = 1;
35
37 constexpr Slice() = default;
38
40 // NOLINTNEXTLINE (google-explicit-constructor)
42 : Slice(0, std::numeric_limits<int>::max(), 1) {}
43
47 // NOLINTNEXTLINE (google-explicit-constructor)
48 constexpr Slice(int start) {
49 this->start = start;
50 this->stop = (start == -1) ? std::numeric_limits<int>::max() : start + 1;
51 this->step = 1;
52 }
53
58 template <typename Start, typename Stop>
59 requires(std::same_as<Start, slicing::none_t> ||
60 std::convertible_to<Start, int>) &&
61 (std::same_as<Stop, slicing::none_t> ||
62 std::convertible_to<Stop, int>)
63 constexpr Slice(Start start, Stop stop)
64 : Slice(std::move(start), std::move(stop), 1) {}
65
71 template <typename Start, typename Stop, typename Step>
72 requires(std::same_as<Start, slicing::none_t> ||
73 std::convertible_to<Start, int>) &&
74 (std::same_as<Stop, slicing::none_t> ||
75 std::convertible_to<Stop, int>) &&
76 (std::same_as<Step, slicing::none_t> ||
77 std::convertible_to<Step, int>)
78 constexpr Slice(Start start, Stop stop, Step step) {
79 if constexpr (std::same_as<Step, slicing::none_t>) {
80 this->step = 1;
81 } else {
82 slp_assert(step != 0);
83
84 this->step = step;
85 }
86
87 // Avoid UB for step = -step if step is INT_MIN
88 if (this->step == std::numeric_limits<int>::min()) {
89 this->step = -std::numeric_limits<int>::max();
90 }
91
92 if constexpr (std::same_as<Start, slicing::none_t>) {
93 if (this->step < 0) {
94 this->start = std::numeric_limits<int>::max();
95 } else {
96 this->start = 0;
97 }
98 } else {
99 this->start = start;
100 }
101
102 if constexpr (std::same_as<Stop, slicing::none_t>) {
103 if (this->step < 0) {
104 this->stop = std::numeric_limits<int>::min();
105 } else {
106 this->stop = std::numeric_limits<int>::max();
107 }
108 } else {
109 this->stop = stop;
110 }
111 }
112
118 constexpr int adjust(int length) {
119 slp_assert(step != 0);
120 slp_assert(step >= -std::numeric_limits<int>::max());
121
122 if (start < 0) {
123 start += length;
124
125 if (start < 0) {
126 start = (step < 0) ? -1 : 0;
127 }
128 } else if (start >= length) {
129 start = (step < 0) ? length - 1 : length;
130 }
131
132 if (stop < 0) {
133 stop += length;
134
135 if (stop < 0) {
136 stop = (step < 0) ? -1 : 0;
137 }
138 } else if (stop >= length) {
139 stop = (step < 0) ? length - 1 : length;
140 }
141
142 if (step < 0) {
143 if (stop < start) {
144 return (start - stop - 1) / -step + 1;
145 } else {
146 return 0;
147 }
148 } else {
149 if (start < stop) {
150 return (stop - start - 1) / step + 1;
151 } else {
152 return 0;
153 }
154 }
155 }
156};
157
158} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Represents a sequence of elements in an iterable object.
Definition slice.hpp:25
constexpr Slice()=default
Constructs a Slice.
constexpr Slice(Start start, Stop stop)
Definition slice.hpp:63
constexpr Slice(slicing::none_t)
Constructs a slice.
Definition slice.hpp:41
constexpr int adjust(int length)
Definition slice.hpp:118
constexpr Slice(Start start, Stop stop, Step step)
Definition slice.hpp:78
constexpr Slice(int start)
Definition slice.hpp:48
Type tag used to designate an omitted argument of the slice.
Definition slice.hpp:17