// // varstor.h // #ifndef VARSTOR_H #define VARSTOR_H #ifdef __cplusplus #include "private.h" #define VS_HASHSIZE 31 template class CVSEntry { public: TKey key; T t; CVSEntry *next; }; template class CVarStor { public: CVarStor() { memset(_HashTbl, 0, sizeof(_HashTbl)); } ~CVarStor(); T *_Create(TKey key); T *_Find(TKey key); BOOL _Remove(TKey key); private: UINT _HashFunc(TKey key) { return (DWORD)key % PM_HASHSIZE; } CVSEntry **_FindEntry(TKey key); CVSEntry *_HashTbl[PM_HASHSIZE]; }; //+--------------------------------------------------------------------------- // // dtor // //---------------------------------------------------------------------------- template CVarStor::~CVarStor() { CVSEntry *pe; CVSEntry *peTmp; int i; // free anything left in the hashtbl for (i=0; inext; delete pe; pe = peTmp; } } } //+--------------------------------------------------------------------------- // // _Set // //---------------------------------------------------------------------------- template T *CVarStor::_Create(TKey key) { UINT uIndex; CVSEntry *pe; BOOL fRet; fRet = TRUE; Assert(_FindEntry(key) == NULL); if ((pe = new CVSEntry) == NULL) return NULL; // new entry uIndex = _HashFunc(key); pe->key = key; pe->next = _HashTbl[uIndex]; _HashTbl[uIndex] = pe; return &pe->t; } //+--------------------------------------------------------------------------- // // _Find // //---------------------------------------------------------------------------- template T *CVarStor::_Find(TKey key) { CVSEntry **ppe; if (ppe = _FindEntry(key)) { return &(*ppe)->t; } return NULL; } //+--------------------------------------------------------------------------- // // _Remove // //---------------------------------------------------------------------------- template BOOL CVarStor::_Remove(TKey key) { CVSEntry *pe; CVSEntry **ppe; if (ppe = _FindEntry(key)) { pe = *ppe; *ppe = pe->next; delete pe; return TRUE; } return FALSE; } //+--------------------------------------------------------------------------- // // _FindEntry // //---------------------------------------------------------------------------- template CVSEntry **CVarStor::_FindEntry(TKey key) { CVSEntry **ppe; ppe = &_HashTbl[_HashFunc(key)]; while (*ppe) { if ((*ppe)->key == key) { return ppe; } ppe = &(*ppe)->next; } return NULL; } #endif // __cplusplus #endif // VARSTOR_H