Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: src/array.js

Issue 6035: Various minor improvements of sort. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // In-place QuickSort algorithm. 650 // In-place QuickSort algorithm.
651 // For short (length <= 22) arrays, insertion sort is used for efficiency. 651 // For short (length <= 22) arrays, insertion sort is used for efficiency.
652 652
653 var custom_compare = IS_FUNCTION(comparefn);
654
653 function Compare(x,y) { 655 function Compare(x,y) {
654 if (IS_UNDEFINED(x)) { 656 if (custom_compare) {
655 if (IS_UNDEFINED(y)) return 0;
656 return 1;
657 }
658 if (IS_UNDEFINED(y)) return -1;
659
660 if (IS_FUNCTION(comparefn)) {
661 return comparefn.call(null, x, y); 657 return comparefn.call(null, x, y);
662 } 658 }
663 if (%_IsSmi(x) && %_IsSmi(y)) { 659 if (%_IsSmi(x) && %_IsSmi(y)) {
664 return %SmiLexicographicCompare(x, y); 660 return %SmiLexicographicCompare(x, y);
665 } 661 }
666 x = ToString(x); 662 x = ToString(x);
667 y = ToString(y); 663 y = ToString(y);
668 if (x == y) return 0; 664 if (x == y) return 0;
669 else return x < y ? -1 : 1; 665 else return x < y ? -1 : 1;
670 }; 666 };
(...skipping 13 matching lines...) Expand all
684 min = max = mid; 680 min = max = mid;
685 break; 681 break;
686 } 682 }
687 if (order < 0) { 683 if (order < 0) {
688 min = mid + 1; 684 min = mid + 1;
689 } else { 685 } else {
690 max = mid; 686 max = mid;
691 } 687 }
692 } 688 }
693 // place element at position min==max. 689 // place element at position min==max.
694 for (var j = min; j < i; j++) { 690 for (var j = i; j > min; j--) {
695 var tmp = a[j]; 691 a[j] = a[j - 1];
696 a[j] = element;
697 element = tmp;
698 } 692 }
699 a[i] = element; 693 a[min] = element;
700 } 694 }
701 } 695 }
702 696
703 function QuickSort(a, from, to) { 697 function QuickSort(a, from, to) {
704 // Insertion sort is faster for short arrays. 698 // Insertion sort is faster for short arrays.
705 if (to - from <= 22) { 699 if (to - from <= 22) {
706 InsertionSort(a, from, to); 700 InsertionSort(a, from, to);
707 return; 701 return;
708 } 702 }
709 var pivot_index = $floor($random() * (to - from)) + from; 703 var pivot_index = $floor($random() * (to - from)) + from;
710 var pivot = a[pivot_index]; 704 var pivot = a[pivot_index];
711 // Issue 95: Keep the pivot element out of the comparisons to avoid 705 // Issue 95: Keep the pivot element out of the comparisons to avoid
712 // infinite recursion if comparefn(pivot, pivot) != 0. 706 // infinite recursion if comparefn(pivot, pivot) != 0.
(...skipping 23 matching lines...) Expand all
736 high_start++; 730 high_start++;
737 QuickSort(a, from, low_end); 731 QuickSort(a, from, low_end);
738 QuickSort(a, high_start, to); 732 QuickSort(a, high_start, to);
739 } 733 }
740 734
741 var old_length = ToUint32(this.length); 735 var old_length = ToUint32(this.length);
742 736
743 %RemoveArrayHoles(this); 737 %RemoveArrayHoles(this);
744 738
745 var length = ToUint32(this.length); 739 var length = ToUint32(this.length);
740
741 // Move undefined elements to the end of the array.
742 for (var i = 0; i < length; ) {
743 if (IS_UNDEFINED(this[i])) {
744 length--;
745 this[i] = this[length];
746 this[length] = undefined;
747 } else {
748 i++;
749 }
750 }
751
746 QuickSort(this, 0, length); 752 QuickSort(this, 0, length);
747 753
748 // We only changed the length of the this object (in 754 // We only changed the length of the this object (in
749 // RemoveArrayHoles) if it was an array. We are not allowed to set 755 // RemoveArrayHoles) if it was an array. We are not allowed to set
750 // the length of the this object if it is not an array because this 756 // the length of the this object if it is not an array because this
751 // might introduce a new length property. 757 // might introduce a new length property.
752 if (IS_ARRAY(this)) { 758 if (IS_ARRAY(this)) {
753 this.length = old_length; 759 this.length = old_length;
754 } 760 }
755 761
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 ArrayEvery: 1, 953 ArrayEvery: 1,
948 ArrayMap: 1, 954 ArrayMap: 1,
949 ArrayIndexOf: 1, 955 ArrayIndexOf: 1,
950 ArrayLastIndexOf: 1, 956 ArrayLastIndexOf: 1,
951 ArrayPush: 1 957 ArrayPush: 1
952 }); 958 });
953 }; 959 };
954 960
955 961
956 SetupArray(); 962 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698