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.

252 lines
7.7 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. FLEXARRAY.H
  5. Abstract:
  6. CFlexArray and CWStringArray implementation.
  7. These objects can operate from any allocator, and be constructed
  8. on arbitrary memory blocks.
  9. History:
  10. 11-Apr-96 a-raymcc Created.
  11. --*/
  12. #ifndef _FLEXARRY_H_
  13. #define _FLEXARRY_H_
  14. #include "corepol.h"
  15. #include <arena.h>
  16. #include <sync.h>
  17. //***************************************************************************
  18. //
  19. // class CFlexArray
  20. //
  21. // This class is a generic pointer array.
  22. //
  23. //***************************************************************************
  24. class POLARITY CFlexArray
  25. {
  26. protected:
  27. int m_nSize; // apparent size
  28. int m_nExtent; // de facto size
  29. int m_nGrowByPercent;
  30. void** m_pArray;
  31. public:
  32. enum { no_error, failed, out_of_memory, array_full, range_error };
  33. // Constructs a flex array at an initial size and
  34. // specifies the initial size and growth-size chunk.
  35. // =================================================
  36. CFlexArray(
  37. IN int nInitialSize = 0,
  38. IN int nGrowByPercent = 100
  39. );
  40. ~CFlexArray();
  41. CFlexArray(CFlexArray &);
  42. CFlexArray& operator=(CFlexArray &);
  43. int CopyDataFrom(const CFlexArray& aOther);
  44. int EnsureExtent(int nExtent);
  45. // Gets an element at a particular location.
  46. // =========================================
  47. inline void * GetAt(int nIndex) const { return m_pArray[nIndex]; }
  48. // Returns a ptr in the array; allows use on left-hand side of assignment.
  49. // =======================================================================
  50. inline void * operator[](int nIndex) const { return m_pArray[nIndex]; }
  51. inline void *& operator[](int nIndex) { return m_pArray[nIndex]; }
  52. // Sets the element at the requested location.
  53. // ===========================================
  54. void inline SetAt(int nIndex, void *p) { m_pArray[nIndex] = p; }
  55. // Removes an element.
  56. // ====================
  57. int RemoveAt(int nIndex);
  58. // Inserts an element.
  59. // ===================
  60. int InsertAt(int nIndex, void *);
  61. // Removes all zero entries (null ptrs) and shrinks the array size.
  62. // ================================================================
  63. void Compress();
  64. // Removes all zero entries from the end of the array and shrinks it
  65. // =================================================================
  66. void Trim();
  67. // Adds a new element to the end of the array.
  68. // ===========================================
  69. int inline Add(void *pSrc) { return InsertAt(m_nSize, pSrc); }
  70. // Gets the apparent size of the array (number of used elements)
  71. // =============================================================
  72. int inline Size() const { return m_nSize; }
  73. // Sets the apparent size of the array
  74. // ===================================
  75. void inline SetSize(int nNewSize) { m_nSize = nNewSize;}
  76. // Removes all entries and reduces array size to zero. The elements
  77. // are simply removed; not deallocated (this class doesn't know what
  78. // they are).
  79. // =================================================================
  80. void Empty();
  81. // Gets a pointer to the internal array.
  82. // =====================================
  83. inline void** GetArrayPtr() { return m_pArray; }
  84. inline void* const* GetArrayPtr() const { return m_pArray; }
  85. // Gets a pointer to the internal array and Resets the contents to none
  86. // ====================================================================
  87. void** UnbindPtr();
  88. void Bind(CFlexArray & Src);
  89. // For debugging.
  90. // ==============
  91. void DebugDump();
  92. void Sort();
  93. protected:
  94. static int __cdecl CompareEls(const void* pelem1, const void* pelem2);
  95. };
  96. //***************************************************************************
  97. //
  98. // class CWStringArray
  99. //
  100. // This class is a generic wide-string array.
  101. //
  102. //***************************************************************************
  103. class POLARITY CWStringArray
  104. {
  105. CFlexArray m_Array;
  106. public:
  107. enum { no_error, failed, out_of_memory, array_full, range_error };
  108. enum { not_found = -1, no_case, with_case };
  109. CWStringArray(
  110. int nSize = 0,
  111. int nGrowBy = 100
  112. );
  113. CWStringArray(CWStringArray &Src);
  114. ~CWStringArray();
  115. CWStringArray& operator =(CWStringArray &Src);
  116. // Gets the read-only ptr to the string at the requested index.
  117. // =============================================================
  118. inline wchar_t *GetAt(int nIndex) const { return (wchar_t *) m_Array[nIndex]; }
  119. // Same as GetAt().
  120. // ================
  121. inline wchar_t *operator[](int nIndex) const{ return (wchar_t *) m_Array[nIndex]; }
  122. // Appends a new element to the end of the array. Copies the param.
  123. // ================================================================
  124. int Add(const wchar_t *pStr);
  125. // Inserts a new element within the array.
  126. // =======================================
  127. int InsertAt(int nIndex, const wchar_t *pStr);
  128. // Removes an element at the specified index. Takes care of
  129. // cleanup.
  130. // =========================================================
  131. int RemoveAt(int nIndex);
  132. // Inserts a copy of <pStr> at that location after removing
  133. // the prior string and deallocating it.
  134. // ========================================================
  135. int SetAt(int nIndex, const wchar_t *pStr);
  136. // Directly replaces the pointer at the specified location
  137. // with the ptr value in <pStr>. No allocs or deallocs are done.
  138. // =============================================================
  139. int ReplaceAt(int nIndex, wchar_t *pStr);
  140. // Unchecked replacement
  141. // Deletes the string at the location and sets the entry to zero
  142. // without compressing the array.
  143. // =============================================================
  144. int DeleteStr(int nIndex);
  145. // Returns the 'apparent' size of the array.
  146. // =========================================
  147. inline int Size() const { return m_Array.Size(); }
  148. // Empties the array by cleaning up after all strings and
  149. // setting the size to zero.
  150. // ======================================================
  151. void Empty();
  152. // Locates a string or returns -1 if not found.
  153. // ============================================
  154. int FindStr(const wchar_t *pTarget, int nFlags);
  155. // Compresses the array by removing all zero elements.
  156. // ===================================================
  157. inline void Compress() { m_Array.Compress(); }
  158. // Sorts the array according to UNICODE order.
  159. // ===========================================
  160. void Sort();
  161. inline LPCWSTR* GetArrayPtr() { return (LPCWSTR*) m_Array.GetArrayPtr(); }
  162. // Standard set-theoretic operations.
  163. // ==================================
  164. static void Difference(
  165. CWStringArray &Src1,
  166. CWStringArray &Src2,
  167. CWStringArray &Diff
  168. );
  169. static void Intersection(
  170. CWStringArray &Src1,
  171. CWStringArray &Src2,
  172. CWStringArray &Output
  173. );
  174. static void Union(
  175. CWStringArray &Src1,
  176. CWStringArray &Src2,
  177. CWStringArray &Output
  178. );
  179. };
  180. template <typename T>
  181. class POLARITY CLockableFlexArray : public CFlexArray
  182. {
  183. private:
  184. T m_cs;
  185. public:
  186. void Lock() { m_cs.Enter(); };
  187. void Unlock() { m_cs.Leave(); };
  188. };
  189. #endif