| OLD | NEW |
| 1 part of dart._internal; | 1 part of dart._internal; |
| 2 | 2 class Sort {static const int _INSERTION_SORT_THRESHOLD = 32; |
| 3 class Sort { | 3 static void sort(List a, int compare(a, b)) { |
| 4 static const int _INSERTION_SORT_THRESHOLD = 32; | 4 _doSort(a, 0, a.length - 1, compare); |
| 5 static void sort(List a, int compare(a, b)) { | 5 } |
| 6 _doSort(a, 0, a.length - 1, compare); | 6 static void sortRange(List a, int from, int to, int compare(a, b)) { |
| 7 } | 7 if ((from < 0) || (to > a.length) || (to < from)) { |
| 8 static void sortRange(List a, int from, int to, int compare(a, b)) { | 8 throw "OutOfRange"; |
| 9 if ((from < 0) || (to > a.length) || (to < from)) { | 9 } |
| 10 throw "OutOfRange"; | 10 _doSort(a, from, to - 1, compare); |
| 11 } | 11 } |
| 12 _doSort(a, from, to - 1, compare); | 12 static void _doSort(List a, int left, int right, int compare(a, b)) { |
| 13 } | 13 if ((right - left) <= _INSERTION_SORT_THRESHOLD) { |
| 14 static void _doSort(List a, int left, int right, int compare(a, b)) { | 14 _insertionSort(a, left, right, compare); |
| 15 if ((right - left) <= _INSERTION_SORT_THRESHOLD) { | 15 } |
| 16 _insertionSort(a, left, right, compare); | 16 else { |
| 17 } else { | 17 _dualPivotQuicksort(a, left, right, compare); |
| 18 _dualPivotQuicksort(a, left, right, compare); | 18 } |
| 19 } | 19 } |
| 20 } | 20 static void _insertionSort(List a, int left, int right, int compare(a, b)) { |
| 21 static void _insertionSort(List a, int left, int right, int compare(a, b)) { | 21 for (int i = left + 1; |
| 22 for (int i = left + 1; i <= right; i++) { | 22 i <= right; |
| 23 var el = a[i]; | 23 i++) { |
| 24 int j = i; | 24 var el = a[i]; |
| 25 while ((j > left) && (compare(a[j - 1], el) > 0)) { | 25 int j = i; |
| 26 a[j] = a[j - 1]; | 26 while ((j > left) && (compare(a[j - 1], el) > 0)) { |
| 27 j--; | 27 a[j] = a[j - 1]; |
| 28 } | 28 j--; |
| 29 a[j] = el; | 29 } |
| 30 } | 30 a[j] = el; |
| 31 } | 31 } |
| 32 static void _dualPivotQuicksort( | 32 } |
| 33 List a, int left, int right, int compare(a, b)) { | 33 static void _dualPivotQuicksort(List a, int left, int right, int compare(a, b))
{ |
| 34 assert(right - left > _INSERTION_SORT_THRESHOLD); | 34 assert (right - left > _INSERTION_SORT_THRESHOLD); int sixth = (right - left +
1) ~/ 6; |
| 35 int sixth = (right - left + 1) ~/ 6; | 35 int index1 = left + sixth; |
| 36 int index1 = left + sixth; | 36 int index5 = right - sixth; |
| 37 int index5 = right - sixth; | 37 int index3 = (left + right) ~/ 2; |
| 38 int index3 = (left + right) ~/ 2; | 38 int index2 = index3 - sixth; |
| 39 int index2 = index3 - sixth; | 39 int index4 = index3 + sixth; |
| 40 int index4 = index3 + sixth; | 40 var el1 = a[index1]; |
| 41 var el1 = a[index1]; | 41 var el2 = a[index2]; |
| 42 var el2 = a[index2]; | 42 var el3 = a[index3]; |
| 43 var el3 = a[index3]; | 43 var el4 = a[index4]; |
| 44 var el4 = a[index4]; | 44 var el5 = a[index5]; |
| 45 var el5 = a[index5]; | 45 if (compare(el1, el2) > 0) { |
| 46 if (compare(el1, el2) > 0) { | 46 var t = el1; |
| 47 var t = el1; | 47 el1 = el2; |
| 48 el1 = el2; | 48 el2 = t; |
| 49 el2 = t; | 49 } |
| 50 } | 50 if (compare(el4, el5) > 0) { |
| 51 if (compare(el4, el5) > 0) { | 51 var t = el4; |
| 52 var t = el4; | 52 el4 = el5; |
| 53 el4 = el5; | 53 el5 = t; |
| 54 el5 = t; | 54 } |
| 55 } | 55 if (compare(el1, el3) > 0) { |
| 56 if (compare(el1, el3) > 0) { | 56 var t = el1; |
| 57 var t = el1; | 57 el1 = el3; |
| 58 el1 = el3; | 58 el3 = t; |
| 59 el3 = t; | 59 } |
| 60 } | 60 if (compare(el2, el3) > 0) { |
| 61 if (compare(el2, el3) > 0) { | 61 var t = el2; |
| 62 var t = el2; | 62 el2 = el3; |
| 63 el2 = el3; | 63 el3 = t; |
| 64 el3 = t; | 64 } |
| 65 } | 65 if (compare(el1, el4) > 0) { |
| 66 if (compare(el1, el4) > 0) { | 66 var t = el1; |
| 67 var t = el1; | 67 el1 = el4; |
| 68 el1 = el4; | 68 el4 = t; |
| 69 el4 = t; | 69 } |
| 70 } | 70 if (compare(el3, el4) > 0) { |
| 71 if (compare(el3, el4) > 0) { | 71 var t = el3; |
| 72 var t = el3; | 72 el3 = el4; |
| 73 el3 = el4; | 73 el4 = t; |
| 74 el4 = t; | 74 } |
| 75 } | 75 if (compare(el2, el5) > 0) { |
| 76 if (compare(el2, el5) > 0) { | 76 var t = el2; |
| 77 var t = el2; | 77 el2 = el5; |
| 78 el2 = el5; | 78 el5 = t; |
| 79 el5 = t; | 79 } |
| 80 } | 80 if (compare(el2, el3) > 0) { |
| 81 if (compare(el2, el3) > 0) { | 81 var t = el2; |
| 82 var t = el2; | 82 el2 = el3; |
| 83 el2 = el3; | 83 el3 = t; |
| 84 el3 = t; | 84 } |
| 85 } | 85 if (compare(el4, el5) > 0) { |
| 86 if (compare(el4, el5) > 0) { | 86 var t = el4; |
| 87 var t = el4; | 87 el4 = el5; |
| 88 el4 = el5; | 88 el5 = t; |
| 89 el5 = t; | 89 } |
| 90 } | 90 var pivot1 = el2; |
| 91 var pivot1 = el2; | 91 var pivot2 = el4; |
| 92 var pivot2 = el4; | 92 a[index1] = el1; |
| 93 a[index1] = el1; | 93 a[index3] = el3; |
| 94 a[index3] = el3; | 94 a[index5] = el5; |
| 95 a[index5] = el5; | 95 a[index2] = a[left]; |
| 96 a[index2] = a[left]; | 96 a[index4] = a[right]; |
| 97 a[index4] = a[right]; | 97 int less = left + 1; |
| 98 int less = left + 1; | 98 int great = right - 1; |
| 99 int great = right - 1; | 99 bool pivots_are_equal = (compare(pivot1, pivot2) == 0); |
| 100 bool pivots_are_equal = (compare(pivot1, pivot2) == 0); | 100 if (pivots_are_equal) { |
| 101 if (pivots_are_equal) { | 101 var pivot = pivot1; |
| 102 var pivot = pivot1; | 102 for (int k = less; |
| 103 for (int k = less; k <= great; k++) { | 103 k <= great; |
| 104 var ak = a[k]; | 104 k++) { |
| 105 int comp = compare(ak, pivot); | 105 var ak = a[k]; |
| 106 if (comp == 0) continue; | 106 int comp = compare(ak, pivot); |
| 107 if (comp < 0) { | 107 if (comp == 0) continue; |
| 108 if (k != less) { | 108 if (comp < 0) { |
| 109 if (k != less) { |
| 110 a[k] = a[less]; |
| 111 a[less] = ak; |
| 112 } |
| 113 less++; |
| 114 } |
| 115 else { |
| 116 while (true) { |
| 117 comp = compare(a[great], pivot); |
| 118 if (comp > 0) { |
| 119 great--; |
| 120 continue; |
| 121 } |
| 122 else if (comp < 0) { |
| 109 a[k] = a[less]; | 123 a[k] = a[less]; |
| 110 a[less] = ak; | 124 a[less++] = a[great]; |
| 111 } | 125 a[great--] = ak; |
| 112 less++; | 126 break; |
| 113 } else { | 127 } |
| 128 else { |
| 129 a[k] = a[great]; |
| 130 a[great--] = ak; |
| 131 break; |
| 132 } |
| 133 } |
| 134 } |
| 135 } |
| 136 } |
| 137 else { |
| 138 for (int k = less; |
| 139 k <= great; |
| 140 k++) { |
| 141 var ak = a[k]; |
| 142 int comp_pivot1 = compare(ak, pivot1); |
| 143 if (comp_pivot1 < 0) { |
| 144 if (k != less) { |
| 145 a[k] = a[less]; |
| 146 a[less] = ak; |
| 147 } |
| 148 less++; |
| 149 } |
| 150 else { |
| 151 int comp_pivot2 = compare(ak, pivot2); |
| 152 if (comp_pivot2 > 0) { |
| 114 while (true) { | 153 while (true) { |
| 115 comp = compare(a[great], pivot); | 154 int comp = compare(a[great], pivot2); |
| 116 if (comp > 0) { | 155 if (comp > 0) { |
| 117 great--; | 156 great--; |
| 118 continue; | 157 if (great < k) break; |
| 119 } else if (comp < 0) { | 158 continue; |
| 120 a[k] = a[less]; | 159 } |
| 121 a[less++] = a[great]; | 160 else { |
| 122 a[great--] = ak; | 161 comp = compare(a[great], pivot1); |
| 123 break; | 162 if (comp < 0) { |
| 124 } else { | 163 a[k] = a[less]; |
| 125 a[k] = a[great]; | 164 a[less++] = a[great]; |
| 126 a[great--] = ak; | 165 a[great--] = ak; |
| 127 break; | 166 } |
| 128 } | 167 else { |
| 129 } | 168 a[k] = a[great]; |
| 130 } | 169 a[great--] = ak; |
| 131 } | 170 } |
| 132 } else { | 171 break; |
| 133 for (int k = less; k <= great; k++) { | 172 } |
| 134 var ak = a[k]; | 173 } |
| 135 int comp_pivot1 = compare(ak, pivot1); | 174 } |
| 136 if (comp_pivot1 < 0) { | 175 } |
| 137 if (k != less) { | 176 } |
| 138 a[k] = a[less]; | 177 } |
| 139 a[less] = ak; | 178 a[left] = a[less - 1]; |
| 140 } | 179 a[less - 1] = pivot1; |
| 141 less++; | 180 a[right] = a[great + 1]; |
| 142 } else { | 181 a[great + 1] = pivot2; |
| 143 int comp_pivot2 = compare(ak, pivot2); | 182 _doSort(a, left, less - 2, compare); |
| 144 if (comp_pivot2 > 0) { | 183 _doSort(a, great + 2, right, compare); |
| 145 while (true) { | 184 if (pivots_are_equal) { |
| 146 int comp = compare(a[great], pivot2); | 185 return;} |
| 147 if (comp > 0) { | 186 if (less < index1 && great > index5) { |
| 148 great--; | 187 while (compare(a[less], pivot1) == 0) { |
| 149 if (great < k) break; | 188 less++; |
| 150 continue; | 189 } |
| 151 } else { | 190 while (compare(a[great], pivot2) == 0) { |
| 152 comp = compare(a[great], pivot1); | 191 great--; |
| 153 if (comp < 0) { | 192 } |
| 154 a[k] = a[less]; | 193 for (int k = less; |
| 155 a[less++] = a[great]; | 194 k <= great; |
| 156 a[great--] = ak; | 195 k++) { |
| 157 } else { | 196 var ak = a[k]; |
| 158 a[k] = a[great]; | 197 int comp_pivot1 = compare(ak, pivot1); |
| 159 a[great--] = ak; | 198 if (comp_pivot1 == 0) { |
| 160 } | 199 if (k != less) { |
| 161 break; | 200 a[k] = a[less]; |
| 162 } | 201 a[less] = ak; |
| 163 } | 202 } |
| 164 } | 203 less++; |
| 165 } | 204 } |
| 166 } | 205 else { |
| 167 } | 206 int comp_pivot2 = compare(ak, pivot2); |
| 168 a[left] = a[less - 1]; | 207 if (comp_pivot2 == 0) { |
| 169 a[less - 1] = pivot1; | 208 while (true) { |
| 170 a[right] = a[great + 1]; | 209 int comp = compare(a[great], pivot2); |
| 171 a[great + 1] = pivot2; | 210 if (comp == 0) { |
| 172 _doSort(a, left, less - 2, compare); | 211 great--; |
| 173 _doSort(a, great + 2, right, compare); | 212 if (great < k) break; |
| 174 if (pivots_are_equal) { | 213 continue; |
| 175 return; | 214 } |
| 176 } | 215 else { |
| 177 if (less < index1 && great > index5) { | 216 comp = compare(a[great], pivot1); |
| 178 while (compare(a[less], pivot1) == 0) { | 217 if (comp < 0) { |
| 179 less++; | 218 a[k] = a[less]; |
| 180 } | 219 a[less++] = a[great]; |
| 181 while (compare(a[great], pivot2) == 0) { | 220 a[great--] = ak; |
| 182 great--; | 221 } |
| 183 } | 222 else { |
| 184 for (int k = less; k <= great; k++) { | 223 a[k] = a[great]; |
| 185 var ak = a[k]; | 224 a[great--] = ak; |
| 186 int comp_pivot1 = compare(ak, pivot1); | 225 } |
| 187 if (comp_pivot1 == 0) { | 226 break; |
| 188 if (k != less) { | 227 } |
| 189 a[k] = a[less]; | 228 } |
| 190 a[less] = ak; | 229 } |
| 191 } | 230 } |
| 192 less++; | 231 } |
| 193 } else { | 232 _doSort(a, less, great, compare); |
| 194 int comp_pivot2 = compare(ak, pivot2); | 233 } |
| 195 if (comp_pivot2 == 0) { | 234 else { |
| 196 while (true) { | 235 _doSort(a, less, great, compare); |
| 197 int comp = compare(a[great], pivot2); | |
| 198 if (comp == 0) { | |
| 199 great--; | |
| 200 if (great < k) break; | |
| 201 continue; | |
| 202 } else { | |
| 203 comp = compare(a[great], pivot1); | |
| 204 if (comp < 0) { | |
| 205 a[k] = a[less]; | |
| 206 a[less++] = a[great]; | |
| 207 a[great--] = ak; | |
| 208 } else { | |
| 209 a[k] = a[great]; | |
| 210 a[great--] = ak; | |
| 211 } | |
| 212 break; | |
| 213 } | |
| 214 } | |
| 215 } | |
| 216 } | |
| 217 } | |
| 218 _doSort(a, less, great, compare); | |
| 219 } else { | |
| 220 _doSort(a, less, great, compare); | |
| 221 } | 236 } |
| 222 } | 237 } |
| 223 } | 238 } |
| OLD | NEW |