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.

244 lines
5.3 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLSIMPCOLL_H__
  11. #define __ATLSIMPCOLL_H__
  12. #pragma once
  13. #include <atldef.h>
  14. #include <wtypes.h>
  15. #ifndef _ATL_NO_DEBUG_CRT
  16. // Warning: if you define the above symbol, you will have
  17. // to provide your own definition of the ATLASSERT(x) macro
  18. // in order to compile ATL
  19. #include <crtdbg.h>
  20. #endif
  21. #pragma warning(push)
  22. #pragma warning(disable: 4800) // forcing 'int' value to bool
  23. namespace ATL
  24. {
  25. #pragma push_macro("new")
  26. #undef new
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Collection helper - CSimpleMap
  29. template <class TKey, class TVal>
  30. class CSimpleMapEqualHelper
  31. {
  32. public:
  33. static bool IsEqualKey(const TKey& k1, const TKey& k2)
  34. {
  35. return CSimpleArrayEqualHelper<TKey>::IsEqual(k1, k2);
  36. }
  37. static bool IsEqualValue(const TVal& v1, const TVal& v2)
  38. {
  39. return CSimpleArrayEqualHelper<TVal>::IsEqual(v1, v2);
  40. }
  41. };
  42. // intended for small number of simple types or pointers
  43. template <class TKey, class TVal, class TEqual = CSimpleMapEqualHelper< TKey, TVal > >
  44. class CSimpleMap
  45. {
  46. public:
  47. TKey* m_aKey;
  48. TVal* m_aVal;
  49. int m_nSize;
  50. typedef TKey _ArrayKeyType;
  51. typedef TVal _ArrayElementType;
  52. // Construction/destruction
  53. CSimpleMap() : m_aKey(NULL), m_aVal(NULL), m_nSize(0)
  54. { }
  55. ~CSimpleMap()
  56. {
  57. RemoveAll();
  58. }
  59. // Operations
  60. int GetSize() const
  61. {
  62. return m_nSize;
  63. }
  64. BOOL Add(const TKey& key, const TVal& val)
  65. {
  66. TKey* pKey;
  67. pKey = (TKey*)realloc(m_aKey, (m_nSize + 1) * sizeof(TKey));
  68. if(pKey == NULL)
  69. return FALSE;
  70. m_aKey = pKey;
  71. TVal* pVal;
  72. pVal = (TVal*)realloc(m_aVal, (m_nSize + 1) * sizeof(TVal));
  73. if(pVal == NULL)
  74. return FALSE;
  75. m_aVal = pVal;
  76. m_nSize++;
  77. InternalSetAtIndex(m_nSize - 1, key, val);
  78. return TRUE;
  79. }
  80. BOOL Remove(const TKey& key)
  81. {
  82. int nIndex = FindKey(key);
  83. if(nIndex == -1)
  84. return FALSE;
  85. return RemoveAt(nIndex);
  86. }
  87. BOOL RemoveAt(int nIndex)
  88. {
  89. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  90. if (nIndex < 0 || nIndex >= m_nSize)
  91. return FALSE;
  92. m_aKey[nIndex].~TKey();
  93. m_aVal[nIndex].~TVal();
  94. if(nIndex != (m_nSize - 1))
  95. {
  96. memmove((void*)(m_aKey + nIndex), (void*)(m_aKey + nIndex + 1), (m_nSize - (nIndex + 1)) * sizeof(TKey));
  97. memmove((void*)(m_aVal + nIndex), (void*)(m_aVal + nIndex + 1), (m_nSize - (nIndex + 1)) * sizeof(TVal));
  98. }
  99. TKey* pKey;
  100. pKey = (TKey*)realloc(m_aKey, (m_nSize - 1) * sizeof(TKey));
  101. if(pKey != NULL || m_nSize == 1)
  102. m_aKey = pKey;
  103. TVal* pVal;
  104. pVal = (TVal*)realloc(m_aVal, (m_nSize - 1) * sizeof(TVal));
  105. if(pVal != NULL || m_nSize == 1)
  106. m_aVal = pVal;
  107. m_nSize--;
  108. return TRUE;
  109. }
  110. void RemoveAll()
  111. {
  112. if(m_aKey != NULL)
  113. {
  114. for(int i = 0; i < m_nSize; i++)
  115. {
  116. m_aKey[i].~TKey();
  117. m_aVal[i].~TVal();
  118. }
  119. free(m_aKey);
  120. m_aKey = NULL;
  121. }
  122. if(m_aVal != NULL)
  123. {
  124. free(m_aVal);
  125. m_aVal = NULL;
  126. }
  127. m_nSize = 0;
  128. }
  129. BOOL SetAt(const TKey& key, const TVal& val)
  130. {
  131. int nIndex = FindKey(key);
  132. if(nIndex == -1)
  133. return FALSE;
  134. m_aKey[nIndex].~TKey();
  135. m_aVal[nIndex].~TVal();
  136. InternalSetAtIndex(nIndex, key, val);
  137. return TRUE;
  138. }
  139. TVal Lookup(const TKey& key) const
  140. {
  141. int nIndex = FindKey(key);
  142. if(nIndex == -1)
  143. return NULL; // must be able to convert
  144. return GetValueAt(nIndex);
  145. }
  146. TKey ReverseLookup(const TVal& val) const
  147. {
  148. int nIndex = FindVal(val);
  149. if(nIndex == -1)
  150. return NULL; // must be able to convert
  151. return GetKeyAt(nIndex);
  152. }
  153. TKey& GetKeyAt(int nIndex) const
  154. {
  155. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  156. return m_aKey[nIndex];
  157. }
  158. TVal& GetValueAt(int nIndex) const
  159. {
  160. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  161. return m_aVal[nIndex];
  162. }
  163. int FindKey(const TKey& key) const
  164. {
  165. for(int i = 0; i < m_nSize; i++)
  166. {
  167. if(TEqual::IsEqualKey(m_aKey[i], key))
  168. return i;
  169. }
  170. return -1; // not found
  171. }
  172. int FindVal(const TVal& val) const
  173. {
  174. for(int i = 0; i < m_nSize; i++)
  175. {
  176. if(TEqual::IsEqualValue(m_aVal[i], val))
  177. return i;
  178. }
  179. return -1; // not found
  180. }
  181. BOOL SetAtIndex(int nIndex, const TKey& key, const TVal& val)
  182. {
  183. if (nIndex < 0 || nIndex >= m_nSize)
  184. return FALSE;
  185. InternalSetAtIndex(nIndex, key, val);
  186. return TRUE;
  187. }
  188. // Implementation
  189. template <typename T>
  190. class Wrapper
  191. {
  192. public:
  193. Wrapper(const T& _t) : t(_t)
  194. {
  195. }
  196. template <class _Ty>
  197. void *operator new(size_t, _Ty* p)
  198. {
  199. return p;
  200. }
  201. template <class _Ty>
  202. void operator delete(void* /* pv */, _Ty* /* p */)
  203. {
  204. }
  205. T t;
  206. };
  207. void InternalSetAtIndex(int nIndex, const TKey& key, const TVal& val)
  208. {
  209. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  210. new(m_aKey + nIndex) Wrapper<TKey>(key);
  211. new(m_aVal + nIndex) Wrapper<TVal>(val);
  212. }
  213. };
  214. #pragma pop_macro("new")
  215. }; // namespace ATL
  216. #pragma warning(pop)
  217. #endif // __ATLSIMPCOLL_H__