| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 } else { | 670 } else { |
| 671 break; | 671 break; |
| 672 } | 672 } |
| 673 } | 673 } |
| 674 a[j + 1] = element; | 674 a[j + 1] = element; |
| 675 } | 675 } |
| 676 } | 676 } |
| 677 | 677 |
| 678 function QuickSort(a, from, to) { | 678 function QuickSort(a, from, to) { |
| 679 // Insertion sort is faster for short arrays. | 679 // Insertion sort is faster for short arrays. |
| 680 if (to - from <= 22) { | 680 if (to - from <= 10) { |
| 681 InsertionSort(a, from, to); | 681 InsertionSort(a, from, to); |
| 682 return; | 682 return; |
| 683 } | 683 } |
| 684 var pivot_index = $floor($random() * (to - from)) + from; | 684 // Find a pivot as the median of first, last and middle element. |
| 685 var pivot = a[pivot_index]; | 685 var v0 = a[from]; |
| 686 // Issue 95: Keep the pivot element out of the comparisons to avoid | 686 var v1 = a[to - 1]; |
| 687 // infinite recursion if comparefn(pivot, pivot) != 0. | 687 var middle_index = from + ((to - from) >> 1); |
| 688 %_SwapElements(a, from, pivot_index); | 688 var v2 = a[middle_index]; |
| 689 var low_end = from; // Upper bound of the elements lower than pivot. | 689 var c01 = %_CallFunction(global_receiver, v0, v1, comparefn); |
| 690 var high_start = to; // Lower bound of the elements greater than pivot. | 690 if (c01 > 0) { |
| 691 // v1 < v0, so swap them. |
| 692 var tmp = v0; |
| 693 v0 = v1; |
| 694 v1 = tmp; |
| 695 } // v0 <= v1. |
| 696 var c02 = %_CallFunction(global_receiver, v0, v2, comparefn); |
| 697 if (c02 >= 0) { |
| 698 // v2 <= v0 <= v1. |
| 699 var tmp = v0; |
| 700 v0 = v2; |
| 701 v2 = v1; |
| 702 v1 = tmp; |
| 703 } else { |
| 704 // v0 <= v1 && v0 < v2 |
| 705 var c12 = %_CallFunction(global_receiver, v1, v2, comparefn); |
| 706 if (c12 > 0) { |
| 707 // v0 <= v2 < v1 |
| 708 var tmp = v1; |
| 709 v1 = v2; |
| 710 v2 = tmp; |
| 711 } |
| 712 } |
| 713 // v0 <= v1 <= v2 |
| 714 a[from] = v0; |
| 715 a[to - 1] = v2; |
| 716 var pivot = v1; |
| 717 var low_end = from + 1; // Upper bound of elements lower than pivot. |
| 718 var high_start = to - 1; // Lower bound of elements greater than pivot. |
| 719 a[middle_index] = a[low_end]; |
| 720 a[low_end] = pivot; |
| 721 |
| 691 // From low_end to i are elements equal to pivot. | 722 // From low_end to i are elements equal to pivot. |
| 692 // From i to high_start are elements that haven't been compared yet. | 723 // From i to high_start are elements that haven't been compared yet. |
| 693 for (var i = from + 1; i < high_start; ) { | 724 partition: for (var i = low_end + 1; i < high_start; i++) { |
| 694 var element = a[i]; | 725 var element = a[i]; |
| 695 var order = %_CallFunction(global_receiver, element, pivot, comparefn); | 726 var order = %_CallFunction(global_receiver, element, pivot, comparefn); |
| 696 if (order < 0) { | 727 if (order < 0) { |
| 697 %_SwapElements(a, i, low_end); | 728 %_SwapElements(a, i, low_end); |
| 698 i++; | |
| 699 low_end++; | 729 low_end++; |
| 700 } else if (order > 0) { | 730 } else if (order > 0) { |
| 701 high_start--; | 731 do { |
| 732 high_start--; |
| 733 if (high_start == i) break partition; |
| 734 var top_elem = a[high_start]; |
| 735 order = %_CallFunction(global_receiver, top_elem, pivot, comparefn); |
| 736 } while (order > 0); |
| 702 %_SwapElements(a, i, high_start); | 737 %_SwapElements(a, i, high_start); |
| 703 } else { // order == 0 | 738 if (order < 0) { |
| 704 i++; | 739 %_SwapElements(a, i, low_end); |
| 740 low_end++; |
| 741 } |
| 705 } | 742 } |
| 706 } | 743 } |
| 707 QuickSort(a, from, low_end); | 744 QuickSort(a, from, low_end); |
| 708 QuickSort(a, high_start, to); | 745 QuickSort(a, high_start, to); |
| 709 } | 746 } |
| 710 | 747 |
| 711 // Copies elements in the range 0..length from obj's prototype chain | 748 // Copy elements in the range 0..length from obj's prototype chain |
| 712 // to obj itself, if obj has holes. Returns one more than the maximal index | 749 // to obj itself, if obj has holes. Return one more than the maximal index |
| 713 // of a prototype property. | 750 // of a prototype property. |
| 714 function CopyFromPrototype(obj, length) { | 751 function CopyFromPrototype(obj, length) { |
| 715 var max = 0; | 752 var max = 0; |
| 716 for (var proto = obj.__proto__; proto; proto = proto.__proto__) { | 753 for (var proto = obj.__proto__; proto; proto = proto.__proto__) { |
| 717 var indices = %GetArrayKeys(proto, length); | 754 var indices = %GetArrayKeys(proto, length); |
| 718 if (indices.length > 0) { | 755 if (indices.length > 0) { |
| 719 if (indices[0] == -1) { | 756 if (indices[0] == -1) { |
| 720 // It's an interval. | 757 // It's an interval. |
| 721 var proto_length = indices[1]; | 758 var proto_length = indices[1]; |
| 722 for (var i = 0; i < proto_length; i++) { | 759 for (var i = 0; i < proto_length; i++) { |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1209 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
| 1173 "reduce", getFunction("reduce", ArrayReduce, 1), | 1210 "reduce", getFunction("reduce", ArrayReduce, 1), |
| 1174 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1211 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
| 1175 )); | 1212 )); |
| 1176 | 1213 |
| 1177 %FinishArrayPrototypeSetup($Array.prototype); | 1214 %FinishArrayPrototypeSetup($Array.prototype); |
| 1178 } | 1215 } |
| 1179 | 1216 |
| 1180 | 1217 |
| 1181 SetupArray(); | 1218 SetupArray(); |
| OLD | NEW |