Lucene.Net.Core.Support.ListExtensions: Added Sort() methods that first attempt to use List<T>.Sort(), and fallback to CollectionUtil.TimSort() if the cast won't work. This means we don't need to return or cast to List<T> in order to use Sort().
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/1908f17d Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/1908f17d Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/1908f17d Branch: refs/heads/api-work Commit: 1908f17d613ed340a538a68a6de00dc16d2d91e8 Parents: ffa24c2 Author: Shad Storhaug <[email protected]> Authored: Tue Jan 31 14:38:05 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Tue Jan 31 14:38:05 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net.Core/Support/ListExtensions.cs | 111 ++++++++++++++++++++- 1 file changed, 106 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1908f17d/src/Lucene.Net.Core/Support/ListExtensions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Support/ListExtensions.cs b/src/Lucene.Net.Core/Support/ListExtensions.cs index 1a32f0b..d103ed0 100644 --- a/src/Lucene.Net.Core/Support/ListExtensions.cs +++ b/src/Lucene.Net.Core/Support/ListExtensions.cs @@ -41,32 +41,131 @@ namespace Lucene.Net.Support return list; } + /// <summary> + /// If the underlying type is <see cref="List{T}"/>, + /// calls <see cref="List{T}.Sort"/>. If not, + /// uses <see cref="Util.CollectionUtil.TimSort{T}(IList{T})"/> + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list">this <see cref="IList{T}"/></param> + public static void Sort<T>(this IList<T> list) + { + if (list is List<T>) + { + ((List<T>)list).Sort(); + } + else + { + Util.CollectionUtil.TimSort(list); + } + } + + /// <summary> + /// If the underlying type is <see cref="List{T}"/>, + /// calls <see cref="List{T}.Sort(IComparer{T})"/>. If not, + /// uses <see cref="Util.CollectionUtil.TimSort{T}(IList{T}, IComparer{T})"/> + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list">this <see cref="IList{T}"/></param> + /// <param name="comparer">the comparer to use for the sort</param> + public static void Sort<T>(this IList<T> list, IComparer<T> comparer) + { + if (list is List<T>) + { + ((List<T>)list).Sort(comparer); + } + else + { + Util.CollectionUtil.TimSort(list, comparer); + } + } + + /// <summary> + /// If the underlying type is <see cref="List{T}"/>, + /// calls <see cref="List{T}.Sort(IComparer{T})"/>. If not, + /// uses <see cref="Util.CollectionUtil.TimSort{T}(IList{T}, IComparer{T})"/> + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list">this <see cref="IList{T}"/></param> + /// <param name="comparison">the comparison function to use for the sort</param> + public static void Sort<T>(this IList<T> list, Comparison<T> comparison) + { + IComparer<T> comparer = new FunctorComparer<T>(comparison); + Sort(list, comparer); + } + + /// <summary> + /// Sorts the given <see cref="IList{T}"/> using the <see cref="IComparer{T}"/>. + /// This method uses the Tim sort + /// algorithm, but falls back to binary sort for small lists. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list">this <see cref="IList{T}"/></param> public static void TimSort<T>(this IList<T> list) - where T : IComparable<T> { Util.CollectionUtil.TimSort(list); } + /// <summary> + /// Sorts the given <see cref="IList{T}"/> using the <see cref="IComparer{T}"/>. + /// This method uses the Tim sort + /// algorithm, but falls back to binary sort for small lists. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list">this <see cref="IList{T}"/></param> + /// <param name="comparer">The <see cref="IComparer{T}"/> to use for the sort.</param> public static void TimSort<T>(this IList<T> list, IComparer<T> comparer) - where T : IComparable<T> { Util.CollectionUtil.TimSort(list, comparer); } + /// Sorts the given <see cref="IList{T}"/> using the <see cref="IComparer{T}"/>. + /// This method uses the intro sort + /// algorithm, but falls back to insertion sort for small lists. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list">this <see cref="IList{T}"/></param> public static void IntroSort<T>(this IList<T> list) - where T : IComparable<T> { Util.CollectionUtil.IntroSort(list); } + /// <summary> + /// Sorts the given <see cref="IList{T}"/> using the <see cref="IComparer{T}"/>. + /// This method uses the intro sort + /// algorithm, but falls back to insertion sort for small lists. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list">this <see cref="IList{T}"/></param> + /// <param name="comparer">The <see cref="IComparer{T}"/> to use for the sort.</param> public static void IntroSort<T>(this IList<T> list, IComparer<T> comparer) - where T : IComparable<T> { Util.CollectionUtil.IntroSort(list, comparer); } + + #region Nested Type: FunctorComparer<T> + + private sealed class FunctorComparer<T> : IComparer<T> + { + private Comparison<T> comparison; + + public FunctorComparer(Comparison<T> comparison) + { + this.comparison = comparison; + } + + public int Compare(T x, T y) + { + return this.comparison(x, y); + } + } + + #endregion Nested Type: FunctorComparer<T> } - public sealed class SubList<T> : IList<T> + #region SubList<T> + + internal sealed class SubList<T> : IList<T> { private readonly IList<T> list; private readonly int fromIndex; @@ -212,4 +311,6 @@ namespace Lucene.Net.Support } } } + + #endregion SubList<T> } \ No newline at end of file
