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.

351 lines
7.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991 - 1992.
  5. //
  6. // File: SET.HXX
  7. //
  8. // Contents: Bit set
  9. //
  10. // Classes: CSimpleSet, CSet
  11. //
  12. // History: 01-Nov-91 BartoszM Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #define EOS (-1)
  17. #define SET_SIZE (MAX_PERS_ID + 1)
  18. typedef unsigned int set_t;
  19. #define SIMPLE_SET_SIZE (8 * sizeof(set_t))
  20. //+---------------------------------------------------------------------------
  21. //
  22. // Class: CSimpleSet
  23. //
  24. // Purpose: Fast bit set, building block of CSet
  25. //
  26. // History: 01-Nov-91 BartoszM Created.
  27. //
  28. //----------------------------------------------------------------------------
  29. class CSimpleSet
  30. {
  31. public:
  32. CSimpleSet();
  33. void Add ( int i );
  34. void Remove ( int i );
  35. BOOL Contains ( int i ) const;
  36. int FirstElement () const;
  37. BOOL IsEmpty() const;
  38. void Clear();
  39. void Fill();
  40. private:
  41. set_t _mask(int i) const;
  42. set_t _bitset;
  43. };
  44. //+---------------------------------------------------------------------------
  45. //
  46. // Member: CSimpleSet::CSimpleSet, public
  47. //
  48. // Synopsis: Constructor, makes empty set
  49. //
  50. // History: 01-Nov-91 BartoszM Created.
  51. //
  52. //----------------------------------------------------------------------------
  53. inline CSimpleSet::CSimpleSet ()
  54. {
  55. Clear();
  56. }
  57. //+---------------------------------------------------------------------------
  58. //
  59. // Member: CSimpleSet::Add, public
  60. //
  61. // Synopsis: Adds element to set
  62. //
  63. // Arguments: [i] -- element to be added
  64. //
  65. // History: 01-Nov-91 BartoszM Created.
  66. //
  67. //----------------------------------------------------------------------------
  68. inline void CSimpleSet::Add ( int i )
  69. {
  70. _bitset |= _mask(i);
  71. }
  72. //+---------------------------------------------------------------------------
  73. //
  74. // Member: CSimpleSet::Remove, public
  75. //
  76. // Synopsis: Removes element from set
  77. //
  78. // Arguments: [i] -- element
  79. //
  80. // History: 01-Nov-91 BartoszM Created.
  81. //
  82. //----------------------------------------------------------------------------
  83. inline void CSimpleSet::Remove ( int i )
  84. {
  85. _bitset &= ~_mask(i);
  86. }
  87. //+---------------------------------------------------------------------------
  88. //
  89. // Member: CSimpleSet::Contains, public
  90. //
  91. // Synopsis: Checks if set contains element [i]
  92. //
  93. // Arguments: [i] -- element
  94. //
  95. // History: 01-Nov-91 BartoszM Created.
  96. //
  97. //----------------------------------------------------------------------------
  98. inline BOOL CSimpleSet::Contains ( int i ) const
  99. {
  100. return _bitset & _mask(i);
  101. }
  102. //+---------------------------------------------------------------------------
  103. //
  104. // Member: CSimpleSet::IsEmpty, public
  105. //
  106. // Synopsis: Checks if set is empty
  107. //
  108. // History: 01-Nov-91 BartoszM Created.
  109. //
  110. //----------------------------------------------------------------------------
  111. inline BOOL CSimpleSet::IsEmpty() const
  112. {
  113. return _bitset == 0;
  114. }
  115. //+---------------------------------------------------------------------------
  116. //
  117. // Member: CSimpleSet::Clear, public
  118. //
  119. // Synopsis: Removes all elements from set
  120. //
  121. // History: 01-Nov-91 BartoszM Created.
  122. //
  123. //----------------------------------------------------------------------------
  124. inline void CSimpleSet::Clear()
  125. {
  126. _bitset = 0;
  127. }
  128. //+---------------------------------------------------------------------------
  129. //
  130. // Member: CSimpleSet::Fill, public
  131. //
  132. // Synopsis: Fills the set
  133. //
  134. // History: 01-Nov-91 BartoszM Created.
  135. //
  136. //----------------------------------------------------------------------------
  137. inline void CSimpleSet::Fill()
  138. {
  139. _bitset = set_t( -1);
  140. }
  141. //+---------------------------------------------------------------------------
  142. //
  143. // Member: CSimpleSet::_mask, private
  144. //
  145. // Synopsis: Creates a bit mask corresponding to element [i]
  146. //
  147. // Arguments: [i] -- element
  148. //
  149. // History: 01-Nov-91 BartoszM Created.
  150. //
  151. //----------------------------------------------------------------------------
  152. inline set_t CSimpleSet::_mask(int i) const
  153. {
  154. ciAssert ( i < SIMPLE_SET_SIZE );
  155. return ((set_t) 1) << i;
  156. }
  157. //+---------------------------------------------------------------------------
  158. //
  159. // Class: CSet
  160. //
  161. // Purpose: Bit Set of fixed size
  162. //
  163. // History: 01-Nov-91 BartoszM Created.
  164. //
  165. //----------------------------------------------------------------------------
  166. #define SET_ENTRIES (( SET_SIZE + SIMPLE_SET_SIZE - 1 ) / SIMPLE_SET_SIZE )
  167. class CSet
  168. {
  169. public:
  170. CSet();
  171. CSet ( CSet& s );
  172. void Fill();
  173. CSet& operator= ( CSet& s );
  174. void Add ( int i );
  175. BOOL Contains ( int i ) const;
  176. void Remove ( int i );
  177. BOOL IsEmpty() const;
  178. int FirstElement() const;
  179. void Clear();
  180. private:
  181. unsigned _count() const;
  182. int _index(int i) const;
  183. int _offset( int i) const;
  184. CSimpleSet _aSimpleSet[SET_ENTRIES];
  185. };
  186. //+---------------------------------------------------------------------------
  187. //
  188. // Member: CSet::CSet, public
  189. //
  190. // Synopsis: Constructor, makes empty set
  191. //
  192. // History: 01-Nov-91 BartoszM Created.
  193. //
  194. //----------------------------------------------------------------------------
  195. inline CSet::CSet()
  196. {}
  197. //+---------------------------------------------------------------------------
  198. //
  199. // Member: CSet::_count, private
  200. //
  201. // Synopsis: Returns the number of simple sets in the set
  202. //
  203. // History: 01-Nov-91 BartoszM Created.
  204. //
  205. //----------------------------------------------------------------------------
  206. inline unsigned CSet::_count() const
  207. {
  208. return SET_ENTRIES;
  209. }
  210. //+---------------------------------------------------------------------------
  211. //
  212. // Member: CSet::_index, private
  213. //
  214. // Synopsis: Returns index into array of simple sets
  215. //
  216. // Arguments: [i] -- set element
  217. //
  218. // History: 01-Nov-91 BartoszM Created.
  219. //
  220. //----------------------------------------------------------------------------
  221. inline int CSet::_index(int i) const
  222. {
  223. return i / SIMPLE_SET_SIZE;
  224. }
  225. //+---------------------------------------------------------------------------
  226. //
  227. // Member: CSet::_offset, private
  228. //
  229. // Synopsis: Returns offset into particular simple set
  230. //
  231. // Arguments: [i] -- set element
  232. //
  233. // History: 01-Nov-91 BartoszM Created.
  234. //
  235. //----------------------------------------------------------------------------
  236. inline int CSet::_offset( int i) const
  237. {
  238. return i % SIMPLE_SET_SIZE;
  239. }
  240. //+---------------------------------------------------------------------------
  241. //
  242. // Member: CSet::Add, public
  243. //
  244. // Synopsis: Adds element to set
  245. //
  246. // Arguments: [i] -- element to be added
  247. //
  248. // History: 01-Nov-91 BartoszM Created.
  249. //
  250. //----------------------------------------------------------------------------
  251. inline void CSet::Add ( int i )
  252. {
  253. _aSimpleSet[_index(i)].Add(_offset(i));
  254. }
  255. //+---------------------------------------------------------------------------
  256. //
  257. // Member: CSet::Contains, public
  258. //
  259. // Synopsis: Checks if set contains element [i]
  260. //
  261. // Arguments: [i] -- element
  262. //
  263. // History: 01-Nov-91 BartoszM Created.
  264. //
  265. //----------------------------------------------------------------------------
  266. inline BOOL CSet::Contains ( int i ) const
  267. {
  268. return _aSimpleSet[_index(i)].Contains(_offset(i));
  269. }
  270. //+---------------------------------------------------------------------------
  271. //
  272. // Member: CSet::Remove, public
  273. //
  274. // Synopsis: Removes element from set
  275. //
  276. // Arguments: [i] -- element
  277. //
  278. // History: 01-Nov-91 BartoszM Created.
  279. //
  280. //----------------------------------------------------------------------------
  281. inline void CSet::Remove ( int i )
  282. {
  283. _aSimpleSet[_index(i)].Remove(_offset(i));
  284. }