OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * This function returns the first element index that >= target, when no element | 6 * This function returns the first element index that >= target, when no element |
7 * in the array >= target, return array.length. | 7 * in the array >= target, return array.length. |
8 * This function must be called in the shape of binarySearch(array, target). | 8 * This function must be called in the shape of binarySearch(array, target). |
9 * @param {number} target | 9 * @param {number} target |
10 * @return {number} | 10 * @return {number} |
(...skipping 10 matching lines...) Expand all Loading... |
21 else if (this[mid] > target) | 21 else if (this[mid] > target) |
22 right = mid - 1; | 22 right = mid - 1; |
23 else | 23 else |
24 return mid; | 24 return mid; |
25 } | 25 } |
26 return left; | 26 return left; |
27 }; | 27 }; |
28 | 28 |
29 /** | 29 /** |
30 * Return the intersection set of two sorted arrays. | 30 * Return the intersection set of two sorted arrays. |
31 * @param {Array.<*>} left | 31 * @param {Array<*>} left |
32 * @param {Array.<*>} right | 32 * @param {Array<*>} right |
33 * @return {Array.<*>} | 33 * @return {Array<*>} |
34 */ | 34 */ |
35 var intersectionOfSorted = function(left, right) { | 35 var intersectionOfSorted = function(left, right) { |
36 var from = 0; | 36 var from = 0; |
37 return left.reduce(function(previous, current) { | 37 return left.reduce(function(previous, current) { |
38 var idx = right.indexOf(current, from); | 38 var idx = right.indexOf(current, from); |
39 if (idx != -1) { | 39 if (idx != -1) { |
40 previous.push(current); | 40 previous.push(current); |
41 from = idx; | 41 from = idx; |
42 } | 42 } |
43 return previous; | 43 return previous; |
44 }, []); | 44 }, []); |
45 }; | 45 }; |
46 | 46 |
47 /** | 47 /** |
48 * Output object with indented format. | 48 * Output object with indented format. |
49 * @param {Object} obj | 49 * @param {Object} obj |
50 * @param {string} title | 50 * @param {string} title |
51 */ | 51 */ |
52 var inspect = function(obj, title) { | 52 var inspect = function(obj, title) { |
53 if (title) console.log(title); | 53 if (title) console.log(title); |
54 console.log(JSON.stringify(obj, null, 2)); | 54 console.log(JSON.stringify(obj, null, 2)); |
55 }; | 55 }; |
OLD | NEW |