QueryOrderByNode.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // QueryOrderByNode.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2010 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if NET_4_0
  27. using System;
  28. using System.Threading;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.Concurrent;
  32. namespace System.Linq.Parallel.QueryNodes
  33. {
  34. internal class QueryOrderByNode<T> : QueryOrderGuardNode<T>
  35. {
  36. Comparison<T> comparison;
  37. public QueryOrderByNode (QueryBaseNode<T> parent, Comparison<T> comparison)
  38. : base (parent, true)
  39. {
  40. this.comparison = comparison;
  41. }
  42. public QueryOrderByNode (QueryOrderByNode<T> parent, Comparison<T> comparison)
  43. : base (parent.Parent, true)
  44. {
  45. this.comparison = MergeComparison (parent.ComparisonFunc, comparison);
  46. }
  47. public Comparison<T> ComparisonFunc {
  48. get {
  49. return comparison;
  50. }
  51. }
  52. internal override IEnumerable<T> GetSequential ()
  53. {
  54. return Parent.GetSequential ().OrderBy ((e) => e, new ComparisonComparer (comparison));
  55. }
  56. private class ComparisonComparer : IComparer<T>
  57. {
  58. Comparison<T> comparison;
  59. internal ComparisonComparer (Comparison<T> comparison)
  60. {
  61. this.comparison = comparison;
  62. }
  63. int IComparer<T>.Compare (T x, T y)
  64. {
  65. return comparison (x, y);
  66. }
  67. }
  68. internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
  69. {
  70. throw new InvalidOperationException ("Shouldn't be called");
  71. }
  72. internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
  73. {
  74. int partitionCount;
  75. IList<T> aggregList = GetAggregatedList (out partitionCount);
  76. IList<T> result = ParallelQuickSort<T>.Sort (aggregList, comparison);
  77. OrderablePartitioner<T> partitioner = ParallelPartitioner.CreateForStrips (result, 1);
  78. return WrapHelper.Wrap (partitioner.GetOrderablePartitions (options.PartitionCount));
  79. }
  80. IList<T> GetAggregatedList (out int partitionCount)
  81. {
  82. AggregationList<T> result = null;
  83. partitionCount = -1;
  84. ParallelExecuter.ProcessAndAggregate<T, IList<T>> (Parent, () => new List<T> (),
  85. LocalCall,
  86. (ls) => { result = new AggregationList<T> (ls); });
  87. return result;
  88. }
  89. IList<T> LocalCall (IList<T> list, T element)
  90. {
  91. list.Add (element);
  92. return list;
  93. }
  94. static Comparison<T> MergeComparison (Comparison<T> source, Comparison<T> other)
  95. {
  96. return (e1, e2) => {
  97. int result = source (e1, e2);
  98. return result == 0 ? other (e1, e2) : result;
  99. };
  100. }
  101. }
  102. }
  103. #endif