00001 /* 00002 This file is part of the CVD Library. 00003 00004 Copyright (C) 2005 The Authors 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 00019 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 #ifndef __RINGBUFFER_H 00022 #define __RINGBUFFER_H 00023 00024 #include <vector> 00025 00026 namespace CVD 00027 { 00028 00029 00037 template <typename T> 00038 class RingBuffer { 00039 public: 00042 RingBuffer(int size=0){ 00043 my_buffer.resize(size); 00044 my_start=0; 00045 } 00046 00050 RingBuffer(int size, const T &t){ 00051 my_buffer.resize(size,t); 00052 my_start=0; 00053 } 00054 00055 ~RingBuffer(){} 00056 00057 00060 void resize(int size){ 00061 my_buffer.resize(size); 00062 } 00063 00065 int size() const { 00066 return my_buffer.size(); 00067 } 00068 00073 T& operator[](int i){ 00074 return my_buffer[(my_buffer.size()+i+my_start)%my_buffer.size()]; 00075 } 00076 00081 const T& operator[](int i) const { 00082 return my_buffer[(my_buffer.size()+i+my_start)%my_buffer.size()]; 00083 } 00084 00087 void advance(int n=1){ 00088 my_start = (my_start+my_buffer.size()+n)%my_buffer.size(); 00089 } 00090 00091 private: 00092 std::vector<T> my_buffer; 00093 int my_start; 00094 }; 00095 00096 00097 } 00098 00099 #endif 00100