DCL 4.0
Loading...
Searching...
No Matches
PtrHashMap Class Reference

#include <LibState.h>

Classes

struct  Node
struct  NodeBase

Public Member Functions

 PtrHashMap (size_t nBuckets)
 ~PtrHashMap ()
const void *& operator[] (const void *key)
Nodefind (const void *key) const
void erase (Node *pNode)
size_t count () const

Protected Member Functions

size_t bucketNumber (const void *key) const

Protected Attributes

NodeBasem_buckets
size_t m_nBuckets
int m_nCount

Detailed Description

Definition at line 137 of file LibState.h.

Constructor & Destructor Documentation

◆ PtrHashMap()

PtrHashMap::PtrHashMap ( size_t nBuckets)

Definition at line 328 of file LibState.cpp.

329{
330 nBuckets = DCLGetNextPrimNumber(nBuckets);
331
332 size_t nBytes = sizeof(NodeBase) * nBuckets;
333 m_buckets = (NodeBase*)malloc(nBytes);
334 for(size_t i = 0; i < nBuckets; i++) {
335 NodeBase* pNode = &(m_buckets[i]);
336 pNode->pPrev = pNode->pNext = pNode;
337 }
338 m_nBuckets = nBuckets;
339 m_nCount = 0;
340}
DCLCAPI size_t DCLGetNextPrimNumber(size_t _n)
Definition HashFun.cpp:23
int m_nCount
Definition LibState.h:172
size_t m_nBuckets
Definition LibState.h:171
NodeBase * m_buckets
Definition LibState.h:170
NodeBase * pPrev
Definition ListBase.h:18
NodeBase * pNext
Definition ListBase.h:19

◆ ~PtrHashMap()

PtrHashMap::~PtrHashMap ( )

Definition at line 342 of file LibState.cpp.

343{
344 for(size_t i = 0; i < m_nBuckets; i++) {
345 NodeBase* pTempNode = NULL;
346 NodeBase* pMasterNode = &(m_buckets[i]);
347 NodeBase* pNode = pMasterNode->pNext;
348 while(pNode != pMasterNode) {
349 pTempNode = pNode;
350 pNode = pNode->pNext;
351 free(pTempNode);
352 }
353 }
354 free(m_buckets);
355}
#define NULL
Definition Config.h:340

Member Function Documentation

◆ bucketNumber()

size_t PtrHashMap::bucketNumber ( const void * key) const
inlineprotected

Definition at line 175 of file LibState.h.

176{
177 return (size_t)key % m_nBuckets;
178}

◆ count()

size_t PtrHashMap::count ( ) const
inline

Definition at line 180 of file LibState.h.

181{
182 return m_nCount;
183}

◆ erase()

void PtrHashMap::erase ( Node * pNode)

Definition at line 394 of file LibState.cpp.

395{
396 __DCL_ASSERT_N(pNode != NULL);
397
398 pNode->pPrev->pNext = pNode->pNext;
399 pNode->pNext->pPrev = pNode->pPrev;
400
401 free(pNode);
402 m_nCount--;
403}
#define __DCL_ASSERT_N(expr)
Definition LibState.cpp:27

◆ find()

PtrHashMap::Node * PtrHashMap::find ( const void * key) const

Definition at line 378 of file LibState.cpp.

379{
380 size_t nBucket = bucketNumber(key);
381 NodeBase* pMasterNode = &(m_buckets[nBucket]);
382 NodeBase* pNode = pMasterNode->pNext;
383// int i = 1;
384 while(pNode != pMasterNode) {
385 if (((Node*)pNode)->key == key)
386 return (Node*)pNode;
387 pNode = pNode->pNext;
388// printf("collision: %d\n", i++);
389
390 }
391 return NULL;
392}
size_t bucketNumber(const void *key) const
Definition LibState.h:175

◆ operator[]()

const void *& PtrHashMap::operator[] ( const void * key)

Definition at line 357 of file LibState.cpp.

358{
359 size_t nBucket = bucketNumber(key);
360 NodeBase* pMasterNode = &(m_buckets[nBucket]);
361 NodeBase* pNode = pMasterNode->pNext;
362 while(pNode != pMasterNode) {
363 if (((Node*)pNode)->key == key)
364 return ((Node*)pNode)->value;
365 pNode = pNode->pNext;
366 }
367
368 Node* pNewNode = (Node*)malloc(sizeof(Node));
369 pNewNode->pPrev = pMasterNode;
370 pNewNode->pNext = pMasterNode->pNext;
371 pNewNode->pNext->pPrev = pNewNode;
372 pMasterNode->pNext = pNewNode;
373 m_nCount++;
374 pNewNode->key = key;
375 return pNewNode->value;
376}
struct NodeBase * pPrev
Definition LibState.h:142
struct NodeBase * pNext
Definition LibState.h:143

Member Data Documentation

◆ m_buckets

NodeBase* PtrHashMap::m_buckets
protected

Definition at line 170 of file LibState.h.

◆ m_nBuckets

size_t PtrHashMap::m_nBuckets
protected

Definition at line 171 of file LibState.h.

◆ m_nCount

int PtrHashMap::m_nCount
protected

Definition at line 172 of file LibState.h.


The documentation for this class was generated from the following files: