2007-08-21 Marek Safar <marek.safar@gmail.com>
authorMarek Safar <marek.safar@gmail.com>
Tue, 21 Aug 2007 15:41:42 +0000 (15:41 -0000)
committerMarek Safar <marek.safar@gmail.com>
Tue, 21 Aug 2007 15:41:42 +0000 (15:41 -0000)
* AOrderedEnumerable: New abtract base.

* Enumerable.cs, Queryable.cs: Public methods update.

* InternalOrderedSequence.cs: Fixed ThenBy.

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

mcs/class/System.Core/System.Core.dll.sources
mcs/class/System.Core/System.Linq/AOrderedEnumerable.cs [new file with mode: 0644]
mcs/class/System.Core/System.Linq/ChangeLog
mcs/class/System.Core/System.Linq/Enumerable.cs
mcs/class/System.Core/System.Linq/InternalOrderedSequence.cs
mcs/class/System.Core/System.Linq/OrderedSequence.cs [deleted file]
mcs/class/System.Core/System.Linq/Queryable.cs

index 723062bdccdf7a11b72567e682ba056b818f1bc1..e2d0e41dde7859653f6d0aa9686c6b13ccd87586 100644 (file)
@@ -25,7 +25,7 @@ System.Linq/IQueryable.cs
 System.Linq/IQueryable_T.cs
 System.Linq/Lookup.cs
 System.Linq/ILookup_T.cs
-System.Linq/OrderedSequence.cs
+System.Linq/AOrderedEnumerable.cs
 System.Linq/Queryable.cs
 System.Linq/IQueryProvider.cs
 System.Linq.Expressions/BinaryExpression.cs
diff --git a/mcs/class/System.Core/System.Linq/AOrderedEnumerable.cs b/mcs/class/System.Core/System.Linq/AOrderedEnumerable.cs
new file mode 100644 (file)
index 0000000..a6131b2
--- /dev/null
@@ -0,0 +1,55 @@
+//
+// AOrderedEnumerable.cs
+//
+// Authors:
+//     Marek Safar  <marek.safar@gmail.com>
+//
+// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace System.Linq
+{
+        abstract class AOrderedEnumerable<TElement> : IOrderedEnumerable<TElement>
+        {
+               protected AOrderedEnumerable<TElement> parent;
+                       
+               public abstract IEnumerator<TElement> GetEnumerator ();
+                public abstract IEnumerable<TElement> Sort (IEnumerable<TElement> parentSource);
+                
+               IEnumerator IEnumerable.GetEnumerator ()
+               {
+                       return GetEnumerator ();
+               }
+                        
+               public IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey> (
+                       Func<TElement, TKey> selector, IComparer<TKey> comparer, bool descending)
+               {
+                       parent = new InternalOrderedSequence<TElement, TKey> (this, selector, comparer, descending);
+                       return parent;
+               }
+        }
+}
+
index 6a78b31fefd1c9445d85284bfdd7fcc778baba33..cc3dd68b5e6803068984dae0674a7648e639d2c4 100644 (file)
@@ -1,3 +1,11 @@
+2007-08-21  Marek Safar <marek.safar@gmail.com>
+
+       * AOrderedEnumerable: New abstract base.
+       
+       * Enumerable.cs, Queryable.cs: Public methods update.
+       
+       * InternalOrderedSequence.cs: Fixed ThenBy.
+
 2007-08-21  Marek Safar <marek.safar@gmail.com>
 
        * Enumerable.cs, Queryable.cs: Public methods update.
index 86d813364e3688c0e49eca34d380f0d7ce940721..5d36ea1b159376ac74278e17751229acd73d2f1d 100644 (file)
@@ -1999,7 +1999,7 @@ namespace System.Linq
                 throw new ArgumentNullException();
 
             return new InternalOrderedSequence<TSource, TKey>(
-                    source, keySelector, (comparer ?? Comparer<TKey>.Default), false, null);
+                    source, keySelector, comparer, false);
         }
 
         #endregion
