diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-27 07:21:21 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-27 07:21:21 +0100 |
commit | 3a3c9967ac079bf15ac0cea7ff52c040c9764392 (patch) | |
tree | e649007a2765c85bc5eca6cc793ef697c3e2310e /common | |
parent | 2c064bde5ccc2ea4b4314d9c7754934c1434a924 (diff) |
Added CyclicInt convenience class.
Diffstat (limited to 'common')
-rw-r--r-- | common/cyclicint.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/common/cyclicint.h b/common/cyclicint.h new file mode 100644 index 0000000..3b24399 --- /dev/null +++ b/common/cyclicint.h @@ -0,0 +1,80 @@ +#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<int N> +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 |