Bag.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Original code dervied from:
  2. // https://github.com/thelinuxlich/artemis_CSharp/blob/master/Artemis_XNA_INDEPENDENT/Utils/Bag.cs
  3. // --------------------------------------------------------------------------------------------------------------------
  4. // <copyright file="Bag.cs" company="GAMADU.COM">
  5. // Copyright © 2013 GAMADU.COM. All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without modification, are
  8. // permitted provided that the following conditions are met:
  9. //
  10. // 1. Redistributions of source code must retain the above copyright notice, this list of
  11. // conditions and the following disclaimer.
  12. //
  13. // 2. Redistributions in binary form must reproduce the above copyright notice, this list
  14. // of conditions and the following disclaimer in the documentation and/or other materials
  15. // provided with the distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY GAMADU.COM 'AS IS' AND ANY EXPRESS OR IMPLIED
  18. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GAMADU.COM OR
  20. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. //
  27. // The views and conclusions contained in the software and documentation are those of the
  28. // authors and should not be interpreted as representing official policies, either expressed
  29. // or implied, of GAMADU.COM.
  30. // </copyright>
  31. // <summary>
  32. // Class Bag.
  33. // </summary>
  34. // --------------------------------------------------------------------------------------------------------------------
  35. using System;
  36. using System.Collections;
  37. using System.Collections.Generic;
  38. namespace MonoGame.Extended.Collections
  39. {
  40. public class Bag<T> : IEnumerable<T>
  41. {
  42. private T[] _items;
  43. private readonly bool _isPrimitive;
  44. public int Capacity => _items.Length;
  45. public bool IsEmpty => Count == 0;
  46. public int Count { get; private set; }
  47. public Bag(int capacity = 16)
  48. {
  49. _isPrimitive = typeof(T).IsPrimitive;
  50. _items = new T[capacity];
  51. }
  52. public T this[int index]
  53. {
  54. get => index >= _items.Length ? default(T) : _items[index];
  55. set
  56. {
  57. EnsureCapacity(index + 1);
  58. if (index >= Count)
  59. Count = index + 1;
  60. _items[index] = value;
  61. }
  62. }
  63. public void Add(T element)
  64. {
  65. EnsureCapacity(Count + 1);
  66. _items[Count] = element;
  67. ++Count;
  68. }
  69. public void AddRange(Bag<T> range)
  70. {
  71. for (int index = 0, j = range.Count; j > index; ++index)
  72. Add(range[index]);
  73. }
  74. public void Clear()
  75. {
  76. if(Count == 0)
  77. return;
  78. // non-primitive types are cleared so the garbage collector can release them
  79. if (!_isPrimitive)
  80. Array.Clear(_items, 0, Count);
  81. Count = 0;
  82. }
  83. public bool Contains(T element)
  84. {
  85. for (var index = Count - 1; index >= 0; --index)
  86. {
  87. if (element.Equals(_items[index]))
  88. return true;
  89. }
  90. return false;
  91. }
  92. public T RemoveAt(int index)
  93. {
  94. var result = _items[index];
  95. --Count;
  96. _items[index] = _items[Count];
  97. _items[Count] = default(T);
  98. return result;
  99. }
  100. public bool Remove(T element)
  101. {
  102. for (var index = Count - 1; index >= 0; --index)
  103. {
  104. if (element.Equals(_items[index]))
  105. {
  106. --Count;
  107. _items[index] = _items[Count];
  108. _items[Count] = default(T);
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. public bool RemoveAll(Bag<T> bag)
  115. {
  116. var isResult = false;
  117. for (var index = bag.Count - 1; index >= 0; --index)
  118. {
  119. if (Remove(bag[index]))
  120. isResult = true;
  121. }
  122. return isResult;
  123. }
  124. private void EnsureCapacity(int capacity)
  125. {
  126. if (capacity < _items.Length)
  127. return;
  128. var newCapacity = Math.Max((int)(_items.Length * 1.5), capacity);
  129. var oldElements = _items;
  130. _items = new T[newCapacity];
  131. Array.Copy(oldElements, 0, _items, 0, oldElements.Length);
  132. }
  133. IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
  134. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  135. /// <summary>
  136. /// Get the <see cref="BagEnumerator"/> for this <see cref="Bag{T}"/>.
  137. /// </summary>
  138. /// <returns></returns>
  139. /// <remarks>
  140. /// Use this method preferentially over <see cref="IEnumerable.GetEnumerator"/> while enumerating via foreach
  141. /// to avoid boxing the enumerator on every iteration, which can be expensive in high-performance environments.
  142. /// </remarks>
  143. public BagEnumerator GetEnumerator()
  144. {
  145. return new BagEnumerator(this);
  146. }
  147. /// <summary>
  148. /// Enumerates a Bag.
  149. /// </summary>
  150. public struct BagEnumerator : IEnumerator<T>
  151. {
  152. private readonly Bag<T> _bag;
  153. private volatile int _index;
  154. /// <summary>
  155. /// Creates a new <see cref="BagEnumerator"/> for this <see cref="Bag{T}"/>.
  156. /// </summary>
  157. /// <param name="bag"></param>
  158. public BagEnumerator(Bag<T> bag)
  159. {
  160. _bag = bag;
  161. _index = -1;
  162. }
  163. readonly T IEnumerator<T>.Current => _bag[_index];
  164. readonly object IEnumerator.Current => _bag[_index];
  165. /// <summary>
  166. /// Gets the element in the <see cref="Bag{T}"/> at the current position of the enumerator.
  167. /// </summary>
  168. public readonly T Current => _bag[_index];
  169. /// <inheritdoc/>
  170. public bool MoveNext()
  171. {
  172. return ++_index < _bag.Count;
  173. }
  174. /// <inheritdoc/>
  175. public readonly void Dispose()
  176. {
  177. }
  178. /// <inheritdoc/>
  179. public readonly void Reset()
  180. {
  181. }
  182. }
  183. }
  184. }