QueryOrderByNode.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. using System;
  27. using System.Threading;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Concurrent;
  31. namespace System.Linq.Parallel.QueryNodes
  32. {
  33. internal class QueryOrderByNode<T> : QueryOrderGuardNode<T>
  34. {
  35. Comparison<T> comparison;
  36. public QueryOrderByNode (QueryBaseNode<T> parent, Comparison<T> comparison)
  37. : base (parent, true)
  38. {
  39. this.comparison = comparison;
  40. }
  41. public QueryOrderByNode (QueryOrderByNode<T> parent, Comparison<T> comparison)
  42. : base (parent.Parent, true)
  43. {
  44. this.comparison = MergeComparison (parent.ComparisonFunc, comparison);
  45. }
  46. public Comparison<T> ComparisonFunc {
  47. get {
  48. return comparison;
  49. }
  50. }
  51. internal override IEnumerable<T> GetSequential ()
  52. {
  53. return Parent.GetSequential ().OrderBy ((e) => e, new ComparisonComparer (comparison));
  54. }
  55. private class ComparisonComparer : IComparer<T>
  56. {
  57. Comparison<T> comparison;
  58. internal ComparisonComparer (Comparison<T> comparison)
  59. {
  60. this.comparison = comparison;
  61. }
  62. int IComparer<T>.Compare (T x, T y)
  63. {
  64. return comparison (x, y);
  65. }
  66. }
  67. internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
  68. {
  69. throw new InvalidOperationException ("Shouldn't be called");
  70. }
  71. internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
  72. {
  73. int partitionCount;
  74. IList<T> aggregList = GetAggregatedList (out partitionCount);
  75. IList<T> result = ParallelQuickSort<T>.Sort (aggregList, comparison);
  76. OrderablePartitioner<T> partitioner = ParallelPartitioner.CreateForStrips (result, 1);
  77. return WrapHelper.Wrap (partitioner.GetOrderablePartitions (options.PartitionCount));
  78. }
  79. IList<T> GetAggregatedList (out int partitionCount)
  80. {
  81. AggregationList<T> result = null;
  82. partitionCount = -1;
  83. ParallelExecuter.ProcessAndAggregate<T, IList<T>> (Parent, () => new List<T> (),
  84. LocalCall,
  85. (ls) => { result = new AggregationList<T> (ls); });
  86. return result;
  87. }
  88. IList<T> LocalCall (IList<T> list, T element)
  89. {
  90. list.Add (element);
  91. return list;
  92. }
  93. static Comparison<T> MergeComparison (Comparison<T> source, Comparison<T> other)
  94. {
  95. return (e1, e2) => {
  96. int result = source (e1, e2);
  97. return result == 0 ? other (e1, e2) : result;
  98. };
  99. }
  100. }
  101. }