diff options
-rw-r--r-- | src/list.h | 20 | ||||
-rw-r--r-- | src/ringbuffer.cpp | 2 | ||||
-rw-r--r-- | src/ringbuffer.h | 100 | ||||
-rw-r--r-- | src/tests/ringbuffer.cpp | 29 |
4 files changed, 141 insertions, 10 deletions
@@ -69,10 +69,10 @@ namespace Bu | |||
69 | { | 69 | { |
70 | if( pCur == NULL ) break; | 70 | if( pCur == NULL ) break; |
71 | va.destroy( pCur->pValue ); | 71 | va.destroy( pCur->pValue ); |
72 | va.deallocate( pCur->pValue, sizeof( value ) ); | 72 | va.deallocate( pCur->pValue, 1 ); |
73 | Link *pTmp = pCur->pNext; | 73 | Link *pTmp = pCur->pNext; |
74 | la.destroy( pCur ); | 74 | la.destroy( pCur ); |
75 | la.deallocate( pCur, sizeof( Link ) ); | 75 | la.deallocate( pCur, 1 ); |
76 | pCur = pTmp; | 76 | pCur = pTmp; |
77 | } | 77 | } |
78 | pFirst = pLast = NULL; | 78 | pFirst = pLast = NULL; |
@@ -81,8 +81,8 @@ namespace Bu | |||
81 | 81 | ||
82 | void append( const value &v ) | 82 | void append( const value &v ) |
83 | { | 83 | { |
84 | Link *pNew = la.allocate( sizeof( Link ) ); | 84 | Link *pNew = la.allocate( 1 ); |
85 | pNew->pValue = va.allocate( sizeof( value ) ); | 85 | pNew->pValue = va.allocate( 1 ); |
86 | va.construct( pNew->pValue, v ); | 86 | va.construct( pNew->pValue, v ); |
87 | nSize++; | 87 | nSize++; |
88 | if( pFirst == NULL ) | 88 | if( pFirst == NULL ) |
@@ -102,8 +102,8 @@ namespace Bu | |||
102 | 102 | ||
103 | void prepend( const value &v ) | 103 | void prepend( const value &v ) |
104 | { | 104 | { |
105 | Link *pNew = la.allocate( sizeof( Link ) ); | 105 | Link *pNew = la.allocate( 1 ); |
106 | pNew->pValue = va.allocate( sizeof( value ) ); | 106 | pNew->pValue = va.allocate( 1 ); |
107 | va.construct( pNew->pValue, v ); | 107 | va.construct( pNew->pValue, v ); |
108 | nSize++; | 108 | nSize++; |
109 | if( pFirst == NULL ) | 109 | if( pFirst == NULL ) |
@@ -316,10 +316,10 @@ namespace Bu | |||
316 | if( pPrev == NULL ) | 316 | if( pPrev == NULL ) |
317 | { | 317 | { |
318 | va.destroy( pCur->pValue ); | 318 | va.destroy( pCur->pValue ); |
319 | va.deallocate( pCur->pValue, sizeof( value ) ); | 319 | va.deallocate( pCur->pValue, 1 ); |
320 | pFirst = pCur->pNext; | 320 | pFirst = pCur->pNext; |
321 | la.destroy( pCur ); | 321 | la.destroy( pCur ); |
322 | la.deallocate( pCur, sizeof( Link ) ); | 322 | la.deallocate( pCur, 1 ); |
323 | if( pFirst == NULL ) | 323 | if( pFirst == NULL ) |
324 | pLast = NULL; | 324 | pLast = NULL; |
325 | nSize--; | 325 | nSize--; |
@@ -328,10 +328,10 @@ namespace Bu | |||
328 | else | 328 | else |
329 | { | 329 | { |
330 | va.destroy( pCur->pValue ); | 330 | va.destroy( pCur->pValue ); |
331 | va.deallocate( pCur->pValue, sizeof( value ) ); | 331 | va.deallocate( pCur->pValue, 1 ); |
332 | Link *pTmp = pCur->pNext; | 332 | Link *pTmp = pCur->pNext; |
333 | la.destroy( pCur ); | 333 | la.destroy( pCur ); |
334 | la.deallocate( pCur, sizeof( Link ) ); | 334 | la.deallocate( pCur, 1 ); |
335 | pPrev->pNext = pTmp; | 335 | pPrev->pNext = pTmp; |
336 | if( pTmp != NULL ) | 336 | if( pTmp != NULL ) |
337 | pTmp->pPrev = pPrev; | 337 | pTmp->pPrev = pPrev; |
diff --git a/src/ringbuffer.cpp b/src/ringbuffer.cpp new file mode 100644 index 0000000..feb9bc7 --- /dev/null +++ b/src/ringbuffer.cpp | |||
@@ -0,0 +1,2 @@ | |||
1 | #include "bu/ringbuffer.h" | ||
2 | |||
diff --git a/src/ringbuffer.h b/src/ringbuffer.h new file mode 100644 index 0000000..9480043 --- /dev/null +++ b/src/ringbuffer.h | |||
@@ -0,0 +1,100 @@ | |||
1 | #ifndef RING_BUFFER_H | ||
2 | #define RING_BUFFER_H | ||
3 | |||
4 | #include <memory> | ||
5 | #include "bu/exceptionbase.h" | ||
6 | |||
7 | namespace Bu | ||
8 | { | ||
9 | template<typename value, typename valuealloc=std::allocator<value> > | ||
10 | class RingBuffer | ||
11 | { | ||
12 | public: | ||
13 | RingBuffer( int nCapacity ) : | ||
14 | nCapacity( nCapacity ), | ||
15 | nStart( -1 ), | ||
16 | nEnd( -2 ) | ||
17 | { | ||
18 | aData = va.allocate( nCapacity ); | ||
19 | for( int j = 0; j < nCapacity; j++ ) | ||
20 | { | ||
21 | va.construct( &aData[j], value() ); | ||
22 | } | ||
23 | } | ||
24 | |||
25 | virtual ~RingBuffer() | ||
26 | { | ||
27 | for( int j = 0; j < nCapacity; j++ ) | ||
28 | { | ||
29 | va.destroy( &aData[j] ); | ||
30 | } | ||
31 | va.deallocate( aData, nCapacity ); | ||
32 | } | ||
33 | |||
34 | int getCapacity() | ||
35 | { | ||
36 | return nCapacity; | ||
37 | } | ||
38 | |||
39 | bool isFilled() | ||
40 | { | ||
41 | return (nStart == nEnd); | ||
42 | } | ||
43 | |||
44 | bool isEmpty() | ||
45 | { | ||
46 | return (nStart == -1); | ||
47 | } | ||
48 | |||
49 | void enqueue( const value &v ) | ||
50 | { | ||
51 | if( nStart == -1 ) | ||
52 | { | ||
53 | nStart = 0; | ||
54 | nEnd = 1; | ||
55 | aData[0] = v; | ||
56 | } | ||
57 | else if( nStart == nEnd ) | ||
58 | { | ||
59 | throw ExceptionBase("Hey, it's full!"); | ||
60 | } | ||
61 | else | ||
62 | { | ||
63 | aData[nEnd] = v; | ||
64 | nEnd = (nEnd+1)%nCapacity; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | value dequeue() | ||
69 | { | ||
70 | if( nStart == -1 ) | ||
71 | { | ||
72 | throw ExceptionBase("No data"); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | value &v = aData[nStart]; | ||
77 | nStart = (nStart+1)%nCapacity; | ||
78 | if( nStart == nEnd ) | ||
79 | { | ||
80 | nStart = -1; | ||
81 | nEnd = -2; | ||
82 | } | ||
83 | return v; | ||
84 | } | ||
85 | } | ||
86 | |||
87 | value &operator[]( int nIndex ) | ||
88 | { | ||
89 | return aData[(nIndex+nStart)%nCapacity]; | ||
90 | } | ||
91 | |||
92 | private: | ||
93 | int nCapacity; | ||
94 | value *aData; | ||
95 | valuealloc va; | ||
96 | int nStart, nEnd; | ||
97 | }; | ||
98 | } | ||
99 | |||
100 | #endif | ||
diff --git a/src/tests/ringbuffer.cpp b/src/tests/ringbuffer.cpp new file mode 100644 index 0000000..259ec1f --- /dev/null +++ b/src/tests/ringbuffer.cpp | |||
@@ -0,0 +1,29 @@ | |||
1 | #include "bu/ringbuffer.h" | ||
2 | #include <stdlib.h> | ||
3 | |||
4 | int main() | ||
5 | { | ||
6 | Bu::RingBuffer<uint32_t> ibuf( 10 ); | ||
7 | |||
8 | for( int k = 0; k < 2; k++ ) | ||
9 | { | ||
10 | int j = 1; | ||
11 | for(; j < 7; j++ ) | ||
12 | { | ||
13 | ibuf.enqueue( j ); | ||
14 | } | ||
15 | |||
16 | for(; j < 20; j++ ) | ||
17 | { | ||
18 | ibuf.enqueue( j ); | ||
19 | printf("- %d\n", ibuf.dequeue() ); | ||
20 | } | ||
21 | |||
22 | for(;;) | ||
23 | { | ||
24 | if( ibuf.isEmpty() ) break; | ||
25 | printf(". %d\n", ibuf.dequeue() ); | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | |||