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