31 lines
976 B
C
Raw Normal View History

2016-03-24 21:56:54 +08:00
/***************************************************************
* *
* Taiko Sanro - Arduino *
* Cache data structure *
* *
* Chris *
* wisaly@gmail.com *
* *
***************************************************************/
#ifndef CACHE_H
#define CACHE_H
2016-03-27 20:36:32 +08:00
template <class T, int L>
class Cache {
2016-03-24 21:56:54 +08:00
public:
2023-12-21 17:54:39 -08:00
Cache() { memset(data_, 0, sizeof(data_)); }
inline void put(T value) {
current_ = (current_ + 1) & (L - 1);
data_[current_] = value;
}
inline T get(int offset = 0) const {
return data_[(current_ + offset) & (L - 1)];
}
2016-03-24 21:56:54 +08:00
private:
2023-12-21 17:54:39 -08:00
T data_[L];
2016-03-24 21:56:54 +08:00
int current_ = 0;
};
2023-12-21 17:54:39 -08:00
#endif // CACHE_H