00001 /* 00002 * SPUC - Signal processing using C++ - A DSP library 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2, or (at your option) 00007 * any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 #ifndef CIRCB 00019 #define CIRCB 00020 namespace SPUC { 00024 // Circular buffer class 00025 template <class T> class circ_buffer { 00026 00027 protected: 00028 int len; 00029 int ptr; 00030 T *buf; 00031 00032 public: 00033 circ_buffer(void) : buf(NULL), len(0), ptr(0) { ; } 00034 circ_buffer(const circ_buffer<T>& A); 00035 circ_buffer(int d); 00036 circ_buffer(int d, T init_value); 00037 ~circ_buffer(void) { 00038 delete[] buf; 00039 } 00040 00041 int size(void) const {return len;}; 00042 00043 T operator [](int i) const { 00044 return buf[(ptr+i)%len]; 00045 }; 00046 circ_buffer<T> circ_buffer::operator =(circ_buffer<T>& A); 00047 void put(T data_in) { 00048 ptr = (ptr-1+len)%len; 00049 buf[ptr] = data_in; 00050 } 00051 }; 00052 00053 template <class T> circ_buffer<T>::circ_buffer(const circ_buffer<T>& A) 00054 { 00055 len = A.len; 00056 ptr = A.ptr; 00057 buf = new T[len]; 00058 for (int i = 0; i < len; i++) buf[i] = A.buf[i]; 00059 00060 } 00061 // copy constructor 00062 template <class T> circ_buffer<T>::circ_buffer(int len1) 00063 { 00064 len = len1; 00065 ptr = len-1; 00066 buf = new T[len]; 00067 } 00068 00069 template <class T> circ_buffer<T>::circ_buffer(int len1, T init_value) 00070 { 00071 circ_buffer(len1); 00072 for (int i= 0; i < len; i++) buf[i] = init_value; 00073 } 00074 00075 template <class T> circ_buffer<T> circ_buffer<T>::operator =(circ_buffer<T>& A) 00076 { 00077 if (this->len != A.size()) { 00078 // remove existing circ 00079 delete[] buf; 00080 // create room for A 00081 len = A.size(); 00082 buf = new T[len]; 00083 } 00084 ptr = A.ptr; 00085 00086 for (int i = 0; i < len; i++) buf[i] = A.buf[i]; 00087 return *this; 00088 } 00089 } 00090 #endif 00091 00092 00093 00094