From da89e6d30e57bd6dbb10b4d36b093ce9bbf5c666 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 04:50:36 +0000 Subject: The first batch seem to have made it alright. Unfortunately the Archive class isn't done yet, I'm going to make it rely on streams, so those will be next, then we can make it work all sortsa' well. --- src/old/exceptionbase.cpp | 70 ----- src/old/exceptionbase.h | 105 ------- src/old/exceptions.cpp | 8 - src/old/exceptions.h | 25 -- src/old/fstring.cpp | 13 - src/old/fstring.h | 651 ---------------------------------------- src/old/hash.cpp | 113 ------- src/old/hash.h | 744 ---------------------------------------------- src/old/hashable.cpp | 1 - src/old/hashable.h | 12 - src/old/serializable.cpp | 8 - src/old/serializable.h | 34 --- src/old/serializer.cpp | 338 --------------------- src/old/serializer.h | 80 ----- src/old/stream.cpp | 10 - src/old/stream.h | 27 -- 16 files changed, 2239 deletions(-) delete mode 100644 src/old/exceptionbase.cpp delete mode 100644 src/old/exceptionbase.h delete mode 100644 src/old/exceptions.cpp delete mode 100644 src/old/exceptions.h delete mode 100644 src/old/fstring.cpp delete mode 100644 src/old/fstring.h delete mode 100644 src/old/hash.cpp delete mode 100644 src/old/hash.h delete mode 100644 src/old/hashable.cpp delete mode 100644 src/old/hashable.h delete mode 100644 src/old/serializable.cpp delete mode 100644 src/old/serializable.h delete mode 100644 src/old/serializer.cpp delete mode 100644 src/old/serializer.h delete mode 100644 src/old/stream.cpp delete mode 100644 src/old/stream.h (limited to 'src/old') diff --git a/src/old/exceptionbase.cpp b/src/old/exceptionbase.cpp deleted file mode 100644 index f3d22da..0000000 --- a/src/old/exceptionbase.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "exceptionbase.h" -#include - -ExceptionBase::ExceptionBase( const char *lpFormat, ... ) throw() : - nErrorCode( 0 ), - sWhat( NULL ) -{ - va_list ap; - - va_start(ap, lpFormat); - setWhat( lpFormat, ap ); - va_end(ap); -} - -ExceptionBase::ExceptionBase( int nCode, const char *lpFormat, ... ) throw() : - nErrorCode( nCode ), - sWhat( NULL ) -{ - va_list ap; - - va_start(ap, lpFormat); - setWhat( lpFormat, ap ); - va_end(ap); -} - -ExceptionBase::ExceptionBase( int nCode ) throw() : - nErrorCode( nCode ), - sWhat( NULL ) -{ -} - -ExceptionBase::~ExceptionBase() throw() -{ - if( sWhat ) - { - delete[] sWhat; - sWhat = NULL; - } -} - -void ExceptionBase::setWhat( const char *lpFormat, va_list &vargs ) -{ - if( sWhat ) delete[] sWhat; - int nSize; - - nSize = vsnprintf( NULL, 0, lpFormat, vargs ); - sWhat = new char[nSize+1]; - vsnprintf( sWhat, nSize+1, lpFormat, vargs ); -} - -void ExceptionBase::setWhat( const char *lpText ) -{ - if( sWhat ) delete[] sWhat; - int nSize; - - nSize = strlen( lpText ); - sWhat = new char[nSize+1]; - strcpy( sWhat, lpText ); -} - -const char *ExceptionBase::what() const throw() -{ - return sWhat; -} - -int ExceptionBase::getErrorCode() -{ - return nErrorCode; -} - diff --git a/src/old/exceptionbase.h b/src/old/exceptionbase.h deleted file mode 100644 index 6f1eca7..0000000 --- a/src/old/exceptionbase.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef EXCEPTION_BASE_H -#define EXCEPTION_BASE_H - -#include -#include -#include - -/** - * A generalized Exception base class. This is nice for making general and - * flexible child classes that can create new error code classes. - */ -class ExceptionBase : public std::exception -{ -public: - /** - * Construct an exception with an error code of zero, but with a - * description. The use of this is not reccomended most of the time, it's - * generally best to include an error code with the exception so your - * program can handle the exception in a better way. - * @param sFormat The format of the text. See printf for more info. - */ - ExceptionBase( const char *sFormat, ... ) throw(); - - /** - * - * @param nCode - * @param sFormat - */ - ExceptionBase( int nCode, const char *sFormat, ... ) throw(); - - /** - * - * @param nCode - * @return - */ - ExceptionBase( int nCode=0 ) throw(); - - /** - * - * @return - */ - virtual ~ExceptionBase() throw(); - - /** - * - * @return - */ - virtual const char *what() const throw(); - - /** - * - * @return - */ - int getErrorCode(); - - /** - * - * @param lpFormat - * @param vargs - */ - void setWhat( const char *lpFormat, va_list &vargs ); - - /** - * - * @param lpText - */ - void setWhat( const char *lpText ); - -private: - int nErrorCode; /**< The code for the error that occured. */ - char *sWhat; /**< The text string telling people what went wrong. */ -}; - -#define subExceptionDecl( name ) \ -class name : public ExceptionBase \ -{ \ - public: \ - name( const char *sFormat, ... ) throw (); \ - name( int nCode, const char *sFormat, ... ) throw(); \ - name( int nCode=0 ) throw (); \ -}; - -#define subExceptionDef( name ) \ -name::name( const char *lpFormat, ... ) throw() : \ - ExceptionBase( 0 ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode, const char *lpFormat, ... ) throw() : \ - ExceptionBase( nCode ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode ) throw() : \ - ExceptionBase( nCode ) \ -{ \ -} - -#endif diff --git a/src/old/exceptions.cpp b/src/old/exceptions.cpp deleted file mode 100644 index ce79a5e..0000000 --- a/src/old/exceptions.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "exceptions.h" -#include - -subExceptionDef( XmlException ) -subExceptionDef( FileException ) -subExceptionDef( ConnectionException ) -subExceptionDef( PluginException ) - diff --git a/src/old/exceptions.h b/src/old/exceptions.h deleted file mode 100644 index 0ab2b15..0000000 --- a/src/old/exceptions.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef EXCEPTIONS_H -#define EXCEPTIONS_H - -#include "exceptionbase.h" -#include - -subExceptionDecl( XmlException ) -subExceptionDecl( FileException ) -subExceptionDecl( ConnectionException ) -subExceptionDecl( PluginException ) - -enum eFileException -{ - excodeEOF -}; - -enum eConnectionException -{ - excodeReadError, - excodeBadReadError, - excodeConnectionClosed, - excodeSocketTimeout -}; - -#endif diff --git a/src/old/fstring.cpp b/src/old/fstring.cpp deleted file mode 100644 index 82d024d..0000000 --- a/src/old/fstring.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "fstring.h" -#include "hash.h" - -template<> uint32_t __calcHashCode( const FString &k ) -{ - return __calcHashCode( k.c_str() ); -} - -template<> bool __cmpHashKeys( const FString &a, const FString &b ) -{ - return a == b; -} - diff --git a/src/old/fstring.h b/src/old/fstring.h deleted file mode 100644 index c5397cc..0000000 --- a/src/old/fstring.h +++ /dev/null @@ -1,651 +0,0 @@ -#ifndef F_STRING_H -#define F_STRING_H - -#include -#include -#include "serializable.h" -#include "serializer.h" - -template< typename chr > -struct FStringChunk -{ - long nLength; - chr *pData; - FStringChunk *pNext; -}; - -/** - * Flexible String class. This class was designed with string passing and - * generation in mind. Like the standard string class you can specify what - * datatype to use for each character. Unlike the standard string class, - * collection of appended and prepended terms is done lazily, making long - * operations that involve many appends very inexpensive. In addition internal - * ref-counting means that if you pass strings around between functions there's - * almost no overhead in time or memory since a reference is created and no - * data is actually copied. This also means that you never need to put any - * FBasicString into a ref-counting container class. - */ -template< typename chr, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > -class FBasicString : public Serializable -{ -#ifndef VALTEST -#define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) ) -#endif -private: - typedef struct FStringChunk Chunk; - typedef struct FBasicString MyType; - -public: - FBasicString() : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - } - - FBasicString( const chr *pData ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( pData ); - } - - FBasicString( const chr *pData, long nLength ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( pData, nLength ); - } - - FBasicString( const MyType &rSrc ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - // Here we have no choice but to copy, since the other guy is a const. - // In the case that the source were flat, we could get a reference, it - // would make some things faster, but not matter in many other cases. - - joinShare( rSrc ); - //copyFrom( rSrc ); - } - - FBasicString( const MyType &rSrc, long nLength ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( rSrc.pFirst->pData, nLength ); - } - - FBasicString( const MyType &rSrc, long nStart, long nLength ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( rSrc.pFirst->pData+nStart, nLength ); - } - - FBasicString( long nSize ) : - nLength( nSize ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - pFirst = pLast = newChunk( nSize ); - } - - virtual ~FBasicString() - { - clear(); - } - - void append( const chr *pData ) - { - long nLen; - for( nLen = 0; pData[nLen] != (chr)0; nLen++ ); - - Chunk *pNew = newChunk( nLen ); - cpy( pNew->pData, pData, nLen ); - - appendChunk( pNew ); - } - - void append( const chr *pData, long nLen ) - { - Chunk *pNew = newChunk( nLen ); - - cpy( pNew->pData, pData, nLen ); - - appendChunk( pNew ); - } - - void prepend( const chr *pData ) - { - long nLen; - for( nLen = 0; pData[nLen] != (chr)0; nLen++ ); - - Chunk *pNew = newChunk( nLen ); - cpy( pNew->pData, pData, nLen ); - - prependChunk( pNew ); - } - - void prepend( const chr *pData, long nLen ) - { - Chunk *pNew = newChunk( nLen ); - - cpy( pNew->pData, pData, nLen ); - - prependChunk( pNew ); - } - - void clear() - { - realClear(); - } - - void resize( long nNewSize ) - { - if( nLength == nNewSize ) - return; - - flatten(); - - Chunk *pNew = newChunk( nNewSize ); - long nNewLen = (nNewSizepData, pFirst->pData, nNewLen ); - pNew->pData[nNewLen] = (chr)0; - aChr.deallocate( pFirst->pData, pFirst->nLength+1 ); - aChunk.deallocate( pFirst, 1 ); - pFirst = pLast = pNew; - nLength = nNewSize; - } - - long getSize() const - { - return nLength; - } - - chr *getStr() - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - const chr *getStr() const - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - chr *c_str() - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - const chr *c_str() const - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - MyType &operator +=( const chr *pData ) - { - append( pData ); - - return (*this); - } - - MyType &operator +=( const MyType &rSrc ) - { - rSrc.flatten(); - append( rSrc.pFirst->pData, rSrc.nLength ); - - return (*this); - } - - MyType &operator +=( const chr pData ) - { - chr tmp[2] = { pData, (chr)0 }; - append( tmp ); - - return (*this); - } - - MyType &operator =( const chr *pData ) - { - clear(); - append( pData ); - - return (*this); - } - - MyType &operator =( const MyType &rSrc ) - { - //if( rSrc.isFlat() ) - //{ - joinShare( rSrc ); - //} - //else - //{ - // copyFrom( rSrc ); - //} - // - - return (*this); - } - - bool operator ==( const chr *pData ) const - { - if( pFirst == NULL ) { - if( pData == NULL ) - return true; - return false; - } - - flatten(); - const chr *a = pData; - chr *b = pFirst->pData; - for( ; *a!=(chr)0; a++, b++ ) - { - if( *a != *b ) - return false; - } - - return true; - } - - bool operator ==( const MyType &pData ) const - { - if( pFirst == pData.pFirst ) - return true; - if( pFirst == NULL ) - return false; - - flatten(); - pData.flatten(); - const chr *a = pData.pFirst->pData; - chr *b = pFirst->pData; - for( ; *a!=(chr)0; a++, b++ ) - { - if( *a != *b ) - return false; - } - - return true; - } - - bool operator !=(const chr *pData ) const - { - return !(*this == pData); - } - - bool operator !=(const MyType &pData ) const - { - return !(*this == pData); - } - - chr &operator[]( long nIndex ) - { - flatten(); - - return pFirst->pData[nIndex]; - } - - const chr &operator[]( long nIndex ) const - { - flatten(); - - return pFirst->pData[nIndex]; - } - - bool isWS( long nIndex ) const - { - flatten(); - - return pFirst->pData[nIndex]==' ' || pFirst->pData[nIndex]=='\t' - || pFirst->pData[nIndex]=='\r' || pFirst->pData[nIndex]=='\n'; - } - - bool isAlpha( long nIndex ) const - { - flatten(); - - return (pFirst->pData[nIndex] >= 'a' && pFirst->pData[nIndex] <= 'z') - || (pFirst->pData[nIndex] >= 'A' && pFirst->pData[nIndex] <= 'Z'); - } - - void toLower() - { - flatten(); - unShare(); - - for( long j = 0; j < nLength; j++ ) - { - if( pFirst->pData[j] >= 'A' && pFirst->pData[j] <= 'Z' ) - pFirst->pData[j] -= 'A'-'a'; - } - } - - void toUpper() - { - flatten(); - unShare(); - - for( long j = 0; j < nLength; j++ ) - { - if( pFirst->pData[j] >= 'a' && pFirst->pData[j] <= 'z' ) - pFirst->pData[j] += 'A'-'a'; - } - } - - void serialize( class Serializer &ar ) - { - if( ar.isLoading() ) - { - clear(); - long nLen; - ar >> nLen; - - Chunk *pNew = newChunk( nLen ); - ar.read( pNew->pData, nLen*sizeof(chr) ); - appendChunk( pNew ); - } - else - { - flatten(); - - ar << nLength; - ar.write( pFirst->pData, nLength*sizeof(chr) ); - } - } - -private: - void flatten() const - { - if( isFlat() ) - return; - - if( pFirst == NULL ) - return; - - unShare(); - - Chunk *pNew = newChunk( nLength ); - chr *pos = pNew->pData; - Chunk *i = pFirst; - for(;;) - { - cpy( pos, i->pData, i->nLength ); - pos += i->nLength; - i = i->pNext; - if( i == NULL ) - break; - } - realClear(); - - pLast = pFirst = pNew; - nLength = pNew->nLength; - } - - void realClear() const - { - if( pFirst == NULL ) - return; - - if( isShared() ) - { - decRefs(); - } - else - { - Chunk *i = pFirst; - for(;;) - { - Chunk *n = i->pNext; - aChr.deallocate( i->pData, i->nLength+1 ); - aChunk.deallocate( i, 1 ); - if( n == NULL ) - break; - i = n; - } - pFirst = pLast = NULL; - nLength = 0; - } - } - - void copyFrom( const FBasicString &rSrc ) - { - if( rSrc.pFirst == NULL ) - return; - - decRefs(); - - Chunk *pNew = newChunk( rSrc.nLength ); - chr *pos = pNew->pData; - Chunk *i = rSrc.pFirst; - for(;;) - { - cpy( pos, i->pData, i->nLength ); - pos += i->nLength; - i = i->pNext; - if( i == NULL ) - break; - } - clear(); - - appendChunk( pNew ); - } - - bool isFlat() const - { - return (pFirst == pLast); - } - - bool isShared() const - { - return (pnRefs != NULL); - } - - Chunk *newChunk() const - { - Chunk *pNew = aChunk.allocate( 1 ); - pNew->pNext = NULL; - return pNew; - } - - Chunk *newChunk( long nLen ) const - { - Chunk *pNew = aChunk.allocate( 1 ); - pNew->pNext = NULL; - pNew->nLength = nLen; - pNew->pData = aChr.allocate( nLen+1 ); - pNew->pData[nLen] = (chr)0; - return pNew; - } - - void appendChunk( Chunk *pNewChunk ) - { - unShare(); - - if( pFirst == NULL ) - pLast = pFirst = pNewChunk; - else - { - pLast->pNext = pNewChunk; - pLast = pNewChunk; - } - - nLength += pNewChunk->nLength; - } - - void prependChunk( Chunk *pNewChunk ) - { - unShare(); - - if( pFirst == NULL ) - pLast = pFirst = pNewChunk; - else - { - pNewChunk->pNext = pFirst; - pFirst = pNewChunk; - } - - nLength += pNewChunk->nLength; - } - - void joinShare( MyType &rSrc ) - { - clear(); - - if( !rSrc.isFlat() ) - rSrc.flatten(); - - rSrc.initCount(); - pnRefs = rSrc.pnRefs; - (*pnRefs)++; - nLength = rSrc.nLength; - pFirst = rSrc.pFirst; - pLast = rSrc.pLast; - } - - void joinShare( const MyType &rSrc ) - { - clear(); - - rSrc.flatten(); - - if( !rSrc.isShared() ) - { - rSrc.pnRefs = new uint32_t; - (*rSrc.pnRefs) = 1; - } - pnRefs = rSrc.pnRefs; - (*pnRefs)++; - nLength = rSrc.nLength; - pFirst = rSrc.pFirst; - pLast = rSrc.pLast; - } - - /** - * This takes an object that was shared and makes a copy of the base data - * that was being shared so that this copy can be changed. This should be - * added before any call that will change this object; - */ - void unShare() const - { - if( isShared() == false ) - return; - - Chunk *pNew = newChunk( nLength ); - chr *pos = pNew->pData; - Chunk *i = pFirst; - for(;;) - { - cpy( pos, i->pData, i->nLength ); - pos += i->nLength; - i = i->pNext; - if( i == NULL ) - break; - } - decRefs(); - pLast = pFirst = pNew; - nLength = pNew->nLength; - } - - /** - * This decrements our ref count and pulls us out of the share. If the ref - * count hits zero because of this, it destroys the share. This is not - * safe to call on it's own, it's much better to call unShare. - */ - void decRefs() const - { - if( isShared() ) - { - (*pnRefs)--; - if( (*pnRefs) == 0 ) - destroyShare(); - else - { - pnRefs = NULL; - pFirst = NULL; - pLast = NULL; - nLength = 0; - } - } - } - - /** - * While the unShare function removes an instance from a share, this - * function destroys the data that was in the share, removing the share - * itself. This should only be called when the refcount for the share has - * or is about to reach zero. - */ - void destroyShare() const - { - delete pnRefs; - pnRefs = NULL; - realClear(); - } - -#ifdef VALTEST - void cpy( chr *dest, const chr *src, long count ) const - { - for( int j = 0; j < count; j++ ) - { - *dest = *src; - dest++; - src++; - } - } -#endif - - void initCount() const - { - if( !isShared() ) - { - pnRefs = new uint32_t; - (*pnRefs) = 1; - } - } - -private: - mutable long nLength; - mutable uint32_t *pnRefs; - mutable Chunk *pFirst; - mutable Chunk *pLast; - - mutable chralloc aChr; - mutable chunkalloc aChunk; -}; - -typedef FBasicString FString; - -#include "hash.h" -template<> uint32_t __calcHashCode( const FString &k ); -template<> bool __cmpHashKeys( const FString &a, const FString &b ); - - -#endif diff --git a/src/old/hash.cpp b/src/old/hash.cpp deleted file mode 100644 index c52e6b1..0000000 --- a/src/old/hash.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "hash.h" - -subExceptionDef( HashException ) - -template<> uint32_t __calcHashCode( const int &k ) -{ - return k; -} - -template<> bool __cmpHashKeys( const int &a, const int &b ) -{ - return a == b; -} - -template<> uint32_t __calcHashCode( const unsigned int &k ) -{ - return k; -} - -template<> bool __cmpHashKeys( const unsigned int &a, const unsigned int &b ) -{ - return a == b; -} - -template<> -uint32_t __calcHashCode( const char * const &k ) -{ - if (k == NULL) - { - return 0; - } - - unsigned long int nPos = 0; - for( const char *s = k; *s; s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool __cmpHashKeys( const char * const &a, const char * const &b ) -{ - if( a == b ) - return true; - - for(int j=0; a[j] == b[j]; j++ ) - if( a[j] == '\0' ) - return true; - - return false; -} - -template<> -uint32_t __calcHashCode( char * const &k ) -{ - if (k == NULL) - { - return 0; - } - - unsigned long int nPos = 0; - for( const char *s = k; *s; s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool __cmpHashKeys( char * const &a, char * const &b ) -{ - if( a == b ) - return true; - - for(int j=0; a[j] == b[j]; j++ ) - if( a[j] == '\0' ) - return true; - - return false; -} - -template<> uint32_t __calcHashCode( const std::string &k ) -{ - std::string::size_type j, sz = k.size(); - const char *s = k.c_str(); - - unsigned long int nPos = 0; - for( j = 0; j < sz; j++, s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool __cmpHashKeys( const std::string &a, const std::string &b ) -{ - return a == b; -} - -template<> uint32_t __calcHashCode( const Hashable &k ) -{ - return 0; - //return k.getHashCode(); -} - -template<> bool __cmpHashKeys( const Hashable &a, const Hashable &b ) -{ - return false; - //return a.compareForHash( b ); -} - diff --git a/src/old/hash.h b/src/old/hash.h deleted file mode 100644 index e819379..0000000 --- a/src/old/hash.h +++ /dev/null @@ -1,744 +0,0 @@ -#ifndef HASH_H -#define HASH_H - -#include -#include -#include -#include -#include -#include "exceptionbase.h" -#include "hashable.h" -#include "serializable.h" -#include "serializer.h" - -#define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) - -subExceptionDecl( HashException ) - -enum eHashException -{ - excodeNotFilled -}; - -template -uint32_t __calcHashCode( const T &k ); - -template -bool __cmpHashKeys( const T &a, const T &b ); - -struct __calcNextTSize_fast -{ - uint32_t operator()( uint32_t nCapacity, uint32_t nFill, uint32_t nDeleted ) const - { - if( nDeleted >= nCapacity/2 ) - return nCapacity; - return nCapacity*2+1; - } -}; - -template, typename valuealloc = std::allocator, typename challoc = std::allocator > -class Hash; - -template< typename key, typename _value, typename sizecalc = __calcNextTSize_fast, typename keyalloc = std::allocator, typename valuealloc = std::allocator<_value>, typename challoc = std::allocator > -struct HashProxy -{ - friend class Hash; -private: - HashProxy( Hash &h, key *k, uint32_t nPos, uint32_t hash ) : - hsh( h ), - pKey( k ), - nPos( nPos ), - hash( hash ), - bFilled( false ) - { - } - - HashProxy( Hash &h, uint32_t nPos, _value *pValue ) : - hsh( h ), - nPos( nPos ), - pValue( pValue ), - bFilled( true ) - { - } - - Hash &hsh; - key *pKey; - uint32_t nPos; - _value *pValue; - uint32_t hash; - bool bFilled; - -public: - operator _value &() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return *pValue; - } - - _value &value() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return *pValue; - } - - bool isFilled() - { - return bFilled; - } - - void erase() - { - if( bFilled ) - { - hsh._erase( nPos ); - hsh.onDelete(); - } - } - - _value operator=( _value nval ) - { - if( bFilled ) - { - hsh.va.destroy( pValue ); - hsh.va.construct( pValue, nval ); - hsh.onUpdate(); - } - else - { - hsh.fill( nPos, *pKey, nval, hash ); - hsh.onInsert(); - } - - return nval; - } - - _value *operator->() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return pValue; - } -}; - -template -class Hash -{ - friend struct HashProxy; -public: - Hash() : - nCapacity( 11 ), - nFilled( 0 ), - nDeleted( 0 ), - bFilled( NULL ), - bDeleted( NULL ), - aKeys( NULL ), - aValues( NULL ), - aHashCodes( NULL ) - { - nKeysSize = bitsToBytes( nCapacity ); - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - } - - Hash( const Hash &src ) : - nCapacity( src.nCapacity ), - nFilled( 0 ), - nDeleted( 0 ), - bFilled( NULL ), - bDeleted( NULL ), - aKeys( NULL ), - aValues( NULL ), - aHashCodes( NULL ) - { - nKeysSize = bitsToBytes( nCapacity ); - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - - for( uint32_t j = 0; j < src.nCapacity; j++ ) - { - if( src.isFilled( j ) ) - { - insert( src.aKeys[j], src.aValues[j] ); - } - } - } - - Hash &operator=( const Hash &src ) - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - { - va.destroy( &aValues[j] ); - ka.destroy( &aKeys[j] ); - } - } - va.deallocate( aValues, nCapacity ); - ka.deallocate( aKeys, nCapacity ); - ca.deallocate( bFilled, nKeysSize ); - ca.deallocate( bDeleted, nKeysSize ); - ca.deallocate( aHashCodes, nCapacity ); - - nFilled = 0; - nDeleted = 0; - nCapacity = src.nCapacity; - nKeysSize = bitsToBytes( nCapacity ); - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - - for( uint32_t j = 0; j < src.nCapacity; j++ ) - { - if( src.isFilled( j ) ) - { - insert( src.aKeys[j], src.aValues[j] ); - } - } - - return *this; - } - - virtual ~Hash() - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - { - va.destroy( &aValues[j] ); - ka.destroy( &aKeys[j] ); - } - } - va.deallocate( aValues, nCapacity ); - ka.deallocate( aKeys, nCapacity ); - ca.deallocate( bFilled, nKeysSize ); - ca.deallocate( bDeleted, nKeysSize ); - ca.deallocate( aHashCodes, nCapacity ); - } - - uint32_t getCapacity() - { - return nCapacity; - } - - uint32_t getFill() - { - return nFilled; - } - - uint32_t size() - { - return nFilled-nDeleted; - } - - uint32_t getDeleted() - { - return nDeleted; - } - - virtual HashProxy operator[]( key k ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - return HashProxy( *this, nPos, &aValues[nPos] ); - } - else - { - return HashProxy( *this, &k, nPos, hash ); - } - } - - virtual void insert( key k, value v ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - va.destroy( &aValues[nPos] ); - va.construct( &aValues[nPos], v ); - onUpdate(); - } - else - { - fill( nPos, k, v, hash ); - onInsert(); - } - } - - virtual void erase( key k ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - _erase( nPos ); - onDelete(); - } - } - - struct iterator; - virtual void erase( struct iterator &i ) - { - if( this != &i.hsh ) - throw HashException("This iterator didn't come from this Hash."); - if( isFilled( i.nPos ) && !isDeleted( i.nPos ) ) - { - _erase( i.nPos ); - onDelete(); - } - } - - virtual void clear() - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - { - va.destroy( &aValues[j] ); - ka.destroy( &aKeys[j] ); - onDelete(); - } - } - - clearBits(); - } - - virtual value &get( key k ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - return aValues[nPos]; - } - else - { - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - } - } - - virtual bool has( key k ) - { - bool bFill; - probe( __calcHashCode( k ), k, bFill, false ); - - return bFill; - } - - typedef struct iterator - { - friend class Hash; - private: - iterator( Hash &hsh ) : - hsh( hsh ), - nPos( 0 ), - bFinished( false ) - { - nPos = hsh.getFirstPos( bFinished ); - } - - iterator( Hash &hsh, bool bDone ) : - hsh( hsh ), - nPos( 0 ), - bFinished( bDone ) - { - } - - Hash &hsh; - uint32_t nPos; - bool bFinished; - - public: - iterator operator++( int ) - { - if( bFinished == false ) - nPos = hsh.getNextPos( nPos, bFinished ); - - return *this; - } - - iterator operator++() - { - if( bFinished == false ) - nPos = hsh.getNextPos( nPos, bFinished ); - - return *this; - } - - bool operator==( const iterator &oth ) - { - if( bFinished != oth.bFinished ) - return false; - if( bFinished == true ) - { - return true; - } - else - { - if( oth.nPos == nPos ) - return true; - return false; - } - } - - bool operator!=( const iterator &oth ) - { - return !(*this == oth ); - } - - iterator operator=( const iterator &oth ) - { - if( &hsh != &oth.hsh ) - throw HashException( - "Cannot mix iterators from different hash objects."); - nPos = oth.nPos; - bFinished = oth.bFinished; - } - - std::pair operator *() - { - return hsh.getAtPos( nPos ); - } - - key &getKey() - { - return hsh.getKeyAtPos( nPos ); - } - - value &getValue() - { - return hsh.getValueAtPos( nPos ); - } - }; - - iterator begin() - { - return iterator( *this ); - } - - iterator end() - { - return iterator( *this, true ); - } - - std::list getKeys() - { - std::list lKeys; - - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - { - if( !isDeleted( j ) ) - { - lKeys.push_back( aKeys[j] ); - } - } - } - - return lKeys; - } - -protected: - virtual void onInsert() {} - virtual void onUpdate() {} - virtual void onDelete() {} - virtual void onReHash() {} - - virtual void clearBits() - { - for( uint32_t j = 0; j < nKeysSize; j++ ) - { - bFilled[j] = bDeleted[j] = 0; - } - } - - virtual void fill( uint32_t loc, key &k, value &v, uint32_t hash ) - { - bFilled[loc/32] |= (1<<(loc%32)); - va.construct( &aValues[loc], v ); - ka.construct( &aKeys[loc], k ); - aHashCodes[loc] = hash; - nFilled++; - //printf("Filled: %d, Deleted: %d, Capacity: %d\n", - // nFilled, nDeleted, nCapacity ); - } - - virtual void _erase( uint32_t loc ) - { - bDeleted[loc/32] |= (1<<(loc%32)); - va.destroy( &aValues[loc] ); - ka.destroy( &aKeys[loc] ); - nDeleted++; - //printf("Filled: %d, Deleted: %d, Capacity: %d\n", - // nFilled, nDeleted, nCapacity ); - } - - virtual std::pair getAtPos( uint32_t nPos ) - { - return std::pair(aKeys[nPos],aValues[nPos]); - } - - virtual key &getKeyAtPos( uint32_t nPos ) - { - return aKeys[nPos]; - } - - virtual value &getValueAtPos( uint32_t nPos ) - { - return aValues[nPos]; - } - - virtual uint32_t getFirstPos( bool &bFinished ) - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - return j; - } - - bFinished = true; - return 0; - } - - virtual uint32_t getNextPos( uint32_t nPos, bool &bFinished ) - { - for( uint32_t j = nPos+1; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - return j; - } - - bFinished = true; - return 0; - } - - uint32_t probe( uint32_t hash, key k, bool &bFill, bool rehash=true ) - { - uint32_t nCur = hash%nCapacity; - - // First we scan to see if the key is already there, abort if we - // run out of probing room, or we find a non-filled entry - for( int8_t j = 0; - isFilled( nCur ) && j < 32; - nCur = (nCur + (1< uint32_t __calcHashCode( const int &k ); -template<> bool __cmpHashKeys( const int &a, const int &b ); - -template<> uint32_t __calcHashCode( const unsigned int &k ); -template<> bool __cmpHashKeys( const unsigned int &a, const unsigned int &b ); - -template<> uint32_t __calcHashCode( const char * const &k ); -template<> bool __cmpHashKeys( const char * const &a, const char * const &b ); - -template<> uint32_t __calcHashCode( char * const &k ); -template<> bool __cmpHashKeys( char * const &a, char * const &b ); - -template<> uint32_t __calcHashCode( const std::string &k ); -template<> bool __cmpHashKeys( const std::string &a, const std::string &b ); - -template<> uint32_t __calcHashCode( const Hashable &k ); -template<> bool __cmpHashKeys( const Hashable &a, const Hashable &b ); - -template -Serializer &operator<<( Serializer &ar, Hash &h ) -{ - ar << h.size(); - for( typename Hash::iterator i = h.begin(); i != h.end(); i++ ) - { - std::pair p = *i; - ar << p.first << p.second; - } - - return ar; -} - -template -Serializer &operator>>( Serializer &ar, Hash &h ) -{ - h.clear(); - uint32_t nSize; - ar >> nSize; - - for( uint32_t j = 0; j < nSize; j++ ) - { - key k; value v; - ar >> k >> v; - h.insert( k, v ); - } - - return ar; -} - -template -Serializer &operator&&( Serializer &ar, Hash &h ) -{ - if( ar.isLoading() ) - { - return ar >> h; - } - else - { - return ar << h; - } -} - -#endif diff --git a/src/old/hashable.cpp b/src/old/hashable.cpp deleted file mode 100644 index 8565956..0000000 --- a/src/old/hashable.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "hashable.h" diff --git a/src/old/hashable.h b/src/old/hashable.h deleted file mode 100644 index 98643d5..0000000 --- a/src/old/hashable.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef HASHABLE_H -#define HASHABLE_H - -class Hashable -{ -public: - virtual ~Hashable() {}; - virtual unsigned long int getHashCode() = 0; - virtual bool compareForHash( Hashable &other ) = 0; -}; - -#endif diff --git a/src/old/serializable.cpp b/src/old/serializable.cpp deleted file mode 100644 index fd50943..0000000 --- a/src/old/serializable.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "serializable.h" - -Serializable::Serializable() -{ -} -Serializable::~Serializable() -{ -} diff --git a/src/old/serializable.h b/src/old/serializable.h deleted file mode 100644 index 06def29..0000000 --- a/src/old/serializable.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef SERIALIZER_H -#define SERIALIZER_H - -//#include "serializer.h" - -/** - * The base class for any class you want to serialize. Simply include this as - * a base class, implement the purely virtual serialize function and you've got - * an easily serializable class. - */ -class Serializable -{ -public: - /** - * Does nothing, here for completeness. - */ - Serializable(); - - /** - * Here to ensure the deconstructor is virtual. - */ - virtual ~Serializable(); - - /** - * This is the main workhorse of the serialization system, just override and - * you've got a serializable class. A reference to the Serializer archive - * used is passed in as your only parameter, query it to discover if you are - * loading or saving. - * @param ar A reference to the Serializer object to use. - */ - virtual void serialize( class Serializer &ar )=0; -}; - -#endif diff --git a/src/old/serializer.cpp b/src/old/serializer.cpp deleted file mode 100644 index 636224e..0000000 --- a/src/old/serializer.cpp +++ /dev/null @@ -1,338 +0,0 @@ -#include "serializer.h" -#include "serializable.h" -#include - -Serializer::Serializer(bool bLoading): - bLoading(bLoading) -{ -} -Serializer::~Serializer() -{ -} - -bool Serializer::isLoading() -{ - return bLoading; -} -Serializer &Serializer::operator<<(bool p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int8_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int16_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int32_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int64_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(uint8_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(uint16_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(uint32_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(uint64_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(long p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(float p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(double p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(long double p) -{ - write( &p, sizeof(p) ); - return *this; -} - -Serializer &Serializer::operator>>(bool &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int8_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int16_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int32_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int64_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(uint8_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(uint16_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(uint32_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(uint64_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(long &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(float &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(double &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(long double &p) -{ - read( &p, sizeof(p) ); - return *this; -} - -Serializer &Serializer::operator&&(bool &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int8_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int16_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int32_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int64_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint8_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint16_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint32_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint64_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(float &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(double &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(long double &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - - -Serializer &operator<<(Serializer &s, Serializable &p) -{ - p.serialize( s ); - return s; -} - -Serializer &operator>>(Serializer &s, Serializable &p) -{ - p.serialize( s ); - return s; -} - -Serializer &operator&&(Serializer &s, Serializable &p) -{ - if (s.isLoading()) - { - return s >> p; - } - else - { - return s << p; - } -} - -Serializer &operator<<( Serializer &ar, std::string &s ) -{ - ar << (uint32_t)s.length(); - ar.write( s.c_str(), s.length() ); - - return ar; -} - -Serializer &operator>>( Serializer &ar, std::string &s ) -{ - uint32_t l; - ar >> l; - char *tmp = new char[l+1]; - tmp[l] = '\0'; - ar.read( tmp, l ); - s = tmp; - delete[] tmp; - - return ar; -} - diff --git a/src/old/serializer.h b/src/old/serializer.h deleted file mode 100644 index 3af489c..0000000 --- a/src/old/serializer.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef SERIALIZABLE_H -#define SERIALIZABLE_H - -#include -#include -#include -//#include "serializable.h" - -class Serializer -{ -private: - bool bLoading; -public: - bool isLoading(); - - enum - { - load = true, - save = false - }; - - Serializer(bool bLoading); - virtual ~Serializer(); - virtual void close()=0; - - virtual void write(const void *, int32_t)=0; - virtual void read(void *, int32_t)=0; - - virtual Serializer &operator<<(bool); - virtual Serializer &operator<<(int8_t); - virtual Serializer &operator<<(int16_t); - virtual Serializer &operator<<(int32_t); - virtual Serializer &operator<<(int64_t); - virtual Serializer &operator<<(uint8_t); - virtual Serializer &operator<<(uint16_t); - virtual Serializer &operator<<(uint32_t); - virtual Serializer &operator<<(uint64_t); - virtual Serializer &operator<<(long); - virtual Serializer &operator<<(float); - virtual Serializer &operator<<(double); - virtual Serializer &operator<<(long double); - - virtual Serializer &operator>>(bool &); - virtual Serializer &operator>>(int8_t &); - virtual Serializer &operator>>(int16_t &); - virtual Serializer &operator>>(int32_t &); - virtual Serializer &operator>>(int64_t &); - virtual Serializer &operator>>(uint8_t &); - virtual Serializer &operator>>(uint16_t &); - virtual Serializer &operator>>(uint32_t &); - virtual Serializer &operator>>(uint64_t &); - virtual Serializer &operator>>(long &); - virtual Serializer &operator>>(float &); - virtual Serializer &operator>>(double &); - virtual Serializer &operator>>(long double &); - - virtual Serializer &operator&&(bool &); - virtual Serializer &operator&&(int8_t &); - virtual Serializer &operator&&(int16_t &); - virtual Serializer &operator&&(int32_t &); - virtual Serializer &operator&&(int64_t &); - virtual Serializer &operator&&(uint8_t &); - virtual Serializer &operator&&(uint16_t &); - virtual Serializer &operator&&(uint32_t &); - virtual Serializer &operator&&(uint64_t &); - virtual Serializer &operator&&(float &); - virtual Serializer &operator&&(double &); - virtual Serializer &operator&&(long double &); - - //virtual Serializer &operator&(Serializable &); -}; - -Serializer &operator<<(Serializer &, class Serializable &); -Serializer &operator>>(Serializer &, class Serializable &); -Serializer &operator&&(Serializer &s, class Serializable &p); - -Serializer &operator<<(Serializer &, std::string &); -Serializer &operator>>(Serializer &, std::string &); - -#endif diff --git a/src/old/stream.cpp b/src/old/stream.cpp deleted file mode 100644 index 856a58d..0000000 --- a/src/old/stream.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "stream.h" - -Stream::Stream() -{ -} - -Stream::~Stream() -{ -} - diff --git a/src/old/stream.h b/src/old/stream.h deleted file mode 100644 index e086e28..0000000 --- a/src/old/stream.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef STREAM_H -#define STREAM_H - -#include -#include - -class Stream -{ -public: - Stream(); - virtual ~Stream(); - - virtual void close() = 0; - virtual size_t read( char *pBuf, size_t nBytes ) = 0; - virtual size_t write( const char *pBuf, size_t nBytes ) = 0; - - virtual long tell() = 0; - virtual void seek( long offset ) = 0; - virtual void setPos( long pos ) = 0; - virtual void setPosEnd( long pos ) = 0; - virtual bool isEOS() = 0; - -private: - -}; - -#endif -- cgit v1.2.3