Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions mcs/class/System/System.Collections.Generic/SortedDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
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;
Expand Down Expand Up @@ -76,6 +78,7 @@ public DictionaryEntry AsDE ()
}
}

[Serializable]
class NodeHelper : RBTree.INodeHelper<TKey> {
public IComparer<TKey> cmp;

Expand Down Expand Up @@ -127,6 +130,17 @@ public SortedDictionary (IDictionary<TKey,TValue> dic, IComparer<TKey> comparer)
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
Expand Down Expand Up @@ -226,6 +240,18 @@ public bool TryGetValue (TKey key, out TValue value)
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
Expand Down Expand Up @@ -280,7 +306,7 @@ bool ICollection<KeyValuePair<TKey,TValue>>.Remove (KeyValuePair<TKey,TValue> it
{
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);
}

Expand Down Expand Up @@ -466,7 +492,7 @@ object ICollection.SyncRoot {

IEnumerator IEnumerable.GetEnumerator ()
{
return new Enumerator (_dic);
return new Enumerator (_dic);
}

public struct Enumerator : IEnumerator<TValue>,IEnumerator, IDisposable
Expand Down Expand Up @@ -601,7 +627,7 @@ object ICollection.SyncRoot {

IEnumerator IEnumerable.GetEnumerator ()
{
return new Enumerator (_dic);
return new Enumerator (_dic);
}

public struct Enumerator : IEnumerator<TKey>, IEnumerator, IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
#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 ()
Expand Down Expand Up @@ -565,8 +566,58 @@ public void ValueEnumerator_Current ()
((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> ();
Expand Down