-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy patharrayUtils.ts
More file actions
65 lines (58 loc) · 1.95 KB
/
arrayUtils.ts
File metadata and controls
65 lines (58 loc) · 1.95 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
56
57
58
59
60
61
62
63
64
65
export module ArrayUtils {
/**
* Given a specified number of items, returns a array of bucket counts where buckets
* are as evenly divided as possible, and every bucket contains no more than maxPerBucket
* items.
*/
export function createEvenBuckets(numItems: number, maxPerBucket: number): number[] {
if (numItems < 1) {
return [];
}
if (maxPerBucket < 1) {
throw new Error("maxPerBucket cannot be less than 1 but was: " + maxPerBucket);
}
if (numItems <= maxPerBucket) {
return [numItems];
}
// Calculate the smallest divisor where the largest bucket is size <= maxPerBucket
let divisor = Math.ceil(numItems / maxPerBucket);
let integerDivideResult = Math.floor(numItems / divisor);
let remainder = numItems % divisor;
let bucketCounts: number[] = [];
for (let i = 0; i < divisor; i++) {
// If there is a remainder, we need to distribute the extra items among the first few buckets
bucketCounts.push(integerDivideResult + (i < remainder ? 1 : 0));
}
return bucketCounts;
}
/**
* Given an array of items, bucket them into partitions where buckets are as evenly
* divided as possible, and every bucket contains no more than maxPerBucket items.
* Also retains the ordering of the items when partitions are flattened.
*/
export function partition(items: any[], maxPerBucket: number): any[][] {
if (items.length === 0) {
return [];
}
let bucketCounts = ArrayUtils.createEvenBuckets(items.length, maxPerBucket);
let partitions: any[][] = [];
let sliceStart = 0;
for (let i = 0; i < bucketCounts.length; i++) {
let sliceEnd = sliceStart + bucketCounts[i];
partitions.push(items.slice(sliceStart, sliceEnd));
sliceStart = sliceEnd;
}
return partitions;
}
/**
* Given an array, returns true if all items are defined; false otherwise.
*/
export function isArrayComplete(arr: any[]): boolean {
for (let i = 0; i < arr.length; i++) {
if (!arr[i]) {
return false;
}
}
return true;
}
}