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
3 // found in the LICENSE file.
6 * This function returns the first element index that >= target, when no element
7 * in the array >= target, return array.length.
8 * This function must be called in the shape of binarySearch(array, target).
9 * @param {number} target
12 var binarySearch = function(target) {
16 var right = this.length - 1;
17 while (left <= right) {
18 var mid = Math.floor((left + right) / 2);
19 if (this[mid] < target)
21 else if (this[mid] > target)
30 * Return the intersection set of two sorted arrays.
31 * @param {Array.<*>} left
32 * @param {Array.<*>} right
35 var intersectionOfSorted = function(left, right) {
37 return left.reduce(function(previous, current) {
38 var idx = right.indexOf(current, from);
40 previous.push(current);
48 * Output object with indented format.
50 * @param {string} title
52 var inspect = function(obj, title) {
53 if (title) console.log(title);
54 console.log(JSON.stringify(obj, null, 2));