Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.2 KiB

  1. //
  2. // varstor.h
  3. //
  4. #ifndef VARSTOR_H
  5. #define VARSTOR_H
  6. #ifdef __cplusplus
  7. #include "private.h"
  8. #define VS_HASHSIZE 31
  9. template<class TKey, class T>
  10. class CVSEntry
  11. {
  12. public:
  13. TKey key;
  14. T t;
  15. CVSEntry<TKey, T> *next;
  16. };
  17. template<class TKey, class T>
  18. class CVarStor
  19. {
  20. public:
  21. CVarStor() { memset(_HashTbl, 0, sizeof(_HashTbl)); }
  22. ~CVarStor();
  23. T *_Create(TKey key);
  24. T *_Find(TKey key);
  25. BOOL _Remove(TKey key);
  26. private:
  27. UINT _HashFunc(TKey key) { return (DWORD)key % PM_HASHSIZE; }
  28. CVSEntry<TKey, T> **_FindEntry(TKey key);
  29. CVSEntry<TKey, T> *_HashTbl[PM_HASHSIZE];
  30. };
  31. //+---------------------------------------------------------------------------
  32. //
  33. // dtor
  34. //
  35. //----------------------------------------------------------------------------
  36. template<class TKey, class T>
  37. CVarStor<TKey, T>::~CVarStor()
  38. {
  39. CVSEntry<TKey, T> *pe;
  40. CVSEntry<TKey, T> *peTmp;
  41. int i;
  42. // free anything left in the hashtbl
  43. for (i=0; i<ARRAYSIZE(_HashTbl); i++)
  44. {
  45. pe = _HashTbl[i];
  46. while (pe != NULL)
  47. {
  48. peTmp = pe->next;
  49. delete pe;
  50. pe = peTmp;
  51. }
  52. }
  53. }
  54. //+---------------------------------------------------------------------------
  55. //
  56. // _Set
  57. //
  58. //----------------------------------------------------------------------------
  59. template<class TKey, class T>
  60. T *CVarStor<TKey, T>::_Create(TKey key)
  61. {
  62. UINT uIndex;
  63. CVSEntry<TKey, T> *pe;
  64. BOOL fRet;
  65. fRet = TRUE;
  66. Assert(_FindEntry(key) == NULL);
  67. if ((pe = new CVSEntry<TKey, T>) == NULL)
  68. return NULL;
  69. // new entry
  70. uIndex = _HashFunc(key);
  71. pe->key = key;
  72. pe->next = _HashTbl[uIndex];
  73. _HashTbl[uIndex] = pe;
  74. return &pe->t;
  75. }
  76. //+---------------------------------------------------------------------------
  77. //
  78. // _Find
  79. //
  80. //----------------------------------------------------------------------------
  81. template<class TKey, class T>
  82. T *CVarStor<TKey, T>::_Find(TKey key)
  83. {
  84. CVSEntry<TKey, T> **ppe;
  85. if (ppe = _FindEntry(key))
  86. {
  87. return &(*ppe)->t;
  88. }
  89. return NULL;
  90. }
  91. //+---------------------------------------------------------------------------
  92. //
  93. // _Remove
  94. //
  95. //----------------------------------------------------------------------------
  96. template<class TKey, class T>
  97. BOOL CVarStor<TKey, T>::_Remove(TKey key)
  98. {
  99. CVSEntry<TKey, T> *pe;
  100. CVSEntry<TKey, T> **ppe;
  101. if (ppe = _FindEntry(key))
  102. {
  103. pe = *ppe;
  104. *ppe = pe->next;
  105. delete pe;
  106. return TRUE;
  107. }
  108. return FALSE;
  109. }
  110. //+---------------------------------------------------------------------------
  111. //
  112. // _FindEntry
  113. //
  114. //----------------------------------------------------------------------------
  115. template<class TKey, class T>
  116. CVSEntry<TKey, T> **CVarStor<TKey, T>::_FindEntry(TKey key)
  117. {
  118. CVSEntry<TKey, T> **ppe;
  119. ppe = &_HashTbl[_HashFunc(key)];
  120. while (*ppe)
  121. {
  122. if ((*ppe)->key == key)
  123. {
  124. return ppe;
  125. }
  126. ppe = &(*ppe)->next;
  127. }
  128. return NULL;
  129. }
  130. #endif // __cplusplus
  131. #endif // VARSTOR_H