diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2006-11-21 19:52:12 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2006-11-21 19:52:12 +0000 |
| commit | 8b2878e6595e40d2a37bb9983274ccb5827a9192 (patch) | |
| tree | 05a902091cebcaa40fb69818b335fee99ebf1aa5 /src | |
| parent | 4e55dab4511ffa8307e3ae7523659e6087632f95 (diff) | |
| download | libbu++-8b2878e6595e40d2a37bb9983274ccb5827a9192.tar.gz libbu++-8b2878e6595e40d2a37bb9983274ccb5827a9192.tar.bz2 libbu++-8b2878e6595e40d2a37bb9983274ccb5827a9192.tar.xz libbu++-8b2878e6595e40d2a37bb9983274ccb5827a9192.zip | |
Hash uses real exceptions now, and has a clear() function.
Diffstat (limited to 'src')
| -rw-r--r-- | src/hash.cpp | 2 | ||||
| -rw-r--r-- | src/hash.h | 38 | ||||
| -rw-r--r-- | src/tests/hash.cpp | 12 |
3 files changed, 49 insertions, 3 deletions
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 @@ | |||
| 1 | #include "hash.h" | 1 | #include "hash.h" |
| 2 | 2 | ||
| 3 | subExceptionDef( HashException ) | ||
| 4 | |||
| 3 | template<> uint32_t __calcHashCode<const int>( const int k ) | 5 | template<> uint32_t __calcHashCode<const int>( const int k ) |
| 4 | { | 6 | { |
| 5 | return k; | 7 | return k; |
| @@ -5,10 +5,18 @@ | |||
| 5 | #include <string.h> | 5 | #include <string.h> |
| 6 | #include <memory> | 6 | #include <memory> |
| 7 | #include <iostream> | 7 | #include <iostream> |
| 8 | #include "exceptionbase.h" | ||
| 8 | #include "hashable.h" | 9 | #include "hashable.h" |
| 9 | 10 | ||
| 10 | #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) | 11 | #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) |
| 11 | 12 | ||
| 13 | subExceptionDecl( HashException ) | ||
| 14 | |||
| 15 | enum eHashException | ||
| 16 | { | ||
| 17 | excodeNotFilled | ||
| 18 | }; | ||
| 19 | |||
| 12 | template<typename T> | 20 | template<typename T> |
| 13 | uint32_t __calcHashCode( T k ); | 21 | uint32_t __calcHashCode( T k ); |
| 14 | 22 | ||
| @@ -59,14 +67,20 @@ public: | |||
| 59 | operator _value() | 67 | operator _value() |
| 60 | { | 68 | { |
| 61 | if( bFilled == false ) | 69 | if( bFilled == false ) |
| 62 | throw "Nope, no data there"; | 70 | throw HashException( |
| 71 | excodeNotFilled, | ||
| 72 | "No data assosiated with that key." | ||
| 73 | ); | ||
| 63 | return *pValue; | 74 | return *pValue; |
| 64 | } | 75 | } |
| 65 | 76 | ||
| 66 | _value value() | 77 | _value value() |
| 67 | { | 78 | { |
| 68 | if( bFilled == false ) | 79 | if( bFilled == false ) |
| 69 | throw "Nope, no data there"; | 80 | throw HashException( |
| 81 | excodeNotFilled, | ||
| 82 | "No data assosiated with that key." | ||
| 83 | ); | ||
| 70 | return *pValue; | 84 | return *pValue; |
| 71 | } | 85 | } |
| 72 | 86 | ||
| @@ -199,6 +213,21 @@ public: | |||
| 199 | } | 213 | } |
| 200 | } | 214 | } |
| 201 | 215 | ||
| 216 | void clear() | ||
| 217 | { | ||
| 218 | for( uint32_t j = 0; j < nCapacity; j++ ) | ||
| 219 | { | ||
| 220 | if( isFilled( j ) ) | ||
| 221 | if( !isDeleted( j ) ) | ||
| 222 | { | ||
| 223 | va.destroy( &aValues[j] ); | ||
| 224 | ka.destroy( &aKeys[j] ); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | clearBits(); | ||
| 229 | } | ||
| 230 | |||
| 202 | value get( key k ) | 231 | value get( key k ) |
| 203 | { | 232 | { |
| 204 | uint32_t hash = __calcHashCode( k ); | 233 | uint32_t hash = __calcHashCode( k ); |
| @@ -211,7 +240,10 @@ public: | |||
| 211 | } | 240 | } |
| 212 | else | 241 | else |
| 213 | { | 242 | { |
| 214 | throw "Hey, no such thing..."; | 243 | throw HashException( |
| 244 | excodeNotFilled, | ||
| 245 | "No data assosiated with that key." | ||
| 246 | ); | ||
| 215 | } | 247 | } |
| 216 | } | 248 | } |
| 217 | 249 | ||
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() | |||
| 78 | printf("%d: %s\n", (*j).second, (*j).first.c_str() ); | 78 | printf("%d: %s\n", (*j).second, (*j).first.c_str() ); |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | printf("Testing\n-------------------\n\n"); | ||
| 81 | for( int j = 0; j < 33; j++ ) | 82 | for( int j = 0; j < 33; j++ ) |
| 82 | { | 83 | { |
| 83 | if( sTest.has(names[j]) ) | 84 | if( sTest.has(names[j]) ) |
| @@ -95,5 +96,16 @@ int main() | |||
| 95 | printf("Missing element %d, '%s'\n", j, names[j] ); | 96 | printf("Missing element %d, '%s'\n", j, names[j] ); |
| 96 | } | 97 | } |
| 97 | } | 98 | } |
| 99 | |||
| 100 | printf("Clearing\n-------------------\n\n"); | ||
| 101 | |||
| 102 | sTest.clear(); | ||
| 103 | |||
| 104 | for( Hash<std::string, int>::iterator i = sTest.begin(); | ||
| 105 | i != sTest.end(); i++ ) | ||
| 106 | { | ||
| 107 | Hash<std::string, int>::iterator j = i; | ||
| 108 | printf("%d: %s\n", (*j).second, (*j).first.c_str() ); | ||
| 109 | } | ||
| 98 | } | 110 | } |
| 99 | 111 | ||
