From 7a722a88727d7e73a9e41fe86595e4dacdc86459 Mon Sep 17 00:00:00 2001 From: Ma Qiming Date: Thu, 24 Mar 2016 21:56:54 +0800 Subject: [PATCH] extract cache to class --- sanro/cache.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 sanro/cache.h diff --git a/sanro/cache.h b/sanro/cache.h new file mode 100644 index 0000000..891de9a --- /dev/null +++ b/sanro/cache.h @@ -0,0 +1,49 @@ +/*************************************************************** + * * + * Taiko Sanro - Arduino * + * Cache data structure * + * * + * Chris * + * wisaly@gmail.com * + * * + ***************************************************************/ + +#ifndef CACHE_H +#define CACHE_H + +template +class Cache +{ +public: + Cache(); + void put(T value); + T get(int offset = 0) const; + +private: + T data_[L]; + int current_ = 0; +}; + +template +Cache::Cache() +{ + for (int i = 0; i < L; i++){ + data_[i] = 0; + } +} + +template +void Cache::put(T value) +{ + data_[current_] = value; + current_ = (current_ + 1) % L; +} + +template +T Cache::get(int offset) const +{ + int index = (current_ + offset) % L; + return data_[index]; +} + +#endif // CACHE_H