Ver código fonte

* System.Linq/Enumerable.cs: LongCount() can be optimized for arrays, and Reverse() can
be implemented in terms of IList w/o needing a temporary List<T> copy.
* Test/System.Linq/EnumerableTest.cs: test Reverse() for non-IList types.
* Test/System.Linq/EnumerableMoreTest.cs: test LongCount() for non-array types.

svn path=/trunk/mcs/; revision=102827

Jonathan Pryor 17 anos atrás
pai
commit
7b4f6d8c73

+ 5 - 0
mcs/class/System.Core/System.Linq/ChangeLog

@@ -1,3 +1,8 @@
+2008-05-08  Jonathan Pryor  <[email protected]>
+
+	* Enumerable.cs: LongCount() can be optimized for arrays, and Reverse() can
+	  be implemented in terms of IList w/o needing a temporary List<T> copy.
+
 2008-05-06  Sasha Kogan  <[email protected]>
 
 	* Queryable.cs (Average, GroupBy, Union): fix signatures to

+ 14 - 0
mcs/class/System.Core/System.Linq/Enumerable.cs

@@ -1066,6 +1066,10 @@ namespace System.Linq
 		{
 			Check.Source (source);
 
+			TSource [] array = source as TSource [];
+			if (array != null)
+				return array.LongLength;
+
 			long counter = 0;
 			using (var enumerator = source.GetEnumerator ())
 				while (enumerator.MoveNext ())
@@ -1682,9 +1686,19 @@ namespace System.Linq
 		{
 			Check.Source (source);
 
+			IList<TSource> list = source as IList<TSource>;
+			if (list != null)
+				return CreateReverseIterator (list);
+
 			return CreateReverseIterator (source);
 		}
 
+		static IEnumerable<TSource> CreateReverseIterator<TSource> (IList<TSource> source)
+		{
+			for (int i = source.Count; i > 0; --i)
+				yield return source [i - 1];
+		}
+
 		static IEnumerable<TSource> CreateReverseIterator<TSource> (IEnumerable<TSource> source)
 		{
 			var list = new List<TSource> (source);

+ 5 - 0
mcs/class/System.Core/Test/System.Linq/ChangeLog

@@ -1,3 +1,8 @@
+2008-05-08  Jonathan Pryor  <[email protected]>
+
+	* EnumerableTest.cs: test Reverse() for non-IList types.
+	* EnumerableMoreTest.cs: test LongCount() for non-array types.
+
 2008-04-30  Jb Evain  <[email protected]>
 
 	* EnumerableTest.cs: tests for average on int and long.

+ 1 - 0
mcs/class/System.Core/Test/System.Linq/EnumerableMoreTest.cs

@@ -590,6 +590,7 @@ namespace MonoTests.System.Linq {
 
 			// LongCount<TSource> ()
 			Assert.AreEqual (5, data.LongCount ());
+			Assert.AreEqual (5, Enumerable.Range (0, 5).LongCount ());
 
 			// LongCount<TSource> (Func<TSource, bool>)
 			Assert.AreEqual (3, data.LongCount (x => x < 5));

+ 1 - 0
mcs/class/System.Core/Test/System.Linq/EnumerableTest.cs

@@ -180,6 +180,7 @@ namespace MonoTests.System.Linq {
 			int [] result = {4, 3, 2, 1, 0};
 
 			AssertAreSame (result, data.Reverse ());
+			AssertAreSame (result, Enumerable.Range (0, 5).Reverse ());
 		}
 
 		[Test]