aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/Collections
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2015-06-09 19:30:44 +0100
committerJon Skeet <skeet@pobox.com>2015-06-09 19:30:44 +0100
commite38294a62d7f37c0661273a9a26fda16d557423f (patch)
tree316989251907553408e7b32a12792f496333e075 /csharp/src/ProtocolBuffers/Collections
parentf52426827e4d5e8da7d205af538799740b5199b9 (diff)
downloadprotobuf-e38294a62d7f37c0661273a9a26fda16d557423f.tar.gz
protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.tar.bz2
protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.zip
First pass at the mutable API. Quite a bit more to do - in particular, it's pretty slow right now.
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.cs168
-rw-r--r--csharp/src/ProtocolBuffers/Collections/RepeatedFieldExtensions.cs47
8 files changed, 219 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..7dcd060e
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
@@ -0,0 +1,168 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Google.Protobuf.Collections
+{
+ public sealed class RepeatedField<T> : IList<T>, IEquatable<RepeatedField<T>>
+ {
+ private readonly List<T> list = new List<T>();
+
+ public void Add(T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ list.Add(item);
+ }
+
+ public void Clear()
+ {
+ list.Clear();
+ }
+
+ public bool Contains(T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ return list.Contains(item);
+ }
+
+ public void CopyTo(T[] array, int arrayIndex)
+ {
+ list.CopyTo(array);
+ }
+
+ public bool Remove(T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ return list.Remove(item);
+ }
+
+ public int Count { get { return list.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");
+ }
+ // We know that all the values will be valid, because it's a RepeatedField.
+ list.AddRange(values);
+ }
+
+ public void Add(IEnumerable<T> values)
+ {
+ if (values == null)
+ {
+ throw new ArgumentNullException("values");
+ }
+ foreach (T item in values)
+ {
+ Add(item);
+ }
+ }
+
+ // TODO(jonskeet): Create our own mutable struct for this, rather than relying on List<T>.
+ public List<T>.Enumerator GetEnumerator()
+ {
+ return list.GetEnumerator();
+ }
+
+ IEnumerator<T> IEnumerable<T>.GetEnumerator()
+ {
+ return list.GetEnumerator();
+ }
+
+ public override bool Equals(object obj)
+ {
+ return Equals(obj as RepeatedField<T>);
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 23;
+ foreach (T item in this)
+ {
+ hash = hash * 31 + item.GetHashCode();
+ }
+ return hash;
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ 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(this[i], other[i]))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public int IndexOf(T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ return list.IndexOf(item);
+ }
+
+ public void Insert(int index, T item)
+ {
+ if (item == null)
+ {
+ throw new ArgumentNullException("item");
+ }
+ list.Insert(index, item);
+ }
+
+ public void RemoveAt(int index)
+ {
+ list.RemoveAt(index);
+ }
+
+ public T this[int index]
+ {
+ get { return list[index]; }
+ set
+ {
+ if (value == null)
+ {
+ throw new ArgumentNullException("value");
+ }
+ list[index] = value;
+ }
+ }
+ }
+}
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;
+ }
+ */
+ }
+}