Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fix bug #311: On LinkedList.Clear, detach each node instead of dropping them en masse. #161

Merged
merged 1 commit into from over 2 years ago

4 participants

Todd Foster Leszek 'skolima' Ciesielski Jeffrey Stedfast Alan McGovern
Todd Foster

No description provided.

Leszek 'skolima' Ciesielski

This is consistent with LinkedList.Clear having O(n) complexity: http://msdn.microsoft.com/en-us/library/wdcbyw41%28v=vs.80%29.aspx . So +1 from me for this patch.

Jeffrey Stedfast
Owner

Any particular reason you used RemoveLast() instead of RemoveFirst()?

Owner

It seems that you should be using Remove() instead of either RemoveFirst() or RemoveLast(), since you've already checked that first != null.

Owner

Looks like the most performant solution would be:

while (first != null)
Remove (first.back);

I'm guessing you chose RemoveLast() due to RemoveFirst() incurring an extra assignment per loop than RemoveLast().

Correct: I used RemoveLast to possibly avoid an extra assignment.

I chose to use RemoveLast() instead of Remove(first.back) because it was there (DRY). Let it take care of any housekeeping or optimization, including any future code changes.

wwim wwim referenced this pull request from a commit in wwim/mono August 03, 2011
Marek Habersack [runtime, globalization] Fix for Xamarin bug #161. Fix culture info d…
…ata for certain locale short date formats

Modified locales which don't use '/' as the short date separator.
ec0e9d7
Alan McGovern alanmcgovern merged commit d23d8aa into from August 18, 2011
Alan McGovern alanmcgovern closed this August 18, 2011
Duarte Nunes duarten referenced this pull request from a commit in duarten/mono August 03, 2011
Marek Habersack [runtime, globalization] Fix for Xamarin bug #161. Fix culture info d…
…ata for certain locale short date formats

Modified locales which don't use '/' as the short date separator.
ec2cfa6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 17, 2011
Fix bug #311: On LinkedList.Clear, detach each node instead of droppi…
…ng them en masse.
e787a12
This page is out of date. Refresh to see the latest.
5  mcs/class/System/System.Collections.Generic/LinkedList.cs
@@ -182,9 +182,8 @@ public void AddLast (LinkedListNode <T> node)
182 182
 		
183 183
 		public void Clear ()
184 184
 		{
185  
-			count = 0;
186  
-			first = null;
187  
-			version++;
  185
+			while (first != null)
  186
+				RemoveLast();
188 187
 		}
189 188
 		
190 189
 		public bool Contains (T value)
5  mcs/class/System/Test/System.Collections.Generic/LinkedListTest.cs
@@ -84,8 +84,13 @@ public void NonCircularNodeTest ()
84 84
 		[Test]
85 85
 		public void ClearTest ()
86 86
 		{
  87
+			LinkedListNode <int> node = intlist.First;
87 88
 			intlist.Clear ();
  89
+
88 90
 			Assert.AreEqual (0, intlist.Count);
  91
+			Assert.AreEqual (2, node.Value);
  92
+			Assert.IsNull (node.Next);
  93
+			Assert.IsNull (node.Previous);
89 94
 		}
90 95
 
91 96
 		[Test]
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.