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 631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
642 | 642 |
643 function ArraySort(comparefn) { | 643 function ArraySort(comparefn) { |
644 // In-place QuickSort algorithm. | 644 // In-place QuickSort algorithm. |
645 // For short (length <= 22) arrays, insertion sort is used for efficiency. | 645 // For short (length <= 22) arrays, insertion sort is used for efficiency. |
646 | 646 |
647 var global_receiver; | 647 var global_receiver; |
648 | 648 |
649 function InsertionSortWithFunc(a, from, to) { | 649 function InsertionSortWithFunc(a, from, to) { |
650 for (var i = from + 1; i < to; i++) { | 650 for (var i = from + 1; i < to; i++) { |
651 var element = a[i]; | 651 var element = a[i]; |
652 // place element in a[from..i[ | 652 for (var j = i - 1; j >= from; j--) { |
653 // binary search | 653 var tmp = a[j]; |
654 var min = from; | 654 var order = %_CallFunction(global_receiver, tmp, element, comparefn); |
655 var max = i; | 655 if (order > 0) { |
656 // The search interval is a[min..max[ | 656 a[j + 1] = tmp; |
657 while (min < max) { | 657 } else { |
658 var mid = min + ((max - min) >> 1); | |
659 var order = %_CallFunction(global_receiver, a[mid], element, comparefn); | |
660 if (order == 0) { | |
661 min = max = mid; | |
662 break; | 658 break; |
663 } | 659 } |
664 if (order < 0) { | |
665 min = mid + 1; | |
666 } else { | |
667 max = mid; | |
668 } | |
669 } | 660 } |
670 // place element at position min==max. | 661 a[j + 1] = element; |
671 for (var j = i; j > min; j--) { | |
672 a[j] = a[j - 1]; | |
673 } | |
674 a[min] = element; | |
675 } | 662 } |
676 } | 663 } |
677 | 664 |
678 function QuickSortWithFunc(a, from, to) { | 665 function QuickSortWithFunc(a, from, to) { |
679 // Insertion sort is faster for short arrays. | 666 // Insertion sort is faster for short arrays. |
680 if (to - from <= 22) { | 667 if (to - from <= 22) { |
681 InsertionSortWithFunc(a, from, to); | 668 InsertionSortWithFunc(a, from, to); |
682 return; | 669 return; |
683 } | 670 } |
684 var pivot_index = $floor($random() * (to - from)) + from; | 671 var pivot_index = $floor($random() * (to - from)) + from; |
(...skipping 20 matching lines...) Expand all Loading... | |
705 a[high_start] = element; | 692 a[high_start] = element; |
706 } else { // order == 0 | 693 } else { // order == 0 |
707 i++; | 694 i++; |
708 } | 695 } |
709 } | 696 } |
710 QuickSortWithFunc(a, from, low_end); | 697 QuickSortWithFunc(a, from, low_end); |
711 QuickSortWithFunc(a, high_start, to); | 698 QuickSortWithFunc(a, high_start, to); |
712 } | 699 } |
713 | 700 |
714 function Compare(x,y) { | 701 function Compare(x,y) { |
715 // Assume the comparefn, if any, is a consistent comparison function. | |
716 // If it isn't, we are allowed arbitrary behavior by ECMA 15.4.4.11. | |
717 if (x === y) return 0; | 702 if (x === y) return 0; |
718 if (%_IsSmi(x) && %_IsSmi(y)) { | 703 if (%_IsSmi(x) && %_IsSmi(y)) { |
719 return %SmiLexicographicCompare(x, y); | 704 return %SmiLexicographicCompare(x, y); |
720 } | 705 } |
721 x = ToString(x); | 706 x = ToString(x); |
722 y = ToString(y); | 707 y = ToString(y); |
723 if (x == y) return 0; | 708 if (x == y) return 0; |
724 else return x < y ? -1 : 1; | 709 else return x < y ? -1 : 1; |
725 }; | 710 }; |
726 | 711 |
727 function InsertionSort(a, from, to) { | 712 function InsertionSort(a, from, to) { |
728 for (var i = from + 1; i < to; i++) { | 713 for (var i = from + 1; i < to; i++) { |
729 var element = a[i]; | 714 var element = a[i]; |
730 // Pre-convert the element to a string for comparison if we know | |
731 // it will happen on each compare anyway. | |
732 var key = %_IsSmi(element) ? element : ToString(element); | 715 var key = %_IsSmi(element) ? element : ToString(element); |
Lasse Reichstein
2010/04/12 07:35:42
I'm very much attached to this line, but on the ot
antonm
2010/04/12 15:24:25
Sure. Sending to golem in no time.
| |
733 // place element in a[from..i[ | 716 for (var j = i - 1; j >= from; j--) { |
734 // binary search | 717 var tmp = a[j]; |
735 var min = from; | 718 var order = Compare(tmp, key); |
736 var max = i; | 719 if (order > 0) { |
737 // The search interval is a[min..max[ | 720 a[j + 1] = tmp; |
738 while (min < max) { | 721 } else { |
739 var mid = min + ((max - min) >> 1); | |
740 var order = Compare(a[mid], key); | |
741 if (order == 0) { | |
742 min = max = mid; | |
743 break; | 722 break; |
744 } | 723 } |
745 if (order < 0) { | |
746 min = mid + 1; | |
747 } else { | |
748 max = mid; | |
749 } | |
750 } | 724 } |
751 // place element at position min==max. | 725 a[j + 1] = element; |
752 for (var j = i; j > min; j--) { | |
753 a[j] = a[j - 1]; | |
754 } | |
755 a[min] = element; | |
756 } | 726 } |
757 } | 727 } |
758 | 728 |
759 function QuickSort(a, from, to) { | 729 function QuickSort(a, from, to) { |
760 // Insertion sort is faster for short arrays. | 730 // Insertion sort is faster for short arrays. |
761 if (to - from <= 22) { | 731 if (to - from <= 22) { |
762 InsertionSort(a, from, to); | 732 InsertionSort(a, from, to); |
763 return; | 733 return; |
764 } | 734 } |
765 var pivot_index = $floor($random() * (to - from)) + from; | 735 var pivot_index = $floor($random() * (to - from)) + from; |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1211 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1181 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
1212 "reduce", getFunction("reduce", ArrayReduce, 1), | 1182 "reduce", getFunction("reduce", ArrayReduce, 1), |
1213 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1183 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
1214 )); | 1184 )); |
1215 | 1185 |
1216 %FinishArrayPrototypeSetup($Array.prototype); | 1186 %FinishArrayPrototypeSetup($Array.prototype); |
1217 } | 1187 } |
1218 | 1188 |
1219 | 1189 |
1220 SetupArray(); | 1190 SetupArray(); |
OLD | NEW |