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.

327 lines
7.6 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. CharVector.h
  5. Abstract:
  6. A light-weight vector implementation.
  7. Created:
  8. 03/14/2000 robkenny
  9. Modified:
  10. 06/19/2000 robkenny Converted Resize() and Append() to return BOOL to know if malloc failed.
  11. 08/14/2001 robkenny Inserted inside the ShimLib namespace.
  12. --*/
  13. #pragma once
  14. #include <new> // for in-place new
  15. namespace ShimLib
  16. {
  17. /*++
  18. Vector template
  19. This vector is extremely fast and simple.
  20. --*/
  21. template <class ClassType> class VectorT
  22. {
  23. public:
  24. typedef VectorT<ClassType> MyType;
  25. protected:
  26. ClassType * vectorList;
  27. int nVectorListMax; // Current size of vectorList array
  28. int nVectorList; // Number of entries in the vectorList array
  29. bool bListIsLocked; // Are we allowed to modify the array?
  30. inline void ValidateIndex(int index) const
  31. {
  32. #if DBG
  33. if (index < 0 || index >= nVectorList)
  34. {
  35. DebugPrintf("VectorT", eDbgLevelError, "VectorT: index %d out of bounds.\n", index);
  36. }
  37. if (nVectorListMax < 0 || nVectorList > nVectorListMax)
  38. {
  39. DebugPrintf("VectorT", eDbgLevelError, "VectorT: invalid nVectorListMax(%d) nVectorList(%d).\n", nVectorListMax, nVectorList);
  40. }
  41. #endif
  42. }
  43. public:
  44. VectorT()
  45. {
  46. // Init
  47. vectorList = NULL;
  48. nVectorListMax = 0;
  49. nVectorList = 0;
  50. bListIsLocked = false;
  51. }
  52. ~VectorT()
  53. {
  54. Erase();
  55. }
  56. // A Copy constructor
  57. VectorT(const MyType & copyMe)
  58. {
  59. // Init
  60. vectorList = NULL;
  61. nVectorListMax = 0;
  62. nVectorList = 0;
  63. bListIsLocked = false;
  64. Duplicate(copyMe);
  65. }
  66. // Assignment operator, this gets a copy of other
  67. MyType & operator = (const MyType & other)
  68. {
  69. if (this != & other)
  70. Duplicate(other);
  71. return *this;
  72. }
  73. // Copy copyMe into this
  74. void Duplicate(const MyType & copyMe)
  75. {
  76. Erase();
  77. // Copy class data
  78. if (Resize(copyMe.nVectorListMax))
  79. {
  80. nVectorListMax = copyMe.nVectorListMax;
  81. nVectorList = copyMe.nVectorList;
  82. bListIsLocked = copyMe.bListIsLocked;
  83. // Copy array data
  84. size_t nBytes = nVectorListMax * sizeof(ClassType);
  85. memcpy(vectorList, copyMe.vectorList, nBytes);
  86. }
  87. }
  88. void CopyElement(int index, const ClassType & element)
  89. {
  90. // Use memcpy to avoid any assignment operators.
  91. void * dest = & Get(index);
  92. const void * src = & element;
  93. memcpy(dest, src, sizeof(ClassType));
  94. }
  95. // Allow this to be treated like an array.
  96. ClassType & operator [] (int index)
  97. {
  98. ValidateIndex(index);
  99. return vectorList[index];
  100. }
  101. const ClassType & operator [] (int index) const
  102. {
  103. ValidateIndex(index);
  104. return vectorList[index];
  105. }
  106. // return the value of the index member
  107. ClassType & Get(int index)
  108. {
  109. ValidateIndex(index);
  110. return vectorList[index];
  111. }
  112. // return the const value of the index member
  113. const ClassType & Get(int index) const
  114. {
  115. ValidateIndex(index);
  116. return vectorList[index];
  117. }
  118. BOOL Resize(int size)
  119. {
  120. if (size > nVectorListMax)
  121. {
  122. size_t newVectorListSize = size * sizeof(ClassType);
  123. ClassType * newVectorList = (ClassType *)malloc(newVectorListSize);
  124. if (newVectorList)
  125. {
  126. size_t origSize = nVectorListMax * sizeof(ClassType);
  127. memcpy(newVectorList, vectorList, origSize);
  128. free(vectorList);
  129. vectorList = newVectorList;
  130. nVectorListMax = size;
  131. }
  132. }
  133. // We were successful if there is enough space in the array
  134. return nVectorListMax >= size;
  135. }
  136. // return the number of entries in the list
  137. int Size() const
  138. {
  139. return nVectorList;
  140. }
  141. // return the current MAXIMUM number of entries in the list
  142. int MaxSize() const
  143. {
  144. return nVectorListMax;
  145. }
  146. // Lock the list (prevent further additions)
  147. void Lock(bool lock = true)
  148. {
  149. bListIsLocked = lock;
  150. }
  151. // return true if the list is locked.
  152. bool IsLocked() const
  153. {
  154. return bListIsLocked;
  155. }
  156. // Reset number of entries in the list to 0
  157. void Reset()
  158. {
  159. nVectorList = 0;
  160. }
  161. // Remove all entries in the list
  162. void Erase()
  163. {
  164. Reset();
  165. if (vectorList)
  166. free(vectorList);
  167. vectorList = NULL;
  168. nVectorListMax = 0;
  169. }
  170. // Search for the member in the list, return index or -1
  171. int Find(const ClassType & member) const
  172. {
  173. for (int i = 0; i < Size(); ++i)
  174. {
  175. if (Get(i) == member)
  176. return i;
  177. }
  178. return -1;
  179. }
  180. // Add this item to the end of the list
  181. BOOL Append(const ClassType & member)
  182. {
  183. if (!bListIsLocked)
  184. {
  185. // Increase array size
  186. if (Resize(nVectorList + 1))
  187. {
  188. nVectorList += 1;
  189. CopyElement(nVectorList-1, member);
  190. return TRUE;
  191. }
  192. }
  193. return FALSE;
  194. }
  195. // Append this to the list, if it does not already exist
  196. // Return FALSE if any allocation failed.
  197. BOOL AppendUnique(const ClassType & member)
  198. {
  199. if (!bListIsLocked)
  200. {
  201. int index = Find(member);
  202. if (index == -1)
  203. {
  204. return Append(member);
  205. }
  206. }
  207. return TRUE;
  208. }
  209. // Add this item to the end of the list,
  210. // Use the assignment operator to set the new member.
  211. BOOL AppendConstruct(const ClassType & member)
  212. {
  213. if (!bListIsLocked)
  214. {
  215. // Increase array size
  216. if (Resize(nVectorList + 1))
  217. {
  218. // Must increase the size of the array before calling Get()
  219. // otherwise we'll over index the array.
  220. nVectorList += 1;
  221. ClassType & last = Get(nVectorList-1);
  222. new (&last) ClassType; // inplace new
  223. last = member;
  224. return TRUE;
  225. }
  226. }
  227. return FALSE;
  228. }
  229. // remove this index from the list. This does not keep the list order.
  230. void Remove(int index)
  231. {
  232. if (!bListIsLocked)
  233. {
  234. if (index >= 0 && index < Size())
  235. {
  236. // Remove the entry by copying the last entry over this index
  237. // Only move if this is not the last entry.
  238. if (index < Size() - 1)
  239. {
  240. CopyElement(index, Get(Size() - 1));
  241. }
  242. nVectorList -= 1;
  243. }
  244. }
  245. }
  246. };
  247. /*++
  248. Char Vector type class.
  249. --*/
  250. class CharVector : public VectorT<char *>
  251. {
  252. };
  253. class CAutoCrit
  254. {
  255. CRITICAL_SECTION * criticalSection;
  256. public:
  257. CAutoCrit(CRITICAL_SECTION * cs)
  258. {
  259. criticalSection = cs;
  260. EnterCriticalSection(criticalSection);
  261. }
  262. ~CAutoCrit()
  263. {
  264. LeaveCriticalSection(criticalSection);
  265. }
  266. };
  267. }; // end of namespace ShimLib