RepeatedField.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Google.Protobuf.Collections
  5. {
  6. /// <summary>
  7. /// The contents of a repeated field: essentially, a collection with some extra
  8. /// restrictions (no null values) and capabilities (deep cloning and freezing).
  9. /// </summary>
  10. /// <typeparam name="T">The element type of the repeated field.</typeparam>
  11. public sealed class RepeatedField<T> : IList<T>, IDeepCloneable<RepeatedField<T>>, IEquatable<RepeatedField<T>>, IFreezable
  12. {
  13. private static readonly T[] EmptyArray = new T[0];
  14. private bool frozen;
  15. private const int MinArraySize = 8;
  16. private T[] array = EmptyArray;
  17. private int count = 0;
  18. /// <summary>
  19. /// Creates a deep clone of this repeated field.
  20. /// </summary>
  21. /// <remarks>
  22. /// If the field type is
  23. /// a message type, each element is also cloned; otherwise, it is
  24. /// assumed that the field type is primitive (including string and
  25. /// bytes, both of which are immutable) and so a simple copy is
  26. /// equivalent to a deep clone.
  27. /// </remarks>
  28. /// <returns>A deep clone of this repeated field.</returns>
  29. public RepeatedField<T> Clone()
  30. {
  31. RepeatedField<T> clone = new RepeatedField<T>();
  32. // Clone is implicitly *not* frozen, even if this object is.
  33. if (array != EmptyArray)
  34. {
  35. clone.array = (T[])array.Clone();
  36. IDeepCloneable<T>[] cloneableArray = clone.array as IDeepCloneable<T>[];
  37. if (cloneableArray != null)
  38. {
  39. for (int i = 0; i < count; i++)
  40. {
  41. clone.array[i] = cloneableArray[i].Clone();
  42. }
  43. }
  44. }
  45. clone.count = count;
  46. return clone;
  47. }
  48. public bool IsFrozen { get { return frozen; } }
  49. public void Freeze()
  50. {
  51. frozen = true;
  52. IFreezable[] freezableArray = array as IFreezable[];
  53. if (freezableArray != null)
  54. {
  55. for (int i = 0; i < count; i++)
  56. {
  57. freezableArray[i].Freeze();
  58. }
  59. }
  60. }
  61. private void EnsureSize(int size)
  62. {
  63. size = Math.Max(size, MinArraySize);
  64. if (array.Length < size)
  65. {
  66. int newSize = Math.Max(array.Length * 2, size);
  67. var tmp = new T[newSize];
  68. Array.Copy(array, 0, tmp, 0, array.Length);
  69. array = tmp;
  70. }
  71. }
  72. public void Add(T item)
  73. {
  74. if (item == null)
  75. {
  76. throw new ArgumentNullException("item");
  77. }
  78. this.CheckMutable();
  79. EnsureSize(count + 1);
  80. array[count++] = item;
  81. }
  82. /// <summary>
  83. /// Hack to allow us to add enums easily... will only work with int-based types.
  84. /// </summary>
  85. /// <param name="readEnum"></param>
  86. internal void AddInt32(int item)
  87. {
  88. this.CheckMutable();
  89. EnsureSize(count + 1);
  90. int[] castArray = (int[]) (object) array;
  91. castArray[count++] = item;
  92. }
  93. public void Clear()
  94. {
  95. this.CheckMutable();
  96. array = EmptyArray;
  97. count = 0;
  98. }
  99. public bool Contains(T item)
  100. {
  101. return IndexOf(item) != -1;
  102. }
  103. public void CopyTo(T[] array, int arrayIndex)
  104. {
  105. Array.Copy(this.array, 0, array, arrayIndex, count);
  106. }
  107. public bool Remove(T item)
  108. {
  109. this.CheckMutable();
  110. int index = IndexOf(item);
  111. if (index == -1)
  112. {
  113. return false;
  114. }
  115. Array.Copy(array, index + 1, array, index, count - index - 1);
  116. count--;
  117. array[count] = default(T);
  118. return true;
  119. }
  120. public int Count { get { return count; } }
  121. // TODO(jonskeet): If we implement freezing, make this reflect it.
  122. public bool IsReadOnly { get { return IsFrozen; } }
  123. public void Add(RepeatedField<T> values)
  124. {
  125. if (values == null)
  126. {
  127. throw new ArgumentNullException("values");
  128. }
  129. this.CheckMutable();
  130. EnsureSize(count + values.count);
  131. // We know that all the values will be valid, because it's a RepeatedField.
  132. Array.Copy(values.array, 0, array, count, values.count);
  133. count += values.count;
  134. }
  135. public void Add(IEnumerable<T> values)
  136. {
  137. if (values == null)
  138. {
  139. throw new ArgumentNullException("values");
  140. }
  141. this.CheckMutable();
  142. // TODO: Check for ICollection and get the Count?
  143. foreach (T item in values)
  144. {
  145. Add(item);
  146. }
  147. }
  148. public RepeatedField<T>.Enumerator GetEnumerator()
  149. {
  150. return new Enumerator(this);
  151. }
  152. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  153. {
  154. return GetEnumerator();
  155. }
  156. public override bool Equals(object obj)
  157. {
  158. return Equals(obj as RepeatedField<T>);
  159. }
  160. IEnumerator IEnumerable.GetEnumerator()
  161. {
  162. return GetEnumerator();
  163. }
  164. /// <summary>
  165. /// Returns an enumerator of the values in this list as integers.
  166. /// Used for enum types.
  167. /// </summary>
  168. internal Int32Enumerator GetInt32Enumerator()
  169. {
  170. return new Int32Enumerator((int[])(object)array, count);
  171. }
  172. public override int GetHashCode()
  173. {
  174. int hash = 0;
  175. for (int i = 0; i < count; i++)
  176. {
  177. hash = hash * 31 + array[i].GetHashCode();
  178. }
  179. return hash;
  180. }
  181. public bool Equals(RepeatedField<T> other)
  182. {
  183. if (ReferenceEquals(other, null))
  184. {
  185. return false;
  186. }
  187. if (ReferenceEquals(other, this))
  188. {
  189. return true;
  190. }
  191. if (other.Count != this.Count)
  192. {
  193. return false;
  194. }
  195. // TODO(jonskeet): Does this box for enums?
  196. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  197. for (int i = 0; i < count; i++)
  198. {
  199. if (!comparer.Equals(array[i], other.array[i]))
  200. {
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. public int IndexOf(T item)
  207. {
  208. if (item == null)
  209. {
  210. throw new ArgumentNullException("item");
  211. }
  212. // TODO(jonskeet): Does this box for enums?
  213. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  214. for (int i = 0; i < count; i++)
  215. {
  216. if (comparer.Equals(array[i], item))
  217. {
  218. return i;
  219. }
  220. }
  221. return -1;
  222. }
  223. public void Insert(int index, T item)
  224. {
  225. if (item == null)
  226. {
  227. throw new ArgumentNullException("item");
  228. }
  229. if (index < 0 || index > count)
  230. {
  231. throw new ArgumentOutOfRangeException("index");
  232. }
  233. this.CheckMutable();
  234. EnsureSize(count + 1);
  235. Array.Copy(array, index, array, index + 1, count - index);
  236. count++;
  237. }
  238. public void RemoveAt(int index)
  239. {
  240. if (index < 0 || index >= count)
  241. {
  242. throw new ArgumentOutOfRangeException("index");
  243. }
  244. this.CheckMutable();
  245. Array.Copy(array, index + 1, array, index, count - index - 1);
  246. count--;
  247. array[count] = default(T);
  248. }
  249. public T this[int index]
  250. {
  251. get
  252. {
  253. if (index < 0 || index >= count)
  254. {
  255. throw new ArgumentOutOfRangeException("index");
  256. }
  257. return array[index];
  258. }
  259. set
  260. {
  261. if (index < 0 || index >= count)
  262. {
  263. throw new ArgumentOutOfRangeException("index");
  264. }
  265. this.CheckMutable();
  266. if (value == null)
  267. {
  268. throw new ArgumentNullException("value");
  269. }
  270. array[index] = value;
  271. }
  272. }
  273. public struct Enumerator : IEnumerator<T>
  274. {
  275. private int index;
  276. private readonly RepeatedField<T> field;
  277. public Enumerator(RepeatedField<T> field)
  278. {
  279. this.field = field;
  280. this.index = -1;
  281. }
  282. public bool MoveNext()
  283. {
  284. if (index + 1 >= field.Count)
  285. {
  286. return false;
  287. }
  288. index++;
  289. return true;
  290. }
  291. public void Reset()
  292. {
  293. index = -1;
  294. }
  295. public T Current
  296. {
  297. get
  298. {
  299. if (index == -1 || index >= field.count)
  300. {
  301. throw new InvalidOperationException();
  302. }
  303. return field.array[index];
  304. }
  305. }
  306. object IEnumerator.Current
  307. {
  308. get { return Current; }
  309. }
  310. public void Dispose()
  311. {
  312. }
  313. }
  314. internal struct Int32Enumerator : IEnumerator<int>
  315. {
  316. private int index;
  317. private readonly int[] array;
  318. private readonly int count;
  319. public Int32Enumerator(int[] array, int count)
  320. {
  321. this.array = array;
  322. this.index = -1;
  323. this.count = count;
  324. }
  325. public bool MoveNext()
  326. {
  327. if (index + 1 >= count)
  328. {
  329. return false;
  330. }
  331. index++;
  332. return true;
  333. }
  334. public void Reset()
  335. {
  336. index = -1;
  337. }
  338. // No guard here, as we're only going to use this internally...
  339. public int Current { get { return array[index]; } }
  340. object IEnumerator.Current
  341. {
  342. get { return Current; }
  343. }
  344. public void Dispose()
  345. {
  346. }
  347. }
  348. }
  349. }