Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Normalize.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <cstddef>
7
9
10namespace stratax::indexing {
11
13using size_type = std::size_t;
15using difference_type = std::ptrdiff_t;
16
27 difference_type index,
28 size_type size)
29{
30 const size_type magnitude = index < 0
31 ? static_cast<size_type>(-(index + 1)) + 1
32 : 0;
33
34 if ((index >= 0 && static_cast<size_type>(index) >= size) ||
35 (index < 0 && magnitude > size))
36 {
37 throw Exceptions::IndexError("Index is out of bounds.");
38 }
39
40 return index >= 0 ? static_cast<size_type>(index) : size - magnitude;
41}
42
43} // namespace stratax::indexing
Stratax exception types derived from std::runtime_error.
size_type normalize_index(difference_type index, size_type size)
Validates a signed index and converts it to a non-negative offset.
Definition Normalize.hpp:26
std::ptrdiff_t difference_type
Signed type for Python-style indices.
Definition Normalize.hpp:15
std::size_t size_type
Unsigned extent and normalized-index type.
Definition Normalize.hpp:13
Index or slice cannot be used for the requested access.