diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-07-20 17:30:48 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-07-20 17:30:48 +0000 |
commit | 86f24cc82a3d0297e80f7c24ad624d43ba097000 (patch) | |
tree | 47deb396dcf6b4bf765e106aff2ee406a0699fae | |
parent | 1ebe2d47412625d2be6ffe05cf761761becf0088 (diff) | |
download | libbu++-86f24cc82a3d0297e80f7c24ad624d43ba097000.tar.gz libbu++-86f24cc82a3d0297e80f7c24ad624d43ba097000.tar.bz2 libbu++-86f24cc82a3d0297e80f7c24ad624d43ba097000.tar.xz libbu++-86f24cc82a3d0297e80f7c24ad624d43ba097000.zip |
Added the "getKey" function to the HashTable, allowing you to get at the
internal persistant IDs using a temporary id.
-rw-r--r-- | src/hashtable.cpp | 45 | ||||
-rw-r--r-- | src/hashtable.h | 2 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/hashtable.cpp b/src/hashtable.cpp index 8a31f57..ea13236 100644 --- a/src/hashtable.cpp +++ b/src/hashtable.cpp | |||
@@ -241,6 +241,51 @@ const void *HashTable::get( const void *id, unsigned long int nSkip ) | |||
241 | return NULL; | 241 | return NULL; |
242 | } | 242 | } |
243 | 243 | ||
244 | const void *HashTable::getKey( const void *id, unsigned long int nSkip ) | ||
245 | { | ||
246 | unsigned long int nPos = hFunc->hash( id )%nTableSize; | ||
247 | |||
248 | for( unsigned long int j=0; j < 32; nPos = (nPos+(1<<j))%nTableSize, j++ ) | ||
249 | { | ||
250 | if( !isFilled( nPos ) ) return NULL; | ||
251 | if( hFunc->cmpIDs( id, aTable[nPos].id ) && | ||
252 | aTable[nPos].bDeleted == false ) | ||
253 | { | ||
254 | if( nSkip == 0 ) | ||
255 | { | ||
256 | return aTable[nPos].id; | ||
257 | } | ||
258 | else | ||
259 | { | ||
260 | nSkip--; | ||
261 | } | ||
262 | } | ||
263 | } | ||
264 | |||
265 | if( bAllowDupes ) | ||
266 | { | ||
267 | int nOldPos = nPos; | ||
268 | for( nPos++; nPos != nOldPos; nPos=(nPos+1)%nTableSize ) | ||
269 | { | ||
270 | if( !isFilled( nPos ) ) return NULL; | ||
271 | if( hFunc->cmpIDs( id, aTable[nPos].id ) && | ||
272 | aTable[nPos].bDeleted == false ) | ||
273 | { | ||
274 | if( nSkip == 0 ) | ||
275 | { | ||
276 | return aTable[nPos].id; | ||
277 | } | ||
278 | else | ||
279 | { | ||
280 | nSkip--; | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | |||
286 | return NULL; | ||
287 | } | ||
288 | |||
244 | void *HashTable::getFirstItemPos() | 289 | void *HashTable::getFirstItemPos() |
245 | { | 290 | { |
246 | HashPos *pos = new HashPos; | 291 | HashPos *pos = new HashPos; |
diff --git a/src/hashtable.h b/src/hashtable.h index 170793f..f319cb0 100644 --- a/src/hashtable.h +++ b/src/hashtable.h | |||
@@ -90,6 +90,8 @@ public: | |||
90 | *@returns A pointer to the data stored at the given id. | 90 | *@returns A pointer to the data stored at the given id. |
91 | */ | 91 | */ |
92 | const void *get( const void *id, unsigned long int nSkip=0 ); | 92 | const void *get( const void *id, unsigned long int nSkip=0 ); |
93 | |||
94 | const void *getKey( const void *id, unsigned long int nSkip=0 ); | ||
93 | 95 | ||
94 | /** Gets the total capacity of the hashtable. This is actually the number | 96 | /** Gets the total capacity of the hashtable. This is actually the number |
95 | * of total positions available inside the hashtable at the moment. This | 97 | * of total positions available inside the hashtable at the moment. This |