aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/Collections
diff options
context:
space:
mode:
authorJan Tattermusch <jtattermusch@users.noreply.github.com>2015-06-19 12:59:07 -0700
committerJan Tattermusch <jtattermusch@users.noreply.github.com>2015-06-19 12:59:07 -0700
commit45b70328f218dc2b3e20191c2cfa92872ef10d04 (patch)
tree8c4441b6abee41b5e960e7013e5aeb6761616a22 /csharp/src/ProtocolBuffers/Collections
parent5b3a8e76356ef2dcb4a87c3fa7323bdec01cf7ce (diff)
parent50a3a809e849fad5a53be5ccbaefaa02a106b535 (diff)
downloadprotobuf-45b70328f218dc2b3e20191c2cfa92872ef10d04.tar.gz
protobuf-45b70328f218dc2b3e20191c2cfa92872ef10d04.tar.bz2
protobuf-45b70328f218dc2b3e20191c2cfa92872ef10d04.zip
Merge pull request #515 from jskeet/proto3-only
Proto3 experimental C# fork
Diffstat (limited to 'csharp/src/ProtocolBuffers/Collections')
-rw-r--r--csharp/src/ProtocolBuffers/Collections/Dictionaries.cs2
-rw-r--r--csharp/src/ProtocolBuffers/Collections/Enumerables.cs2
-rw-r--r--csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs58
-rw-r--r--csharp/src/ProtocolBuffers/Collections/Lists.cs2
-rw-r--r--csharp/src/ProtocolBuffers/Collections/PopsicleList.cs208
-rw-r--r--csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs2
-rw-r--r--csharp/src/ProtocolBuffers/Collections/RepeatedField.cs329
-rw-r--r--csharp/src/ProtocolBuffers/Collections/RepeatedFieldExtensions.cs47
8 files changed, 380 insertions, 270 deletions
diff --git a/csharp/src/ProtocolBuffers/Collections/Dictionaries.cs b/csharp/src/ProtocolBuffers/Collections/Dictionaries.cs
index 1f3791b1..22fe90a6 100644
--- a/csharp/src/ProtocolBuffers/Collections/Dictionaries.cs
+++ b/csharp/src/ProtocolBuffers/Collections/Dictionaries.cs
@@ -33,7 +33,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
-namespace Google.ProtocolBuffers.Collections
+namespace Google.Protobuf.Collections
{
/// <summary>
/// Utility class for dictionaries.
diff --git a/csharp/src/ProtocolBuffers/Collections/Enumerables.cs b/csharp/src/ProtocolBuffers/Collections/Enumerables.cs
index 7ad9a832..217aefd7 100644
--- a/csharp/src/ProtocolBuffers/Collections/Enumerables.cs
+++ b/csharp/src/ProtocolBuffers/Collections/Enumerables.cs
@@ -32,7 +32,7 @@
using System;
using System.Collections;
-namespace Google.ProtocolBuffers.Collections
+namespace Google.Protobuf.Collections
{
/// <summary>
/// Utility class for IEnumerable (and potentially the generic version in the future).
diff --git a/csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs b/csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs
deleted file mode 100644
index a1a75815..00000000
--- a/csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs
+++ /dev/null
@@ -1,58 +0,0 @@
-// Protocol Buffers - Google's data interchange format
-// Copyright 2008 Google Inc. All rights reserved.
-// http://github.com/jskeet/dotnet-protobufs/
-// Original C++/Java/Python code:
-// http://code.google.com/p/protobuf/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-using System.Collections.Generic;
-
-namespace Google.ProtocolBuffers.Collections
-{
- /// <summary>
- /// A list which has an Add method which accepts an IEnumerable[T].
- /// This allows whole collections to be added easily using collection initializers.
- /// It causes a potential overload confusion if T : IEnumerable[T], but in
- /// practice that won't happen in protocol buffers.
- /// </summary>
- /// <remarks>This is only currently implemented by PopsicleList, and it's likely
- /// to stay that way - hence the name. More genuinely descriptive names are
- /// horribly ugly. (At least, the ones the author could think of...)</remarks>
- /// <typeparam name="T">The element type of the list</typeparam>
- public interface IPopsicleList<T> : IList<T>
- {
- void Add(IEnumerable<T> collection);
- }
-
- /// <summary>
- /// Used to efficiently cast the elements of enumerations
- /// </summary>
- internal interface ICastArray
- {
- IEnumerable<TItemType> CastArray<TItemType>();
- }
-} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/Collections/Lists.cs b/csharp/src/ProtocolBuffers/Collections/Lists.cs
index a24e1d9b..cf681767 100644
--- a/csharp/src/ProtocolBuffers/Collections/Lists.cs
+++ b/csharp/src/ProtocolBuffers/Collections/Lists.cs
@@ -32,7 +32,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
-namespace Google.ProtocolBuffers.Collections
+namespace Google.Protobuf.Collections
{
/// <summary>
/// Utility non-generic class for calling into Lists{T} using type inference.
diff --git a/csharp/src/ProtocolBuffers/Collections/PopsicleList.cs b/csharp/src/ProtocolBuffers/Collections/PopsicleList.cs
deleted file mode 100644
index 3de97f89..00000000
--- a/csharp/src/ProtocolBuffers/Collections/PopsicleList.cs
+++ /dev/null
@@ -1,208 +0,0 @@
-// Protocol Buffers - Google's data interchange format
-// Copyright 2008 Google Inc. All rights reserved.
-// http://github.com/jskeet/dotnet-protobufs/
-// Original C++/Java/Python code:
-// http://code.google.com/p/protobuf/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-using System;
-using System.Collections;
-using System.Collections.Generic;
-
-namespace Google.ProtocolBuffers.Collections
-{
- /// <summary>
- /// Proxies calls to a <see cref="List{T}" />, but allows the list
- /// to be made read-only (with the <see cref="MakeReadOnly" /> method),
- /// after which any modifying methods throw <see cref="NotSupportedException" />.
- /// </summary>
- public sealed class PopsicleList<T> : IPopsicleList<T>, ICastArray
- {
- private static readonly bool CheckForNull = default(T) == null;
- private static readonly T[] EmptySet = new T[0];
-
- private List<T> items;
- private bool readOnly;
-
- /// <summary>
- /// Makes this list read-only ("freezes the popsicle"). From this
- /// point on, mutating methods (Clear, Add etc) will throw a
- /// NotSupportedException. There is no way of "defrosting" the list afterwards.
- /// </summary>
- public void MakeReadOnly()
- {
- readOnly = true;
- }
-
- public int IndexOf(T item)
- {
- return items == null ? -1 : items.IndexOf(item);
- }
-
- public void Insert(int index, T item)
- {
- ValidateModification();
- if (CheckForNull)
- {
- ThrowHelper.ThrowIfNull(item);
- }
- items.Insert(index, item);
- }
-
- public void RemoveAt(int index)
- {
- ValidateModification();
- items.RemoveAt(index);
- }
-
- public T this[int index]
- {
- get
- {
- if (items == null)
- {
- throw new ArgumentOutOfRangeException();
- }
- return items[index];
- }
- set
- {
- ValidateModification();
- if (CheckForNull)
- {
- ThrowHelper.ThrowIfNull(value);
- }
- items[index] = value;
- }
- }
-
- public void Add(T item)
- {
- ValidateModification();
- if (CheckForNull)
- {
- ThrowHelper.ThrowIfNull(item);
- }
- items.Add(item);
- }
-
- public void Clear()
- {
- ValidateModification();
- items.Clear();
- }
-
- public bool Contains(T item)
- {
- return items == null ? false : items.Contains(item);
- }
-
- public void CopyTo(T[] array, int arrayIndex)
- {
- if (items != null)
- {
- items.CopyTo(array, arrayIndex);
- }
- }
-
- public int Count
- {
- get { return items == null ? 0 : items.Count; }
- }
-
- public bool IsReadOnly
- {
- get { return readOnly; }
- }
-
- public bool Remove(T item)
- {
- ValidateModification();
- return items.Remove(item);
- }
-
- public IEnumerator<T> GetEnumerator()
- {
- IEnumerable<T> tenum = (IEnumerable<T>)items ?? EmptySet;
- return tenum.GetEnumerator();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- public void Add(IEnumerable<T> collection)
- {
- ValidateModification();
- ThrowHelper.ThrowIfNull(collection);
-
- if (!CheckForNull || collection is PopsicleList<T>)
- {
- items.AddRange(collection);
- }
- else
- {
- // Assumption, it's ok to enumerate collections more than once.
- if (collection is ICollection<T>)
- {
- ThrowHelper.ThrowIfAnyNull(collection);
- items.AddRange(collection);
- }
- else
- {
- foreach (T item in collection)
- {
- ThrowHelper.ThrowIfNull(item);
- items.Add(item);
- }
- }
- }
- }
-
- private void ValidateModification()
- {
- if (readOnly)
- {
- throw new NotSupportedException("List is read-only");
- }
- if (items == null)
- {
- items = new List<T>();
- }
- }
-
- IEnumerable<TItemType> ICastArray.CastArray<TItemType>()
- {
- if (items == null)
- {
- return PopsicleList<TItemType>.EmptySet;
- }
- return (TItemType[]) (object) items.ToArray();
- }
- }
-} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs b/csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs
index b0bc55ff..031ebd02 100644
--- a/csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs
+++ b/csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs
@@ -33,7 +33,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
-namespace Google.ProtocolBuffers.Collections
+namespace Google.Protobuf.Collections
{
/// <summary>
/// Read-only wrapper around another dictionary.
diff --git a/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
new file mode 100644
index 00000000..25651784
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
@@ -0,0 +1,329 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Google.Protobuf.Collections
+{
+ public sealed class RepeatedField<T> : IList<T>, IEquatable<RepeatedField<T>>
+ {
+ private static readonly T[] EmptyArray = new T[0];
+
+ private const int MinArraySize = 8;
+ private T[] array = EmptyArray;
+ private int count = 0;
+
+ private void EnsureSize(int size)
+ {
+ size = Math.Max(size, MinArraySize);
+ if (array.Length < size)
+ {
+ int newSize = Math.Max(array.Length * 2, size);
+ var tmp = new T[newSize];
+ Array.Copy(array, 0, tmp, 0, array.Length);
+ array = tmp;
+ }
+ }
+
+ public void Add(T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ EnsureSize(count + 1);
+ array[count++] = item;
+ }
+
+ /// <summary>
+ /// Hack to allow us to add enums easily... will only work with int-based types.
+ /// </summary>
+ /// <param name="readEnum"></param>
+ internal void AddInt32(int item)
+ {
+ EnsureSize(count + 1);
+ int[] castArray = (int[]) (object) array;
+ castArray[count++] = item;
+ }
+
+ public void Clear()
+ {
+ array = EmptyArray;
+ count = 0;
+ }
+
+ public bool Contains(T item)
+ {
+ return IndexOf(item) != -1;
+ }
+
+ public void CopyTo(T[] array, int arrayIndex)
+ {
+ Array.Copy(this.array, 0, array, arrayIndex, count);
+ }
+
+ public bool Remove(T item)
+ {
+ int index = IndexOf(item);
+ if (index == -1)
+ {
+ return false;
+ }
+ Array.Copy(array, index + 1, array, index, count - index - 1);
+ count--;
+ array[count] = default(T);
+ return true;
+ }
+
+ public int Count { get { return count; } }
+
+ // TODO(jonskeet): If we implement freezing, make this reflect it.
+ public bool IsReadOnly { get { return false; } }
+
+ public void Add(RepeatedField<T> values)
+ {
+ if (values == null)
+ {
+ throw new ArgumentNullException("values");
+ }
+ EnsureSize(count + values.count);
+ // We know that all the values will be valid, because it's a RepeatedField.
+ Array.Copy(values.array, 0, array, count, values.count);
+ count += values.count;
+ }
+
+ public void Add(IEnumerable<T> values)
+ {
+ if (values == null)
+ {
+ throw new ArgumentNullException("values");
+ }
+ // TODO: Check for ICollection and get the Count?
+ foreach (T item in values)
+ {
+ Add(item);
+ }
+ }
+
+ public RepeatedField<T>.Enumerator GetEnumerator()
+ {
+ return new Enumerator(this);
+ }
+
+ IEnumerator<T> IEnumerable<T>.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ public override bool Equals(object obj)
+ {
+ return Equals(obj as RepeatedField<T>);
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ /// <summary>
+ /// Returns an enumerator of the values in this list as integers.
+ /// Used for enum types.
+ /// </summary>
+ internal Int32Enumerator GetInt32Enumerator()
+ {
+ return new Int32Enumerator((int[])(object)array, count);
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 23;
+ for (int i = 0; i < count; i++)
+ {
+ hash = hash * 31 + array[i].GetHashCode();
+ }
+ return hash;
+ }
+
+ public bool Equals(RepeatedField<T> other)
+ {
+ if (ReferenceEquals(other, null))
+ {
+ return false;
+ }
+ if (ReferenceEquals(other, this))
+ {
+ return true;
+ }
+ if (other.Count != this.Count)
+ {
+ return false;
+ }
+ // TODO(jonskeet): Does this box for enums?
+ EqualityComparer<T> comparer = EqualityComparer<T>.Default;
+ for (int i = 0; i < count; i++)
+ {
+ if (!comparer.Equals(array[i], other.array[i]))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public int IndexOf(T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ // TODO(jonskeet): Does this box for enums?
+ EqualityComparer<T> comparer = EqualityComparer<T>.Default;
+ for (int i = 0; i < count; i++)
+ {
+ if (comparer.Equals(array[i], item))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public void Insert(int index, T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ if (index < 0 || index > count)
+ {
+ throw new ArgumentOutOfRangeException("index");
+ }
+ EnsureSize(count + 1);
+ Array.Copy(array, index, array, index + 1, count - index);
+ count++;
+ }
+
+ public void RemoveAt(int index)
+ {
+ if (index < 0 || index >= count)
+ {
+ throw new ArgumentOutOfRangeException("index");
+ }
+ Array.Copy(array, index + 1, array, index, count - index - 1);
+ count--;
+ array[count] = default(T);
+ }
+
+ public T this[int index]
+ {
+ get
+ {
+ if (index < 0 || index >= count)
+ {
+ throw new ArgumentOutOfRangeException("index");
+ }
+ return array[index];
+ }
+ set
+ {
+ if (index < 0 || index >= count)
+ {
+ throw new ArgumentOutOfRangeException("index");
+ }
+ if (value == null)
+ {
+ throw new ArgumentNullException("value");
+ }
+ array[index] = value;
+ }
+ }
+
+ public struct Enumerator : IEnumerator<T>
+ {
+ private int index;
+ private readonly RepeatedField<T> field;
+
+ public Enumerator(RepeatedField<T> field)
+ {
+ this.field = field;
+ this.index = -1;
+ }
+
+ public bool MoveNext()
+ {
+ if (index + 1 >= field.Count)
+ {
+ return false;
+ }
+ index++;
+ return true;
+ }
+
+ public void Reset()
+ {
+ index = -1;
+ }
+
+ public T Current
+ {
+ get
+ {
+ if (index == -1 || index >= field.count)
+ {
+ throw new InvalidOperationException();
+ }
+ return field.array[index];
+ }
+ }
+
+ object IEnumerator.Current
+ {
+ get { return Current; }
+ }
+
+ public void Dispose()
+ {
+ }
+ }
+
+ internal struct Int32Enumerator : IEnumerator<int>
+ {
+ private int index;
+ private readonly int[] array;
+ private readonly int count;
+
+ public Int32Enumerator(int[] array, int count)
+ {
+ this.array = array;
+ this.index = -1;
+ this.count = count;
+ }
+
+ public bool MoveNext()
+ {
+ if (index + 1 >= count)
+ {
+ return false;
+ }
+ index++;
+ return true;
+ }
+
+ public void Reset()
+ {
+ index = -1;
+ }
+
+ // No guard here, as we're only going to use this internally...
+ public int Current { get { return array[index]; } }
+
+ object IEnumerator.Current
+ {
+ get { return Current; }
+ }
+
+ public void Dispose()
+ {
+ }
+ }
+ }
+}
diff --git a/csharp/src/ProtocolBuffers/Collections/RepeatedFieldExtensions.cs b/csharp/src/ProtocolBuffers/Collections/RepeatedFieldExtensions.cs
new file mode 100644
index 00000000..c5a934a1
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/RepeatedFieldExtensions.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Google.Protobuf.Collections
+{
+ public static class RepeatedFieldExtensions
+ {
+ internal static uint CalculateSize<T>(this RepeatedField<T> list, Func<T, int> sizeComputer)
+ {
+ int size = 0;
+ foreach (var item in list)
+ {
+ size += sizeComputer(item);
+ }
+ return (uint)size;
+ }
+
+ /*
+ /// <summary>
+ /// Calculates the serialized data size, including one tag per value.
+ /// </summary>
+ public static int CalculateTotalSize<T>(this RepeatedField<T> list, int tagSize, Func<T, int> sizeComputer)
+ {
+ if (list.Count == 0)
+ {
+ return 0;
+ }
+ return (int)(dataSize + tagSize * list.Count);
+ }
+
+ /// <summary>
+ /// Calculates the serialized data size, as a packed array (tag, length, data).
+ /// </summary>
+ public static int CalculateTotalPackedSize(int tagSize)
+ {
+ if (Count == 0)
+ {
+ return 0;
+ }
+ uint dataSize = CalculateSize();
+ return tagSize + CodedOutputStream.ComputeRawVarint32Size(dataSize) + (int)dataSize;
+ }
+ */
+ }
+}