forked from libapps/libapps-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_array.js
More file actions
55 lines (50 loc) · 1.26 KB
/
lib_array.js
File metadata and controls
55 lines (50 loc) · 1.26 KB
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
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Helper functions for (typed) arrays.
*/
/**
* Concatenate an arbitrary number of typed arrays of the same type into a new
* typed array of this type.
*
* @template TYPED_ARRAY
* @param {...!TYPED_ARRAY} arrays
* @return {!TYPED_ARRAY}
*/
export function concatTyped(...arrays) {
let resultLength = 0;
for (const array of arrays) {
resultLength += array.length;
}
const result = new arrays[0].constructor(resultLength);
let pos = 0;
for (const array of arrays) {
result.set(array, pos);
pos += array.length;
}
return result;
}
/**
* Compare two array-like objects entrywise.
*
* @template ARRAY_LIKE
* @param {?ARRAY_LIKE} a The first array to compare.
* @param {?ARRAY_LIKE} b The second array to compare.
* @return {boolean} true if both arrays are null or they agree entrywise;
* false otherwise.
*/
export function compare(a, b) {
if (a === null || b === null) {
return a === null && b === null;
}
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}