Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/mcs/class
diff options
context:
space:
mode:
authorMarek Safar <marek.safar@gmail.com>2014-01-14 17:21:55 +0400
committerMarek Safar <marek.safar@gmail.com>2014-01-14 17:25:45 +0400
commitc5eab2d5dc25433c8ce8ad529b8bdfe2dcf8190c (patch)
treee5816224a2492ab0913a2161be750c7c0f63f001 /mcs/class
parentfefea60aa083078e4bd261f9dfffc7e9f43fba4a (diff)
[System] Use generic equality comparer in Find/FindLast methods. Fixes #5245
Diffstat (limited to 'mcs/class')
-rw-r--r--mcs/class/System/System.Collections.Generic/LinkedList.cs56
-rw-r--r--mcs/class/System/Test/System.Collections.Generic/LinkedListTest.cs31
2 files changed, 58 insertions, 29 deletions
diff --git a/mcs/class/System/System.Collections.Generic/LinkedList.cs b/mcs/class/System/System.Collections.Generic/LinkedList.cs
index 4f95b0c54c8..de67153fc0c 100644
--- a/mcs/class/System/System.Collections.Generic/LinkedList.cs
+++ b/mcs/class/System/System.Collections.Generic/LinkedList.cs
@@ -188,18 +188,7 @@ namespace System.Collections.Generic
public bool Contains (T value)
{
- LinkedListNode <T> node = first;
- if (node == null)
- return false;
- do
- {
- if (value.Equals (node.Value))
- return true;
- node = node.forward;
- }
- while (node != first);
-
- return false;
+ return Find (value) != null;
}
public void CopyTo (T [] array, int index)
@@ -225,35 +214,44 @@ namespace System.Collections.Generic
while (node != first);
}
- public LinkedListNode <T> Find (T value)
+ public LinkedListNode<T> Find (T value)
{
- LinkedListNode <T> node = first;
+ var node = first;
if (node == null)
return null;
- do
- {
- if ( (value == null && node.Value == null) ||
- (value != null && value.Equals (node.Value)) )
- return node;
+
+ do {
+ if (value == null) {
+ if (node.Value == null)
+ return node;
+ } else {
+ if (EqualityComparer<T>.Default.Equals (node.Value, value))
+ return node;
+ }
+
node = node.forward;
- }
- while (node != first);
+ } while (node != first);
return null;
}
- public LinkedListNode <T> FindLast (T value)
+ public LinkedListNode<T> FindLast (T value)
{
- LinkedListNode <T> node = first;
+ var node = first;
if (node == null)
return null;
- do
- {
+
+ do {
node = node.back;
- if (value.Equals (node.Value))
- return node;
- }
- while (node != first);
+
+ if (value == null) {
+ if (node.Value == null)
+ return node;
+ } else {
+ if (EqualityComparer<T>.Default.Equals (node.Value, value))
+ return node;
+ }
+ } while (node != first);
return null;
}
diff --git a/mcs/class/System/Test/System.Collections.Generic/LinkedListTest.cs b/mcs/class/System/Test/System.Collections.Generic/LinkedListTest.cs
index f5605744bdd..ad898b102dd 100644
--- a/mcs/class/System/Test/System.Collections.Generic/LinkedListTest.cs
+++ b/mcs/class/System/Test/System.Collections.Generic/LinkedListTest.cs
@@ -12,6 +12,24 @@ namespace MonoTests.System.Collections.Generic
[TestFixture]
public class LinkedListTest
{
+ class EquatableValue : IEquatable<EquatableValue>
+ {
+ public readonly string Value;
+
+ public EquatableValue (string value)
+ {
+ this.Value = value;
+ }
+
+ public bool Equals (EquatableValue other)
+ {
+ if (other == null)
+ return false;
+
+ return string.Equals (Value, other.Value, StringComparison.OrdinalIgnoreCase);
+ }
+ }
+
LinkedList <int> intlist;
LinkedList <string> strings;
@@ -280,6 +298,19 @@ namespace MonoTests.System.Collections.Generic
Assert.AreEqual ("efgh", li.Last.Value);
Assert.AreEqual ("abcd", li.First.Value);
}
+
+ [Test]
+ public void EqualityComparer ()
+ {
+ var list = new LinkedList<EquatableValue> ();
+ var mv = new EquatableValue ("first");
+ list.AddFirst (mv);
+
+ var test = new EquatableValue ("FIRST");
+ Assert.IsTrue (list.Contains (test), "#1");
+ Assert.AreSame (mv, list.Find (test).Value, "#2");
+ Assert.AreSame (mv, list.FindLast (test).Value, "#3");
+ }
}
}