Stratax
Scientific computing containers and operations
Loading...
Searching...
No Matches
Slice.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Exceptions.hpp"
4
5#include <cstddef>
6
7namespace stratax::core {
8
15class Slice
16{
17private:
18 std::size_t start_;
19 std::size_t stop_;
20
21public:
32 Slice(std::size_t start, std::size_t stop)
33 : start_(start),
34 stop_(stop)
35 {
36 if (start > stop) {
37 throw Exceptions::IndexError("Slice start cannot be greater than stop");
38 }
39 }
40
46 [[nodiscard]] std::size_t start() const noexcept
47 {
48 return start_;
49 }
50
56 [[nodiscard]] std::size_t stop() const noexcept
57 {
58 return stop_;
59 }
60
66 [[nodiscard]] std::size_t size() const noexcept
67 {
68 return stop_ - start_;
69 }
70
76 [[nodiscard]] bool empty() const noexcept
77 {
78 return size() == 0;
79 }
80
88 [[nodiscard]] bool operator==(const Slice& other) const noexcept
89 {
90 return start_ == other.start_ && stop_ == other.stop_;
91 }
92
100 [[nodiscard]] bool operator!=(const Slice& other) const noexcept
101 {
102 return !(*this == other);
103 }
104};
105
106}
Signals an invalid index access.
std::size_t stop() const noexcept
Returns the index one past the end of the slice.
Definition Slice.hpp:56
bool operator==(const Slice &other) const noexcept
Compares two slices for identical bounds.
Definition Slice.hpp:88
std::size_t size() const noexcept
Returns the number of indices covered by the slice.
Definition Slice.hpp:66
Slice(std::size_t start, std::size_t stop)
Creates a half-open slice range.
Definition Slice.hpp:32
bool empty() const noexcept
Returns whether the slice selects no elements.
Definition Slice.hpp:76
bool operator!=(const Slice &other) const noexcept
Returns whether two slices have different bounds.
Definition Slice.hpp:100
std::size_t start() const noexcept
Returns the first index in the slice.
Definition Slice.hpp:46