triangle-utils 1.4.107 → 1.4.108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/UtilsMisc.d.ts +9 -1
- package/dist/src/UtilsMisc.js +26 -3
- package/package.json +1 -1
- package/src/UtilsMisc.ts +28 -3
package/dist/src/UtilsMisc.d.ts
CHANGED
|
@@ -7,7 +7,15 @@ export declare class UtilsMisc {
|
|
|
7
7
|
readonly config: GlobalConfig;
|
|
8
8
|
constructor(config: GlobalConfig);
|
|
9
9
|
safe_run<T>(f: () => Promise<T>): Promise<T | undefined>;
|
|
10
|
-
iterate<T, U>(inputs: T[], f: (input: T, iterator_id: number, index: number) => Promise<U>, num_iterators?: number,
|
|
10
|
+
iterate<T, U>(inputs: T[], f: (input: T, iterator_id: number, index: number) => Promise<U>, num_iterators?: number, options?: {
|
|
11
|
+
num_iterators?: number;
|
|
12
|
+
verbosity?: number | boolean;
|
|
13
|
+
}): Promise<(U | undefined)[]>;
|
|
14
|
+
batch_iterate<T, U>(inputs: T[], f: (inputs: T[], iterator_id: number, index: number) => Promise<U>, options: {
|
|
15
|
+
batch_size?: number;
|
|
16
|
+
num_iterators?: number;
|
|
17
|
+
verbosity?: number | boolean;
|
|
18
|
+
}): Promise<(U | undefined)[]>;
|
|
11
19
|
static wait(duration: number): Promise<unknown>;
|
|
12
20
|
static get_current_time(): string;
|
|
13
21
|
static get_current_milliseconds(): number;
|
package/dist/src/UtilsMisc.js
CHANGED
|
@@ -21,8 +21,8 @@ export class UtilsMisc {
|
|
|
21
21
|
}
|
|
22
22
|
return undefined;
|
|
23
23
|
}
|
|
24
|
-
async iterate(inputs, f, num_iterators = 1,
|
|
25
|
-
const
|
|
24
|
+
async iterate(inputs, f, num_iterators = 1, options = {}) {
|
|
25
|
+
const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
|
|
26
26
|
let index = 0;
|
|
27
27
|
let iterators = [];
|
|
28
28
|
const outputs = new Array(inputs.length).fill(undefined);
|
|
@@ -31,7 +31,7 @@ export class UtilsMisc {
|
|
|
31
31
|
while (index < inputs.length) {
|
|
32
32
|
const local_index = index;
|
|
33
33
|
index += 1;
|
|
34
|
-
if (
|
|
34
|
+
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
35
35
|
console.log(iterator_id + ":" + local_index + "/" + inputs.length);
|
|
36
36
|
}
|
|
37
37
|
const output = await this.safe_run(() => f(inputs[local_index], iterator_id, local_index));
|
|
@@ -42,6 +42,29 @@ export class UtilsMisc {
|
|
|
42
42
|
await Promise.all(iterators);
|
|
43
43
|
return outputs;
|
|
44
44
|
}
|
|
45
|
+
async batch_iterate(inputs, f, options) {
|
|
46
|
+
const batch_size = options.batch_size || 1;
|
|
47
|
+
const num_iterators = options.num_iterators || 0;
|
|
48
|
+
const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
|
|
49
|
+
let index = 0;
|
|
50
|
+
let iterators = [];
|
|
51
|
+
const outputs = new Array(inputs.length).fill(undefined);
|
|
52
|
+
for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
|
|
53
|
+
iterators.push((async () => {
|
|
54
|
+
while (index < inputs.length) {
|
|
55
|
+
const local_index = index;
|
|
56
|
+
index += batch_size;
|
|
57
|
+
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
58
|
+
console.log(iterator_id + ":" + local_index + "/" + inputs.length);
|
|
59
|
+
}
|
|
60
|
+
const output = await this.safe_run(() => f(inputs.slice(local_index, local_index + batch_size), iterator_id, local_index));
|
|
61
|
+
outputs[local_index] = output;
|
|
62
|
+
}
|
|
63
|
+
})());
|
|
64
|
+
}
|
|
65
|
+
await Promise.all(iterators);
|
|
66
|
+
return outputs;
|
|
67
|
+
}
|
|
45
68
|
static async wait(duration) {
|
|
46
69
|
return new Promise(resolve => setTimeout(resolve, duration));
|
|
47
70
|
}
|
package/package.json
CHANGED
package/src/UtilsMisc.ts
CHANGED
|
@@ -31,8 +31,8 @@ export class UtilsMisc {
|
|
|
31
31
|
return undefined
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
async iterate<T, U>(inputs : T[], f : (input : T, iterator_id : number, index : number) => Promise<U>, num_iterators : number = 1,
|
|
35
|
-
const
|
|
34
|
+
async iterate<T, U>(inputs : T[], f : (input : T, iterator_id : number, index : number) => Promise<U>, num_iterators : number = 1, options : { num_iterators? : number, verbosity? : number | boolean } = {}) {
|
|
35
|
+
const verbosity : number = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0)
|
|
36
36
|
let index = 0
|
|
37
37
|
let iterators = []
|
|
38
38
|
const outputs : (U | undefined)[] = new Array(inputs.length).fill(undefined)
|
|
@@ -41,7 +41,7 @@ export class UtilsMisc {
|
|
|
41
41
|
while (index < inputs.length) {
|
|
42
42
|
const local_index = index
|
|
43
43
|
index += 1
|
|
44
|
-
if (
|
|
44
|
+
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
45
45
|
console.log(iterator_id + ":" + local_index + "/" + inputs.length)
|
|
46
46
|
}
|
|
47
47
|
const output = await this.safe_run(() => f(inputs[local_index], iterator_id, local_index))
|
|
@@ -53,6 +53,31 @@ export class UtilsMisc {
|
|
|
53
53
|
return outputs
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
async batch_iterate<T, U>(inputs : T[], f : (inputs : T[], iterator_id : number, index : number) => Promise<U>,
|
|
57
|
+
options : { batch_size? : number, num_iterators? : number, verbosity? : number | boolean }) {
|
|
58
|
+
const batch_size = options.batch_size || 1
|
|
59
|
+
const num_iterators = options.num_iterators || 0
|
|
60
|
+
const verbosity : number = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0)
|
|
61
|
+
let index = 0
|
|
62
|
+
let iterators = []
|
|
63
|
+
const outputs : (U | undefined)[] = new Array(inputs.length).fill(undefined)
|
|
64
|
+
for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
|
|
65
|
+
iterators.push((async () => {
|
|
66
|
+
while (index < inputs.length) {
|
|
67
|
+
const local_index = index
|
|
68
|
+
index += batch_size
|
|
69
|
+
if (verbosity !== undefined && (local_index % verbosity === 0)) {
|
|
70
|
+
console.log(iterator_id + ":" + local_index + "/" + inputs.length)
|
|
71
|
+
}
|
|
72
|
+
const output = await this.safe_run(() => f(inputs.slice(local_index, local_index + batch_size), iterator_id, local_index))
|
|
73
|
+
outputs[local_index] = output
|
|
74
|
+
}
|
|
75
|
+
})())
|
|
76
|
+
}
|
|
77
|
+
await Promise.all(iterators)
|
|
78
|
+
return outputs
|
|
79
|
+
}
|
|
80
|
+
|
|
56
81
|
|
|
57
82
|
static async wait(duration : number) {
|
|
58
83
|
return new Promise(resolve => setTimeout(resolve, duration))
|