aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/list.h7
-rw-r--r--src/listhash.cpp8
-rw-r--r--src/listhash.h54
-rw-r--r--src/ringbuffer.h207
4 files changed, 168 insertions, 108 deletions
diff --git a/src/list.h b/src/list.h
index ba1d2c4..b1e0d0f 100644
--- a/src/list.h
+++ b/src/list.h
@@ -16,6 +16,7 @@
16 16
17namespace Bu 17namespace Bu
18{ 18{
19 /** @cond DEVEL */
19 template<typename value> 20 template<typename value>
20 struct ListLink 21 struct ListLink
21 { 22 {
@@ -185,6 +186,7 @@ namespace Bu
185 } 186 }
186 } 187 }
187 }; 188 };
189 /** @endcond */
188 190
189 /** 191 /**
190 * Linked list template container. This class is similar to the stl list 192 * Linked list template container. This class is similar to the stl list
@@ -197,14 +199,15 @@ namespace Bu
197 *@param value (typename) The type of data to store in your list 199 *@param value (typename) The type of data to store in your list
198 *@param valuealloc (typename) Memory Allocator for your value type 200 *@param valuealloc (typename) Memory Allocator for your value type
199 *@param linkalloc (typename) Memory Allocator for the list links. 201 *@param linkalloc (typename) Memory Allocator for the list links.
202 *@extends SharedCore
200 *@ingroup Containers 203 *@ingroup Containers
201 */ 204 */
202 template<typename value, typename valuealloc=std::allocator<value>, 205 template<typename value, typename valuealloc=std::allocator<value>,
203 typename linkalloc=std::allocator<struct ListLink<value> > > 206 typename linkalloc=std::allocator<struct ListLink<value> > >
204 class List : public SharedCore< 207 class List /** @cond */ : public SharedCore<
205 List<value, valuealloc, linkalloc>, 208 List<value, valuealloc, linkalloc>,
206 ListCore<value, valuealloc, linkalloc> 209 ListCore<value, valuealloc, linkalloc>
207 > 210 > /** @endcond */
208 { 211 {
209 private: 212 private:
210 typedef struct ListLink<value> Link; 213 typedef struct ListLink<value> Link;
diff --git a/src/listhash.cpp b/src/listhash.cpp
deleted file mode 100644
index b798a1f..0000000
--- a/src/listhash.cpp
+++ /dev/null
@@ -1,8 +0,0 @@
1/*
2 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8
diff --git a/src/listhash.h b/src/listhash.h
deleted file mode 100644
index e5ec4ee..0000000
--- a/src/listhash.h
+++ /dev/null
@@ -1,54 +0,0 @@
1/*
2 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_LIST_HASH_H
9#define BU_LIST_HASH_H
10
11#include "bu/hash.h"
12#include "bu/list.h"
13
14namespace Bu
15{
16 template<typename key, typename value, typename sizecalc = __calcNextTSize_fast, typename keyalloc = std::allocator<key>, typename valuealloc = std::allocator<Bu::List<value> >, typename challoc = std::allocator<uint32_t> >
17 class ListHash : public Hash<key, Bu::List<value>, sizecalc, keyalloc, valuealloc, challoc>
18 {
19 typedef Hash<key, Bu::List<value>, sizecalc, keyalloc, valuealloc, challoc> ParentType;
20 public:
21 ListHash()
22 {
23 }
24
25 ListHash( const ListHash &src ) :
26 ParentType( src )
27 {
28 }
29
30 virtual ~ListHash()
31 {
32 }
33
34 ListHash &operator=( const ListHash &src )
35 {
36 *dynamic_cast<ParentType *>(this) =
37 dynamic_cast<ParentType &>(src);
38 }
39
40 virtual void insert( const key &k, const value &v )
41 {
42 if( !has( k ) )
43 {
44 ParentType::insert( k, List<value>() );
45 }
46 get( k ).append( v );
47 }
48
49 private:
50 };
51
52};
53
54#endif
diff --git a/src/ringbuffer.h b/src/ringbuffer.h
index e984d18..04add42 100644
--- a/src/ringbuffer.h
+++ b/src/ringbuffer.h
@@ -10,97 +10,216 @@
10 10
11#include <memory> 11#include <memory>
12#include "bu/exceptionbase.h" 12#include "bu/exceptionbase.h"
13#include "bu/queue.h"
14#include "bu/sharedcore.h"
13 15
14namespace Bu 16namespace Bu
15{ 17{
16 /** 18 template<typename value, typename valuealloc> class RingBuffer;
17 *@ingroup Containers 19
18 */ 20 template<typename value, typename valuealloc>
19 template<typename value, typename valuealloc=std::allocator<value> > 21 class RingBufferCore
20 class RingBuffer
21 { 22 {
22 public: 23 friend class RingBuffer<value, valuealloc>;
23 RingBuffer( int nCapacity ) : 24 friend class SharedCore<RingBuffer<value, valuealloc>,
24 nCapacity( nCapacity ), 25 RingBufferCore<value, valuealloc> >;
25 nStart( -1 ), 26 private:
26 nEnd( -2 ) 27 RingBufferCore() :
28 iCapacity( 0 ),
29 iStart( -1 ),
30 iEnd( -2 ),
31 aData( NULL )
27 { 32 {
28 aData = va.allocate( nCapacity );
29 } 33 }
30 34
31 virtual ~RingBuffer() 35 virtual ~RingBufferCore()
32 { 36 {
33 for( int j = nStart; j < nEnd; j=(j+1%nCapacity) ) 37 clear();
34 {
35 va.destroy( &aData[j] );
36 }
37 va.deallocate( aData, nCapacity );
38 } 38 }
39 39
40 int getCapacity() 40 void init( int iNewCapacity )
41 { 41 {
42 return nCapacity; 42 if( iCapacity > 0 )
43 } 43 return;
44 44
45 bool isFilled() 45 iCapacity = iNewCapacity;
46 { 46 iStart = -1;
47 return (nStart == nEnd); 47 iEnd = -2;
48 aData = va.allocate( iCapacity );
48 } 49 }
49 50
50 bool isEmpty() 51 void clear()
51 { 52 {
52 return (nStart == -1); 53 for( int j = iStart; j < iEnd; j=(j+1%iCapacity) )
54 {
55 va.destroy( &aData[j] );
56 }
57 va.deallocate( aData, iCapacity );
58 aData = NULL;
59 iCapacity = 0;
53 } 60 }
54 61
55 void enqueue( const value &v ) 62 void enqueue( const value &v )
56 { 63 {
57 if( nStart == -1 ) 64 if( iStart == -1 )
58 { 65 {
59 nStart = 0; 66 iStart = 0;
60 nEnd = 1; 67 iEnd = 1;
61 va.construct( &aData[0], v ); 68 va.construct( &aData[0], v );
62 } 69 }
63 else if( nStart == nEnd ) 70 else if( iStart == iEnd )
64 { 71 {
65 throw ExceptionBase("Hey, it's full!"); 72 throw ExceptionBase("Hey, it's full!");
66 } 73 }
67 else 74 else
68 { 75 {
69 va.construct( &aData[nEnd], v ); 76 va.construct( &aData[iEnd], v );
70 nEnd = (nEnd+1)%nCapacity; 77 iEnd = (iEnd+1)%iCapacity;
71 } 78 }
72 } 79 }
73 80
74 value dequeue() 81 value dequeue()
75 { 82 {
76 if( nStart == -1 ) 83 if( iStart == -1 )
77 { 84 {
78 throw ExceptionBase("No data"); 85 throw ExceptionBase("No data");
79 } 86 }
80 else 87 else
81 { 88 {
82 value &v = aData[nStart]; 89 value &v = aData[iStart];
83 va.destroy( &aData[nStart] ); 90 va.destroy( &aData[iStart] );
84 nStart = (nStart+1)%nCapacity; 91 iStart = (iStart+1)%iCapacity;
85 if( nStart == nEnd ) 92 if( iStart == iEnd )
86 { 93 {
87 nStart = -1; 94 iStart = -1;
88 nEnd = -2; 95 iEnd = -2;
89 } 96 }
90 return v; 97 return v;
91 } 98 }
92 } 99 }
93 100
94 value &operator[]( int nIndex ) 101 value &get( int iIndex )
95 { 102 {
96 return aData[(nIndex+nStart)%nCapacity]; 103 return aData[(iIndex+iStart)%iCapacity];
97 } 104 }
98 105
99 private: 106 int getSize()
100 int nCapacity; 107 {
108 if( iStart < 0 )
109 return 0;
110 if( iEnd == iStart )
111 return iCapacity;
112 if( iEnd < iStart )
113 return iEnd-iStart;
114 return iCapacity-(iEnd-iStart);
115 }
116
117 int iCapacity;
118 int iStart, iEnd;
101 value *aData; 119 value *aData;
102 valuealloc va; 120 valuealloc va;
103 int nStart, nEnd; 121 };
122
123 /**
124 *@ingroup Containers
125 */
126 template<typename value, typename valuealloc=std::allocator<value> >
127 class RingBuffer : public Queue<value>, public SharedCore<
128 RingBuffer<value, valuealloc>,
129 RingBufferCore<value, valuealloc>
130 >
131 {
132 private:
133 typedef RingBuffer<value, valuealloc> MyType;
134 typedef RingBufferCore<value, valuealloc> Core;
135
136 protected:
137 using SharedCore<MyType, Core>::core;
138 using SharedCore<MyType, Core>::_hardCopy;
139 using SharedCore<MyType, Core>::_allocateCore;
140
141 public:
142 RingBuffer( int iCapacity )
143 {
144 core->init( iCapacity );
145 }
146
147 RingBuffer( const RingBuffer &rSrc ) :
148 SharedCore<MyType, Core>( rSrc )
149 {
150 }
151
152 virtual ~RingBuffer()
153 {
154 }
155
156 int getCapacity() const
157 {
158 return core->iCapacity;
159 }
160
161 bool isFilled() const
162 {
163 return (core->iStart == core->iEnd);
164 }
165
166 bool isEmpty() const
167 {
168 return (core->iStart == -1);
169 }
170
171 virtual void enqueue( const value &v )
172 {
173 _hardCopy();
174
175 core->enqueue( v );
176 }
177
178 virtual value dequeue()
179 {
180 _hardCopy();
181
182 return core->dequeue();
183 }
184
185 virtual int getSize() const
186 {
187 return core->getSize();
188 }
189
190 virtual value &peek()
191 {
192 _hardCopy();
193
194 return core->get( 0 );
195 }
196
197 virtual const value &peek() const
198 {
199 return core->get( 0 );
200 }
201
202 value &operator[]( int iIndex )
203 {
204 _hardCopy();
205
206 return core->get( iIndex );
207 }
208
209 protected:
210 virtual Core *_copyCore( Core *src )
211 {
212 Core *pRet = _allocateCore();
213
214 pRet->init( src->iCapacity );
215 int iSize = src->getSize();
216 for( int j = 0; j < iSize; j++ )
217 {
218 pRet->enqueue( src->get( j ) );
219 }
220
221 return pRet;
222 }
104 }; 223 };
105} 224}
106 225