Radium Engine  1.5.20
Loading...
Searching...
No Matches
CircularIndex.hpp
1#pragma once
2
3#include <Core/RaCore.hpp>
4
5namespace Ra {
6namespace Core {
7namespace Utils {
8
9class RA_CORE_API CircularIndex
10{
11 public:
13 CircularIndex( const size_t size = 1, const size_t i = 0 );
14 CircularIndex( const CircularIndex& index );
15
17 ~CircularIndex();
18
20 inline size_t size() const;
21 inline void setSize( const size_t size );
22
24 inline size_t value() const;
25 inline void setValue( const size_t i );
26
28 inline operator size_t();
29 inline CircularIndex& operator=( const CircularIndex& id );
30 inline CircularIndex& operator=( const size_t i );
31 inline CircularIndex& operator++();
32 inline CircularIndex& operator--();
33 inline CircularIndex operator+( const int off );
34 inline CircularIndex operator-( const int off );
35 inline bool operator==( const CircularIndex& id ) const;
36 inline bool operator<( const CircularIndex& id ) const;
37 inline bool operator==( const size_t i ) const;
38 inline bool operator<( const size_t i ) const;
39
40 protected:
42 size_t idx;
43 size_t N;
44};
45
47inline size_t CircularIndex::size() const {
48 return N;
49}
50
51inline void CircularIndex::setSize( const size_t size ) {
52 N = size;
53}
54
56inline size_t CircularIndex::value() const {
57 return idx;
58}
59
60inline void CircularIndex::setValue( const size_t i ) {
61 const size_t n = N;
62 idx = ( n + ( i % n ) ) % n;
63}
64
66inline CircularIndex::operator size_t() {
67 return idx;
68}
69
70inline CircularIndex& CircularIndex::operator=( const CircularIndex& id ) {
71 N = id.N;
72 idx = id.idx;
73 return *this;
74}
75
76inline CircularIndex& CircularIndex::operator=( const size_t i ) {
77 setValue( i );
78 return *this;
79}
80
81inline CircularIndex& CircularIndex::operator++() {
82 setValue( idx + 1 );
83 return *this;
84}
85
86inline CircularIndex& CircularIndex::operator--() {
87 setValue( idx - 1 );
88 return *this;
89}
90
91inline CircularIndex CircularIndex::operator+( const int off ) {
92 return CircularIndex( N, idx + size_t( int( N ) + off % int( N ) ) );
93}
94
95inline CircularIndex CircularIndex::operator-( const int off ) {
96 return ( *this + ( -off ) );
97}
98
99inline bool CircularIndex::operator==( const CircularIndex& id ) const {
100 return ( idx == id.idx );
101}
102
103inline bool CircularIndex::operator<( const CircularIndex& id ) const {
104 return ( idx < id.idx );
105}
106
107inline bool CircularIndex::operator==( const size_t i ) const {
108 return ( idx == CircularIndex( N, i ) );
109}
110
111inline bool CircularIndex::operator<( const size_t i ) const {
112 return ( idx < size_t( CircularIndex( N, i ) ) );
113}
114
115} // namespace Utils
116} // namespace Core
117} // namespace Ra
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3