fasdf gfaha / CircularBuffer
Revision:
2:4080c1770d51
Parent:
1:b24970e4c038
Child:
3:1a33490e1990
--- a/CircularBuffer.h	Sun Aug 05 08:15:32 2012 +0000
+++ b/CircularBuffer.h	Sun Aug 05 22:18:39 2012 +0000
@@ -17,16 +17,25 @@
 protected:
     T data[S];
     int currentIndex;
+    inline int getRealIndex(int i);
 };
 
 
 
 template <typename T, int S>
+inline int CircularBuffer<T, S>::getRealIndex(int index)
+{
+    int realIndex = (currentIndex - index) % S;
+    if (realIndex < 0) realIndex += S;
+    return realIndex;
+}
+
+
+
+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];
+    return data[getRealIndex(index)];
 }
 
 
@@ -34,9 +43,7 @@
 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];
+    return data[getRealIndex(index)];
 }
 
 
@@ -44,9 +51,7 @@
 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;
+    data[getRealIndex(index)] = value;
 }
 
 
@@ -66,4 +71,6 @@
     currentIndex = currentIndex >= amount ? currentIndex - amount : currentIndex - amount + S;
 }
 
+
+
 #endif // CIRCULARBUFFER_H
\ No newline at end of file