fasdf gfaha / CircularBuffer
Revision:
1:b24970e4c038
Parent:
0:787a5401f57e
Child:
2:4080c1770d51
--- a/CircularBuffer.h	Sun Aug 05 07:52:40 2012 +0000
+++ b/CircularBuffer.h	Sun Aug 05 08:15:32 2012 +0000
@@ -1,47 +1,69 @@
-#ifndef CIRCULARBUFFER_H
-#define CIRCULARBUFFER_H
-
-
-
-template <typename T, int S>
-class CircularBuffer
-{
-public:
-    CircularBuffer() { index = 0; }
-    T value(int i);
-    void push(T value);
-    void revert(int amount);
-    
-protected:
-    T data[S];
-    int index;
-};
-
-
-
-template <typename T, int S>
-inline T CircularBuffer<T, S>::value(int i)
-{
-    int realIndex = (index - i) % S;
-    if (realIndex < 0) realIndex += S;
-    return data[realIndex];
-}
-
-
-
-template <typename T, int S>
-inline void CircularBuffer<T, S>::push(T value)
-{
-    index = ++index % S;
-    data[index] = value;
-}
-
-
-
-template <typename T, int S>
-inline void CircularBuffer<T, S>::revert(int amount)
-{
-    index = index >= amount ? index - amount : index - amount + S;
-}
-
+#ifndef CIRCULARBUFFER_H
+#define CIRCULARBUFFER_H
+
+
+
+template <typename T, int S>
+class CircularBuffer
+{
+public:
+    CircularBuffer() { currentIndex = 0; }
+    T read(int index);
+    T& operator[](int index);
+    void write(T value, int index);
+    void push(T value);
+    void revert(int amount);
+    
+protected:
+    T data[S];
+    int currentIndex;
+};
+
+
+
+template <typename T, int S>
+inline T CircularBuffer<T, S>::read(int index)
+{
+    int realIndex = (currentIndex - index) % S;
+    if (realIndex < 0) realIndex += S;
+    return data[realIndex];
+}
+
+
+
+template <typename T, int S>
+inline T& CircularBuffer<T, S>::operator[](int index)
+{
+    int realIndex = (currentIndex - index) % S;
+    if (realIndex < 0) realIndex += S;
+    return data[realIndex];
+}
+
+
+
+template <typename T, int S>
+inline void CircularBuffer<T, S>::write(T value, int index)
+{
+    int realIndex = (currentIndex - index) % S;
+    if (realIndex < 0) realIndex += S;
+    data[realIndex] = value;
+}
+
+
+
+template <typename T, int S>
+inline void CircularBuffer<T, S>::push(T value)
+{
+    currentIndex = ++currentIndex % S;
+    data[currentIndex] = value;
+}
+
+
+
+template <typename T, int S>
+inline void CircularBuffer<T, S>::revert(int amount)
+{
+    currentIndex = currentIndex >= amount ? currentIndex - amount : currentIndex - amount + S;
+}
+
 #endif // CIRCULARBUFFER_H
\ No newline at end of file