| OLD | NEW |
| (Empty) | |
| 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 |
| 33 using System; |
| 34 using System.Collections; |
| 35 using System.Collections.Generic; |
| 36 using Google.Protobuf.Compatibility; |
| 37 |
| 38 namespace Google.Protobuf.Collections |
| 39 { |
| 40 /// <summary> |
| 41 /// The contents of a repeated field: essentially, a collection with some ex
tra |
| 42 /// restrictions (no null values) and capabilities (deep cloning). |
| 43 /// </summary> |
| 44 /// <typeparam name="T">The element type of the repeated field.</typeparam> |
| 45 public sealed class RepeatedField<T> : IList<T>, IList, IDeepCloneable<Repea
tedField<T>>, IEquatable<RepeatedField<T>> |
| 46 { |
| 47 private static readonly T[] EmptyArray = new T[0]; |
| 48 private const int MinArraySize = 8; |
| 49 |
| 50 private T[] array = EmptyArray; |
| 51 private int count = 0; |
| 52 |
| 53 /// <summary> |
| 54 /// Creates a deep clone of this repeated field. |
| 55 /// </summary> |
| 56 /// <remarks> |
| 57 /// If the field type is |
| 58 /// a message type, each element is also cloned; otherwise, it is |
| 59 /// assumed that the field type is primitive (including string and |
| 60 /// bytes, both of which are immutable) and so a simple copy is |
| 61 /// equivalent to a deep clone. |
| 62 /// </remarks> |
| 63 /// <returns>A deep clone of this repeated field.</returns> |
| 64 public RepeatedField<T> Clone() |
| 65 { |
| 66 RepeatedField<T> clone = new RepeatedField<T>(); |
| 67 if (array != EmptyArray) |
| 68 { |
| 69 clone.array = (T[])array.Clone(); |
| 70 IDeepCloneable<T>[] cloneableArray = clone.array as IDeepCloneab
le<T>[]; |
| 71 if (cloneableArray != null) |
| 72 { |
| 73 for (int i = 0; i < count; i++) |
| 74 { |
| 75 clone.array[i] = cloneableArray[i].Clone(); |
| 76 } |
| 77 } |
| 78 } |
| 79 clone.count = count; |
| 80 return clone; |
| 81 } |
| 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.</p
aram> |
| 88 public void AddEntriesFrom(CodedInputStream input, FieldCodec<T> codec) |
| 89 { |
| 90 // TODO: Inline some of the Add code, so we can avoid checking the s
ize 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) == Wir
eFormat.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 |
| 119 /// <summary> |
| 120 /// Calculates the size of this collection based on the given codec. |
| 121 /// </summary> |
| 122 /// <param name="codec">The codec to use when encoding each field.</para
m> |
| 123 /// <returns>The number of bytes that would be written to a <see cref="C
odedOutputStream"/> by <see cref="WriteTo"/>, |
| 124 /// using the same codec.</returns> |
| 125 public int CalculateSize(FieldCodec<T> codec) |
| 126 { |
| 127 if (count == 0) |
| 128 { |
| 129 return 0; |
| 130 } |
| 131 uint tag = codec.Tag; |
| 132 if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == Wir
eFormat.WireType.LengthDelimited) |
| 133 { |
| 134 int dataSize = CalculatePackedDataSize(codec); |
| 135 return CodedOutputStream.ComputeRawVarint32Size(tag) + |
| 136 CodedOutputStream.ComputeLengthSize(dataSize) + |
| 137 dataSize; |
| 138 } |
| 139 else |
| 140 { |
| 141 var sizeCalculator = codec.ValueSizeCalculator; |
| 142 int size = count * CodedOutputStream.ComputeRawVarint32Size(tag)
; |
| 143 for (int i = 0; i < count; i++) |
| 144 { |
| 145 size += sizeCalculator(array[i]); |
| 146 } |
| 147 return size; |
| 148 } |
| 149 } |
| 150 |
| 151 private int CalculatePackedDataSize(FieldCodec<T> codec) |
| 152 { |
| 153 int fixedSize = codec.FixedSize; |
| 154 if (fixedSize == 0) |
| 155 { |
| 156 var calculator = codec.ValueSizeCalculator; |
| 157 int tmp = 0; |
| 158 for (int i = 0; i < count; i++) |
| 159 { |
| 160 tmp += calculator(array[i]); |
| 161 } |
| 162 return tmp; |
| 163 } |
| 164 else |
| 165 { |
| 166 return fixedSize * Count; |
| 167 } |
| 168 } |
| 169 |
| 170 /// <summary> |
| 171 /// Writes the contents of this collection to the given <see cref="Coded
OutputStream"/>, |
| 172 /// encoding each value using the specified codec. |
| 173 /// </summary> |
| 174 /// <param name="output">The output stream to write to.</param> |
| 175 /// <param name="codec">The codec to use when encoding each value.</para
m> |
| 176 public void WriteTo(CodedOutputStream output, FieldCodec<T> codec) |
| 177 { |
| 178 if (count == 0) |
| 179 { |
| 180 return; |
| 181 } |
| 182 var writer = codec.ValueWriter; |
| 183 var tag = codec.Tag; |
| 184 if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == Wir
eFormat.WireType.LengthDelimited) |
| 185 { |
| 186 // Packed primitive type |
| 187 uint size = (uint)CalculatePackedDataSize(codec); |
| 188 output.WriteTag(tag); |
| 189 output.WriteRawVarint32(size); |
| 190 for (int i = 0; i < count; i++) |
| 191 { |
| 192 writer(output, array[i]); |
| 193 } |
| 194 } |
| 195 else |
| 196 { |
| 197 // Not packed: a simple tag/value pair for each value. |
| 198 // Can't use codec.WriteTagAndValue, as that omits default value
s. |
| 199 for (int i = 0; i < count; i++) |
| 200 { |
| 201 output.WriteTag(tag); |
| 202 writer(output, array[i]); |
| 203 } |
| 204 } |
| 205 } |
| 206 |
| 207 private void EnsureSize(int size) |
| 208 { |
| 209 if (array.Length < size) |
| 210 { |
| 211 size = Math.Max(size, MinArraySize); |
| 212 int newSize = Math.Max(array.Length * 2, size); |
| 213 var tmp = new T[newSize]; |
| 214 Array.Copy(array, 0, tmp, 0, array.Length); |
| 215 array = tmp; |
| 216 } |
| 217 } |
| 218 |
| 219 /// <summary> |
| 220 /// Adds the specified item to the collection. |
| 221 /// </summary> |
| 222 /// <param name="item">The item to add.</param> |
| 223 public void Add(T item) |
| 224 { |
| 225 if (item == null) |
| 226 { |
| 227 throw new ArgumentNullException("item"); |
| 228 } |
| 229 EnsureSize(count + 1); |
| 230 array[count++] = item; |
| 231 } |
| 232 |
| 233 /// <summary> |
| 234 /// Removes all items from the collection. |
| 235 /// </summary> |
| 236 public void Clear() |
| 237 { |
| 238 array = EmptyArray; |
| 239 count = 0; |
| 240 } |
| 241 |
| 242 /// <summary> |
| 243 /// Determines whether this collection contains the given item. |
| 244 /// </summary> |
| 245 /// <param name="item">The item to find.</param> |
| 246 /// <returns><c>true</c> if this collection contains the given item; <c>
false</c> otherwise.</returns> |
| 247 public bool Contains(T item) |
| 248 { |
| 249 return IndexOf(item) != -1; |
| 250 } |
| 251 |
| 252 /// <summary> |
| 253 /// Copies this collection to the given array. |
| 254 /// </summary> |
| 255 /// <param name="array">The array to copy to.</param> |
| 256 /// <param name="arrayIndex">The first index of the array to copy to.</p
aram> |
| 257 public void CopyTo(T[] array, int arrayIndex) |
| 258 { |
| 259 Array.Copy(this.array, 0, array, arrayIndex, count); |
| 260 } |
| 261 |
| 262 /// <summary> |
| 263 /// Removes the specified item from the collection |
| 264 /// </summary> |
| 265 /// <param name="item">The item to remove.</param> |
| 266 /// <returns><c>true</c> if the item was found and removed; <c>false</c>
otherwise.</returns> |
| 267 public bool Remove(T item) |
| 268 { |
| 269 int index = IndexOf(item); |
| 270 if (index == -1) |
| 271 { |
| 272 return false; |
| 273 } |
| 274 Array.Copy(array, index + 1, array, index, count - index - 1); |
| 275 count--; |
| 276 array[count] = default(T); |
| 277 return true; |
| 278 } |
| 279 |
| 280 /// <summary> |
| 281 /// Gets the number of elements contained in the collection. |
| 282 /// </summary> |
| 283 public int Count { get { return count; } } |
| 284 |
| 285 /// <summary> |
| 286 /// Gets a value indicating whether the collection is read-only. |
| 287 /// </summary> |
| 288 public bool IsReadOnly { get { return false; } } |
| 289 |
| 290 // TODO: Remove this overload and just handle it in the one below, at ex
ecution time? |
| 291 |
| 292 /// <summary> |
| 293 /// Adds all of the specified values into this collection. |
| 294 /// </summary> |
| 295 /// <param name="values">The values to add to this collection.</param> |
| 296 public void Add(RepeatedField<T> values) |
| 297 { |
| 298 if (values == null) |
| 299 { |
| 300 throw new ArgumentNullException("values"); |
| 301 } |
| 302 EnsureSize(count + values.count); |
| 303 // We know that all the values will be valid, because it's a Repeate
dField. |
| 304 Array.Copy(values.array, 0, array, count, values.count); |
| 305 count += values.count; |
| 306 } |
| 307 |
| 308 /// <summary> |
| 309 /// Adds all of the specified values into this collection. |
| 310 /// </summary> |
| 311 /// <param name="values">The values to add to this collection.</param> |
| 312 public void Add(IEnumerable<T> values) |
| 313 { |
| 314 if (values == null) |
| 315 { |
| 316 throw new ArgumentNullException("values"); |
| 317 } |
| 318 // TODO: Check for ICollection and get the Count, to optimize? |
| 319 foreach (T item in values) |
| 320 { |
| 321 Add(item); |
| 322 } |
| 323 } |
| 324 |
| 325 /// <summary> |
| 326 /// Returns an enumerator that iterates through the collection. |
| 327 /// </summary> |
| 328 /// <returns> |
| 329 /// An enumerator that can be used to iterate through the collection. |
| 330 /// </returns> |
| 331 public IEnumerator<T> GetEnumerator() |
| 332 { |
| 333 for (int i = 0; i < count; i++) |
| 334 { |
| 335 yield return array[i]; |
| 336 } |
| 337 } |
| 338 |
| 339 /// <summary> |
| 340 /// Determines whether the specified <see cref="System.Object" />, is eq
ual to this instance. |
| 341 /// </summary> |
| 342 /// <param name="obj">The <see cref="System.Object" /> to compare with t
his instance.</param> |
| 343 /// <returns> |
| 344 /// <c>true</c> if the specified <see cref="System.Object" /> is equal
to this instance; otherwise, <c>false</c>. |
| 345 /// </returns> |
| 346 public override bool Equals(object obj) |
| 347 { |
| 348 return Equals(obj as RepeatedField<T>); |
| 349 } |
| 350 |
| 351 /// <summary> |
| 352 /// Returns an enumerator that iterates through a collection. |
| 353 /// </summary> |
| 354 /// <returns> |
| 355 /// An <see cref="T:System.Collections.IEnumerator" /> object that can b
e used to iterate through the collection. |
| 356 /// </returns> |
| 357 IEnumerator IEnumerable.GetEnumerator() |
| 358 { |
| 359 return GetEnumerator(); |
| 360 } |
| 361 |
| 362 /// <summary> |
| 363 /// Returns a hash code for this instance. |
| 364 /// </summary> |
| 365 /// <returns> |
| 366 /// A hash code for this instance, suitable for use in hashing algorithm
s and data structures like a hash table. |
| 367 /// </returns> |
| 368 public override int GetHashCode() |
| 369 { |
| 370 int hash = 0; |
| 371 for (int i = 0; i < count; i++) |
| 372 { |
| 373 hash = hash * 31 + array[i].GetHashCode(); |
| 374 } |
| 375 return hash; |
| 376 } |
| 377 |
| 378 /// <summary> |
| 379 /// Compares this repeated field with another for equality. |
| 380 /// </summary> |
| 381 /// <param name="other">The repeated field to compare this with.</param> |
| 382 /// <returns><c>true</c> if <paramref name="other"/> refers to an equal
repeated field; <c>false</c> otherwise.</returns> |
| 383 public bool Equals(RepeatedField<T> other) |
| 384 { |
| 385 if (ReferenceEquals(other, null)) |
| 386 { |
| 387 return false; |
| 388 } |
| 389 if (ReferenceEquals(other, this)) |
| 390 { |
| 391 return true; |
| 392 } |
| 393 if (other.Count != this.Count) |
| 394 { |
| 395 return false; |
| 396 } |
| 397 EqualityComparer<T> comparer = EqualityComparer<T>.Default; |
| 398 for (int i = 0; i < count; i++) |
| 399 { |
| 400 if (!comparer.Equals(array[i], other.array[i])) |
| 401 { |
| 402 return false; |
| 403 } |
| 404 } |
| 405 return true; |
| 406 } |
| 407 |
| 408 /// <summary> |
| 409 /// Returns the index of the given item within the collection, or -1 if
the item is not |
| 410 /// present. |
| 411 /// </summary> |
| 412 /// <param name="item">The item to find in the collection.</param> |
| 413 /// <returns>The zero-based index of the item, or -1 if it is not found.
</returns> |
| 414 public int IndexOf(T item) |
| 415 { |
| 416 if (item == null) |
| 417 { |
| 418 throw new ArgumentNullException("item"); |
| 419 } |
| 420 EqualityComparer<T> comparer = EqualityComparer<T>.Default; |
| 421 for (int i = 0; i < count; i++) |
| 422 { |
| 423 if (comparer.Equals(array[i], item)) |
| 424 { |
| 425 return i; |
| 426 } |
| 427 } |
| 428 return -1; |
| 429 } |
| 430 |
| 431 /// <summary> |
| 432 /// Inserts the given item at the specified index. |
| 433 /// </summary> |
| 434 /// <param name="index">The index at which to insert the item.</param> |
| 435 /// <param name="item">The item to insert.</param> |
| 436 public void Insert(int index, T item) |
| 437 { |
| 438 if (item == null) |
| 439 { |
| 440 throw new ArgumentNullException("item"); |
| 441 } |
| 442 if (index < 0 || index > count) |
| 443 { |
| 444 throw new ArgumentOutOfRangeException("index"); |
| 445 } |
| 446 EnsureSize(count + 1); |
| 447 Array.Copy(array, index, array, index + 1, count - index); |
| 448 array[index] = item; |
| 449 count++; |
| 450 } |
| 451 |
| 452 /// <summary> |
| 453 /// Removes the item at the given index. |
| 454 /// </summary> |
| 455 /// <param name="index">The zero-based index of the item to remove.</par
am> |
| 456 public void RemoveAt(int index) |
| 457 { |
| 458 if (index < 0 || index >= count) |
| 459 { |
| 460 throw new ArgumentOutOfRangeException("index"); |
| 461 } |
| 462 Array.Copy(array, index + 1, array, index, count - index - 1); |
| 463 count--; |
| 464 array[count] = default(T); |
| 465 } |
| 466 |
| 467 /// <summary> |
| 468 /// Gets or sets the item at the specified index. |
| 469 /// </summary> |
| 470 /// <value> |
| 471 /// The element at the specified index. |
| 472 /// </value> |
| 473 /// <param name="index">The zero-based index of the element to get or se
t.</param> |
| 474 /// <returns>The item at the specified index.</returns> |
| 475 public T this[int index] |
| 476 { |
| 477 get |
| 478 { |
| 479 if (index < 0 || index >= count) |
| 480 { |
| 481 throw new ArgumentOutOfRangeException("index"); |
| 482 } |
| 483 return array[index]; |
| 484 } |
| 485 set |
| 486 { |
| 487 if (index < 0 || index >= count) |
| 488 { |
| 489 throw new ArgumentOutOfRangeException("index"); |
| 490 } |
| 491 if (value == null) |
| 492 { |
| 493 throw new ArgumentNullException("value"); |
| 494 } |
| 495 array[index] = value; |
| 496 } |
| 497 } |
| 498 |
| 499 #region Explicit interface implementation for IList and ICollection. |
| 500 bool IList.IsFixedSize { get { return false; } } |
| 501 |
| 502 void ICollection.CopyTo(Array array, int index) |
| 503 { |
| 504 Array.Copy(this.array, 0, array, index, count); |
| 505 } |
| 506 |
| 507 bool ICollection.IsSynchronized { get { return false; } } |
| 508 |
| 509 object ICollection.SyncRoot { get { return this; } } |
| 510 |
| 511 object IList.this[int index] |
| 512 { |
| 513 get { return this[index]; } |
| 514 set { this[index] = (T)value; } |
| 515 } |
| 516 |
| 517 int IList.Add(object value) |
| 518 { |
| 519 Add((T) value); |
| 520 return count - 1; |
| 521 } |
| 522 |
| 523 bool IList.Contains(object value) |
| 524 { |
| 525 return (value is T && Contains((T)value)); |
| 526 } |
| 527 |
| 528 int IList.IndexOf(object value) |
| 529 { |
| 530 if (!(value is T)) |
| 531 { |
| 532 return -1; |
| 533 } |
| 534 return IndexOf((T)value); |
| 535 } |
| 536 |
| 537 void IList.Insert(int index, object value) |
| 538 { |
| 539 Insert(index, (T) value); |
| 540 } |
| 541 |
| 542 void IList.Remove(object value) |
| 543 { |
| 544 if (!(value is T)) |
| 545 { |
| 546 return; |
| 547 } |
| 548 Remove((T)value); |
| 549 } |
| 550 #endregion |
| 551 } |
| 552 } |
| OLD | NEW |