gz-cpp-util 1.3
A c++20 library containing various utilities
util.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4
5
6namespace gz::util {
7//
8// INDEX UTILITY
9//
13 template<std::unsigned_integral I, std::unsigned_integral S>
14 inline void incrementIndex(I& i, const S containerSize) {
15 if (i < containerSize - 1) { i++; }
16 else { i = 0; }
17 }
21 template<std::unsigned_integral I, std::unsigned_integral S>
22 inline void decrementIndex(I& i, const S containerSize) {
23 if (i > 0) { i--; }
24 else { i = containerSize - 1; }
25 }
29 template<std::unsigned_integral I, std::unsigned_integral S>
30 inline I getIncrementedIndex(const I i, const S containerSize) {
31 if (i < containerSize - 1) { return i + 1; }
32 else { return 0; }
33 }
37 template<std::unsigned_integral I, std::unsigned_integral S>
38 inline I getDecrementedIndex(const I i, const S containerSize) {
39 if (i > 0) { return i - 1; }
40 else { return containerSize - 1; }
41 }
42
50 template<std::integral I, std::unsigned_integral S>
51 std::size_t getValidIndex(const I i, const S containerSize) {
52 if (i < 0) {
53 return (containerSize - 1) - (-i) % containerSize;
54 }
55 else if (i >= static_cast<int>(containerSize)) {
56 return i % containerSize;
57 }
58 return i;
59 }
60
61
62} // namespace gz::util
63
void incrementIndex(I &i, const S containerSize)
Increment an index. Up to containerSize, then restart at 0.
Definition: util.hpp:14
void decrementIndex(I &i, const S containerSize)
Decrement an index. Down to 0, then restart at containerSize - 1.
Definition: util.hpp:22
I getDecrementedIndex(const I i, const S containerSize)
Like decrementIndex, but returns a new number.
Definition: util.hpp:38
std::size_t getValidIndex(const I i, const S containerSize)
Wrap an index around, to make it valid.
Definition: util.hpp:51
I getIncrementedIndex(const I i, const S containerSize)
Like incrementIndex, but returns a new number.
Definition: util.hpp:30