Windows NT 4.0 source code leak
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.

357 lines
9.1 KiB

4 years ago
  1. // Indicate.h
  2. // Class CIndicatorSet by Ron Murray
  3. // Created 8 September 1992
  4. #ifndef __INDICATE_H__
  5. #define __INDICATE_H__
  6. #include "Defines.h"
  7. #include "RefCnt.h"
  8. #include "SaveLoad.h"
  9. #include "Util.h"
  10. #include "CallBKQ.h"
  11. #include "Compress.h"
  12. void ClearBits(PUINT pulBase, UINT iBit, UINT cBits);
  13. void SetBits (PUINT pulBase, UINT iBit, UINT cBits);
  14. void MoveBits (PUINT pulSrc, UINT iSrcFirst, PUINT pulDest, UINT iDestFirst, UINT cBits);
  15. class CTokenList;
  16. class CAValRef;
  17. class CSegHashTable;
  18. class CTextDatabase;
  19. class CTextSet;
  20. class CCompressedSet;
  21. class CIndicatorSet : public CRCObject
  22. {
  23. friend class CTokenList;
  24. friend class CAValRef;
  25. friend class CSegHashTable;
  26. friend class CTextDatabase;
  27. friend class CTextSet;
  28. friend class CCompressedSet;
  29. friend class CTokenCollection;
  30. friend class CTitleCollection;
  31. friend class CFind;
  32. friend void FoundValidToken(UINT iValue, PVOID pvTag, PVOID pvEnvironment);
  33. public:
  34. // Creators:
  35. static CIndicatorSet* NewIndicatorSet(UINT cItems, BOOL fFillWithOnes= FALSE);
  36. static CIndicatorSet* NewIndicatorSet(UINT cItems, UINT iFirstOne, UINT cOnes);
  37. static CIndicatorSet* NewIndicatorSet(UINT cItems, PUINT paulBitVector, BOOL fFromFile= FALSE);
  38. static CIndicatorSet* NewIndicatorSet(UINT cItems, PUINT pafMasks, UINT fMask, UINT value);
  39. static CIndicatorSet* NewIndicatorSet(CIndicatorSet *pis); // To make a copy of an indicator set.
  40. static CIndicatorSet* NewIndicatorSet(CCompressedSet *pcs, UINT cbits= 0); // To decompress a compressed set
  41. // Destructor:
  42. virtual ~CIndicatorSet();
  43. // Reference Counting Routines --
  44. DECLARE_REF_COUNTERS(CIndicatorSet)
  45. // Save/Load Interface --
  46. void StoreImage(CPersist *pDiskImage);
  47. static CIndicatorSet * CreateImage(CPersist *pDiskImage);
  48. static void SkipImage(CPersist *pDiskImage);
  49. // Access Functions:
  50. int ChangeBit(long iBit, UINT usMethod);
  51. BOOL IsBitSet(long iBit);
  52. BOOL AnyOnes();
  53. void ClearAll();
  54. enum { CLEAR_BIT=0, SET_BIT, TOGGLE_BIT }; // usMethod values...
  55. CIndicatorSet *CopyFrom(CIndicatorSet *pis);
  56. CIndicatorSet * ANDWith(CIndicatorSet *pis);
  57. CIndicatorSet *NANDWith(CIndicatorSet *pis);
  58. CIndicatorSet * ORWith(CIndicatorSet *pis);
  59. CIndicatorSet * NORWith(CIndicatorSet *pis);
  60. CIndicatorSet *LESSWith(CIndicatorSet *pis);
  61. CIndicatorSet * LEQWith(CIndicatorSet *pis);
  62. CIndicatorSet * EQVWith(CIndicatorSet *pis);
  63. CIndicatorSet * NEQWith(CIndicatorSet *pis);
  64. CIndicatorSet * GTRWith(CIndicatorSet *pis); // And Not (minus)
  65. CIndicatorSet * GEQWith(CIndicatorSet *pis);
  66. CIndicatorSet *Catenate(CIndicatorSet *pis);
  67. CIndicatorSet *NEScan();
  68. CIndicatorSet *Invert();
  69. CIndicatorSet *ShiftIndicators(int cBitsDistance); // +ve => right shift
  70. // -ve => left shift
  71. CIndicatorSet *TakeIndicators(long cBits); // +ve => Take from left , pad on right
  72. // -ve => Take from right, pad on left
  73. CIndicatorSet *MarkedPartitions(CIndicatorSet *pisMarks, BOOL fPartitionedAtEnd= TRUE);
  74. inline void SetBit(long iBit)
  75. {
  76. InvalidateCache(); RawSetBit(iBit);
  77. }
  78. inline void ClearBit(long iBit)
  79. {
  80. InvalidateCache(); RawClearBit(iBit);
  81. }
  82. inline void ToggleBit(long iBit)
  83. {
  84. InvalidateCache(); RawToggleBit(iBit);
  85. }
  86. int ActiveIndices(int iLowBound, int iHighBound, int * piOneBits);
  87. UINT ItemCount(); // returns Rho IS
  88. int SelectionCount(); // returns +/IS
  89. int MarkedItems(int iMarked, int *piUnmarked, int cMarkedItems);
  90. // returns cMarkedItems Take iMarked Drop IS / Iota Rho IS
  91. int PredecessorMarkCount(int iItem); // returns +/ iItem Take IS
  92. protected:
  93. void ConnectImage(CPersist *pDiskImage);
  94. inline void InvalidateCache() { m_fCountsValid= FALSE; } // Must be called after
  95. // calling RawSetBit, RawClearBit
  96. // or RawToggleBit.
  97. inline void RawSetBit(long iBit)
  98. {
  99. ASSERT(UINT(iBit) < m_cEntries);
  100. m_paulBits[IDWORD_OF_IBIT(iBit)] |= 1 << IBIT_RESIDUE(iBit);
  101. }
  102. inline void RawClearBit(long iBit)
  103. {
  104. ASSERT(UINT(iBit) < m_cEntries);
  105. m_paulBits[IDWORD_OF_IBIT(iBit)] &= ~(1 << IBIT_RESIDUE(iBit));
  106. }
  107. inline void RawToggleBit(long iBit)
  108. {
  109. ASSERT(UINT(iBit) < m_cEntries);
  110. m_paulBits[IDWORD_OF_IBIT(iBit)] ^= 1 << IBIT_RESIDUE(iBit);
  111. }
  112. private:
  113. // Constructor:
  114. CIndicatorSet();
  115. // Initializers:
  116. void InitialIndicatorSet(UINT cItems, BOOL fFillWithOnes= FALSE);
  117. void InitialIndicatorSet(UINT cItems, UINT iFirstOne, UINT cOnes);
  118. void InitialIndicatorSet(UINT cItems, PUINT paulBitVector, BOOL fFromFile= FALSE);
  119. void InitialIndicatorSet(UINT cItems, PUINT pafMasks, UINT fMask, UINT value);
  120. void InitialIndicatorSet(CIndicatorSet *pis); // To make a copy of an indicator set.
  121. void InitialIndicatorSet(CCompressedSet *pcs, UINT cbits= 0); // To decompress a compressed set
  122. // Private member variables:
  123. BOOL m_fFromFileImage;
  124. UINT m_cEntries;
  125. UINT m_how_constructed; // See construction types below.
  126. enum CreationType {Unbuilt, From_Count, From_Bits};
  127. UINT * m_paCumulativeSums;
  128. UINT * m_paulBits;
  129. BOOL m_fCountsValid;
  130. enum { BITS_PER_SUM= 8192 };
  131. // Private routines:
  132. inline int BytesPerSum() { return BITS_PER_SUM/8; }
  133. inline int CBrackets() { return (m_cEntries+BITS_PER_SUM-1)/BITS_PER_SUM; }
  134. inline PBYTE PBCluster(int i) { return ((PBYTE) m_paulBits) + i * BytesPerSum(); }
  135. void SetupFromBitVector(UINT cItems, BOOL fFromFile= FALSE);
  136. void ConstructBitCounts();
  137. };
  138. class CCmpEnumerator;
  139. class CCompressedSet : public CRCObject
  140. {
  141. friend class CCmpEnumerator;
  142. public:
  143. // Creators --
  144. static CCompressedSet *NewCompressedSet(CDataRing *pdrIn, UINT cIndices, UINT iBase, UINT iLimit);
  145. static CCompressedSet *NewCompressedSet(PUINT paiSequence, UINT ciSequence, UINT iLimit);
  146. static CCompressedSet *NewCompressedSet(CIndicatorSet *pis);
  147. static CCompressedSet *NewCompressedSet(CCompressedSet *pcs);
  148. // Destructor --
  149. ~CCompressedSet();
  150. // Reference Counting Routines --
  151. DECLARE_REF_COUNTERS(CCompressedSet)
  152. // Save/Load Interface --
  153. void StoreImage(CPersist *pDiskImage);
  154. static CCompressedSet * CreateImage(CPersist *pDiskImage);
  155. static void SkipImage(CPersist *pDiskImage);
  156. // Access routines --
  157. void IndicateMembers(CIndicatorSet *pis);
  158. UINT ItemCount();
  159. UINT SelectionCount();
  160. protected:
  161. void ConnectImage(CPersist *pDiskImage);
  162. private:
  163. // Constructor --
  164. CCompressedSet();
  165. // Internal routines --
  166. static BOOL ScanDWords (PVOID pv, PUINT pdw, PUINT pcdw);
  167. static BOOL DumpIndicatorBits(PVOID pv, PUINT pdw, PUINT pcdw);
  168. static void StoreDWords (PVOID pv, PUINT pdw, UINT cdw);
  169. static void SetBits (PVOID pv, PUINT pdw, UINT cdw);
  170. // Private data members --
  171. BOOL m_fFromFile;
  172. UINT m_cPositions;
  173. union
  174. {
  175. UINT m_iRefFirst;
  176. PUINT m_pdwCompressed;
  177. };
  178. union
  179. {
  180. UINT m_iRefSecond;
  181. UINT m_cdwCompressed;
  182. };
  183. union
  184. {
  185. RefBits m_rb;
  186. UINT m_iRefThird;
  187. };
  188. };
  189. class CCmpEnumerator : public CDWInputQueue
  190. {
  191. public:
  192. // Creator --
  193. static CCmpEnumerator *NewEnumerator(CCompressedSet *pcs);
  194. // Destructor --
  195. ~CCmpEnumerator();
  196. protected:
  197. // Initialing --
  198. void Initial(CCompressedSet *pcs);
  199. // Constructor --
  200. CCmpEnumerator();
  201. private:
  202. // Internal routines --
  203. static BOOL ReadSmall(PVOID pv, PUINT pdw, PUINT pcdw);
  204. static BOOL ReadLarge(PVOID pv, PUINT pdw, PUINT pcdw);
  205. // Private data members --
  206. CCompressedSet *m_pcs;
  207. UINT m_iNext;
  208. CExpandor *m_pex;
  209. };
  210. // In some cases we know that cBits will always lie in the interval 0..31.
  211. // In those situations we can use RawShiftLeft and RawShiftRight. Note the
  212. // use of ASSERT to verify our assumption in the debugging environment.
  213. inline UINT RawShiftLeft(UINT ulValue, UINT cBits)
  214. {
  215. ASSERT(cBits < 32);
  216. return(ulValue << cBits);
  217. }
  218. inline UINT RawShiftRight(UINT ulValue, UINT cBits)
  219. {
  220. ASSERT(cBits < 32);
  221. return(ulValue >> cBits);
  222. }
  223. inline BOOL CIndicatorSet::IsBitSet(long iBit)
  224. {
  225. // Returns the value of entry slot iBit.
  226. ASSERT(iBit >= 0 && iBit < long(m_cEntries));
  227. return 1 & RawShiftRight(m_paulBits[IDWORD_OF_IBIT(iBit)], IBIT_RESIDUE(iBit));
  228. }
  229. inline UINT CCompressedSet::ItemCount()
  230. {
  231. ASSERT(m_cPositions >= (m_rb.fRefPair? 3 : m_rb.cReferences));
  232. return m_cPositions;
  233. }
  234. inline UINT CCompressedSet::SelectionCount()
  235. {
  236. ASSERT(m_cPositions >= (m_rb.fRefPair? 3 : m_rb.cReferences));
  237. return m_rb.fRefPair? 3 : m_rb.cReferences;
  238. }
  239. #endif // __INDICATE_H__