Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 710 * @param {!Array.<T>} array2 | 710 * @param {!Array.<T>} array2 |
| 711 * @param {function(T,T):number} comparator | 711 * @param {function(T,T):number} comparator |
| 712 * @return {!Array.<T>} | 712 * @return {!Array.<T>} |
| 713 * @template T | 713 * @template T |
| 714 */ | 714 */ |
| 715 function mergeOrIntersect(array1, array2, comparator, mergeNotIntersect) | 715 function mergeOrIntersect(array1, array2, comparator, mergeNotIntersect) |
| 716 { | 716 { |
| 717 var result = []; | 717 var result = []; |
| 718 var i = 0; | 718 var i = 0; |
| 719 var j = 0; | 719 var j = 0; |
| 720 while (i < array1.length || j < array2.length) { | 720 while (i < array1.length && j < array2.length) { |
| 721 if (i === array1.length) { | 721 var compareValue = comparator(array1[i], array2[j]); |
| 722 result = result.concat(array2.slice(j)); | 722 if (mergeNotIntersect || !compareValue) |
| 723 j = array2.length; | 723 result.push(compareValue <= 0 ? array1[i] : array2[j]); |
| 724 } else if (j === array2.length) { | 724 if (compareValue <= 0) |
| 725 result = result.concat(array1.slice(i)); | 725 i++; |
| 726 i = array1.length; | 726 if (compareValue >= 0) |
| 727 } else { | 727 j++; |
| 728 var compareValue = comparator(array1[i], array2[j]) | 728 } |
| 729 if (compareValue < 0) { | 729 if (mergeNotIntersect) { |
| 730 if (mergeNotIntersect) | 730 result = result.concat(array1.slice(i)); |
|
pfeldman
2014/02/14 16:02:17
Isn't this expensive? Both concat and slice are cr
alph
2014/02/14 16:31:37
Indeed. Fixed.
| |
| 731 result.push(array1[i]); | 731 result = result.concat(array2.slice(j)); |
| 732 ++i; | |
| 733 } else if (compareValue > 0) { | |
| 734 if (mergeNotIntersect) | |
| 735 result.push(array2[j]); | |
| 736 ++j; | |
| 737 } else { | |
| 738 result.push(array1[i]); | |
| 739 ++i; | |
| 740 ++j; | |
| 741 } | |
| 742 } | |
| 743 } | 732 } |
| 744 return result; | 733 return result; |
| 745 } | 734 } |
| 746 | 735 |
| 747 Object.defineProperty(Array.prototype, "intersectOrdered", | 736 Object.defineProperty(Array.prototype, "intersectOrdered", |
| 748 { | 737 { |
| 749 /** | 738 /** |
| 750 * @param {!Array.<T>} array | 739 * @param {!Array.<T>} array |
| 751 * @param {function(T,T):number} comparator | 740 * @param {function(T,T):number} comparator |
| 752 * @return {!Array.<T>} | 741 * @return {!Array.<T>} |
| (...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1534 this._outgoingCallback(); | 1523 this._outgoingCallback(); |
| 1535 } | 1524 } |
| 1536 } | 1525 } |
| 1537 | 1526 |
| 1538 /** | 1527 /** |
| 1539 * @param {*} value | 1528 * @param {*} value |
| 1540 */ | 1529 */ |
| 1541 function suppressUnused(value) | 1530 function suppressUnused(value) |
| 1542 { | 1531 { |
| 1543 } | 1532 } |
| OLD | NEW |