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