annotate src/CircularBuffer.h @ 117:ca2d83d29814 tip master

Merge branch 'release/1.0.5'
author Adam Stark <adamstark.uk@gmail.com>
date Fri, 18 Aug 2023 20:07:33 +0200
parents 8fb1610c9192
children
rev   line source
adamstark@93 1 //=======================================================================
adamstark@93 2 /** @file CircularBuffer.h
adamstark@111 3 * @brief A circular buffer
adamstark@93 4 * @author Adam Stark
adamstark@93 5 * @copyright Copyright (C) 2008-2014 Queen Mary University of London
adamstark@93 6 *
adamstark@93 7 * This program is free software: you can redistribute it and/or modify
adamstark@93 8 * it under the terms of the GNU General Public License as published by
adamstark@93 9 * the Free Software Foundation, either version 3 of the License, or
adamstark@93 10 * (at your option) any later version.
adamstark@93 11 *
adamstark@93 12 * This program is distributed in the hope that it will be useful,
adamstark@93 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
adamstark@93 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
adamstark@93 15 * GNU General Public License for more details.
adamstark@93 16 *
adamstark@93 17 * You should have received a copy of the GNU General Public License
adamstark@93 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adamstark@93 19 */
adamstark@93 20 //=======================================================================
adamstark@89 21
adamstark@89 22 #ifndef CircularBuffer_h
adamstark@89 23 #define CircularBuffer_h
adamstark@89 24
adamstark@89 25 #include <vector>
adamstark@89 26
adamstark@93 27 //=======================================================================
adamstark@93 28 /** A circular buffer that allows you to add new samples to the end
adamstark@93 29 * whilst removing them from the beginning. This is implemented in an
adamstark@107 30 * efficient way which doesn't involve any memory allocation as samples
adamstark@107 31 * are added to the end of the buffer
adamstark@93 32 */
adamstark@89 33 class CircularBuffer
adamstark@89 34 {
adamstark@89 35 public:
adamstark@93 36
adamstark@93 37 /** Constructor */
adamstark@93 38 CircularBuffer()
adamstark@93 39 : writeIndex (0)
adamstark@89 40 {
adamstark@89 41
adamstark@89 42 }
adamstark@89 43
adamstark@93 44 /** Access the ith element in the buffer */
adamstark@93 45 double &operator[] (int i)
adamstark@89 46 {
adamstark@89 47 int index = (i + writeIndex) % buffer.size();
adamstark@89 48 return buffer[index];
adamstark@89 49 }
adamstark@93 50
adamstark@93 51 /** Add a new sample to the end of the buffer */
adamstark@89 52 void addSampleToEnd (double v)
adamstark@89 53 {
adamstark@89 54 buffer[writeIndex] = v;
adamstark@89 55 writeIndex = (writeIndex + 1) % buffer.size();
adamstark@89 56 }
adamstark@89 57
adamstark@93 58 /** Resize the buffer */
adamstark@93 59 void resize (int size)
adamstark@89 60 {
adamstark@92 61 buffer.resize (size);
adamstark@105 62 std::fill (buffer.begin(), buffer.end(), 0.0);
adamstark@89 63 writeIndex = 0;
adamstark@89 64 }
adamstark@89 65
adamstark@107 66 /** Returns the size of the buffer */
adamstark@105 67 int size()
adamstark@105 68 {
adamstark@105 69 return static_cast<int> (buffer.size());
adamstark@105 70 }
adamstark@105 71
adamstark@89 72 private:
adamstark@89 73
adamstark@89 74 std::vector<double> buffer;
adamstark@89 75 int writeIndex;
adamstark@89 76 };
adamstark@89 77
adamstark@89 78 #endif /* CircularBuffer_hpp */