#ifndef CYCLICINT_H #define CYCLICINT_H //! Class containing an integer which ranges from 0..N-1. //! //! Exceeding the ends will overflow to the other end. Implicit cast to/from int is provided. template class CyclicInt { private: int value; CyclicInt set(int v) { value = v < N ? v < 0 ? N - -v % N : v : v % N; return *this; } public: CyclicInt() { } CyclicInt(int v) { set(v); } CyclicInt operator=(int v) { return set(v); } CyclicInt operator+(CyclicInt v) { return value + v.value; } CyclicInt operator+(int v) { return value + v; } CyclicInt operator+=(CyclicInt v) { return set(value + v.value); } CyclicInt operator++() { return set(value + 1); } CyclicInt operator++(int) { int v = value; set(value + 1); return v; } CyclicInt operator-(CyclicInt v) { return value - v.value; } CyclicInt operator-(int v) { return value - v; } CyclicInt operator-=(CyclicInt v) { return set(value - v.value); } CyclicInt operator--() { return set(value - 1); } CyclicInt operator--(int) { int v = value; set(value - 1); return v; } operator int() { return value; } }; #endif