From 93c028162318a00b9bd03fc4a48383f830cc529d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 15 Oct 2010 15:12:31 +0000 Subject: RingBuffer is now SharedCore. I think that's all the container classes, there may be a few other things that should change too, we'll see. Played with doxygen docs on List, we can actually use @cond to remove things from the docs, either permenently or conditionally, and so I could trick it into making all of the sharedcore classes inherit from the same SharedCore in the docs instead of different ones. Or, just not inherit from SharedCore at all. What to do...? :-P I also got rid of ListHash, it wasn't working out yet anyway. --- src/list.h | 7 +- src/listhash.cpp | 8 --- src/listhash.h | 54 --------------- src/ringbuffer.h | 207 +++++++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 168 insertions(+), 108 deletions(-) delete mode 100644 src/listhash.cpp delete mode 100644 src/listhash.h 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 @@ namespace Bu { + /** @cond DEVEL */ template struct ListLink { @@ -185,6 +186,7 @@ namespace Bu } } }; + /** @endcond */ /** * Linked list template container. This class is similar to the stl list @@ -197,14 +199,15 @@ namespace Bu *@param value (typename) The type of data to store in your list *@param valuealloc (typename) Memory Allocator for your value type *@param linkalloc (typename) Memory Allocator for the list links. + *@extends SharedCore *@ingroup Containers */ template, typename linkalloc=std::allocator > > - class List : public SharedCore< + class List /** @cond */ : public SharedCore< List, ListCore - > + > /** @endcond */ { private: typedef struct ListLink 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 @@ -/* - * Copyright (C) 2007-2010 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - - 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 @@ -/* - * Copyright (C) 2007-2010 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_LIST_HASH_H -#define BU_LIST_HASH_H - -#include "bu/hash.h" -#include "bu/list.h" - -namespace Bu -{ - template, typename valuealloc = std::allocator >, typename challoc = std::allocator > - class ListHash : public Hash, sizecalc, keyalloc, valuealloc, challoc> - { - typedef Hash, sizecalc, keyalloc, valuealloc, challoc> ParentType; - public: - ListHash() - { - } - - ListHash( const ListHash &src ) : - ParentType( src ) - { - } - - virtual ~ListHash() - { - } - - ListHash &operator=( const ListHash &src ) - { - *dynamic_cast(this) = - dynamic_cast(src); - } - - virtual void insert( const key &k, const value &v ) - { - if( !has( k ) ) - { - ParentType::insert( k, List() ); - } - get( k ).append( v ); - } - - private: - }; - -}; - -#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 @@ #include #include "bu/exceptionbase.h" +#include "bu/queue.h" +#include "bu/sharedcore.h" namespace Bu { - /** - *@ingroup Containers - */ - template > - class RingBuffer + template class RingBuffer; + + template + class RingBufferCore { - public: - RingBuffer( int nCapacity ) : - nCapacity( nCapacity ), - nStart( -1 ), - nEnd( -2 ) + friend class RingBuffer; + friend class SharedCore, + RingBufferCore >; + private: + RingBufferCore() : + iCapacity( 0 ), + iStart( -1 ), + iEnd( -2 ), + aData( NULL ) { - aData = va.allocate( nCapacity ); } - virtual ~RingBuffer() + virtual ~RingBufferCore() { - for( int j = nStart; j < nEnd; j=(j+1%nCapacity) ) - { - va.destroy( &aData[j] ); - } - va.deallocate( aData, nCapacity ); + clear(); } - int getCapacity() + void init( int iNewCapacity ) { - return nCapacity; - } + if( iCapacity > 0 ) + return; - bool isFilled() - { - return (nStart == nEnd); + iCapacity = iNewCapacity; + iStart = -1; + iEnd = -2; + aData = va.allocate( iCapacity ); } - bool isEmpty() + void clear() { - return (nStart == -1); + for( int j = iStart; j < iEnd; j=(j+1%iCapacity) ) + { + va.destroy( &aData[j] ); + } + va.deallocate( aData, iCapacity ); + aData = NULL; + iCapacity = 0; } - + void enqueue( const value &v ) { - if( nStart == -1 ) + if( iStart == -1 ) { - nStart = 0; - nEnd = 1; + iStart = 0; + iEnd = 1; va.construct( &aData[0], v ); } - else if( nStart == nEnd ) + else if( iStart == iEnd ) { throw ExceptionBase("Hey, it's full!"); } else { - va.construct( &aData[nEnd], v ); - nEnd = (nEnd+1)%nCapacity; + va.construct( &aData[iEnd], v ); + iEnd = (iEnd+1)%iCapacity; } } value dequeue() { - if( nStart == -1 ) + if( iStart == -1 ) { throw ExceptionBase("No data"); } else { - value &v = aData[nStart]; - va.destroy( &aData[nStart] ); - nStart = (nStart+1)%nCapacity; - if( nStart == nEnd ) + value &v = aData[iStart]; + va.destroy( &aData[iStart] ); + iStart = (iStart+1)%iCapacity; + if( iStart == iEnd ) { - nStart = -1; - nEnd = -2; + iStart = -1; + iEnd = -2; } return v; } } - value &operator[]( int nIndex ) + value &get( int iIndex ) { - return aData[(nIndex+nStart)%nCapacity]; + return aData[(iIndex+iStart)%iCapacity]; } - private: - int nCapacity; + int getSize() + { + if( iStart < 0 ) + return 0; + if( iEnd == iStart ) + return iCapacity; + if( iEnd < iStart ) + return iEnd-iStart; + return iCapacity-(iEnd-iStart); + } + + int iCapacity; + int iStart, iEnd; value *aData; valuealloc va; - int nStart, nEnd; + }; + + /** + *@ingroup Containers + */ + template > + class RingBuffer : public Queue, public SharedCore< + RingBuffer, + RingBufferCore + > + { + private: + typedef RingBuffer MyType; + typedef RingBufferCore Core; + + protected: + using SharedCore::core; + using SharedCore::_hardCopy; + using SharedCore::_allocateCore; + + public: + RingBuffer( int iCapacity ) + { + core->init( iCapacity ); + } + + RingBuffer( const RingBuffer &rSrc ) : + SharedCore( rSrc ) + { + } + + virtual ~RingBuffer() + { + } + + int getCapacity() const + { + return core->iCapacity; + } + + bool isFilled() const + { + return (core->iStart == core->iEnd); + } + + bool isEmpty() const + { + return (core->iStart == -1); + } + + virtual void enqueue( const value &v ) + { + _hardCopy(); + + core->enqueue( v ); + } + + virtual value dequeue() + { + _hardCopy(); + + return core->dequeue(); + } + + virtual int getSize() const + { + return core->getSize(); + } + + virtual value &peek() + { + _hardCopy(); + + return core->get( 0 ); + } + + virtual const value &peek() const + { + return core->get( 0 ); + } + + value &operator[]( int iIndex ) + { + _hardCopy(); + + return core->get( iIndex ); + } + + protected: + virtual Core *_copyCore( Core *src ) + { + Core *pRet = _allocateCore(); + + pRet->init( src->iCapacity ); + int iSize = src->getSize(); + for( int j = 0; j < iSize; j++ ) + { + pRet->enqueue( src->get( j ) ); + } + + return pRet; + } }; } -- cgit v1.2.3