mirror of
https://github.com/ShikyC/Taiko-Drum-Controller-Arduino.git
synced 2024-11-12 00:50:46 +01:00
extract cache to class
This commit is contained in:
parent
61e729ff1d
commit
7a722a8872
49
sanro/cache.h
Normal file
49
sanro/cache.h
Normal file
@ -0,0 +1,49 @@
|
||||
/***************************************************************
|
||||
* *
|
||||
* Taiko Sanro - Arduino *
|
||||
* Cache data structure *
|
||||
* *
|
||||
* Chris *
|
||||
* wisaly@gmail.com *
|
||||
* *
|
||||
***************************************************************/
|
||||
|
||||
#ifndef CACHE_H
|
||||
#define CACHE_H
|
||||
|
||||
template<class T, int L>
|
||||
class Cache
|
||||
{
|
||||
public:
|
||||
Cache();
|
||||
void put(T value);
|
||||
T get(int offset = 0) const;
|
||||
|
||||
private:
|
||||
T data_[L];
|
||||
int current_ = 0;
|
||||
};
|
||||
|
||||
template<class T, int L>
|
||||
Cache<T,L>::Cache()
|
||||
{
|
||||
for (int i = 0; i < L; i++){
|
||||
data_[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, int L>
|
||||
void Cache<T,L>::put(T value)
|
||||
{
|
||||
data_[current_] = value;
|
||||
current_ = (current_ + 1) % L;
|
||||
}
|
||||
|
||||
template<class T, int L>
|
||||
T Cache<T,L>::get(int offset) const
|
||||
{
|
||||
int index = (current_ + offset) % L;
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
#endif // CACHE_H
|
Loading…
Reference in New Issue
Block a user