PetaVision  Alpha
RingBuffer.hpp
1 /*
2  * RingBuffer.hpp
3  *
4  * Created on: Aug 23, 2016
5  * Author: pschultz
6  */
7 
8 #ifndef RINGBUFFER_HPP_
9 #define RINGBUFFER_HPP_
10 
11 #include <vector>
12 
13 namespace PV {
14 
15 template <typename T>
16 class RingBuffer {
17  public:
18  RingBuffer(int numLevels, int numItems, T initialValue = (T)0) {
19  mCurrentLevel = 0;
20  mNumLevels = numLevels;
21  mNumItems = numItems;
22  mBuffer.resize(numLevels);
23  for (auto &b : mBuffer) {
24  b.resize(numItems, initialValue);
25  }
26  }
27  virtual ~RingBuffer() {}
28 
29  int getNumLevels() { return mNumLevels; }
30 
31  int getNumItems() { return mNumItems; }
32 
33  void newLevel() { mCurrentLevel = (mNumLevels + mCurrentLevel - 1) % mNumLevels; }
34 
35  T *getBuffer(int level, int offset) { return &mBuffer[levelIndex(level)].at(offset); }
36 
37  T *getBuffer(int offset) { return &mBuffer[mCurrentLevel].at(offset); }
38 
39  T *getBuffer() { return mBuffer[mCurrentLevel].data(); }
40 
41  private:
42  int levelIndex(int level) const { return ((level + mCurrentLevel) % mNumLevels); }
43 
44  private:
45  int mCurrentLevel;
46  int mNumLevels;
47  int mNumItems;
48  std::vector<std::vector<T>> mBuffer;
49 };
50 
51 } /* namespace PV */
52 
53 #endif /* RINGBUFFER_HPP_ */