From 8b2878e6595e40d2a37bb9983274ccb5827a9192 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 21 Nov 2006 19:52:12 +0000 Subject: Hash uses real exceptions now, and has a clear() function. --- src/hash.cpp | 2 ++ src/hash.h | 38 +++++++++++++++++++++++++++++++++++--- src/tests/hash.cpp | 12 ++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/hash.cpp b/src/hash.cpp index 0241753..a32fa2a 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -1,5 +1,7 @@ #include "hash.h" +subExceptionDef( HashException ) + template<> uint32_t __calcHashCode( const int k ) { return k; diff --git a/src/hash.h b/src/hash.h index b5bf8d6..71abff5 100644 --- a/src/hash.h +++ b/src/hash.h @@ -5,10 +5,18 @@ #include #include #include +#include "exceptionbase.h" #include "hashable.h" #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) +subExceptionDecl( HashException ) + +enum eHashException +{ + excodeNotFilled +}; + template uint32_t __calcHashCode( T k ); @@ -59,14 +67,20 @@ public: operator _value() { if( bFilled == false ) - throw "Nope, no data there"; + throw HashException( + excodeNotFilled, + "No data assosiated with that key." + ); return *pValue; } _value value() { if( bFilled == false ) - throw "Nope, no data there"; + throw HashException( + excodeNotFilled, + "No data assosiated with that key." + ); return *pValue; } @@ -199,6 +213,21 @@ public: } } + void clear() + { + for( uint32_t j = 0; j < nCapacity; j++ ) + { + if( isFilled( j ) ) + if( !isDeleted( j ) ) + { + va.destroy( &aValues[j] ); + ka.destroy( &aKeys[j] ); + } + } + + clearBits(); + } + value get( key k ) { uint32_t hash = __calcHashCode( k ); @@ -211,7 +240,10 @@ public: } else { - throw "Hey, no such thing..."; + throw HashException( + excodeNotFilled, + "No data assosiated with that key." + ); } } diff --git a/src/tests/hash.cpp b/src/tests/hash.cpp index a7f0a57..58e0112 100644 --- a/src/tests/hash.cpp +++ b/src/tests/hash.cpp @@ -78,6 +78,7 @@ int main() printf("%d: %s\n", (*j).second, (*j).first.c_str() ); } + printf("Testing\n-------------------\n\n"); for( int j = 0; j < 33; j++ ) { if( sTest.has(names[j]) ) @@ -95,5 +96,16 @@ int main() printf("Missing element %d, '%s'\n", j, names[j] ); } } + + printf("Clearing\n-------------------\n\n"); + + sTest.clear(); + + for( Hash::iterator i = sTest.begin(); + i != sTest.end(); i++ ) + { + Hash::iterator j = i; + printf("%d: %s\n", (*j).second, (*j).first.c_str() ); + } } -- cgit v1.2.3