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

Side by Side Diff: src/array.js

Issue 6006: Faster 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 630 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // In-place QuickSort algorithm. 650 // In-place QuickSort algorithm.
651 // For short (length <= 22) arrays, insertion sort is used for efficiency.
651 652
652 function Compare(x,y) { 653 function Compare(x,y) {
653 if (IS_UNDEFINED(x)) { 654 if (IS_UNDEFINED(x)) {
654 if (IS_UNDEFINED(y)) return 0; 655 if (IS_UNDEFINED(y)) return 0;
655 return 1; 656 return 1;
656 } 657 }
657 if (IS_UNDEFINED(y)) return -1; 658 if (IS_UNDEFINED(y)) return -1;
658 659
659 if (IS_FUNCTION(comparefn)) { 660 if (IS_FUNCTION(comparefn)) {
660 return comparefn.call(null, x, y); 661 return comparefn.call(null, x, y);
661 } 662 }
662 if (%_IsSmi(x) && %_IsSmi(y)) { 663 if (%_IsSmi(x) && %_IsSmi(y)) {
663 return %SmiLexicographicCompare(x, y); 664 return %SmiLexicographicCompare(x, y);
664 } 665 }
665 x = ToString(x); 666 x = ToString(x);
666 y = ToString(y); 667 y = ToString(y);
667 if (x == y) return 0; 668 if (x == y) return 0;
668 else return x < y ? -1 : 1; 669 else return x < y ? -1 : 1;
669 }; 670 };
670 671
672 function InsertionSort(a, from, to) {
673 for(var i = from + 1; i < to; i++) {
Christian Plesner Hansen 2008/09/30 09:50:56 There should be a space between 'for' and '('.
674 var element = a[i];
675 // place element in a[from..i[
676 // binary search
677 var min = from;
678 var max = i;
679 while(min < max) {
680 var mid = min + ((max - min) >> 1);
681 var order = Compare(a[mid], element);
682 if (order == 0) {
683 min = max = mid;
684 break;
685 }
686 if (order < 0) {
687 min = mid + 1;
688 } else {
689 max = mid;
Christian Plesner Hansen 2008/09/30 09:50:56 I'm not 100% sure but I think this should be max =
690 }
691 }
692 // place element at position min==max.
693 for(var j = min; j < i; j++) {
694 var tmp = a[j];
695 a[j] = element;
696 element = tmp;
697 }
698 a[i] = element;
699 }
700 }
701
671 function QuickSort(a, from, to) { 702 function QuickSort(a, from, to) {
672 if (from >= to - 1) return; 703 // Insertion sort is faster for short arrays.
704 if (to - from <= 22) {
705 InsertionSort(a, from, to);
706 return;
707 }
673 var pivot_index = $floor($random() * (to - from)) + from; 708 var pivot_index = $floor($random() * (to - from)) + from;
674 var pivot = a[pivot_index]; 709 var pivot = a[pivot_index];
675 // Issue 95: Keep the pivot element out of the comparisons to avoid 710 // Issue 95: Keep the pivot element out of the comparisons to avoid
676 // infinite recursion if comparefn(pivot, pivot) != 0. 711 // infinite recursion if comparefn(pivot, pivot) != 0.
677 a[pivot_index] = a[to - 1]; 712 a[pivot_index] = a[to - 1];
678 a[to - 1] = pivot; 713 a[to - 1] = pivot;
679 var low_end = from; // Upper bound of the elements lower than pivot. 714 var low_end = from; // Upper bound of the elements lower than pivot.
680 var high_start = to - 1; // Lower bound of the elements greater than pivot. 715 var high_start = to - 1; // Lower bound of the elements greater than pivot.
681 for (var i = from; i < high_start; ) { 716 for (var i = from; i < high_start; ) {
682 var element = a[i]; 717 var element = a[i];
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
911 ArrayEvery: 1, 946 ArrayEvery: 1,
912 ArrayMap: 1, 947 ArrayMap: 1,
913 ArrayIndexOf: 1, 948 ArrayIndexOf: 1,
914 ArrayLastIndexOf: 1, 949 ArrayLastIndexOf: 1,
915 ArrayPush: 1 950 ArrayPush: 1
916 }); 951 });
917 }; 952 };
918 953
919 954
920 SetupArray(); 955 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