RepeatedField.cs 26 KB

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