Mercurial > LightClone
view LightClone/Source/HashMap.h @ 21:b4dc5d674e22
Added GUI event system and some support data structures
author | koryspansel |
---|---|
date | Thu, 15 Sep 2011 18:42:12 -0700 |
parents | |
children | 3a63df04f3c0 |
line wrap: on
line source
/* * HashMap */ #ifndef __HASHMAP_H__ #define __HASHMAP_H__ #include "Types.h" /* * DefaultHash */ template<typename Key> struct DefaultHash { /* * operator() */ uint32 operator()(const Key& kKey) { return (uint32)(&kKey); } }; /* * HashMap */ template<typename Key, typename Value, typename HashFunction = DefaultHash<Key>, uint32 Size = 97> class HashMap { /* * Node */ struct Node { /* * kKey */ Key kKey; /* * kValue */ Value kValue; /* * pNext */ Node* pNext; /* * Node */ Node(Key& kNodeKey) : kKey(kNodeKey), pNext(NULL) { } }; /* * pTable */ Node* pTable[Size]; /* * kHash */ HashFunction kHash; public: /* * HashMap */ HashMap() //: kHash() { for(uint32 i = 0; i < Size; ++i) { pTable[i] = 0; } } /* * Add */ Value* Add(Key& kKey) { uint32 nSlot = GetSlot(kKey); Node* pNode = new Node(kKey); pNode->pNext = pTable[nSlot]; pTable[nSlot] = pNode; return &pNode->kValue; } /* * Remove */ void Remove(const Key& kKey) { } /* * Find */ Value* Find(const Key& kKey) { uint32 nSlot = GetSlot(kKey); Node* pNode = pTable[nSlot]; while(pNode) { if(pNode->kKey == kKey) { return &pNode->kValue; } ++pNode; } return NULL; } private: /* * GetSlot */ uint32 GetSlot(const Key& kKey) { return kHash(kKey) % Size; } }; #endif //__HASHMAP_H__