Taiko-Nijiiro-Analog-IOBoard/sanro/cache.h

46 lines
1.1 KiB
C
Raw Normal View History

2016-03-24 14:56:54 +01:00
/***************************************************************
* *
* Taiko Sanro - Arduino *
* Cache data structure *
* *
* Chris *
* wisaly@gmail.com *
* *
***************************************************************/
#ifndef CACHE_H
#define CACHE_H
2016-03-27 14:36:32 +02:00
template <class T, int L>
class Cache {
2016-03-24 14:56:54 +01:00
public:
2016-03-27 14:36:32 +02:00
Cache ();
void put (T value);
T get (int offset = 0) const;
2016-03-24 14:56:54 +01:00
private:
2016-03-27 14:36:32 +02:00
T data_ [L];
2016-03-24 14:56:54 +01:00
int current_ = 0;
};
2016-03-27 14:36:32 +02:00
template <class T, int L>
Cache <T, L>::Cache () {
for (int i = 0; i < L; i++) {
data_ [i] = 0;
2016-03-24 14:56:54 +01:00
}
}
2016-03-27 14:36:32 +02:00
template <class T, int L>
void Cache <T, L>::put (T value) {
data_ [current_] = value;
2016-03-24 14:56:54 +01:00
current_ = (current_ + 1) % L;
}
2016-03-27 14:36:32 +02:00
template <class T, int L>
T Cache <T, L>::get (int offset) const {
2016-03-24 14:56:54 +01:00
int index = (current_ + offset) % L;
2016-03-27 14:36:32 +02:00
return data_ [index];
2016-03-24 14:56:54 +01:00
}
2016-03-27 14:36:32 +02:00
#endif // CACHE_H