RepeatedField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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.IO;
  36. namespace Google.Protobuf.Collections
  37. {
  38. /// <summary>
  39. /// The contents of a repeated field: essentially, a collection with some extra
  40. /// restrictions (no null values) and capabilities (deep cloning).
  41. /// </summary>
  42. /// <remarks>
  43. /// This implementation does not generally prohibit the use of types which are not
  44. /// supported by Protocol Buffers but nor does it guarantee that all operations will work in such cases.
  45. /// </remarks>
  46. /// <typeparam name="T">The element type of the repeated field.</typeparam>
  47. public sealed class RepeatedField<T> : IList<T>, IList
  48. {
  49. private static readonly T[] EmptyArray = new T[0];
  50. private const int MinArraySize = 8;
  51. public T[] array = EmptyArray;
  52. public int count = 0;
  53. /// <summary>
  54. /// Adds the entries from the given input stream, decoding them with the specified codec.
  55. /// </summary>
  56. /// <param name="input">The input stream to read from.</param>
  57. /// <param name="codec">The codec to use in order to read each entry.</param>
  58. public void AddEntriesFrom(CodedInputStream input, FieldCodec<T> codec)
  59. {
  60. // TODO: Inline some of the Add code, so we can avoid checking the size on every
  61. // iteration.
  62. uint tag = input.LastTag;
  63. var reader = codec.ValueReader;
  64. // Non-nullable value types can be packed or not.
  65. if (FieldCodec<T>.IsPackedRepeatedField(tag))
  66. {
  67. int length = input.ReadLength();
  68. if (length > 0)
  69. {
  70. int oldLimit = input.PushLimit(length);
  71. while (!input.ReachedLimit)
  72. {
  73. Add(reader(input));
  74. }
  75. input.PopLimit(oldLimit);
  76. }
  77. // Empty packed field. Odd, but valid - just ignore.
  78. }
  79. else
  80. {
  81. // Not packed... (possibly not packable)
  82. do
  83. {
  84. Add(reader(input));
  85. } while (input.MaybeConsumeTag(tag));
  86. }
  87. }
  88. /// <summary>
  89. /// Calculates the size of this collection based on the given codec.
  90. /// </summary>
  91. /// <param name="codec">The codec to use when encoding each field.</param>
  92. /// <returns>The number of bytes that would be written to a <see cref="CodedOutputStream"/> by <see cref="WriteTo"/>,
  93. /// using the same codec.</returns>
  94. public int CalculateSize(FieldCodec<T> codec)
  95. {
  96. if (count == 0)
  97. {
  98. return 0;
  99. }
  100. uint tag = codec.Tag;
  101. if (codec.PackedRepeatedField)
  102. {
  103. int dataSize = CalculatePackedDataSize(codec);
  104. return CodedOutputStream.ComputeRawVarint32Size(tag) +
  105. CodedOutputStream.ComputeLengthSize(dataSize) +
  106. dataSize;
  107. }
  108. else
  109. {
  110. var sizeCalculator = codec.ValueSizeCalculator;
  111. int size = count * CodedOutputStream.ComputeRawVarint32Size(tag);
  112. for (int i = 0; i < count; i++)
  113. {
  114. size += sizeCalculator(array[i]);
  115. }
  116. return size;
  117. }
  118. }
  119. private int CalculatePackedDataSize(FieldCodec<T> codec)
  120. {
  121. int fixedSize = codec.FixedSize;
  122. if (fixedSize == 0)
  123. {
  124. var calculator = codec.ValueSizeCalculator;
  125. int tmp = 0;
  126. for (int i = 0; i < count; i++)
  127. {
  128. tmp += calculator(array[i]);
  129. }
  130. return tmp;
  131. }
  132. else
  133. {
  134. return fixedSize * Count;
  135. }
  136. }
  137. /// <summary>
  138. /// Writes the contents of this collection to the given <see cref="CodedOutputStream"/>,
  139. /// encoding each value using the specified codec.
  140. /// </summary>
  141. /// <param name="output">The output stream to write to.</param>
  142. /// <param name="codec">The codec to use when encoding each value.</param>
  143. public void WriteTo(CodedOutputStream output, FieldCodec<T> codec)
  144. {
  145. if (count == 0)
  146. {
  147. return;
  148. }
  149. var writer = codec.ValueWriter;
  150. var tag = codec.Tag;
  151. if (codec.PackedRepeatedField)
  152. {
  153. // Packed primitive type
  154. uint size = (uint)CalculatePackedDataSize(codec);
  155. output.WriteTag(tag);
  156. output.WriteRawVarint32(size);
  157. for (int i = 0; i < count; i++)
  158. {
  159. writer(output, array[i]);
  160. }
  161. }
  162. else
  163. {
  164. // Not packed: a simple tag/value pair for each value.
  165. // Can't use codec.WriteTagAndValue, as that omits default values.
  166. for (int i = 0; i < count; i++)
  167. {
  168. output.WriteTag(tag);
  169. writer(output, array[i]);
  170. }
  171. }
  172. }
  173. private void EnsureSize(int size)
  174. {
  175. if (array.Length < size)
  176. {
  177. size = Math.Max(size, MinArraySize);
  178. int newSize = Math.Max(array.Length * 2, size);
  179. var tmp = new T[newSize];
  180. Array.Copy(array, 0, tmp, 0, array.Length);
  181. array = tmp;
  182. }
  183. }
  184. /// <summary>
  185. /// Adds the specified item to the collection.
  186. /// </summary>
  187. /// <param name="item">The item to add.</param>
  188. public void Add(T item)
  189. {
  190. ProtoPreconditions.CheckNotNullUnconstrained(item, "item");
  191. EnsureSize(count + 1);
  192. array[count++] = item;
  193. }
  194. /// <summary>
  195. /// Removes all items from the collection.
  196. /// </summary>
  197. public void Clear()
  198. {
  199. array = EmptyArray;
  200. count = 0;
  201. }
  202. /// <summary>
  203. /// Determines whether this collection contains the given item.
  204. /// </summary>
  205. /// <param name="item">The item to find.</param>
  206. /// <returns><c>true</c> if this collection contains the given item; <c>false</c> otherwise.</returns>
  207. public bool Contains(T item)
  208. {
  209. return IndexOf(item) != -1;
  210. }
  211. /// <summary>
  212. /// Copies this collection to the given array.
  213. /// </summary>
  214. /// <param name="array">The array to copy to.</param>
  215. /// <param name="arrayIndex">The first index of the array to copy to.</param>
  216. public void CopyTo(T[] array, int arrayIndex)
  217. {
  218. Array.Copy(this.array, 0, array, arrayIndex, count);
  219. }
  220. /// <summary>
  221. /// Removes the specified item from the collection
  222. /// </summary>
  223. /// <param name="item">The item to remove.</param>
  224. /// <returns><c>true</c> if the item was found and removed; <c>false</c> otherwise.</returns>
  225. public bool Remove(T item)
  226. {
  227. int index = IndexOf(item);
  228. if (index == -1)
  229. {
  230. return false;
  231. }
  232. Array.Copy(array, index + 1, array, index, count - index - 1);
  233. count--;
  234. array[count] = default(T);
  235. return true;
  236. }
  237. /// <summary>
  238. /// Gets the number of elements contained in the collection.
  239. /// </summary>
  240. public int Count
  241. {
  242. get
  243. {
  244. return count;
  245. }
  246. }
  247. /// <summary>
  248. /// Gets a value indicating whether the collection is read-only.
  249. /// </summary>
  250. public bool IsReadOnly
  251. {
  252. get
  253. {
  254. return false;
  255. }
  256. }
  257. /// <summary>
  258. /// Adds all of the specified values into this collection.
  259. /// </summary>
  260. /// <param name="values">The values to add to this collection.</param>
  261. public void AddRange(IEnumerable<T> values)
  262. {
  263. ProtoPreconditions.CheckNotNull(values, "values");
  264. // Optimization 1: If the collection we're adding is already a RepeatedField<T>,
  265. // we know the values are valid.
  266. var otherRepeatedField = values as RepeatedField<T>;
  267. if (otherRepeatedField != null)
  268. {
  269. EnsureSize(count + otherRepeatedField.count);
  270. Array.Copy(otherRepeatedField.array, 0, array, count, otherRepeatedField.count);
  271. count += otherRepeatedField.count;
  272. return;
  273. }
  274. // Optimization 2: The collection is an ICollection, so we can expand
  275. // just once and ask the collection to copy itself into the array.
  276. var collection = values as ICollection;
  277. if (collection != null)
  278. {
  279. var extraCount = collection.Count;
  280. // For reference types and nullable value types, we need to check that there are no nulls
  281. // present. (This isn't a thread-safe approach, but we don't advertise this is thread-safe.)
  282. // We expect the JITter to optimize this test to true/false, so it's effectively conditional
  283. // specialization.
  284. if (default(T) == null)
  285. {
  286. // TODO: Measure whether iterating once to check and then letting the collection copy
  287. // itself is faster or slower than iterating and adding as we go. For large
  288. // collections this will not be great in terms of cache usage... but the optimized
  289. // copy may be significantly faster than doing it one at a time.
  290. foreach (var item in collection)
  291. {
  292. if (item == null)
  293. {
  294. throw new ArgumentException("Sequence contained null element", "values");
  295. }
  296. }
  297. }
  298. EnsureSize(count + extraCount);
  299. collection.CopyTo(array, count);
  300. count += extraCount;
  301. return;
  302. }
  303. // We *could* check for ICollection<T> as well, but very very few collections implement
  304. // ICollection<T> but not ICollection. (HashSet<T> does, for one...)
  305. // Fall back to a slower path of adding items one at a time.
  306. foreach (T item in values)
  307. {
  308. Add(item);
  309. }
  310. }
  311. /// <summary>
  312. /// Adds all of the specified values into this collection. This method is present to
  313. /// allow repeated fields to be constructed from queries within collection initializers.
  314. /// Within non-collection-initializer code, consider using the equivalent <see cref="AddRange"/>
  315. /// method instead for clarity.
  316. /// </summary>
  317. /// <param name="values">The values to add to this collection.</param>
  318. public void Add(IEnumerable<T> values)
  319. {
  320. AddRange(values);
  321. }
  322. /// <summary>
  323. /// Returns an enumerator that iterates through the collection.
  324. /// </summary>
  325. /// <returns>
  326. /// An enumerator that can be used to iterate through the collection.
  327. /// </returns>
  328. public IEnumerator<T> GetEnumerator()
  329. {
  330. for (int i = 0; i < count; i++)
  331. {
  332. yield return array[i];
  333. }
  334. }
  335. /// <summary>
  336. /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
  337. /// </summary>
  338. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  339. /// <returns>
  340. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  341. /// </returns>
  342. public override bool Equals(object obj)
  343. {
  344. return Equals(obj as RepeatedField<T>);
  345. }
  346. /// <summary>
  347. /// Returns an enumerator that iterates through a collection.
  348. /// </summary>
  349. /// <returns>
  350. /// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
  351. /// </returns>
  352. IEnumerator IEnumerable.GetEnumerator()
  353. {
  354. return GetEnumerator();
  355. }
  356. /// <summary>
  357. /// Returns a hash code for this instance.
  358. /// </summary>
  359. /// <returns>
  360. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  361. /// </returns>
  362. public override int GetHashCode()
  363. {
  364. int hash = 0;
  365. for (int i = 0; i < count; i++)
  366. {
  367. hash = hash * 31 + array[i].GetHashCode();
  368. }
  369. return hash;
  370. }
  371. /// <summary>
  372. /// Compares this repeated field with another for equality.
  373. /// </summary>
  374. /// <param name="other">The repeated field to compare this with.</param>
  375. /// <returns><c>true</c> if <paramref name="other"/> refers to an equal repeated field; <c>false</c> otherwise.</returns>
  376. public bool Equals(RepeatedField<T> other)
  377. {
  378. if (ReferenceEquals(other, null))
  379. {
  380. return false;
  381. }
  382. if (ReferenceEquals(other, this))
  383. {
  384. return true;
  385. }
  386. if (other.Count != this.Count)
  387. {
  388. return false;
  389. }
  390. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  391. for (int i = 0; i < count; i++)
  392. {
  393. if (!comparer.Equals(array[i], other.array[i]))
  394. {
  395. return false;
  396. }
  397. }
  398. return true;
  399. }
  400. /// <summary>
  401. /// Returns the index of the given item within the collection, or -1 if the item is not
  402. /// present.
  403. /// </summary>
  404. /// <param name="item">The item to find in the collection.</param>
  405. /// <returns>The zero-based index of the item, or -1 if it is not found.</returns>
  406. public int IndexOf(T item)
  407. {
  408. ProtoPreconditions.CheckNotNullUnconstrained(item, "item");
  409. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  410. for (int i = 0; i < count; i++)
  411. {
  412. if (comparer.Equals(array[i], item))
  413. {
  414. return i;
  415. }
  416. }
  417. return -1;
  418. }
  419. /// <summary>
  420. /// Inserts the given item at the specified index.
  421. /// </summary>
  422. /// <param name="index">The index at which to insert the item.</param>
  423. /// <param name="item">The item to insert.</param>
  424. public void Insert(int index, T item)
  425. {
  426. ProtoPreconditions.CheckNotNullUnconstrained(item, "item");
  427. if (index < 0 || index > count)
  428. {
  429. throw new ArgumentOutOfRangeException("index");
  430. }
  431. EnsureSize(count + 1);
  432. Array.Copy(array, index, array, index + 1, count - index);
  433. array[index] = item;
  434. count++;
  435. }
  436. /// <summary>
  437. /// Removes the item at the given index.
  438. /// </summary>
  439. /// <param name="index">The zero-based index of the item to remove.</param>
  440. public void RemoveAt(int index)
  441. {
  442. if (index < 0 || index >= count)
  443. {
  444. throw new ArgumentOutOfRangeException("index");
  445. }
  446. Array.Copy(array, index + 1, array, index, count - index - 1);
  447. count--;
  448. array[count] = default(T);
  449. }
  450. /// <summary>
  451. /// Gets or sets the item at the specified index.
  452. /// </summary>
  453. /// <value>
  454. /// The element at the specified index.
  455. /// </value>
  456. /// <param name="index">The zero-based index of the element to get or set.</param>
  457. /// <returns>The item at the specified index.</returns>
  458. public T this[int index]
  459. {
  460. get
  461. {
  462. if (index < 0 || index >= count)
  463. {
  464. throw new ArgumentOutOfRangeException("index");
  465. }
  466. return array[index];
  467. }
  468. set
  469. {
  470. if (index < 0 || index >= count)
  471. {
  472. throw new ArgumentOutOfRangeException("index");
  473. }
  474. ProtoPreconditions.CheckNotNullUnconstrained(value, "value");
  475. array[index] = value;
  476. }
  477. }
  478. #region Explicit interface implementation for IList and ICollection.
  479. bool IList.IsFixedSize
  480. {
  481. get
  482. {
  483. return false;
  484. }
  485. }
  486. void ICollection.CopyTo(Array array, int index)
  487. {
  488. Array.Copy(this.array, 0, array, index, count);
  489. }
  490. bool ICollection.IsSynchronized
  491. {
  492. get
  493. {
  494. return false;
  495. }
  496. }
  497. object ICollection.SyncRoot
  498. {
  499. get
  500. {
  501. return this;
  502. }
  503. }
  504. object IList.this[int index]
  505. {
  506. get { return this[index]; }
  507. set { this[index] = (T)value; }
  508. }
  509. int IList.Add(object value)
  510. {
  511. Add((T)value);
  512. return count - 1;
  513. }
  514. bool IList.Contains(object value)
  515. {
  516. return (value is T && Contains((T)value));
  517. }
  518. int IList.IndexOf(object value)
  519. {
  520. if (!(value is T))
  521. {
  522. return -1;
  523. }
  524. return IndexOf((T)value);
  525. }
  526. void IList.Insert(int index, object value)
  527. {
  528. Insert(index, (T)value);
  529. }
  530. void IList.Remove(object value)
  531. {
  532. if (!(value is T))
  533. {
  534. return;
  535. }
  536. Remove((T)value);
  537. }
  538. #endregion
  539. }
  540. }