InternalOrderedSequence.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // InternalOrderedSequence.cs
  3. //
  4. // Authors:
  5. // Alejandro Serrano "Serras" ([email protected])
  6. // Marek Safar <[email protected]>
  7. //
  8. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. namespace System.Linq
  33. {
  34. sealed class InternalOrderedSequence<TElement, TKey> : AOrderedEnumerable<TElement>
  35. {
  36. readonly IEnumerable<TElement> source;
  37. readonly Func<TElement, TKey> key_selector;
  38. readonly IComparer<TKey> comparer;
  39. readonly bool descending;
  40. internal InternalOrderedSequence (IEnumerable<TElement> source, Func<TElement, TKey> keySelector,
  41. IComparer<TKey> comparer, bool descending)
  42. {
  43. this.source = source;
  44. this.key_selector = keySelector;
  45. this.comparer = comparer ?? Comparer<TKey>.Default;
  46. this.descending = descending;
  47. }
  48. public override IEnumerable<TElement> Sort (IEnumerable<TElement> parentSource)
  49. {
  50. if (parent != null)
  51. return parent.Sort (source);
  52. return PerformSort (parentSource);
  53. }
  54. public override IEnumerator<TElement> GetEnumerator ()
  55. {
  56. return PerformSort (source).GetEnumerator ();
  57. }
  58. List<TElement> source_list;
  59. TKey[] keys;
  60. int[] indexes;
  61. IEnumerable<TElement> PerformSort (IEnumerable<TElement> items)
  62. {
  63. // It first enumerates source, collecting all elements
  64. source_list = new List<TElement> (items);
  65. // If the source contains just zero or one element, there's no need to sort
  66. if (source_list.Count <= 1)
  67. return source_list;
  68. // Then evaluate the keySelector function for each element,
  69. // collecting the key values
  70. keys = new TKey [source_list.Count];
  71. indexes = new int [source_list.Count];
  72. for (int i = 0; i < source_list.Count; i++) {
  73. keys [i] = key_selector (source_list [i]);
  74. indexes [i] = i;
  75. }
  76. // Then sorts the elements according to the collected
  77. // key values and the selected ordering
  78. QuickSort(0, indexes.Length - 1);
  79. // Return the values as IEnumerable<TElement>
  80. TElement[] orderedList = new TElement [indexes.Length];
  81. for (int i = 0; i < indexes.Length; i++)
  82. orderedList [i] = source_list [indexes [i]];
  83. return orderedList;
  84. }
  85. int CompareItems (int firstIndex, int secondIndex)
  86. {
  87. int comparison = comparer.Compare (keys [firstIndex], keys [secondIndex]);
  88. // If descending, return the opposite comparison
  89. return (descending ? -comparison : comparison);
  90. }
  91. // We look at the first, middle, and last items in the subarray.
  92. // Then we put the largest on the right side, the smallest on
  93. // the left side, and the median becomes our pivot.
  94. int MedianOfThree (int left, int right)
  95. {
  96. int center = (left + right) / 2;
  97. if (CompareItems (indexes [center], indexes [left]) < 0)
  98. Swap (left, center);
  99. if (CompareItems (indexes [right], indexes [left]) < 0)
  100. Swap (left, right);
  101. if (CompareItems (indexes [right], indexes [center]) < 0)
  102. Swap (center, right);
  103. Swap (center, right - 1);
  104. return indexes [right - 1];
  105. }
  106. void QuickSort (int left, int right)
  107. {
  108. if (left + 3 <= right) {
  109. int l = left, r = right - 1, pivot = MedianOfThree (left, right);
  110. while (true) {
  111. while (CompareItems (indexes [++l], pivot) < 0) {}
  112. while (CompareItems (indexes [--r], pivot) > 0) {}
  113. if (l < r)
  114. Swap (l, r);
  115. else
  116. break;
  117. }
  118. // Restore pivot
  119. Swap (l, right - 1);
  120. // Partition and sort
  121. QuickSort (left, l - 1);
  122. QuickSort (l + 1, right);
  123. } else
  124. // If there are three items in the subarray, insertion sort is better
  125. InsertionSort (left, right);
  126. }
  127. void InsertionSort (int left, int right)
  128. {
  129. for (int i = left + 1; i <= right; i++) {
  130. int j, tmp = indexes [i];
  131. for (j = i; j > left && CompareItems (tmp, indexes [j - 1]) < 0; j--)
  132. indexes [j] = indexes [j - 1];
  133. indexes [j] = tmp;
  134. }
  135. }
  136. void Swap (int left, int right)
  137. {
  138. int temp = indexes [right];
  139. indexes [right] = indexes [left];
  140. indexes [left] = temp;
  141. }
  142. }
  143. }