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 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 this[i++] = %_Arguments(arguments_index++); | 640 this[i++] = %_Arguments(arguments_index++); |
641 } | 641 } |
642 this.length = len - del_count + num_additional_args; | 642 this.length = len - del_count + num_additional_args; |
643 | 643 |
644 // Return the deleted elements. | 644 // Return the deleted elements. |
645 return deleted_elements; | 645 return deleted_elements; |
646 }; | 646 }; |
647 | 647 |
648 | 648 |
649 function ArraySort(comparefn) { | 649 function ArraySort(comparefn) { |
650 // Standard in-place HeapSort algorithm. | 650 // In-place QuickSort algorithm. |
651 | 651 |
652 function Compare(x,y) { | 652 function Compare(x,y) { |
653 if (IS_UNDEFINED(x)) { | 653 if (IS_UNDEFINED(x)) { |
654 if (IS_UNDEFINED(y)) return 0; | 654 if (IS_UNDEFINED(y)) return 0; |
655 return 1; | 655 return 1; |
656 } | 656 } |
657 if (IS_UNDEFINED(y)) return -1; | 657 if (IS_UNDEFINED(y)) return -1; |
658 | 658 |
659 if (IS_FUNCTION(comparefn)) { | 659 if (IS_FUNCTION(comparefn)) { |
660 return comparefn.call(null, x, y); | 660 return comparefn.call(null, x, y); |
661 } | 661 } |
662 if (%_IsSmi(x) && %_IsSmi(y)) { | 662 if (%_IsSmi(x) && %_IsSmi(y)) { |
663 return %SmiLexicographicCompare(x, y); | 663 return %SmiLexicographicCompare(x, y); |
664 } | 664 } |
665 x = ToString(x); | 665 x = ToString(x); |
666 y = ToString(y); | 666 y = ToString(y); |
667 if (x == y) return 0; | 667 if (x == y) return 0; |
668 else return x < y ? -1 : 1; | 668 else return x < y ? -1 : 1; |
669 }; | 669 }; |
670 | 670 |
671 function QuickSort(a, from, to) { | 671 function QuickSort(a, from, to) { |
672 if (from >= to - 1) return; | 672 if (from >= to - 1) return; |
673 var pivot_index = $floor($random() * (to - from)) + from; | 673 var pivot_index = $floor($random() * (to - from)) + from; |
674 var pivot = a[pivot_index]; | 674 var pivot = a[pivot_index]; |
675 a[pivot_index] = a[to - 1]; | 675 var low_end = from; // Upper bound of the elements lower than pivot. |
676 a[to - 1] = pivot; | 676 var high_start = to; // Lower bound of the elements greater than pivot. |
677 var partition = from; | 677 for (var i = from; i < high_start; ) { |
678 for (var i = from; i < to - 1; i++) { | |
679 var element = a[i]; | 678 var element = a[i]; |
680 if (Compare(element, pivot) < 0) { | 679 var order = Compare(element, pivot); |
681 a[i] = a[partition]; | 680 if (order < 0) { |
682 a[partition] = element; | 681 a[i] = a[low_end]; |
683 partition++; | 682 a[low_end] = element; |
| 683 low_end++; |
| 684 i++; |
| 685 } else if (order > 0) { |
| 686 high_start--; |
| 687 a[i] = a[high_start]; |
| 688 a[high_start] = element; |
| 689 } else { // order == 0 |
| 690 i++; |
684 } | 691 } |
685 } | 692 } |
686 a[to - 1] = a[partition]; | 693 QuickSort(a, from, low_end); |
687 a[partition] = pivot; | 694 QuickSort(a, high_start, to); |
688 QuickSort(a, from, partition); | |
689 QuickSort(a, partition + 1, to); | |
690 } | 695 } |
691 | 696 |
692 var old_length = ToUint32(this.length); | 697 var old_length = ToUint32(this.length); |
693 | 698 |
694 %RemoveArrayHoles(this); | 699 %RemoveArrayHoles(this); |
695 | 700 |
696 var length = ToUint32(this.length); | 701 var length = ToUint32(this.length); |
697 QuickSort(this, 0, length); | 702 QuickSort(this, 0, length); |
698 | 703 |
699 // We only changed the length of the this object (in | 704 // We only changed the length of the this object (in |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 ArrayEvery: 1, | 903 ArrayEvery: 1, |
899 ArrayMap: 1, | 904 ArrayMap: 1, |
900 ArrayIndexOf: 1, | 905 ArrayIndexOf: 1, |
901 ArrayLastIndexOf: 1, | 906 ArrayLastIndexOf: 1, |
902 ArrayPush: 1 | 907 ArrayPush: 1 |
903 }); | 908 }); |
904 }; | 909 }; |
905 | 910 |
906 | 911 |
907 SetupArray(); | 912 SetupArray(); |
OLD | NEW |