rexlib 0.1.0
Device-agnostic array computing library for CryoEM and CryoET image processing, with a pluggable backend architecture
Loading...
Searching...
No Matches
slice.hpp
1// SPDX-License-Identifier: GPL-3.0-only
2
3#pragma once
4
5#include "../platform/constexpr.hpp"
6#include "../platform/attributes.hpp"
7
8#include <cstddef>
9#include <ostream>
10
11namespace rexlib
12{
13
21class slice
22{
23public:
24 using start_type = std::ptrdiff_t;
25 using count_type = std::size_t;
26 using step_type = std::ptrdiff_t;
27
35 REXLIB_CONSTEXPR
36 slice(start_type start, count_type count, step_type step ) noexcept;
37
38 slice() = default;
39 slice(const slice& other) = default;
40 slice(slice&& other) = default;
41 ~slice() = default;
42
43 slice& operator=(const slice& other) = default;
44 slice& operator=(slice&& other) = default;
45
51 REXLIB_CONSTEXPR void set_start(start_type start) noexcept;
52
58 REXLIB_CONSTEXPR const start_type& get_start() const noexcept;
59
65 REXLIB_CONSTEXPR void set_count(count_type count) noexcept;
66
72 REXLIB_CONSTEXPR const count_type& get_count() const noexcept;
73
79 REXLIB_CONSTEXPR void set_step(step_type step) noexcept;
80
86 REXLIB_CONSTEXPR const step_type& get_step() const noexcept;
87
88 friend REXLIB_CONSTEXPR
90 {
91 return
92 lhs.get_start() == rhs.get_start() &&
93 lhs.get_count() == rhs.get_count() &&
94 lhs.get_step() == rhs.get_step();
95 }
96
97 friend REXLIB_CONSTEXPR
98 bool operator!=(const slice &lhs, const slice &rhs) noexcept
99 {
100 return !(lhs == rhs);
101 }
102
103 template <typename T>
104 friend std::basic_ostream<T>&
105 operator<<(std::basic_ostream<T> &os, const slice &s)
106 {
107 return
108 os << "slice(" << s.get_start()
109 << ", " << s.get_count()
110 << ", " << s.get_step() << ")";
111 }
112
113private:
114 start_type m_start;
115 count_type m_count;
116 step_type m_step;
117};
118
126REXLIB_CONSTEXPR std::size_t end() noexcept;
127
133REXLIB_CONSTEXPR slice all() noexcept;
134
140REXLIB_CONSTEXPR slice even() noexcept;
141
147REXLIB_CONSTEXPR slice odd() noexcept;
148
158REXLIB_CONSTEXPR
159slice make_slice(std::size_t count) noexcept;
160
171REXLIB_CONSTEXPR
172slice make_slice(std::ptrdiff_t start, std::size_t count) noexcept;
173
185REXLIB_CONSTEXPR
187 std::ptrdiff_t start,
188 std::size_t count,
189 std::ptrdiff_t step
190) noexcept;
191
205slice sanitize_slice(const slice &slice, std::size_t extent);
206
207} // namespace rexlib
208
209#include "slice.inl"
Container for multidimensional numeric data.
Definition array.hpp:31
Class representing an slice of an array.
Definition slice.hpp:22
REXLIB_CONSTEXPR void set_step(step_type step) noexcept
Set the step.
REXLIB_CONSTEXPR const step_type & get_step() const noexcept
Get the step.
REXLIB_CONSTEXPR const start_type & get_start() const noexcept
Get the starting index.
REXLIB_CONSTEXPR void set_start(start_type start) noexcept
Set the starting index.
REXLIB_CONSTEXPR const count_type & get_count() const noexcept
Get the element count.
REXLIB_CONSTEXPR slice(start_type start, count_type count, step_type step) noexcept
Constructor.
REXLIB_CONSTEXPR void set_count(count_type count) noexcept
Set the index element count.
Arrays, the operations on them and the devices that run those operations.
Definition cpu_device.hpp:10
REXLIB_CONSTEXPR slice odd() noexcept
Construct an slice containing odd positions in the axis.
REXLIB_CONSTEXPR slice make_slice(std::size_t count) noexcept
Create an slice using its count value.
REXLIB_CONSTEXPR std::size_t end() noexcept
Get the value representing the end of an array to be used as count.
REXLIB_CONSTEXPR slice all() noexcept
Construct an slice containing all positions in the axis.
slice sanitize_slice(const slice &slice, std::size_t extent)
Sanitize a slice to ensure it is valid and within bounds for a given extent.
REXLIB_CONSTEXPR slice even() noexcept
Construct an slice containing even positions in the axis.