1 #ifndef _DERINGBUFFER_HPP
2 #define _DERINGBUFFER_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Ring buffer template.
24 *//*--------------------------------------------------------------------*/
31 void RingBuffer_selfTest (void);
33 /** Ring buffer template. */
38 RingBuffer (int size);
42 void resize (int newSize);
44 int getSize (void) const { return m_size; }
45 int getNumElements (void) const { return m_numElements; }
46 int getNumFree (void) const { return m_size - m_numElements; }
48 void pushFront (const T& elem);
49 void pushFront (const T* elemBuf, int count);
51 void peekBack (T* elemBuf, int count) const;
52 T peekBack (int offset) const;
55 void popBack (T* elemBuf, int count) { peekBack(elemBuf, count); popBack(count); }
56 void popBack (int count);
67 // RingBuffer implementation.
70 RingBuffer<T>::RingBuffer (int size)
77 m_buffer = new T[m_size];
81 RingBuffer<T>::~RingBuffer ()
87 void RingBuffer<T>::clear (void)
95 void RingBuffer<T>::resize (int newSize)
97 DE_ASSERT(newSize >= m_numElements);
98 T* buf = new T[newSize];
102 // Copy old elements.
103 for (int ndx = 0; ndx < m_numElements; ndx++)
104 buf[ndx] = m_buffer[(m_back + ndx) % m_size];
107 m_front = m_numElements;
111 DE_SWAP(T*, buf, m_buffer);
121 template <typename T>
122 inline void RingBuffer<T>::pushFront (const T& elem)
124 DE_ASSERT(getNumFree() > 0);
125 m_buffer[m_front] = elem;
126 m_front = (m_front + 1) % m_size;
130 template <typename T>
131 void RingBuffer<T>::pushFront (const T* elemBuf, int count)
133 DE_ASSERT(de::inRange(count, 0, getNumFree()));
134 for (int i = 0; i < count; i++)
135 m_buffer[(m_front + i) % m_size] = elemBuf[i];
136 m_front = (m_front + count) % m_size;
137 m_numElements += count;
140 template <typename T>
141 inline T RingBuffer<T>::popBack ()
143 DE_ASSERT(getNumElements() > 0);
145 m_back = (m_back + 1) % m_size;
147 return m_buffer[ndx];
150 template <typename T>
151 inline T RingBuffer<T>::peekBack (int offset) const
153 DE_ASSERT(de::inBounds(offset, 0, getNumElements()));
154 return m_buffer[(m_back + offset) % m_size];
157 template <typename T>
158 void RingBuffer<T>::peekBack (T* elemBuf, int count) const
160 DE_ASSERT(de::inRange(count, 0, getNumElements()));
161 for (int i = 0; i < count; i++)
162 elemBuf[i] = m_buffer[(m_back + i) % m_size];
165 template <typename T>
166 void RingBuffer<T>::popBack (int count)
168 DE_ASSERT(de::inRange(count, 0, getNumElements()));
169 m_back = (m_back + count) % m_size;
170 m_numElements -= count;
175 #endif // _DERINGBUFFER_HPP