* AOrderedEnumerable: New abtract base.
* Enumerable.cs, Queryable.cs: Public methods update.
* InternalOrderedSequence.cs: Fixed ThenBy.
svn path=/trunk/mcs/; revision=84559
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
--- /dev/null
+//
+// 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;
+ }
+ }
+}
+
+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.
throw new ArgumentNullException();
return new InternalOrderedSequence<TSource, TKey>(
- source, keySelector, (comparer ?? Comparer<TKey>.Default), false, null);
+ source, keySelector, comparer, false);
}
#endregion
throw new ArgumentNullException();
return new InternalOrderedSequence<TSource, TKey>(
- source, keySelector, (comparer ?? Comparer<TKey>.Default), true, null);
+ source, keySelector, comparer, true);
}
#endregion
public static IEnumerable<TSource> SkipWhile<TSource>(
- IEnumerable<TSource> source,
+ this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
if (source == null || predicate == null)
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
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
+//
+// 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;
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);
// 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]);
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);
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;
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];
+++ /dev/null
-// 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);
- }
-}
-
#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 ();
#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 ();
#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 ();
#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 ();