OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
770 if (order > 0) { | 770 if (order > 0) { |
771 a[j + 1] = tmp; | 771 a[j + 1] = tmp; |
772 } else { | 772 } else { |
773 break; | 773 break; |
774 } | 774 } |
775 } | 775 } |
776 a[j + 1] = element; | 776 a[j + 1] = element; |
777 } | 777 } |
778 }; | 778 }; |
779 | 779 |
780 var GetThirdIndex = function(a, from, to) { | |
781 var t_array = []; | |
danno
2012/06/19 14:52:17
How about creating an InternalArray of the appropr
Erik Corry
2012/06/19 18:36:29
OK I tried that and it ended badly. I have to be
danno
2012/06/19 19:55:55
Nah, it's fine. I'd rather have the readable code
| |
782 // Use both 'from' and 'to' to determine the pivot candidates. | |
783 var increment = 200 + ((to - from) & 15); | |
784 for (var i = from + 1; i < to - 1; i += increment) { | |
785 t_array.push([i, a[i]]); | |
786 } | |
787 t_array.sort(function(a, b) { | |
788 return %_CallFunction(receiver, a[1], b[1], comparefn) } ); | |
789 var third_index = t_array[t_array.length >> 1][0]; | |
790 return third_index; | |
791 } | |
792 | |
780 var QuickSort = function QuickSort(a, from, to) { | 793 var QuickSort = function QuickSort(a, from, to) { |
794 var third_index = 0; | |
781 while (true) { | 795 while (true) { |
782 // Insertion sort is faster for short arrays. | 796 // Insertion sort is faster for short arrays. |
783 if (to - from <= 10) { | 797 if (to - from <= 10) { |
784 InsertionSort(a, from, to); | 798 InsertionSort(a, from, to); |
785 return; | 799 return; |
786 } | 800 } |
801 if (to - from > 1000) { | |
802 third_index = GetThirdIndex(a, from, to); | |
danno
2012/06/19 14:52:17
It still seems to be the case that you can careful
Erik Corry
2012/06/19 18:36:29
I think we don't as long as it can't happen accide
| |
803 } else { | |
804 third_index = from + ((to - from) >> 1); | |
805 } | |
787 // Find a pivot as the median of first, last and middle element. | 806 // Find a pivot as the median of first, last and middle element. |
788 var v0 = a[from]; | 807 var v0 = a[from]; |
789 var v1 = a[to - 1]; | 808 var v1 = a[to - 1]; |
790 var middle_index = from + ((to - from) >> 1); | 809 var v2 = a[third_index]; |
791 var v2 = a[middle_index]; | |
792 var c01 = %_CallFunction(receiver, v0, v1, comparefn); | 810 var c01 = %_CallFunction(receiver, v0, v1, comparefn); |
793 if (c01 > 0) { | 811 if (c01 > 0) { |
794 // v1 < v0, so swap them. | 812 // v1 < v0, so swap them. |
795 var tmp = v0; | 813 var tmp = v0; |
796 v0 = v1; | 814 v0 = v1; |
797 v1 = tmp; | 815 v1 = tmp; |
798 } // v0 <= v1. | 816 } // v0 <= v1. |
799 var c02 = %_CallFunction(receiver, v0, v2, comparefn); | 817 var c02 = %_CallFunction(receiver, v0, v2, comparefn); |
800 if (c02 >= 0) { | 818 if (c02 >= 0) { |
801 // v2 <= v0 <= v1. | 819 // v2 <= v0 <= v1. |
(...skipping 10 matching lines...) Expand all Loading... | |
812 v1 = v2; | 830 v1 = v2; |
813 v2 = tmp; | 831 v2 = tmp; |
814 } | 832 } |
815 } | 833 } |
816 // v0 <= v1 <= v2 | 834 // v0 <= v1 <= v2 |
817 a[from] = v0; | 835 a[from] = v0; |
818 a[to - 1] = v2; | 836 a[to - 1] = v2; |
819 var pivot = v1; | 837 var pivot = v1; |
820 var low_end = from + 1; // Upper bound of elements lower than pivot. | 838 var low_end = from + 1; // Upper bound of elements lower than pivot. |
821 var high_start = to - 1; // Lower bound of elements greater than pivot. | 839 var high_start = to - 1; // Lower bound of elements greater than pivot. |
822 a[middle_index] = a[low_end]; | 840 a[third_index] = a[low_end]; |
823 a[low_end] = pivot; | 841 a[low_end] = pivot; |
824 | 842 |
825 // From low_end to i are elements equal to pivot. | 843 // From low_end to i are elements equal to pivot. |
826 // From i to high_start are elements that haven't been compared yet. | 844 // From i to high_start are elements that haven't been compared yet. |
827 partition: for (var i = low_end + 1; i < high_start; i++) { | 845 partition: for (var i = low_end + 1; i < high_start; i++) { |
828 var element = a[i]; | 846 var element = a[i]; |
829 var order = %_CallFunction(receiver, element, pivot, comparefn); | 847 var order = %_CallFunction(receiver, element, pivot, comparefn); |
830 if (order < 0) { | 848 if (order < 0) { |
831 a[i] = a[low_end]; | 849 a[i] = a[low_end]; |
832 a[low_end] = element; | 850 a[low_end] = element; |
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1531 // exposed to user code. | 1549 // exposed to user code. |
1532 // Adding only the functions that are actually used. | 1550 // Adding only the functions that are actually used. |
1533 SetUpLockedPrototype(InternalArray, $Array(), $Array( | 1551 SetUpLockedPrototype(InternalArray, $Array(), $Array( |
1534 "join", getFunction("join", ArrayJoin), | 1552 "join", getFunction("join", ArrayJoin), |
1535 "pop", getFunction("pop", ArrayPop), | 1553 "pop", getFunction("pop", ArrayPop), |
1536 "push", getFunction("push", ArrayPush) | 1554 "push", getFunction("push", ArrayPush) |
1537 )); | 1555 )); |
1538 } | 1556 } |
1539 | 1557 |
1540 SetUpArray(); | 1558 SetUpArray(); |
OLD | NEW |