@@ -2020,7 +2020,7 @@ namespace System.Linq
                 throw new ArgumentNullException();
 
             return new InternalOrderedSequence<TSource, TKey>(
-                    source, keySelector, (comparer ?? Comparer<TKey>.Default), true, null);
+                    source, keySelector, comparer, true);
         }
 
         #endregion
@@ -2238,7 +2238,7 @@ namespace System.Linq
 
 
         public static IEnumerable<TSource> SkipWhile<TSource>(
-                IEnumerable<TSource> source,
+                this IEnumerable<TSource> source,
                 Func<TSource, bool> predicate)
         {
             if (source == null || predicate == null)
@@ -2584,11 +2584,10 @@ namespace System.Linq
         public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, 
             Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
         {
-            if (source == null || keySelector == null)
-                throw new ArgumentNullException();
+               if (source == null || keySelector == null)
+                       throw new ArgumentNullException();
 
-            return new InternalOrderedSequence<TSource, TKey>(
-                    source, keySelector, (comparer ?? Comparer<TKey>.Default), false, source);
+               return source.CreateOrderedEnumerable (keySelector, comparer, false);
         }
 
         #endregion
@@ -2605,11 +2604,10 @@ namespace System.Linq
         public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source,
             Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
         {
-            if (source == null || keySelector == null)
-                throw new ArgumentNullException();
+               if (source == null || keySelector == null)
+                       throw new ArgumentNullException();
 
-            return new InternalOrderedSequence<TSource, TKey>(
-                    source, keySelector, (comparer ?? Comparer<TKey>.Default), true, source);
+               return source.CreateOrderedEnumerable (keySelector, comparer, true);
         }
 
         #endregion
index 6059899b088efcf2be1d59693905c2efb4e8cbff..2b0d2a0a6cbc4f42b2db7532744a4e819092b5f0 100644 (file)
@@ -1,3 +1,12 @@
+//
+// InternalOrderedSequence.cs
+//
+// Authors:
+//     Alejandro Serrano "Serras" (trupill@yahoo.es)
+//     Marek Safar  <marek.safar@gmail.com>
+//
+// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
+//
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // "Software"), to deal in the Software without restriction, including
 // distribute, sublicense, and/or sell copies of the Software, and to
 // permit persons to whom the Software is furnished to do so, subject to
 // the following conditions:
-// 
+//
 // The above copyright notice and this permission notice shall be
 // included in all copies or substantial portions of the Software.
-// 
+//
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-//
-// Authors:
-//        Alejandro Serrano "Serras" (trupill@yahoo.es)
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System;
@@ -26,54 +33,39 @@ using System.Collections.Generic;
 
 namespace System.Linq
 {
-        internal class InternalOrderedSequence<TElement, K> : IOrderedEnumerable<TElement>
+       sealed class InternalOrderedSequence<TElement, TKey> : AOrderedEnumerable<TElement>
         {
-                IEnumerable<TElement> source;
-                Func<TElement, K> key_selector;
-                IComparer<K> comparer;
-                bool descending;
-                IOrderedEnumerable<TElement> previous;
+               readonly IEnumerable<TElement> source;
+               readonly Func<TElement, TKey> key_selector;
+               readonly IComparer<TKey> comparer;
+               readonly bool descending;
                 
-                internal InternalOrderedSequence (IEnumerable<TElement> source, Func<TElement, K> keySelector,
-                                                  IComparer<K> comparer, bool descending, IOrderedEnumerable<TElement> previous)
+                internal InternalOrderedSequence (IEnumerable<TElement> source, Func<TElement, TKey> keySelector,
+                                                  IComparer<TKey> comparer, bool descending)
                 {
                         this.source = source;
                         this.key_selector = keySelector;
-                        this.comparer = comparer;
+                        this.comparer = comparer ?? Comparer<TKey>.Default;
                         this.descending = descending;
-                        this.previous = previous;
                 }
                 
-                
-                IEnumerable<TElement> Sort (IEnumerable<TElement> previousList)
+                public override IEnumerable<TElement> Sort (IEnumerable<TElement> parentSource)
                 {
-                        if (previous == null)
-                                return PerformSort (previousList);
-                                
-                       return previous.CreateOrderedEnumerable (key_selector, comparer, descending);
+                       if (parent != null)
+                               return parent.Sort (source);
+                       return PerformSort (parentSource);
                 }
                 
-                public IEnumerator<TElement> GetEnumerator ()
+                public override IEnumerator<TElement> GetEnumerator ()
                 {
-                        return Sort (source).GetEnumerator ();
+                        return PerformSort (source).GetEnumerator ();
                 }
                 
-                IEnumerator IEnumerable.GetEnumerator ()
-                {
-                        return this.GetEnumerator ();
-                }
-                
-               public IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey> (
-                       Func<TElement, TKey> selector, IComparer<TKey> comparer, bool descending)
-               {
-                       throw new NotImplementedException ();
-               }                
-                
                 List<TElement> source_list;
-                K[] keys;
+                TKey[] keys;
                 int[] indexes;
                 
-                private IEnumerable<TElement> PerformSort (IEnumerable<TElement> items)
+                IEnumerable<TElement> PerformSort (IEnumerable<TElement> items)
                 {
                         // It first enumerates source, collecting all elements
                         source_list = new List<TElement> (items);
@@ -84,7 +76,7 @@ namespace System.Linq
                         
                         // Then evaluate the keySelector function for each element,
                         // collecting the key values
-                        keys = new K [source_list.Count];
+                        keys = new TKey [source_list.Count];
                         for (int i = 0; i < source_list.Count; i++)
                                 keys[i] = key_selector(source_list [i]);
                         
@@ -103,14 +95,9 @@ namespace System.Linq
                         return orderedList;
                 }
                 
-                private int CompareItems (int firstIndex, int secondIndex)
+                int CompareItems (int firstIndex, int secondIndex)
                 {
-                        int comparison;
-                        
-                        if (comparer == null)
-                                comparison = ((IComparable<K>)keys [firstIndex]).CompareTo (keys [secondIndex]);
-                        else
-                                comparison =  comparer.Compare (keys [firstIndex], keys [secondIndex]);
+                        int comparison = comparer.Compare (keys [firstIndex], keys [secondIndex]);
                        
                         // If descending, return the opposite comparison
                         return (descending ? -comparison : comparison);
@@ -121,7 +108,7 @@ namespace System.Linq
                     http://en.wikipedia.org/wiki/Quicksort_implementations
                     that was released under the GNU Free Documentation License **/
                
-                private void QuickSort (int[] array, int left, int right)
+                void QuickSort (int[] array, int left, int right)
                 {
                         int lhold = left;
                         int rhold = right;
@@ -154,7 +141,7 @@ namespace System.Linq
                                 QuickSort (array, pivot + 1, rhold);
                 }
                 
-                private void Swap (int[] items, int left, int right)
+                static void Swap (int[] items, int left, int right)
                 {
                         int temp = items [right];
                         items [right] = items [left];
diff --git a/mcs/class/System.Core/System.Linq/OrderedSequence.cs b/mcs/class/System.Core/System.Linq/OrderedSequence.cs
deleted file mode 100644 (file)
index 715c7f1..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-// 
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-// 
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-//
-// Authors:
-//        Alejandro Serrano "Serras" (trupill@yahoo.es)
-//
-
-using System;
-using System.Collections;
-using System.Collections.Generic;
-
-namespace System.Linq
-{
-        public abstract class OrderedSequence<TElement> : IOrderedEnumerable<TElement>
-        {
-                public IEnumerator<TElement> GetEnumerator ()
-                {
-                        return EnumeratorImplementation ();
-                }
-                
-                IEnumerator IEnumerable.GetEnumerator ()
-                {
-                        return EnumeratorImplementation ();
-                }
-                
-               public IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey> (
-                       Func<TElement, TKey> selector, IComparer<TKey> comparer, bool descending)
-               {
-                       throw new NotImplementedException ();
-               }
-                
-               protected abstract IEnumerator<TElement> EnumeratorImplementation ();
-                
-               protected internal abstract IEnumerable<TElement> Sort (IEnumerable<TElement> previousSource);
-        }
-}
-
index 3bae8adb178ebc7db03bd2a736b87db7bc4c965e..7e663509f49127534a8a1854e51f770be925a2c9 100644 (file)
@@ -2214,17 +2214,17 @@ namespace System.Linq
 
                 #region OrderBy
                 
-                public static OrderedSequence<TSource> OrderBy<TSource, K> (
+                public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (
                         this IQueryable<TSource> source,
-                        Func<TSource, K> keySelector)
+                        Func<TSource, TKey> keySelector)
                 {
-                        return OrderBy<TSource, K> (source, keySelector, null);
+                        return OrderBy<TSource, TKey> (source, keySelector, null);
                 }
                 
-                public static OrderedSequence<TSource> OrderBy<TSource, K> (
+                public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (
                         this IQueryable<TSource> source,
-                        Func<TSource, K> keySelector,
-                        IComparer<K> comparer)
+                        Func<TSource, TKey> keySelector,
+                        IComparer<TKey> comparer)
                 {
                         if (source == null || keySelector == null)
                                 throw new ArgumentNullException ();
@@ -2238,17 +2238,17 @@ namespace System.Linq
                 
                 #region OrderByDescending
                 
-                public static OrderedSequence<TSource> OrderByDescending<TSource, K> (
+                public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (
                         this IQueryable<TSource> source,
-                        Func<TSource, K> keySelector)
+                        Func<TSource, TKey> keySelector)
                 {
-                        return OrderByDescending<TSource, K> (source, keySelector, null);
+                        return OrderByDescending<TSource, TKey> (source, keySelector, null);
                 }
                 
-                public static OrderedSequence<TSource> OrderByDescending<TSource, K> (
+                public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (
                         this IQueryable<TSource> source,
-                        Func<TSource, K> keySelector,
-                        IComparer<K> comparer)
+                        Func<TSource, TKey> keySelector,
+                        IComparer<TKey> comparer)
                 {
                         if (source == null || keySelector == null)
                                 throw new ArgumentNullException ();
@@ -2263,17 +2263,17 @@ namespace System.Linq
                 
                 #region ThenBy
                 
-                public static OrderedSequence<TSource> ThenBy<TSource, K> (
-                        this OrderedSequence<TSource> source,
-                        Func<TSource, K> keySelector)
+                public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (
+                        this IOrderedQueryable<TSource> source,
+                        Func<TSource, TKey> keySelector)
                 {
-                        return ThenBy<TSource, K> (source, keySelector, null);
+                        return ThenBy<TSource, TKey> (source, keySelector, null);
                 }
                 
-                public static OrderedSequence<TSource> ThenBy<TSource, K> (
-                        this OrderedSequence<TSource> source,
-                        Func<TSource, K> keySelector,
-                        IComparer<K> comparer)
+                public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (
+                        this IOrderedQueryable<TSource> source,
+                        Func<TSource, TKey> keySelector,
+                        IComparer<TKey> comparer)
                 {
                         if (source == null || keySelector == null)
                                 throw new ArgumentNullException ();
@@ -2287,17 +2287,17 @@ namespace System.Linq
                 
                 #region ThenByDescending
                 
-                public static OrderedSequence<TSource> ThenByDescending<TSource, K> (
-                        this OrderedSequence<TSource> source,
-                        Func<TSource, K> keySelector)
+                public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (
+                        this IOrderedQueryable<TSource> source,
+                        Func<TSource, TKey> keySelector)
                 {
-                        return ThenByDescending<TSource, K> (source, keySelector, null);
+                        return ThenByDescending<TSource, TKey> (source, keySelector, null);
                 }
                 
-                public static OrderedSequence<TSource> ThenByDescending<TSource, K> (
-                        this OrderedSequence<TSource> source,
-                        Func<TSource, K> keySelector,
-                        IComparer<K> comparer)
+                public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (
+                        this IOrderedQueryable<TSource> source,
+                        Func<TSource, TKey> keySelector,
+                        IComparer<TKey> comparer)
                 {
                         if (source == null || keySelector == null)
                                 throw new ArgumentNullException ();