using System;
using System.Collections;
using System.Diagnostics;
+using System.Runtime.Serialization;
+using System.Security.Permissions;
namespace System.Collections.Generic
{
[Serializable]
[DebuggerDisplay ("Count={Count}")]
[DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
- public class SortedDictionary<TKey,TValue> : IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable
+ public class SortedDictionary<TKey,TValue> : IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable, ISerializable
{
class Node : RBTree.Node {
public TKey key;
}
}
+ [Serializable]
class NodeHelper : RBTree.INodeHelper<TKey> {
public IComparer<TKey> cmp;
foreach (KeyValuePair<TKey, TValue> entry in dic)
Add (entry.Key, entry.Value);
}
+
+ protected SortedDictionary (SerializationInfo info, StreamingContext context)
+ {
+ hlp = (NodeHelper)info.GetValue("Helper", typeof(NodeHelper));
+ tree = new RBTree (hlp);
+
+ KeyValuePair<TKey, TValue> [] data = (KeyValuePair<TKey, TValue>[])info.GetValue("KeyValuePairs", typeof(KeyValuePair<TKey, TValue>[]));
+ foreach (KeyValuePair<TKey, TValue> entry in data)
+ Add(entry.Key, entry.Value);
+ }
+
#endregion
#region PublicProperty
return n != null;
}
+ [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
+ public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
+ {
+ if (info == null)
+ throw new ArgumentNullException ("info");
+
+ KeyValuePair<TKey, TValue> [] data = new KeyValuePair<TKey,TValue> [Count];
+ CopyTo (data, 0);
+ info.AddValue ("KeyValuePairs", data);
+ info.AddValue ("Helper", hlp);
+ }
+
#endregion
#region PrivateMethod
{
TValue value;
return TryGetValue (item.Key, out value) &&
- EqualityComparer<TValue>.Default.Equals (item.Value, value) &&
+ EqualityComparer<TValue>.Default.Equals (item.Value, value) &&
Remove (item.Key);
}
IEnumerator IEnumerable.GetEnumerator ()
{
- return new Enumerator (_dic);
+ return new Enumerator (_dic);
}
public struct Enumerator : IEnumerator<TValue>,IEnumerator, IDisposable
IEnumerator IEnumerable.GetEnumerator ()
{
- return new Enumerator (_dic);
+ return new Enumerator (_dic);
}
public struct Enumerator : IEnumerator<TKey>, IEnumerator, IDisposable
#if NET_2_0
using System;
+using System.IO;
using System.Collections;
using System.Collections.Generic;
-using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
namespace MonoTests.System.Collections.Generic
{
[TestFixture]
- public class SortedDictionaryTest
+ public class SortedDictionaryTest
{
[Test]
public void CtorNullComparer ()
((IDisposable) e4).Dispose ();
Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
}
+
+ // Serialize a dictionary out and deserialize it back in again
+ SortedDictionary<int, string> Roundtrip(SortedDictionary<int, string> dic)
+ {
+ BinaryFormatter bf = new BinaryFormatter ();
+ MemoryStream stream = new MemoryStream ();
+ bf.Serialize (stream, dic);
+ stream.Position = 0;
+ return (SortedDictionary<int, string>)bf.Deserialize (stream);
+ }
+
+ [Test]
+ public void Serialize()
+ {
+ SortedDictionary<int, string> test = new SortedDictionary<int, string>();
+ test.Add(1, "a");
+ test.Add(3, "c");
+ test.Add(2, "b");
+
+ SortedDictionary<int, string> result = Roundtrip(test);
+ Assert.AreEqual(3, result.Count);
+
+ Assert.AreEqual("a", result[1]);
+ Assert.AreEqual("b", result[2]);
+ Assert.AreEqual("c", result[3]);
+ }
+
+ [Test]
+ public void SerializeReverseComparer()
+ {
+ SortedDictionary<int,string> test =
+ new SortedDictionary<int,string> (
+ ReverseComparer<int>.Instance);
+
+ test.Add (1, "A");
+ test.Add (3, "B");
+ test.Add (2, "C");
+
+ SortedDictionary<int,string> result = Roundtrip (test);
+
+ SortedDictionary<int,string>.Enumerator e = result.GetEnumerator ();
+ Assert.IsTrue (e.MoveNext (), "#1");
+ Assert.AreEqual ("B", e.Current.Value, "#2");
+ Assert.IsTrue (e.MoveNext (), "#3");
+ Assert.AreEqual ("C", e.Current.Value, "#4");
+ Assert.IsTrue (e.MoveNext (), "#5");
+ Assert.AreEqual ("A", e.Current.Value, "#6");
+ Assert.IsFalse (e.MoveNext (), "#7");
+ }
}
+ [Serializable]
class ReverseComparer<T> : IComparer<T>
{
static ReverseComparer<T> instance = new ReverseComparer<T> ();