00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 #ifndef __SEAL_BUFFER_H__ 00030 #define __SEAL_BUFFER_H__ 00031 00032 #include "vector.h" 00033 #include "array1d.h" 00034 00035 #define BUFFER_BIN_SIZE 512 00036 00037 namespace seal 00038 { 00039 00044 template <typename T> 00045 class Buffer : public RandomAccess<T> 00046 { 00047 public: 00050 static void setBinSize(unsigned int size); 00051 00052 public: 00055 Buffer(); 00056 00059 virtual ~Buffer() {} 00060 00065 void operator<<(const T e); 00066 00073 void operator>>(T &e) throw (OutOfBoundsException); 00074 00077 void purge(void); 00078 00081 bool isEmpty(void) const { if (_size == 0) return true; else return false; } 00082 00085 unsigned int size(void) const { return _size; } 00086 00089 virtual const T& operator[](int index) const throw (OutOfRangeException<int>); 00090 00093 virtual T& operator[](int index) throw (OutOfRangeException<int>); 00094 00095 private: 00096 Vector< Array1D<T> > _arrayVector; 00097 00098 unsigned int _writeIndex; 00099 unsigned int _readIndex; 00100 unsigned int _arrayCount; 00101 unsigned int _size; 00102 00103 private: 00104 static unsigned int _binSize; 00105 }; // Class Buffer 00106 00107 template <typename T> 00108 unsigned int Buffer<T>::_binSize = BUFFER_BIN_SIZE; 00109 00110 template <typename T> 00111 Buffer<T>::Buffer(void) 00112 : RandomAccess<T>(), 00113 _arrayVector(Vector< Array1D<T> >()), 00114 _writeIndex(0), _readIndex(0), _arrayCount(0), 00115 _size(0) 00116 { 00117 _arrayVector.pushBack(Array1D<T>(Buffer<T>::_binSize)); 00118 _arrayCount = 1; 00119 } 00120 00121 template <typename T> 00122 void Buffer<T>::purge(void) 00123 { 00124 _arrayVector.purge(); 00125 _writeIndex = 0; 00126 _readIndex = 0; 00127 _arrayCount = 1; 00128 _size = 0; 00129 00130 _arrayVector.pushBack(Array1D<T>(Buffer<T>::_binSize)); 00131 } 00132 00133 template <typename T> 00134 void Buffer<T>::operator<<(const T e) 00135 { 00136 _arrayVector[_arrayCount-1][_writeIndex] = e; 00137 _writeIndex++; 00138 00139 if (_writeIndex >= Buffer<T>::_binSize) 00140 { 00141 _arrayVector.pushBack(Array1D<T>(Buffer<T>::_binSize)); 00142 _arrayCount++; 00143 _writeIndex = 0; 00144 } 00145 00146 _size++; 00147 } 00148 00149 template <typename T> 00150 void Buffer<T>::operator>>(T &e) throw (OutOfBoundsException) 00151 { 00152 if (_size == 0) 00153 throw OutOfBoundsException(); 00154 00155 e = _arrayVector[0][_readIndex]; 00156 _readIndex++; 00157 00158 if (_readIndex >= Buffer<T>::_binSize) 00159 { 00160 _arrayVector.popFront(); 00161 _arrayCount--; 00162 _readIndex = 0; 00163 } 00164 00165 _size--; 00166 } 00167 00168 template <typename T> 00169 const T& Buffer<T>::operator[](int index) const throw (OutOfRangeException<int>) 00170 { 00171 if (index >= _size) 00172 throw OutOfRangeException<int>(0, _size-1, index); 00173 00174 return _arrayVector[index/(Buffer<T>::_binSize)][index%(Buffer<T>::_binSize)]; 00175 } 00176 00177 template <typename T> 00178 T& Buffer<T>::operator[](int index) throw (OutOfRangeException<int>) 00179 { 00180 if (index >= _size) 00181 throw OutOfRangeException<int>(0, _size-1, index); 00182 00183 return _arrayVector[index/(Buffer<T>::_binSize)][index%(Buffer<T>::_binSize)]; 00184 } 00185 00186 } // End of namespace seal 00187 00188 #endif // __SEAL_BUFFER_H__ 00189