blob: cdb7243d1e5cded2ec076fe3b99f28514d436483 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* This function returns the first element index that >= target, when no element
* in the array >= target, return array.length.
* This function must be called in the shape of binarySearch(array, target).
* @param {number} target
* @return {number}
*/
var binarySearch = function(target) {
'use strict';
var left = 0;
var right = this.length - 1;
while (left <= right) {
var mid = Math.floor((left + right) / 2);
if (this[mid] < target)
left = mid + 1;
else if (this[mid] > target)
right = mid - 1;
else
return mid;
}
return left;
};
/**
* Return the intersection set of two arrays.
* @param {Array.<*>} left
* @param {Array.<*>} right
* @return {Array.<*>}
*/
var intersection = function(left, right) {
return left.reduce(function(previous, current) {
if (right.indexOf(current) != -1)
previous.push(current);
return previous;
}, []);
};
/**
* Return the difference set of left with right.
* Notice: difference(left, right) differentiates with difference(right, left).
* @param {Array.<*>} left
* @param {Array.<*>} right
* @return {Array.<*>}
*/
var difference = function(left, right) {
return left.reduce(function(previous, current) {
if (right.indexOf(current) == -1)
previous.push(current);
return previous;
}, []);
};
|