From 76ed3c164662f4ee4c109bb2054c61c99ea86251 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 26 Jul 2006 21:58:35 +0000 Subject: Added the not-yet-working hash class. More thought must be done. This doesn't actually change any existing code really just adds a new class that you can't use because it's commented out. I'll probably move it to a branch. --- src/hash.cpp | 1 + src/hash.h | 106 +++++++++++++++++++++++++++++++++++++++ src/hashable.cpp | 1 + src/hashable.h | 10 ++++ src/staticstring.cpp | 25 +++++++++ src/staticstring.h | 11 ++-- src/test/exception/exception.cpp | 8 +-- src/test/hash/main.cpp | 10 ++++ 8 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 src/hash.cpp create mode 100644 src/hash.h create mode 100644 src/hashable.cpp create mode 100644 src/hashable.h create mode 100644 src/test/hash/main.cpp (limited to 'src') diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 0000000..b4aac77 --- /dev/null +++ b/src/hash.cpp @@ -0,0 +1 @@ +#include "hash.h" diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..8385bb9 --- /dev/null +++ b/src/hash.h @@ -0,0 +1,106 @@ +#ifndef HASH_H +#define HASH_H +/* +#include +#include +#include "hashable.h" + +#define bitsToBytes( n ) (n/8+(n%8>0 ? 1 : 0)) + +template +class Hash +{ + class iterator + { + friend class Hash; + private: + iterator( Hash *hsh, int nIndex, key keyval, bool bInsert ) : + hsh( hsh ), + nIndex( nIndex ), + keyval( keyval ), + bInsert( bInsert ) + { + } + + public: + iterator() : + hsh( NULL ), + nIndex( -1 ) + { + } + + iterator &operator=( iterator &src ) + { + this->hsh = src.hsh; + this->nIndex = src.nIndex; + } + + iterator &operator=( value &src ) + { + if( bInsert ) + printf("You wanted to insert %d\n", src ); + else + printf("You wanted to insert %d\n", src ); + } + + private: + Hash *hsh; + int nIndex; + bool bInsert; + key keyval; + }; + + template + class VRef + { + public: + VRef( refval &data ) : + data( data ) + { + } + refval &data; + }; + +public: + Hash() : + nCapacity( 11 ), + nFilled( 0 ), + bFilled( NULL ), + aKeys( NULL ), + aValues( NULL ), + aHashCodes( NULL ) + { + int nKeysSize = bitsToBytes( nCapacity ); + bFilled = new unsigned char[ nKeysSize ]; + memset( bFilled, 0, nKeysSize ); + + aKeys = new VRef*[nCapacity]; + aValues = new value[nCapacity]; + } + + virtual ~Hash() + { + } + + iterator operator[]( key keyval ) + { + //iterator i( this, 4, keyval, true ); + //return i; + printf("%s\n", keyval.getString() ); + } + + int hasKey( key keyval ) + { + printf("%s\n", keyval.getString() ); + } + +private: + int nCapacity; + int nFilled; + unsigned char *bFilled; + VRef **aKeys; + unsigned long int *aHashCodes; + value *aValues; +}; +*/ +#endif diff --git a/src/hashable.cpp b/src/hashable.cpp new file mode 100644 index 0000000..8565956 --- /dev/null +++ b/src/hashable.cpp @@ -0,0 +1 @@ +#include "hashable.h" diff --git a/src/hashable.h b/src/hashable.h new file mode 100644 index 0000000..33ff8b8 --- /dev/null +++ b/src/hashable.h @@ -0,0 +1,10 @@ +#ifndef HASHABLE_H +#define HASHABLE_H +/* +class Hashable +{ +public: + virtual unsigned long int getHashCode() = 0; +}; +*/ +#endif diff --git a/src/staticstring.cpp b/src/staticstring.cpp index fa61e62..9eac92e 100644 --- a/src/staticstring.cpp +++ b/src/staticstring.cpp @@ -25,6 +25,13 @@ StaticString::StaticString( const char *lpNewStr, int nNewLen ) setString( lpNewStr, nNewLen ); } +StaticString::StaticString( const char *lpNewStr ) +{ + lpStr = NULL; + nLen = 0; + setString( lpNewStr, -1 ); +} + StaticString::StaticString( StaticString &xSrcStr, int nNewLen ) { lpStr = NULL; @@ -32,6 +39,13 @@ StaticString::StaticString( StaticString &xSrcStr, int nNewLen ) setString( xSrcStr, nNewLen ); } +StaticString::StaticString( StaticString &xSrcStr ) +{ + lpStr = NULL; + nLen = 0; + setString( xSrcStr, -1 ); +} + StaticString::~StaticString() { if( lpStr != NULL ) delete[] lpStr; @@ -225,3 +239,14 @@ bool StaticString::operator!=( StaticString &str ) for(; *a == *b; a++, b++ ) if( *a == '\0' && *b == '\0' ) return false; return true; } + +unsigned long int StaticString::getHashCode() +{ + unsigned long int nPos = nLen; + for( const char *s = (const char *)lpStr; *s; s++ ) + { + nPos = *s + (nPos << 6) + (nPos << 16) - nPos; + } + return nPos; +} + diff --git a/src/staticstring.h b/src/staticstring.h index 732e860..68ca7ea 100644 --- a/src/staticstring.h +++ b/src/staticstring.h @@ -3,6 +3,7 @@ #include #include "serializable.h" +//#include "hashable.h" /** * Simple string managing class. Allows for dynamically allocated string data @@ -11,12 +12,14 @@ * and making accessing meta-info like length fast and reliable as well. *@author Mike Buland */ -class StaticString : public Serializable +class StaticString : public Serializable//, public Hashable { public: StaticString(); - StaticString( const char *lpNewStr, int nNewLen=-1 ); - StaticString( StaticString &xSrcStr, int nNewLen=-1 ); + StaticString( const char *lpNewStr, int nNewLen ); + StaticString( const char *lpNewStr ); + StaticString( StaticString &xSrcStr, int nNewLen ); + StaticString( StaticString &xSrcStr ); StaticString( int nLength ); virtual ~StaticString(); @@ -47,6 +50,8 @@ public: virtual void serialize( class Serializer &ar ); + virtual unsigned long int getHashCode(); + private: char *lpStr; int nLen; diff --git a/src/test/exception/exception.cpp b/src/test/exception/exception.cpp index 33dcd5e..6417692 100644 --- a/src/test/exception/exception.cpp +++ b/src/test/exception/exception.cpp @@ -1,16 +1,16 @@ #include -#include "exception.h" +#include "exceptions.h" int main() { try { - throw Exception( 42, "There was an error on line: %d", __LINE__ ); + throw ExceptionBase( 42, "There was an error on line: %d", __LINE__ ); } - catch( Exception &e ) + catch( ExceptionBase &e ) { std::cout << "Error "<< e.getErrorCode() << ": " << e.what() << "\n"; } - throw Exception( 112, "This exception wasn't caught!"); + throw ExceptionBase( 112, "This exception wasn't caught!"); } diff --git a/src/test/hash/main.cpp b/src/test/hash/main.cpp new file mode 100644 index 0000000..d0f5fa6 --- /dev/null +++ b/src/test/hash/main.cpp @@ -0,0 +1,10 @@ +#include "hash.h" +#include "staticstring.h" + +int main() +{ + //Hash sTest; + + //sTest.hasKey("hello"); +} + -- cgit v1.2.3