InternalOrderedSequence.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. for (int i = 0; i < source_list.Count; i++)
  72. keys[i] = key_selector(source_list [i]);
  73. // Then sorts the elements according to the collected
  74. // key values and the selected ordering
  75. indexes = new int [keys.Length];
  76. for (int i = 0; i < indexes.Length; i++)
  77. indexes [i] = i;
  78. QuickSort(indexes, 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. /** QuickSort implementation
  92. Based on implementation found in Wikipedia
  93. http://en.wikipedia.org/wiki/Quicksort_implementations
  94. that was released under the GNU Free Documentation License **/
  95. void QuickSort (int[] array, int left, int right)
  96. {
  97. int lhold = left;
  98. int rhold = right;
  99. Random random = new Random ();
  100. int pivot = random.Next (left, right);
  101. Swap (array, pivot, left);
  102. pivot = left;
  103. left++;
  104. while (right >= left) {
  105. int leftComparison = CompareItems (indexes [left], indexes [pivot]);
  106. int rightComparison = CompareItems (indexes [right], indexes [pivot]);
  107. if (leftComparison >= 0 && rightComparison < 0)
  108. Swap (array, left, right);
  109. else if (leftComparison >= 0)
  110. right--;
  111. else if (rightComparison < 0)
  112. left++;
  113. else {
  114. right--;
  115. left++;
  116. }
  117. }
  118. Swap (array, pivot, right);
  119. pivot = right;
  120. if (pivot > lhold)
  121. QuickSort (array, lhold, pivot);
  122. if (rhold > pivot + 1)
  123. QuickSort (array, pivot + 1, rhold);
  124. }
  125. static void Swap (int[] items, int left, int right)
  126. {
  127. int temp = items [right];
  128. items [right] = items [left];
  129. items [left] = temp;
  130. }
  131. }
  132. }