From 6119b465b19e9be095971a33c63b0fa9a0e8a224 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 24 Sep 2008 05:52:36 +0000 Subject: Wow, I realized that the Bu::Array class wasn't finished, and went ahead and wrote it, it's pretty feature complete, index, append, iterators. You can't delete anything yet, exactly, but that's tricky in an array anyway, basically you just want to be able to remove elements from the end, and that's halfway there. Also, fixed some documentation and minor issues in Bu::Set, and made the Bu::Archive include fewer other classes while still defining archive oprators for them. I think I may yet move those into the headers for the classes that are being stored instead, makes a little more sense. I also would like to move the Exception classes out of the exceptions.h file and into the appropriate class' files'. There still should probably be a couple of general ones in there, or maybe just in exceptionbase.h, we'll see. --- src/archive.h | 45 ++++++++-- src/array.cpp | 1 + src/array.h | 240 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/set.h | 6 +- src/unit/array.cpp | 59 +++++++++++++ 5 files changed, 328 insertions(+), 23 deletions(-) create mode 100644 src/unit/array.cpp (limited to 'src') diff --git a/src/archive.h b/src/archive.h index 1d57724..519b2a7 100644 --- a/src/archive.h +++ b/src/archive.h @@ -13,7 +13,7 @@ #include #include "bu/hash.h" #include "bu/list.h" -#include "bu/set.h" +//#include "bu/set.h" #include "bu/util.h" namespace Bu @@ -298,11 +298,12 @@ namespace Bu return ar; } - template - Archive &operator<<( Archive &ar, Set &h ) + template class Array; + template + Archive &operator<<( Archive &ar, Array &h ) { ar << h.getSize(); - for( typename Set::iterator i = h.begin(); i != h.end(); i++ ) + for( typename Array::iterator i = h.begin(); i != h.end(); i++ ) { ar << (*i); } @@ -310,17 +311,47 @@ namespace Bu return ar; } - template - Archive &operator>>( Archive &ar, Set &h ) + template + Archive &operator>>(Archive &ar, Array &h ) { h.clear(); long nSize; ar >> nSize; - + + h.setCapacity( nSize ); for( long j = 0; j < nSize; j++ ) { value v; ar >> v; + h.append( v ); + } + return ar; + } + + template class Set; + template + Archive &operator<<( Archive &ar, Set &h ) + { + ar << h.getSize(); + for( typename Set::iterator i = h.begin(); i != h.end(); i++ ) + { + ar << (*i); + } + + return ar; + } + + template + Archive &operator>>( Archive &ar, Set &h ) + { + h.clear(); + long nSize; + ar >> nSize; + + for( long j = 0; j < nSize; j++ ) + { + key v; + ar >> v; h.insert( v ); } diff --git a/src/array.cpp b/src/array.cpp index 8cb58ab..826425c 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -7,3 +7,4 @@ #include "bu/array.h" +namespace Bu { subExceptionDef( ArrayException ) } diff --git a/src/array.h b/src/array.h index 093ece2..9233875 100644 --- a/src/array.h +++ b/src/array.h @@ -13,6 +13,7 @@ namespace Bu { + subExceptionDecl( ArrayException ) /** * Array type container, just like a normal array only flexible and keeps * track of your memory for you. @@ -41,10 +42,14 @@ namespace Bu iSize( 0 ), iCapacity( 0 ) { - // for( Link *pCur = src.pFirst; pCur; pCur = pCur->pNext ) - // { - // append( *pCur->pValue ); - // } + } + + Array( long iSetCap ) : + pData( NULL ), + iSize( 0 ), + iCapacity( 0 ) + { + setCapacity( iSetCap ); } ~Array() @@ -53,33 +58,244 @@ namespace Bu } /** - * Clear the data from the list. + * Clear the array. */ void clear() { + if( pData ) + { + for( int j = 0; j < iSize; j++ ) + { + va.destroy( &pData[j] ); + } + va.deallocate( pData, iCapacity ); + pData = NULL; + } + iSize = 0; + iCapacity = 0; } - //operator + void append( const value &rVal ) + { + if( iSize == iCapacity ) + { + setCapacity( iCapacity + inc ); + } + + va.construct( &pData[iSize++], rVal ); + } + + //operator + value &operator[]( long iIndex ) + { + if( iIndex < 0 || iIndex >= iSize ) + throw ArrayException( + "Index %d out of range 0:%d", iIndex, iSize ); + + return pData[iIndex]; + } + + const value &operator[]( long iIndex ) const + { + if( iIndex < 0 || iIndex >= iSize ) + throw ArrayException( + "Index %d out of range 0:%d", iIndex, iSize ); + + return pData[iIndex]; + } /** - * Get the current size of the list. - *@returns (int) The current size of the list. + * Get the current size of the array. + *@returns The current size of the array. */ - int getSize() const + long getSize() const { return iSize; } - int getCapacity() const + /** + * Get the capacity of the array. This number will grow as data is + * added, and is mainly for the curious, it doesn't really determine + * much for the end user. + *@returns The current capacity of the array. + */ + long getCapacity() const { return iCapacity; } + /** + * Change the capacity of the array, very useful if you know you'll be + * adding a large amount of already counted items to the array, makes + * the appending much faster afterwords. + *@param iNewLen The new capacity of the array. + *@todo Set this up so it can reduce the size of the array as well as + * make it bigger. + */ + void setCapacity( long iNewLen ) + { + if( iNewLen < iCapacity ) return; + value *pNewData = va.allocate( iNewLen ); + if( pData ) + { + for( int j = 0; j < iSize; j++ ) + { + va.construct( &pNewData[j], pData[j] ); + va.destroy( &pData[j] ); + } + va.deallocate( pData, iCapacity ); + } + pData = pNewData; + iCapacity = iNewLen; + } + + typedef struct iterator + { + friend class Array; + private: + iterator( MyType &src, long iPos=0 ) : + src( src ), + iPos( iPos ) + { + } + + MyType &src; + long iPos; + + public: + iterator operator++( int ) + { + if( iPos < 0 ) + throw ArrayException( + "Cannot increment iterator past end of array."); + iPos++; + if( iPos >= src.getSize() ) + iPos = -1; + return *this; + } + + iterator operator++() + { + if( iPos >= 0 ) + iPos++; + if( iPos >= src.getSize() ) + iPos = -1; + return *this; + } + + bool operator==( const iterator &oth ) + { + return iPos == oth.iPos; + } + + bool operator!=( const iterator &oth ) + { + return iPos != oth.iPos; + } + + iterator operator=( const iterator &oth ) + { + if( &src != &oth.src ) + throw ArrayException( + "Cannot mix iterators from different array objects."); + iPos = oth.iPos; + } + + value &operator*() + { + if( iPos < 0 ) + throw ArrayException( + "Cannot dereference finished iterator."); + return src[iPos]; + } + } iterator; + + typedef struct const_iterator + { + friend class Array; + private: + const_iterator( const MyType &src, long iPos=0 ) : + src( src ), + iPos( iPos ) + { + } + + const MyType &src; + long iPos; + + public: + const_iterator operator++( int ) + { + if( iPos < 0 ) + throw ArrayException( + "Cannot increment iterator past end of array."); + iPos++; + if( iPos >= src.getSize() ) + iPos = -1; + return *this; + } + + const_iterator operator++() + { + if( iPos >= 0 ) + iPos++; + if( iPos >= src.getSize() ) + iPos = -1; + return *this; + } + + bool operator==( const const_iterator &oth ) + { + return iPos == oth.iPos; + } + + bool operator!=( const const_iterator &oth ) + { + return iPos != oth.iPos; + } + + const_iterator operator=( const const_iterator &oth ) + { + if( &src != &oth.src ) + throw ArrayException( + "Cannot mix iterators from different array objects."); + iPos = oth.iPos; + } + + const value &operator*() const + { + if( iPos < 0 ) + throw ArrayException( + "Cannot dereference finished iterator."); + return src[iPos]; + } + } const_iterator; + + iterator begin() + { + return iterator( *this ); + } + + const_iterator begin() const + { + return const_iterator( *this ); + } + + iterator end() + { + return iterator( *this, -1 ); + } + + const_iterator end() const + { + return const_iterator( *this, -1 ); + } + private: valuealloc va; value *pData; - int iSize; - int iCapacity; + long iSize; + long iCapacity; }; } diff --git a/src/set.h b/src/set.h index 7dfa191..42f9eb8 100644 --- a/src/set.h +++ b/src/set.h @@ -16,8 +16,6 @@ #include #include "bu/exceptionbase.h" #include "bu/list.h" -///#include "archival.h" -///#include "archive.h" #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) @@ -355,7 +353,7 @@ namespace Bu { if( &hsh != &oth.hsh ) throw SetException( - "Cannot mix iterators from different hash objects."); + "Cannot mix iterators from different set objects."); nPos = oth.nPos; bFinished = oth.bFinished; } @@ -371,7 +369,7 @@ namespace Bu } iterator; /** - * Iteration structure for iterating through the hash (const). + * Iteration structure for iterating through the set (const). */ typedef struct const_iterator { diff --git a/src/unit/array.cpp b/src/unit/array.cpp new file mode 100644 index 0000000..db29882 --- /dev/null +++ b/src/unit/array.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2007-2008 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. + */ + +#include "bu/unitsuite.h" +#include "bu/array.h" + +class Unit : public Bu::UnitSuite +{ +public: + Unit() + { + setName("Array"); + addTest( Unit::general ); + addTest( Unit::iterate ); + } + + virtual ~Unit() + { + } + + void general() + { + Bu::Array ai; + + ai.append( 5 ); + ai.append( 10 ); + unitTest( ai.getSize() == 2 ); + unitTest( ai.getCapacity() == 10 ); + unitTest( ai[0] == 5 ); + unitTest( ai[1] == 10 ); + } + + void iterate() + { + Bu::Array ai; + for( int j = 0; j < 10; j++ ) + ai.append( j ); + + int j = 0; + for( Bu::Array::iterator i = ai.begin(); i != ai.end(); i++ ) + unitTest( (*i) == j++ ); + + const Bu::Array &ci = ai; + j = 0; + for( Bu::Array::const_iterator i = ci.begin(); i != ci.end(); i++ ) + unitTest( (*i) == j++ ); + } + +}; + +int main( int argc, char *argv[] ) +{ + return Unit().run( argc, argv ); +} + -- cgit v1.2.3