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

#include <LibState.h>

Classes

struct  Node
struct  NodeBase

Public Member Functions

 PtrHashMap (size_t _buckerCount)
 ~PtrHashMap ()
const void *& operator[] (const void *_key)
Nodefind (const void *_key) const
void erase (Node *_node)
size_t count () const

Protected Member Functions

size_t bucketIndex (const void *_key) const

Protected Attributes

NodeBase__buckets
size_t __bucketCount
size_t __nodeCount

Detailed Description

Definition at line 136 of file LibState.h.

Constructor & Destructor Documentation

◆ PtrHashMap()

PtrHashMap::PtrHashMap ( size_t _buckerCount)

Definition at line 326 of file LibState.cpp.

327{
328 __nodeCount = 0;
330 __buckets = (NodeBase*)malloc(sizeof(NodeBase) * __bucketCount);
331 for(size_t i = 0; i < __bucketCount; i++) {
332 NodeBase* node = &(__buckets[i]);
333 node->prev = node->next = node;
334 }
335}
DCLCAPI size_t DCLGetNextPrimNumber(size_t _n)
Definition HashFun.cpp:23
NodeBase * __buckets
Definition LibState.h:169
size_t __nodeCount
Definition LibState.h:171
size_t __bucketCount
Definition LibState.h:170

◆ ~PtrHashMap()

PtrHashMap::~PtrHashMap ( )

Definition at line 337 of file LibState.cpp.

338{
339 for(size_t i = 0; i < __bucketCount; i++) {
340 NodeBase* temp = NULL;
341 NodeBase* masterNode = &(__buckets[i]);
342 NodeBase* node = masterNode->next;
343 while(node != masterNode) {
344 temp = node;
345 node = node->next;
346 free(temp);
347 }
348 }
349 free(__buckets);
350}
#define NULL
Definition Config.h:312

Member Function Documentation

◆ bucketIndex()

size_t PtrHashMap::bucketIndex ( const void * _key) const
inlineprotected

Definition at line 174 of file LibState.h.

175{
176 return (size_t)_key % __bucketCount;
177}

◆ count()

size_t PtrHashMap::count ( ) const
inline

Definition at line 179 of file LibState.h.

180{
181 return __nodeCount;
182}

◆ erase()

void PtrHashMap::erase ( Node * _node)

Definition at line 386 of file LibState.cpp.

387{
388 __DCL_ASSERT_N(_node != NULL);
389
390 _node->prev->next = _node->next;
391 _node->next->prev = _node->prev;
392
393 free(_node);
394 __nodeCount--;
395}
#define __DCL_ASSERT_N(expr)
Definition LibState.cpp:27

◆ find()

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

Definition at line 372 of file LibState.cpp.

373{
374 NodeBase* masterNode = __buckets + bucketIndex(_key);
375 NodeBase* node = masterNode->next;
376// int i = 1;
377 while(node != masterNode) {
378 if (((Node*)node)->key == _key)
379 return (Node*)node;
380 node = node->next;
381// printf("collision: %d\n", i++);
382 }
383 return NULL;
384}
size_t bucketIndex(const void *_key) const
Definition LibState.h:174

◆ operator[]()

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

Definition at line 352 of file LibState.cpp.

353{
354 NodeBase* masterNode = __buckets + bucketIndex(_key);
355 NodeBase* node = masterNode->next;
356 while(node != masterNode) {
357 if (((Node*)node)->key == _key)
358 return ((Node*)node)->value;
359 node = node->next;
360 }
361
362 Node* newNode = (Node*)malloc(sizeof(Node));
363 newNode->prev = masterNode;
364 newNode->next = masterNode->next;
365 newNode->next->prev = newNode;
366 masterNode->next = newNode;
367 __nodeCount++;
368 newNode->key = _key;
369 return newNode->value;
370}
struct NodeBase * prev
Definition LibState.h:141

Member Data Documentation

◆ __bucketCount

size_t PtrHashMap::__bucketCount
protected

Definition at line 170 of file LibState.h.

◆ __buckets

NodeBase* PtrHashMap::__buckets
protected

Definition at line 169 of file LibState.h.

◆ __nodeCount

size_t PtrHashMap::__nodeCount
protected

Definition at line 171 of file LibState.h.


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