OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 } | 705 } |
706 x = ToString(x); | 706 x = ToString(x); |
707 y = ToString(y); | 707 y = ToString(y); |
708 if (x == y) return 0; | 708 if (x == y) return 0; |
709 else return x < y ? -1 : 1; | 709 else return x < y ? -1 : 1; |
710 }; | 710 }; |
711 | 711 |
712 function InsertionSort(a, from, to) { | 712 function InsertionSort(a, from, to) { |
713 for (var i = from + 1; i < to; i++) { | 713 for (var i = from + 1; i < to; i++) { |
714 var element = a[i]; | 714 var element = a[i]; |
715 var key = %_IsSmi(element) ? element : ToString(element); | |
716 for (var j = i - 1; j >= from; j--) { | 715 for (var j = i - 1; j >= from; j--) { |
717 var tmp = a[j]; | 716 var tmp = a[j]; |
718 var order = Compare(tmp, key); | 717 var order = Compare(tmp, element); |
719 if (order > 0) { | 718 if (order > 0) { |
720 a[j + 1] = tmp; | 719 a[j + 1] = tmp; |
721 } else { | 720 } else { |
722 break; | 721 break; |
723 } | 722 } |
724 } | 723 } |
725 a[j + 1] = element; | 724 a[j + 1] = element; |
726 } | 725 } |
727 } | 726 } |
728 | 727 |
729 function QuickSort(a, from, to) { | 728 function QuickSort(a, from, to) { |
730 // Insertion sort is faster for short arrays. | 729 // Insertion sort is faster for short arrays. |
731 if (to - from <= 22) { | 730 if (to - from <= 22) { |
732 InsertionSort(a, from, to); | 731 InsertionSort(a, from, to); |
733 return; | 732 return; |
734 } | 733 } |
735 var pivot_index = $floor($random() * (to - from)) + from; | 734 var pivot_index = $floor($random() * (to - from)) + from; |
736 var pivot = a[pivot_index]; | 735 var pivot = a[pivot_index]; |
737 // Pre-convert the element to a string for comparison if we know | |
738 // it will happen on each compare anyway. | |
739 var pivot_key = %_IsSmi(pivot) ? pivot : ToString(pivot); | |
740 // Issue 95: Keep the pivot element out of the comparisons to avoid | 736 // Issue 95: Keep the pivot element out of the comparisons to avoid |
741 // infinite recursion if comparefn(pivot, pivot) != 0. | 737 // infinite recursion if comparefn(pivot, pivot) != 0. |
742 a[pivot_index] = a[from]; | 738 a[pivot_index] = a[from]; |
743 a[from] = pivot; | 739 a[from] = pivot; |
744 var low_end = from; // Upper bound of the elements lower than pivot. | 740 var low_end = from; // Upper bound of the elements lower than pivot. |
745 var high_start = to; // Lower bound of the elements greater than pivot. | 741 var high_start = to; // Lower bound of the elements greater than pivot. |
746 // From low_end to i are elements equal to pivot. | 742 // From low_end to i are elements equal to pivot. |
747 // From i to high_start are elements that haven't been compared yet. | 743 // From i to high_start are elements that haven't been compared yet. |
748 for (var i = from + 1; i < high_start; ) { | 744 for (var i = from + 1; i < high_start; ) { |
749 var element = a[i]; | 745 var element = a[i]; |
750 var order = Compare(element, pivot_key); | 746 var order = Compare(element, pivot); |
751 if (order < 0) { | 747 if (order < 0) { |
752 a[i] = a[low_end]; | 748 a[i] = a[low_end]; |
753 a[low_end] = element; | 749 a[low_end] = element; |
754 i++; | 750 i++; |
755 low_end++; | 751 low_end++; |
756 } else if (order > 0) { | 752 } else if (order > 0) { |
757 high_start--; | 753 high_start--; |
758 a[i] = a[high_start]; | 754 a[i] = a[high_start]; |
759 a[high_start] = element; | 755 a[high_start] = element; |
760 } else { // order == 0 | 756 } else { // order == 0 |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1181 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1177 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
1182 "reduce", getFunction("reduce", ArrayReduce, 1), | 1178 "reduce", getFunction("reduce", ArrayReduce, 1), |
1183 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1179 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
1184 )); | 1180 )); |
1185 | 1181 |
1186 %FinishArrayPrototypeSetup($Array.prototype); | 1182 %FinishArrayPrototypeSetup($Array.prototype); |
1187 } | 1183 } |
1188 | 1184 |
1189 | 1185 |
1190 SetupArray(); | 1186 SetupArray(); |
OLD | NEW |