ItemArray.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Globalization;
  8. namespace System.Windows.Forms
  9. {
  10. internal class ItemArray : IComparer
  11. {
  12. private static int lastMask = 1;
  13. private readonly ListControl listControl;
  14. private Entry[] entries;
  15. private int count;
  16. private int version;
  17. public ItemArray(ListControl listControl)
  18. {
  19. this.listControl = listControl;
  20. }
  21. internal IReadOnlyList<Entry> Entries => entries;
  22. /// <summary>
  23. /// The version of this array. This number changes with each
  24. /// change to the item list.
  25. /// </summary>
  26. public int Version
  27. {
  28. get
  29. {
  30. return version;
  31. }
  32. }
  33. /// <summary>
  34. /// Adds the given item to the array. The state is initially
  35. /// zero.
  36. /// </summary>
  37. public object Add(object item)
  38. {
  39. EnsureSpace(1);
  40. version++;
  41. entries[count] = new Entry(item);
  42. return entries[count++];
  43. }
  44. /// <summary>
  45. /// Adds the given collection of items to the array.
  46. /// </summary>
  47. public void AddRange(ICollection items)
  48. {
  49. if (items == null)
  50. {
  51. throw new ArgumentNullException(nameof(items));
  52. }
  53. EnsureSpace(items.Count);
  54. foreach (object i in items)
  55. {
  56. entries[count++] = new Entry(i);
  57. }
  58. version++;
  59. }
  60. /// <summary>
  61. /// Clears this array.
  62. /// </summary>
  63. public void Clear()
  64. {
  65. if (count > 0)
  66. {
  67. Array.Clear(entries, 0, count);
  68. }
  69. count = 0;
  70. version++;
  71. }
  72. /// <summary>
  73. /// Allocates a new bitmask for use.
  74. /// </summary>
  75. public static int CreateMask()
  76. {
  77. int mask = lastMask;
  78. lastMask <<= 1;
  79. return mask;
  80. }
  81. /// <summary>
  82. /// Ensures that our internal array has space for
  83. /// the requested # of elements.
  84. /// </summary>
  85. private void EnsureSpace(int elements)
  86. {
  87. if (entries == null)
  88. {
  89. entries = new Entry[Math.Max(elements, 4)];
  90. }
  91. else if (count + elements >= entries.Length)
  92. {
  93. int newLength = Math.Max(entries.Length * 2, entries.Length + elements);
  94. Entry[] newEntries = new Entry[newLength];
  95. entries.CopyTo(newEntries, 0);
  96. entries = newEntries;
  97. }
  98. }
  99. /// <summary>
  100. /// Turns a virtual index into an actual index.
  101. /// </summary>
  102. public int GetActualIndex(int virtualIndex, int stateMask)
  103. {
  104. if (stateMask == 0)
  105. {
  106. return virtualIndex;
  107. }
  108. // More complex; we must compute this index.
  109. int calcIndex = -1;
  110. for (int i = 0; i < count; i++)
  111. {
  112. if ((entries[i].state & stateMask) != 0)
  113. {
  114. calcIndex++;
  115. if (calcIndex == virtualIndex)
  116. {
  117. return i;
  118. }
  119. }
  120. }
  121. return -1;
  122. }
  123. /// <summary>
  124. /// Gets the count of items matching the given mask.
  125. /// </summary>
  126. public int GetCount(int stateMask)
  127. {
  128. // If mask is zero, then just give the main count
  129. if (stateMask == 0)
  130. {
  131. return count;
  132. }
  133. // more complex: must provide a count of items
  134. // based on a mask.
  135. int filteredCount = 0;
  136. for (int i = 0; i < count; i++)
  137. {
  138. if ((entries[i].state & stateMask) != 0)
  139. {
  140. filteredCount++;
  141. }
  142. }
  143. return filteredCount;
  144. }
  145. /// <summary>
  146. /// Retrieves an enumerator that will enumerate based on
  147. /// the given mask.
  148. /// </summary>
  149. public IEnumerator GetEnumerator(int stateMask)
  150. {
  151. return GetEnumerator(stateMask, false);
  152. }
  153. /// <summary>
  154. /// Retrieves an enumerator that will enumerate based on
  155. /// the given mask.
  156. /// </summary>
  157. public IEnumerator GetEnumerator(int stateMask, bool anyBit)
  158. {
  159. return new EntryEnumerator(this, stateMask, anyBit);
  160. }
  161. /// <summary>
  162. /// Gets the item at the given index. The index is
  163. /// virtualized against the given mask value.
  164. /// </summary>
  165. public object GetItem(int virtualIndex, int stateMask)
  166. {
  167. int actualIndex = GetActualIndex(virtualIndex, stateMask);
  168. if (actualIndex == -1)
  169. {
  170. throw new IndexOutOfRangeException();
  171. }
  172. return entries[actualIndex].item;
  173. }
  174. /// <summary>
  175. /// Gets the item at the given index. The index is
  176. /// virtualized against the given mask value.
  177. /// </summary>
  178. internal object GetEntryObject(int virtualIndex, int stateMask)
  179. {
  180. int actualIndex = GetActualIndex(virtualIndex, stateMask);
  181. if (actualIndex == -1)
  182. {
  183. throw new IndexOutOfRangeException();
  184. }
  185. return entries[actualIndex];
  186. }
  187. /// <summary>
  188. /// Returns true if the requested state mask is set.
  189. /// The index is the actual index to the array.
  190. /// </summary>
  191. public bool GetState(int index, int stateMask)
  192. {
  193. return ((entries[index].state & stateMask) == stateMask);
  194. }
  195. /// <summary>
  196. /// Returns the virtual index of the item based on the
  197. /// state mask.
  198. /// </summary>
  199. public int IndexOf(object item, int stateMask)
  200. {
  201. int virtualIndex = -1;
  202. for (int i = 0; i < count; i++)
  203. {
  204. if (stateMask == 0 || (entries[i].state & stateMask) != 0)
  205. {
  206. virtualIndex++;
  207. if (entries[i].item.Equals(item))
  208. {
  209. return virtualIndex;
  210. }
  211. }
  212. }
  213. return -1;
  214. }
  215. /// <summary>
  216. /// Returns the virtual index of the item based on the
  217. /// state mask. Uses reference equality to identify the
  218. /// given object in the list.
  219. /// </summary>
  220. public int IndexOfIdentifier(object identifier, int stateMask)
  221. {
  222. int virtualIndex = -1;
  223. for (int i = 0; i < count; i++)
  224. {
  225. if (stateMask == 0 || (entries[i].state & stateMask) != 0)
  226. {
  227. virtualIndex++;
  228. if (entries[i] == identifier)
  229. {
  230. return virtualIndex;
  231. }
  232. }
  233. }
  234. return -1;
  235. }
  236. /// <summary>
  237. /// Inserts item at the given index. The index
  238. /// is not virtualized.
  239. /// </summary>
  240. public void Insert(int index, object item)
  241. {
  242. EnsureSpace(1);
  243. if (index < count)
  244. {
  245. System.Array.Copy(entries, index, entries, index + 1, count - index);
  246. }
  247. entries[index] = new Entry(item);
  248. count++;
  249. version++;
  250. }
  251. /// <summary>
  252. /// Removes the given item from the array. If
  253. /// the item is not in the array, this does nothing.
  254. /// </summary>
  255. public void Remove(object item)
  256. {
  257. int index = IndexOf(item, 0);
  258. if (index != -1)
  259. {
  260. RemoveAt(index);
  261. }
  262. }
  263. /// <summary>
  264. /// Removes the item at the given index.
  265. /// </summary>
  266. public void RemoveAt(int index)
  267. {
  268. count--;
  269. for (int i = index; i < count; i++)
  270. {
  271. entries[i] = entries[i + 1];
  272. }
  273. entries[count] = null;
  274. version++;
  275. }
  276. /// <summary>
  277. /// Sets the item at the given index to a new value.
  278. /// </summary>
  279. public void SetItem(int index, object item)
  280. {
  281. entries[index].item = item;
  282. }
  283. /// <summary>
  284. /// Sets the state data for the given index.
  285. /// </summary>
  286. public void SetState(int index, int stateMask, bool value)
  287. {
  288. if (value)
  289. {
  290. entries[index].state |= stateMask;
  291. }
  292. else
  293. {
  294. entries[index].state &= ~stateMask;
  295. }
  296. version++;
  297. }
  298. /// <summary>
  299. /// Find element in sorted array. If element is not found returns a binary complement of index for inserting
  300. /// </summary>
  301. public int BinarySearch(object element)
  302. {
  303. return Array.BinarySearch(entries, 0, count, element, this);
  304. }
  305. /// <summary>
  306. /// Sorts our array.
  307. /// </summary>
  308. public void Sort()
  309. {
  310. Array.Sort(entries, 0, count, this);
  311. }
  312. public void Sort(Array externalArray)
  313. {
  314. Array.Sort(externalArray, this);
  315. }
  316. int IComparer.Compare(object item1, object item2)
  317. {
  318. if (item1 == null)
  319. {
  320. if (item2 == null)
  321. {
  322. return 0; //both null, then they are equal
  323. }
  324. return -1; //item1 is null, but item2 is valid (greater)
  325. }
  326. if (item2 == null)
  327. {
  328. return 1; //item2 is null, so item 1 is greater
  329. }
  330. if (item1 is Entry)
  331. {
  332. item1 = ((Entry)item1).item;
  333. }
  334. if (item2 is Entry)
  335. {
  336. item2 = ((Entry)item2).item;
  337. }
  338. string itemName1 = listControl.GetItemText(item1);
  339. string itemName2 = listControl.GetItemText(item2);
  340. CompareInfo compInfo = CultureInfo.CurrentCulture.CompareInfo;
  341. return compInfo.Compare(itemName1, itemName2, CompareOptions.StringSort);
  342. }
  343. /// <summary>
  344. /// This is a single entry in our item array.
  345. /// </summary>
  346. internal class Entry
  347. {
  348. public object item;
  349. public int state;
  350. public Entry(object item)
  351. {
  352. this.item = item;
  353. state = 0;
  354. }
  355. }
  356. /// <summary>
  357. /// EntryEnumerator is an enumerator that will enumerate over
  358. /// a given state mask.
  359. /// </summary>
  360. private class EntryEnumerator : IEnumerator
  361. {
  362. private readonly ItemArray items;
  363. private readonly bool anyBit;
  364. private readonly int state;
  365. private int current;
  366. private readonly int version;
  367. /// <summary>
  368. /// Creates a new enumerator that will enumerate over the given state.
  369. /// </summary>
  370. public EntryEnumerator(ItemArray items, int state, bool anyBit)
  371. {
  372. this.items = items;
  373. this.state = state;
  374. this.anyBit = anyBit;
  375. version = items.version;
  376. current = -1;
  377. }
  378. /// <summary>
  379. /// Moves to the next element, or returns false if at the end.
  380. /// </summary>
  381. bool IEnumerator.MoveNext()
  382. {
  383. if (version != items.version)
  384. {
  385. throw new InvalidOperationException();
  386. }
  387. while (true)
  388. {
  389. if (current < items.count - 1)
  390. {
  391. current++;
  392. if (anyBit)
  393. {
  394. if ((items.entries[current].state & state) != 0)
  395. {
  396. return true;
  397. }
  398. }
  399. else
  400. {
  401. if ((items.entries[current].state & state) == state)
  402. {
  403. return true;
  404. }
  405. }
  406. }
  407. else
  408. {
  409. current = items.count;
  410. return false;
  411. }
  412. }
  413. }
  414. /// <summary>
  415. /// Resets the enumeration back to the beginning.
  416. /// </summary>
  417. void IEnumerator.Reset()
  418. {
  419. if (version != items.version)
  420. {
  421. throw new InvalidOperationException();
  422. }
  423. current = -1;
  424. }
  425. /// <summary>
  426. /// Retrieves the current value in the enumerator.
  427. /// </summary>
  428. object IEnumerator.Current
  429. {
  430. get
  431. {
  432. if (current == -1 || current == items.count)
  433. {
  434. throw new InvalidOperationException();
  435. }
  436. return items.entries[current].item;
  437. }
  438. }
  439. }
  440. }
  441. }