aboutsummaryrefslogtreecommitdiff
path: root/src/stable/ringbuffer.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
commitec05778d5718a7912e506764d443a78d6a6179e3 (patch)
tree78a9a01532180030c095acefc45763f07c14edb8 /src/stable/ringbuffer.h
parentb20414ac1fe80a71a90601f4cd1767fa7014a9ba (diff)
downloadlibbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.gz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.bz2
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.xz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.zip
Converted tabs to spaces with tabconv.
Diffstat (limited to '')
-rw-r--r--src/stable/ringbuffer.h462
1 files changed, 231 insertions, 231 deletions
diff --git a/src/stable/ringbuffer.h b/src/stable/ringbuffer.h
index 79c5a15..39d053e 100644
--- a/src/stable/ringbuffer.h
+++ b/src/stable/ringbuffer.h
@@ -15,237 +15,237 @@
15 15
16namespace Bu 16namespace Bu
17{ 17{
18 template<typename value, typename valuealloc> class RingBuffer; 18 template<typename value, typename valuealloc> class RingBuffer;
19 19
20 /** @cond DEVEL */ 20 /** @cond DEVEL */
21 template<typename value, typename valuealloc> 21 template<typename value, typename valuealloc>
22 class RingBufferCore 22 class RingBufferCore
23 { 23 {
24 friend class RingBuffer<value, valuealloc>; 24 friend class RingBuffer<value, valuealloc>;
25 friend class SharedCore<RingBuffer<value, valuealloc>, 25 friend class SharedCore<RingBuffer<value, valuealloc>,
26 RingBufferCore<value, valuealloc> >; 26 RingBufferCore<value, valuealloc> >;
27 private: 27 private:
28 RingBufferCore() : 28 RingBufferCore() :
29 iCapacity( 0 ), 29 iCapacity( 0 ),
30 iStart( -1 ), 30 iStart( -1 ),
31 iEnd( -2 ), 31 iEnd( -2 ),
32 aData( NULL ) 32 aData( NULL )
33 { 33 {
34 } 34 }
35 35
36 virtual ~RingBufferCore() 36 virtual ~RingBufferCore()
37 { 37 {
38 clear(); 38 clear();
39 } 39 }
40 40
41 void init( int iNewCapacity ) 41 void init( int iNewCapacity )
42 { 42 {
43 if( iCapacity > 0 ) 43 if( iCapacity > 0 )
44 return; 44 return;
45 45
46 iCapacity = iNewCapacity; 46 iCapacity = iNewCapacity;
47 iStart = -1; 47 iStart = -1;
48 iEnd = -2; 48 iEnd = -2;
49 aData = va.allocate( iCapacity ); 49 aData = va.allocate( iCapacity );
50 } 50 }
51 51
52 void clear() 52 void clear()
53 { 53 {
54 for( int j = iStart; j < iEnd; j=(j+1%iCapacity) ) 54 for( int j = iStart; j < iEnd; j=(j+1%iCapacity) )
55 { 55 {
56 va.destroy( &aData[j] ); 56 va.destroy( &aData[j] );
57 } 57 }
58 va.deallocate( aData, iCapacity ); 58 va.deallocate( aData, iCapacity );
59 aData = NULL; 59 aData = NULL;
60 iCapacity = 0; 60 iCapacity = 0;
61 } 61 }
62 62
63 void enqueue( const value &v ) 63 void enqueue( const value &v )
64 { 64 {
65 if( iStart == -1 ) 65 if( iStart == -1 )
66 { 66 {
67 iStart = 0; 67 iStart = 0;
68 iEnd = 1; 68 iEnd = 1;
69 va.construct( &aData[0], v ); 69 va.construct( &aData[0], v );
70 return; 70 return;
71 } 71 }
72 else if( iStart == iEnd ) 72 else if( iStart == iEnd )
73 { 73 {
74 // The ringbuffer is full 74 // The ringbuffer is full
75 dequeue(); 75 dequeue();
76 } 76 }
77 va.construct( &aData[iEnd], v ); 77 va.construct( &aData[iEnd], v );
78 iEnd = (iEnd+1)%iCapacity; 78 iEnd = (iEnd+1)%iCapacity;
79 } 79 }
80 80
81 value dequeue() 81 value dequeue()
82 { 82 {
83 if( iStart == -1 ) 83 if( iStart == -1 )
84 { 84 {
85 throw ExceptionBase("No data"); 85 throw ExceptionBase("No data");
86 } 86 }
87 else 87 else
88 { 88 {
89 value &v = aData[iStart]; 89 value &v = aData[iStart];
90 va.destroy( &aData[iStart] ); 90 va.destroy( &aData[iStart] );
91 iStart = (iStart+1)%iCapacity; 91 iStart = (iStart+1)%iCapacity;
92 if( iStart == iEnd ) 92 if( iStart == iEnd )
93 { 93 {
94 iStart = -1; 94 iStart = -1;
95 iEnd = -2; 95 iEnd = -2;
96 } 96 }
97 return v; 97 return v;
98 } 98 }
99 } 99 }
100 100
101 value &get( int iIndex ) 101 value &get( int iIndex )
102 { 102 {
103 return aData[(iIndex+iStart)%iCapacity]; 103 return aData[(iIndex+iStart)%iCapacity];
104 } 104 }
105 105
106 value &first() 106 value &first()
107 { 107 {
108 return aData[iStart]; 108 return aData[iStart];
109 } 109 }
110 110
111 value &last() 111 value &last()
112 { 112 {
113 return aData[(iEnd-1+iCapacity)%iCapacity]; 113 return aData[(iEnd-1+iCapacity)%iCapacity];
114 } 114 }
115 115
116 int getSize() 116 int getSize()
117 { 117 {
118 if( iStart < 0 ) 118 if( iStart < 0 )
119 return 0; 119 return 0;
120 if( iEnd == iStart ) 120 if( iEnd == iStart )
121 return iCapacity; 121 return iCapacity;
122 if( iEnd < iStart ) 122 if( iEnd < iStart )
123 return iEnd-iStart; 123 return iEnd-iStart;
124 return iCapacity-(iEnd-iStart); 124 return iCapacity-(iEnd-iStart);
125 } 125 }
126 126
127 int iCapacity; 127 int iCapacity;
128 int iStart, iEnd; 128 int iStart, iEnd;
129 value *aData; 129 value *aData;
130 valuealloc va; 130 valuealloc va;
131 }; 131 };
132 /** @endcond */ 132 /** @endcond */
133 133
134 /** 134 /**
135 *@ingroup Containers 135 *@ingroup Containers
136 */ 136 */
137 template<typename value, typename valuealloc=std::allocator<value> > 137 template<typename value, typename valuealloc=std::allocator<value> >
138 class RingBuffer : public Queue<value>, public SharedCore< 138 class RingBuffer : public Queue<value>, public SharedCore<
139 RingBuffer<value, valuealloc>, 139 RingBuffer<value, valuealloc>,
140 RingBufferCore<value, valuealloc> 140 RingBufferCore<value, valuealloc>
141 > 141 >
142 { 142 {
143 private: 143 private:
144 typedef RingBuffer<value, valuealloc> MyType; 144 typedef RingBuffer<value, valuealloc> MyType;
145 typedef RingBufferCore<value, valuealloc> Core; 145 typedef RingBufferCore<value, valuealloc> Core;
146 146
147 protected: 147 protected:
148 using SharedCore<MyType, Core>::core; 148 using SharedCore<MyType, Core>::core;
149 using SharedCore<MyType, Core>::_hardCopy; 149 using SharedCore<MyType, Core>::_hardCopy;
150 using SharedCore<MyType, Core>::_allocateCore; 150 using SharedCore<MyType, Core>::_allocateCore;
151 151
152 public: 152 public:
153 RingBuffer( int iCapacity ) 153 RingBuffer( int iCapacity )
154 { 154 {
155 core->init( iCapacity ); 155 core->init( iCapacity );
156 } 156 }
157 157
158 RingBuffer( const RingBuffer &rSrc ) : 158 RingBuffer( const RingBuffer &rSrc ) :
159 SharedCore<MyType, Core>( rSrc ) 159 SharedCore<MyType, Core>( rSrc )
160 { 160 {
161 } 161 }
162 162
163 virtual ~RingBuffer() 163 virtual ~RingBuffer()
164 { 164 {
165 } 165 }
166 166
167 int getCapacity() const 167 int getCapacity() const
168 { 168 {
169 return core->iCapacity; 169 return core->iCapacity;
170 } 170 }
171 171
172 bool isFilled() const 172 bool isFilled() const
173 { 173 {
174 return (core->iStart == core->iEnd); 174 return (core->iStart == core->iEnd);
175 } 175 }
176 176
177 bool isEmpty() const 177 bool isEmpty() const
178 { 178 {
179 return (core->iStart == -1); 179 return (core->iStart == -1);
180 } 180 }
181 181
182 virtual void enqueue( const value &v ) 182 virtual void enqueue( const value &v )
183 { 183 {
184 _hardCopy(); 184 _hardCopy();
185 185
186 core->enqueue( v ); 186 core->enqueue( v );
187 } 187 }
188 188
189 virtual value dequeue() 189 virtual value dequeue()
190 { 190 {
191 _hardCopy(); 191 _hardCopy();
192 192
193 return core->dequeue(); 193 return core->dequeue();
194 } 194 }
195 195
196 virtual int getSize() const 196 virtual int getSize() const
197 { 197 {
198 return core->getSize(); 198 return core->getSize();
199 } 199 }
200 200
201 virtual value &peek() 201 virtual value &peek()
202 { 202 {
203 _hardCopy(); 203 _hardCopy();
204 204
205 return core->get( 0 ); 205 return core->get( 0 );
206 } 206 }
207 207
208 virtual const value &peek() const 208 virtual const value &peek() const
209 { 209 {
210 return core->get( 0 ); 210 return core->get( 0 );
211 } 211 }
212 212
213 virtual value &first() 213 virtual value &first()
214 { 214 {
215 _hardCopy(); 215 _hardCopy();
216 216
217 return core->first(); 217 return core->first();
218 } 218 }
219 219
220 virtual value &last() 220 virtual value &last()
221 { 221 {
222 _hardCopy(); 222 _hardCopy();
223 223
224 return core->last(); 224 return core->last();
225 } 225 }
226 226
227 value &operator[]( int iIndex ) 227 value &operator[]( int iIndex )
228 { 228 {
229 _hardCopy(); 229 _hardCopy();
230 230
231 return core->get( iIndex ); 231 return core->get( iIndex );
232 } 232 }
233 233
234 protected: 234 protected:
235 virtual Core *_copyCore( Core *src ) 235 virtual Core *_copyCore( Core *src )
236 { 236 {
237 Core *pRet = _allocateCore(); 237 Core *pRet = _allocateCore();
238 238
239 pRet->init( src->iCapacity ); 239 pRet->init( src->iCapacity );
240 int iSize = src->getSize(); 240 int iSize = src->getSize();
241 for( int j = 0; j < iSize; j++ ) 241 for( int j = 0; j < iSize; j++ )
242 { 242 {
243 pRet->enqueue( src->get( j ) ); 243 pRet->enqueue( src->get( j ) );
244 } 244 }
245 245
246 return pRet; 246 return pRet;
247 } 247 }
248 }; 248 };
249} 249}
250 250
251#endif 251#endif