Stratax
Scientific computing containers and operations
Toggle main menu visibility
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
7
namespace
stratax::core
{
8
15
class
Slice
16
{
17
private
:
18
std::size_t start_;
19
std::size_t stop_;
20
21
public
:
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
}
Exceptions.hpp
Exceptions::IndexError
Signals an invalid index access.
Definition
Exceptions.hpp:30
stratax::core::Slice::stop
std::size_t stop() const noexcept
Returns the index one past the end of the slice.
Definition
Slice.hpp:56
stratax::core::Slice::operator==
bool operator==(const Slice &other) const noexcept
Compares two slices for identical bounds.
Definition
Slice.hpp:88
stratax::core::Slice::size
std::size_t size() const noexcept
Returns the number of indices covered by the slice.
Definition
Slice.hpp:66
stratax::core::Slice::Slice
Slice(std::size_t start, std::size_t stop)
Creates a half-open slice range.
Definition
Slice.hpp:32
stratax::core::Slice::empty
bool empty() const noexcept
Returns whether the slice selects no elements.
Definition
Slice.hpp:76
stratax::core::Slice::operator!=
bool operator!=(const Slice &other) const noexcept
Returns whether two slices have different bounds.
Definition
Slice.hpp:100
stratax::core::Slice::start
std::size_t start() const noexcept
Returns the first index in the slice.
Definition
Slice.hpp:46
stratax::core
Definition
Buffer.hpp:17
include
stratax
core
Slice.hpp
Generated by
1.17.0