RepeatedField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. // ET修改,这里不释放数组,避免gc
  200. //array = EmptyArray;
  201. count = 0;
  202. }
  203. /// <summary>
  204. /// Determines whether this collection contains the given item.
  205. /// </summary>
  206. /// <param name="item">The item to find.</param>
  207. /// <returns><c>true</c> if this collection contains the given item; <c>false</c> otherwise.</returns>
  208. public bool Contains(T item)
  209. {
  210. return IndexOf(item) != -1;
  211. }
  212. /// <summary>
  213. /// Copies this collection to the given array.
  214. /// </summary>
  215. /// <param name="array">The array to copy to.</param>
  216. /// <param name="arrayIndex">The first index of the array to copy to.</param>
  217. public void CopyTo(T[] array, int arrayIndex)
  218. {
  219. Array.Copy(this.array, 0, array, arrayIndex, count);
  220. }
  221. /// <summary>
  222. /// Removes the specified item from the collection
  223. /// </summary>
  224. /// <param name="item">The item to remove.</param>
  225. /// <returns><c>true</c> if the item was found and removed; <c>false</c> otherwise.</returns>
  226. public bool Remove(T item)
  227. {
  228. int index = IndexOf(item);
  229. if (index == -1)
  230. {
  231. return false;
  232. }
  233. Array.Copy(array, index + 1, array, index, count - index - 1);
  234. count--;
  235. array[count] = default(T);
  236. return true;
  237. }
  238. /// <summary>
  239. /// Gets the number of elements contained in the collection.
  240. /// </summary>
  241. public int Count
  242. {
  243. get
  244. {
  245. return count;
  246. }
  247. }
  248. /// <summary>
  249. /// Gets a value indicating whether the collection is read-only.
  250. /// </summary>
  251. public bool IsReadOnly
  252. {
  253. get
  254. {
  255. return false;
  256. }
  257. }
  258. /// <summary>
  259. /// Adds all of the specified values into this collection.
  260. /// </summary>
  261. /// <param name="values">The values to add to this collection.</param>
  262. public void AddRange(IEnumerable<T> values)
  263. {
  264. ProtoPreconditions.CheckNotNull(values, "values");
  265. // Optimization 1: If the collection we're adding is already a RepeatedField<T>,
  266. // we know the values are valid.
  267. var otherRepeatedField = values as RepeatedField<T>;
  268. if (otherRepeatedField != null)
  269. {
  270. EnsureSize(count + otherRepeatedField.count);
  271. Array.Copy(otherRepeatedField.array, 0, array, count, otherRepeatedField.count);
  272. count += otherRepeatedField.count;
  273. return;
  274. }
  275. // Optimization 2: The collection is an ICollection, so we can expand
  276. // just once and ask the collection to copy itself into the array.
  277. var collection = values as ICollection;
  278. if (collection != null)
  279. {
  280. var extraCount = collection.Count;
  281. // For reference types and nullable value types, we need to check that there are no nulls
  282. // present. (This isn't a thread-safe approach, but we don't advertise this is thread-safe.)
  283. // We expect the JITter to optimize this test to true/false, so it's effectively conditional
  284. // specialization.
  285. if (default(T) == null)
  286. {
  287. // TODO: Measure whether iterating once to check and then letting the collection copy
  288. // itself is faster or slower than iterating and adding as we go. For large
  289. // collections this will not be great in terms of cache usage... but the optimized
  290. // copy may be significantly faster than doing it one at a time.
  291. foreach (var item in collection)
  292. {
  293. if (item == null)
  294. {
  295. throw new ArgumentException("Sequence contained null element", "values");
  296. }
  297. }
  298. }
  299. EnsureSize(count + extraCount);
  300. collection.CopyTo(array, count);
  301. count += extraCount;
  302. return;
  303. }
  304. // We *could* check for ICollection<T> as well, but very very few collections implement
  305. // ICollection<T> but not ICollection. (HashSet<T> does, for one...)
  306. // Fall back to a slower path of adding items one at a time.
  307. foreach (T item in values)
  308. {
  309. Add(item);
  310. }
  311. }
  312. /// <summary>
  313. /// Adds all of the specified values into this collection. This method is present to
  314. /// allow repeated fields to be constructed from queries within collection initializers.
  315. /// Within non-collection-initializer code, consider using the equivalent <see cref="AddRange"/>
  316. /// method instead for clarity.
  317. /// </summary>
  318. /// <param name="values">The values to add to this collection.</param>
  319. public void Add(IEnumerable<T> values)
  320. {
  321. AddRange(values);
  322. }
  323. /// <summary>
  324. /// Returns an enumerator that iterates through the collection.
  325. /// </summary>
  326. /// <returns>
  327. /// An enumerator that can be used to iterate through the collection.
  328. /// </returns>
  329. public IEnumerator<T> GetEnumerator()
  330. {
  331. for (int i = 0; i < count; i++)
  332. {
  333. yield return array[i];
  334. }
  335. }
  336. /// <summary>
  337. /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
  338. /// </summary>
  339. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  340. /// <returns>
  341. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  342. /// </returns>
  343. public override bool Equals(object obj)
  344. {
  345. return Equals(obj as RepeatedField<T>);
  346. }
  347. /// <summary>
  348. /// Returns an enumerator that iterates through a collection.
  349. /// </summary>
  350. /// <returns>
  351. /// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
  352. /// </returns>
  353. IEnumerator IEnumerable.GetEnumerator()
  354. {
  355. return GetEnumerator();
  356. }
  357. /// <summary>
  358. /// Returns a hash code for this instance.
  359. /// </summary>
  360. /// <returns>
  361. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  362. /// </returns>
  363. public override int GetHashCode()
  364. {
  365. int hash = 0;
  366. for (int i = 0; i < count; i++)
  367. {
  368. hash = hash * 31 + array[i].GetHashCode();
  369. }
  370. return hash;
  371. }
  372. /// <summary>
  373. /// Compares this repeated field with another for equality.
  374. /// </summary>
  375. /// <param name="other">The repeated field to compare this with.</param>
  376. /// <returns><c>true</c> if <paramref name="other"/> refers to an equal repeated field; <c>false</c> otherwise.</returns>
  377. public bool Equals(RepeatedField<T> other)
  378. {
  379. if (ReferenceEquals(other, null))
  380. {
  381. return false;
  382. }
  383. if (ReferenceEquals(other, this))
  384. {
  385. return true;
  386. }
  387. if (other.Count != this.Count)
  388. {
  389. return false;
  390. }
  391. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  392. for (int i = 0; i < count; i++)
  393. {
  394. if (!comparer.Equals(array[i], other.array[i]))
  395. {
  396. return false;
  397. }
  398. }
  399. return true;
  400. }
  401. /// <summary>
  402. /// Returns the index of the given item within the collection, or -1 if the item is not
  403. /// present.
  404. /// </summary>
  405. /// <param name="item">The item to find in the collection.</param>
  406. /// <returns>The zero-based index of the item, or -1 if it is not found.</returns>
  407. public int IndexOf(T item)
  408. {
  409. ProtoPreconditions.CheckNotNullUnconstrained(item, "item");
  410. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  411. for (int i = 0; i < count; i++)
  412. {
  413. if (comparer.Equals(array[i], item))
  414. {
  415. return i;
  416. }
  417. }
  418. return -1;
  419. }
  420. /// <summary>
  421. /// Inserts the given item at the specified index.
  422. /// </summary>
  423. /// <param name="index">The index at which to insert the item.</param>
  424. /// <param name="item">The item to insert.</param>
  425. public void Insert(int index, T item)
  426. {
  427. ProtoPreconditions.CheckNotNullUnconstrained(item, "item");
  428. if (index < 0 || index > count)
  429. {
  430. throw new ArgumentOutOfRangeException("index");
  431. }
  432. EnsureSize(count + 1);
  433. Array.Copy(array, index, array, index + 1, count - index);
  434. array[index] = item;
  435. count++;
  436. }
  437. /// <summary>
  438. /// Removes the item at the given index.
  439. /// </summary>
  440. /// <param name="index">The zero-based index of the item to remove.</param>
  441. public void RemoveAt(int index)
  442. {
  443. if (index < 0 || index >= count)
  444. {
  445. throw new ArgumentOutOfRangeException("index");
  446. }
  447. Array.Copy(array, index + 1, array, index, count - index - 1);
  448. count--;
  449. array[count] = default(T);
  450. }
  451. /// <summary>
  452. /// Gets or sets the item at the specified index.
  453. /// </summary>
  454. /// <value>
  455. /// The element at the specified index.
  456. /// </value>
  457. /// <param name="index">The zero-based index of the element to get or set.</param>
  458. /// <returns>The item at the specified index.</returns>
  459. public T this[int index]
  460. {
  461. get
  462. {
  463. if (index < 0 || index >= count)
  464. {
  465. throw new ArgumentOutOfRangeException("index");
  466. }
  467. return array[index];
  468. }
  469. set
  470. {
  471. if (index < 0 || index >= count)
  472. {
  473. throw new ArgumentOutOfRangeException("index");
  474. }
  475. ProtoPreconditions.CheckNotNullUnconstrained(value, "value");
  476. array[index] = value;
  477. }
  478. }
  479. #region Explicit interface implementation for IList and ICollection.
  480. bool IList.IsFixedSize
  481. {
  482. get
  483. {
  484. return false;
  485. }
  486. }
  487. void ICollection.CopyTo(Array array, int index)
  488. {
  489. Array.Copy(this.array, 0, array, index, count);
  490. }
  491. bool ICollection.IsSynchronized
  492. {
  493. get
  494. {
  495. return false;
  496. }
  497. }
  498. object ICollection.SyncRoot
  499. {
  500. get
  501. {
  502. return this;
  503. }
  504. }
  505. object IList.this[int index]
  506. {
  507. get { return this[index]; }
  508. set { this[index] = (T)value; }
  509. }
  510. int IList.Add(object value)
  511. {
  512. Add((T)value);
  513. return count - 1;
  514. }
  515. bool IList.Contains(object value)
  516. {
  517. return (value is T && Contains((T)value));
  518. }
  519. int IList.IndexOf(object value)
  520. {
  521. if (!(value is T))
  522. {
  523. return -1;
  524. }
  525. return IndexOf((T)value);
  526. }
  527. void IList.Insert(int index, object value)
  528. {
  529. Insert(index, (T)value);
  530. }
  531. void IList.Remove(object value)
  532. {
  533. if (!(value is T))
  534. {
  535. return;
  536. }
  537. Remove((T)value);
  538. }
  539. #endregion
  540. }
  541. }