Source code of Windows XP (NT5)
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.

249 lines
7.4 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. // For debugging.
  89. // ==============
  90. void DebugDump();
  91. void Sort();
  92. protected:
  93. static int __cdecl CompareEls(const void* pelem1, const void* pelem2);
  94. };
  95. //***************************************************************************
  96. //
  97. // class CWStringArray
  98. //
  99. // This class is a generic wide-string array.
  100. //
  101. //***************************************************************************
  102. class POLARITY CWStringArray
  103. {
  104. CFlexArray m_Array;
  105. public:
  106. enum { no_error, failed, out_of_memory, array_full, range_error };
  107. enum { not_found = -1, no_case, with_case };
  108. CWStringArray(
  109. int nSize = 0,
  110. int nGrowBy = 100
  111. );
  112. CWStringArray(CWStringArray &Src);
  113. ~CWStringArray();
  114. CWStringArray& operator =(CWStringArray &Src);
  115. // Gets the read-only ptr to the string at the requested index.
  116. // =============================================================
  117. inline wchar_t *GetAt(int nIndex) const { return (wchar_t *) m_Array[nIndex]; }
  118. // Same as GetAt().
  119. // ================
  120. inline wchar_t *operator[](int nIndex) const{ return (wchar_t *) m_Array[nIndex]; }
  121. // Appends a new element to the end of the array. Copies the param.
  122. // ================================================================
  123. int Add(const wchar_t *pStr);
  124. // Inserts a new element within the array.
  125. // =======================================
  126. int InsertAt(int nIndex, const wchar_t *pStr);
  127. // Removes an element at the specified index. Takes care of
  128. // cleanup.
  129. // =========================================================
  130. int RemoveAt(int nIndex);
  131. // Inserts a copy of <pStr> at that location after removing
  132. // the prior string and deallocating it.
  133. // ========================================================
  134. int SetAt(int nIndex, const wchar_t *pStr);
  135. // Directly replaces the pointer at the specified location
  136. // with the ptr value in <pStr>. No allocs or deallocs are done.
  137. // =============================================================
  138. int ReplaceAt(int nIndex, wchar_t *pStr);
  139. // Unchecked replacement
  140. // Deletes the string at the location and sets the entry to zero
  141. // without compressing the array.
  142. // =============================================================
  143. int DeleteStr(int nIndex);
  144. // Returns the 'apparent' size of the array.
  145. // =========================================
  146. inline int Size() const { return m_Array.Size(); }
  147. // Empties the array by cleaning up after all strings and
  148. // setting the size to zero.
  149. // ======================================================
  150. void Empty();
  151. // Locates a string or returns -1 if not found.
  152. // ============================================
  153. int FindStr(const wchar_t *pTarget, int nFlags);
  154. // Compresses the array by removing all zero elements.
  155. // ===================================================
  156. inline void Compress() { m_Array.Compress(); }
  157. // Sorts the array according to UNICODE order.
  158. // ===========================================
  159. void Sort();
  160. inline LPCWSTR* GetArrayPtr() { return (LPCWSTR*) m_Array.GetArrayPtr(); }
  161. // Standard set-theoretic operations.
  162. // ==================================
  163. static void Difference(
  164. CWStringArray &Src1,
  165. CWStringArray &Src2,
  166. CWStringArray &Diff
  167. );
  168. static void Intersection(
  169. CWStringArray &Src1,
  170. CWStringArray &Src2,
  171. CWStringArray &Output
  172. );
  173. static void Union(
  174. CWStringArray &Src1,
  175. CWStringArray &Src2,
  176. CWStringArray &Output
  177. );
  178. };
  179. class POLARITY CLockableFlexArray : public CFlexArray
  180. {
  181. private:
  182. CCritSec m_cs;
  183. public:
  184. void Lock() { EnterCriticalSection(&m_cs); };
  185. void Unlock() { LeaveCriticalSection(&m_cs); };
  186. };
  187. #endif