RepeatedField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Text;
  36. using Google.Protobuf.Compatibility;
  37. namespace Google.Protobuf.Collections
  38. {
  39. /// <summary>
  40. /// The contents of a repeated field: essentially, a collection with some extra
  41. /// restrictions (no null values) and capabilities (deep cloning).
  42. /// </summary>
  43. /// <remarks>
  44. /// This implementation does not generally prohibit the use of types which are not
  45. /// supported by Protocol Buffers but nor does it guarantee that all operations will work in such cases.
  46. /// </remarks>
  47. /// <typeparam name="T">The element type of the repeated field.</typeparam>
  48. public sealed class RepeatedField<T> : IList<T>, IList, IDeepCloneable<RepeatedField<T>>, IEquatable<RepeatedField<T>>
  49. {
  50. private static readonly T[] EmptyArray = new T[0];
  51. private const int MinArraySize = 8;
  52. private T[] array = EmptyArray;
  53. private int count = 0;
  54. /// <summary>
  55. /// Creates a deep clone of this repeated field.
  56. /// </summary>
  57. /// <remarks>
  58. /// If the field type is
  59. /// a message type, each element is also cloned; otherwise, it is
  60. /// assumed that the field type is primitive (including string and
  61. /// bytes, both of which are immutable) and so a simple copy is
  62. /// equivalent to a deep clone.
  63. /// </remarks>
  64. /// <returns>A deep clone of this repeated field.</returns>
  65. public RepeatedField<T> Clone()
  66. {
  67. RepeatedField<T> clone = new RepeatedField<T>();
  68. if (array != EmptyArray)
  69. {
  70. clone.array = (T[])array.Clone();
  71. IDeepCloneable<T>[] cloneableArray = clone.array as IDeepCloneable<T>[];
  72. if (cloneableArray != null)
  73. {
  74. for (int i = 0; i < count; i++)
  75. {
  76. clone.array[i] = cloneableArray[i].Clone();
  77. }
  78. }
  79. }
  80. clone.count = count;
  81. return clone;
  82. }
  83. /// <summary>
  84. /// Adds the entries from the given input stream, decoding them with the specified codec.
  85. /// </summary>
  86. /// <param name="input">The input stream to read from.</param>
  87. /// <param name="codec">The codec to use in order to read each entry.</param>
  88. public void AddEntriesFrom(CodedInputStream input, FieldCodec<T> codec)
  89. {
  90. // TODO: Inline some of the Add code, so we can avoid checking the size on every
  91. // iteration.
  92. uint tag = input.LastTag;
  93. var reader = codec.ValueReader;
  94. // Value types can be packed or not.
  95. if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited)
  96. {
  97. int length = input.ReadLength();
  98. if (length > 0)
  99. {
  100. int oldLimit = input.PushLimit(length);
  101. while (!input.ReachedLimit)
  102. {
  103. Add(reader(input));
  104. }
  105. input.PopLimit(oldLimit);
  106. }
  107. // Empty packed field. Odd, but valid - just ignore.
  108. }
  109. else
  110. {
  111. // Not packed... (possibly not packable)
  112. do
  113. {
  114. Add(reader(input));
  115. } while (input.MaybeConsumeTag(tag));
  116. }
  117. }
  118. /// <summary>
  119. /// Calculates the size of this collection based on the given codec.
  120. /// </summary>
  121. /// <param name="codec">The codec to use when encoding each field.</param>
  122. /// <returns>The number of bytes that would be written to a <see cref="CodedOutputStream"/> by <see cref="WriteTo"/>,
  123. /// using the same codec.</returns>
  124. public int CalculateSize(FieldCodec<T> codec)
  125. {
  126. if (count == 0)
  127. {
  128. return 0;
  129. }
  130. uint tag = codec.Tag;
  131. if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited)
  132. {
  133. int dataSize = CalculatePackedDataSize(codec);
  134. return CodedOutputStream.ComputeRawVarint32Size(tag) +
  135. CodedOutputStream.ComputeLengthSize(dataSize) +
  136. dataSize;
  137. }
  138. else
  139. {
  140. var sizeCalculator = codec.ValueSizeCalculator;
  141. int size = count * CodedOutputStream.ComputeRawVarint32Size(tag);
  142. for (int i = 0; i < count; i++)
  143. {
  144. size += sizeCalculator(array[i]);
  145. }
  146. return size;
  147. }
  148. }
  149. private int CalculatePackedDataSize(FieldCodec<T> codec)
  150. {
  151. int fixedSize = codec.FixedSize;
  152. if (fixedSize == 0)
  153. {
  154. var calculator = codec.ValueSizeCalculator;
  155. int tmp = 0;
  156. for (int i = 0; i < count; i++)
  157. {
  158. tmp += calculator(array[i]);
  159. }
  160. return tmp;
  161. }
  162. else
  163. {
  164. return fixedSize * Count;
  165. }
  166. }
  167. /// <summary>
  168. /// Writes the contents of this collection to the given <see cref="CodedOutputStream"/>,
  169. /// encoding each value using the specified codec.
  170. /// </summary>
  171. /// <param name="output">The output stream to write to.</param>
  172. /// <param name="codec">The codec to use when encoding each value.</param>
  173. public void WriteTo(CodedOutputStream output, FieldCodec<T> codec)
  174. {
  175. if (count == 0)
  176. {
  177. return;
  178. }
  179. var writer = codec.ValueWriter;
  180. var tag = codec.Tag;
  181. if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited)
  182. {
  183. // Packed primitive type
  184. uint size = (uint)CalculatePackedDataSize(codec);
  185. output.WriteTag(tag);
  186. output.WriteRawVarint32(size);
  187. for (int i = 0; i < count; i++)
  188. {
  189. writer(output, array[i]);
  190. }
  191. }
  192. else
  193. {
  194. // Not packed: a simple tag/value pair for each value.
  195. // Can't use codec.WriteTagAndValue, as that omits default values.
  196. for (int i = 0; i < count; i++)
  197. {
  198. output.WriteTag(tag);
  199. writer(output, array[i]);
  200. }
  201. }
  202. }
  203. private void EnsureSize(int size)
  204. {
  205. if (array.Length < size)
  206. {
  207. size = Math.Max(size, MinArraySize);
  208. int newSize = Math.Max(array.Length * 2, size);
  209. var tmp = new T[newSize];
  210. Array.Copy(array, 0, tmp, 0, array.Length);
  211. array = tmp;
  212. }
  213. }
  214. /// <summary>
  215. /// Adds the specified item to the collection.
  216. /// </summary>
  217. /// <param name="item">The item to add.</param>
  218. public void Add(T item)
  219. {
  220. if (item == null)
  221. {
  222. throw new ArgumentNullException("item");
  223. }
  224. EnsureSize(count + 1);
  225. array[count++] = item;
  226. }
  227. /// <summary>
  228. /// Removes all items from the collection.
  229. /// </summary>
  230. public void Clear()
  231. {
  232. array = EmptyArray;
  233. count = 0;
  234. }
  235. /// <summary>
  236. /// Determines whether this collection contains the given item.
  237. /// </summary>
  238. /// <param name="item">The item to find.</param>
  239. /// <returns><c>true</c> if this collection contains the given item; <c>false</c> otherwise.</returns>
  240. public bool Contains(T item)
  241. {
  242. return IndexOf(item) != -1;
  243. }
  244. /// <summary>
  245. /// Copies this collection to the given array.
  246. /// </summary>
  247. /// <param name="array">The array to copy to.</param>
  248. /// <param name="arrayIndex">The first index of the array to copy to.</param>
  249. public void CopyTo(T[] array, int arrayIndex)
  250. {
  251. Array.Copy(this.array, 0, array, arrayIndex, count);
  252. }
  253. /// <summary>
  254. /// Removes the specified item from the collection
  255. /// </summary>
  256. /// <param name="item">The item to remove.</param>
  257. /// <returns><c>true</c> if the item was found and removed; <c>false</c> otherwise.</returns>
  258. public bool Remove(T item)
  259. {
  260. int index = IndexOf(item);
  261. if (index == -1)
  262. {
  263. return false;
  264. }
  265. Array.Copy(array, index + 1, array, index, count - index - 1);
  266. count--;
  267. array[count] = default(T);
  268. return true;
  269. }
  270. /// <summary>
  271. /// Gets the number of elements contained in the collection.
  272. /// </summary>
  273. public int Count { get { return count; } }
  274. /// <summary>
  275. /// Gets a value indicating whether the collection is read-only.
  276. /// </summary>
  277. public bool IsReadOnly { get { return false; } }
  278. // TODO: Remove this overload and just handle it in the one below, at execution time?
  279. /// <summary>
  280. /// Adds all of the specified values into this collection.
  281. /// </summary>
  282. /// <param name="values">The values to add to this collection.</param>
  283. public void Add(RepeatedField<T> values)
  284. {
  285. if (values == null)
  286. {
  287. throw new ArgumentNullException("values");
  288. }
  289. EnsureSize(count + values.count);
  290. // We know that all the values will be valid, because it's a RepeatedField.
  291. Array.Copy(values.array, 0, array, count, values.count);
  292. count += values.count;
  293. }
  294. /// <summary>
  295. /// Adds all of the specified values into this collection.
  296. /// </summary>
  297. /// <param name="values">The values to add to this collection.</param>
  298. public void Add(IEnumerable<T> values)
  299. {
  300. if (values == null)
  301. {
  302. throw new ArgumentNullException("values");
  303. }
  304. // TODO: Check for ICollection and get the Count, to optimize?
  305. foreach (T item in values)
  306. {
  307. Add(item);
  308. }
  309. }
  310. /// <summary>
  311. /// Returns an enumerator that iterates through the collection.
  312. /// </summary>
  313. /// <returns>
  314. /// An enumerator that can be used to iterate through the collection.
  315. /// </returns>
  316. public IEnumerator<T> GetEnumerator()
  317. {
  318. for (int i = 0; i < count; i++)
  319. {
  320. yield return array[i];
  321. }
  322. }
  323. /// <summary>
  324. /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
  325. /// </summary>
  326. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  327. /// <returns>
  328. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  329. /// </returns>
  330. public override bool Equals(object obj)
  331. {
  332. return Equals(obj as RepeatedField<T>);
  333. }
  334. /// <summary>
  335. /// Returns an enumerator that iterates through a collection.
  336. /// </summary>
  337. /// <returns>
  338. /// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
  339. /// </returns>
  340. IEnumerator IEnumerable.GetEnumerator()
  341. {
  342. return GetEnumerator();
  343. }
  344. /// <summary>
  345. /// Returns a hash code for this instance.
  346. /// </summary>
  347. /// <returns>
  348. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  349. /// </returns>
  350. public override int GetHashCode()
  351. {
  352. int hash = 0;
  353. for (int i = 0; i < count; i++)
  354. {
  355. hash = hash * 31 + array[i].GetHashCode();
  356. }
  357. return hash;
  358. }
  359. /// <summary>
  360. /// Compares this repeated field with another for equality.
  361. /// </summary>
  362. /// <param name="other">The repeated field to compare this with.</param>
  363. /// <returns><c>true</c> if <paramref name="other"/> refers to an equal repeated field; <c>false</c> otherwise.</returns>
  364. public bool Equals(RepeatedField<T> other)
  365. {
  366. if (ReferenceEquals(other, null))
  367. {
  368. return false;
  369. }
  370. if (ReferenceEquals(other, this))
  371. {
  372. return true;
  373. }
  374. if (other.Count != this.Count)
  375. {
  376. return false;
  377. }
  378. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  379. for (int i = 0; i < count; i++)
  380. {
  381. if (!comparer.Equals(array[i], other.array[i]))
  382. {
  383. return false;
  384. }
  385. }
  386. return true;
  387. }
  388. /// <summary>
  389. /// Returns the index of the given item within the collection, or -1 if the item is not
  390. /// present.
  391. /// </summary>
  392. /// <param name="item">The item to find in the collection.</param>
  393. /// <returns>The zero-based index of the item, or -1 if it is not found.</returns>
  394. public int IndexOf(T item)
  395. {
  396. if (item == null)
  397. {
  398. throw new ArgumentNullException("item");
  399. }
  400. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  401. for (int i = 0; i < count; i++)
  402. {
  403. if (comparer.Equals(array[i], item))
  404. {
  405. return i;
  406. }
  407. }
  408. return -1;
  409. }
  410. /// <summary>
  411. /// Inserts the given item at the specified index.
  412. /// </summary>
  413. /// <param name="index">The index at which to insert the item.</param>
  414. /// <param name="item">The item to insert.</param>
  415. public void Insert(int index, T item)
  416. {
  417. if (item == null)
  418. {
  419. throw new ArgumentNullException("item");
  420. }
  421. if (index < 0 || index > count)
  422. {
  423. throw new ArgumentOutOfRangeException("index");
  424. }
  425. EnsureSize(count + 1);
  426. Array.Copy(array, index, array, index + 1, count - index);
  427. array[index] = item;
  428. count++;
  429. }
  430. /// <summary>
  431. /// Removes the item at the given index.
  432. /// </summary>
  433. /// <param name="index">The zero-based index of the item to remove.</param>
  434. public void RemoveAt(int index)
  435. {
  436. if (index < 0 || index >= count)
  437. {
  438. throw new ArgumentOutOfRangeException("index");
  439. }
  440. Array.Copy(array, index + 1, array, index, count - index - 1);
  441. count--;
  442. array[count] = default(T);
  443. }
  444. /// <summary>
  445. /// Returns a string representation of this repeated field, in the same
  446. /// way as it would be represented by the default JSON formatter.
  447. /// </summary>
  448. public override string ToString()
  449. {
  450. var builder = new StringBuilder();
  451. JsonFormatter.Default.WriteList(builder, this);
  452. return builder.ToString();
  453. }
  454. /// <summary>
  455. /// Gets or sets the item at the specified index.
  456. /// </summary>
  457. /// <value>
  458. /// The element at the specified index.
  459. /// </value>
  460. /// <param name="index">The zero-based index of the element to get or set.</param>
  461. /// <returns>The item at the specified index.</returns>
  462. public T this[int index]
  463. {
  464. get
  465. {
  466. if (index < 0 || index >= count)
  467. {
  468. throw new ArgumentOutOfRangeException("index");
  469. }
  470. return array[index];
  471. }
  472. set
  473. {
  474. if (index < 0 || index >= count)
  475. {
  476. throw new ArgumentOutOfRangeException("index");
  477. }
  478. if (value == null)
  479. {
  480. throw new ArgumentNullException("value");
  481. }
  482. array[index] = value;
  483. }
  484. }
  485. #region Explicit interface implementation for IList and ICollection.
  486. bool IList.IsFixedSize { get { return false; } }
  487. void ICollection.CopyTo(Array array, int index)
  488. {
  489. Array.Copy(this.array, 0, array, index, count);
  490. }
  491. bool ICollection.IsSynchronized { get { return false; } }
  492. object ICollection.SyncRoot { get { return this; } }
  493. object IList.this[int index]
  494. {
  495. get { return this[index]; }
  496. set { this[index] = (T)value; }
  497. }
  498. int IList.Add(object value)
  499. {
  500. Add((T) value);
  501. return count - 1;
  502. }
  503. bool IList.Contains(object value)
  504. {
  505. return (value is T && Contains((T)value));
  506. }
  507. int IList.IndexOf(object value)
  508. {
  509. if (!(value is T))
  510. {
  511. return -1;
  512. }
  513. return IndexOf((T)value);
  514. }
  515. void IList.Insert(int index, object value)
  516. {
  517. Insert(index, (T) value);
  518. }
  519. void IList.Remove(object value)
  520. {
  521. if (!(value is T))
  522. {
  523. return;
  524. }
  525. Remove((T)value);
  526. }
  527. #endregion
  528. }
  529. }