triangle-utils 1.4.107 → 1.4.109

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.
@@ -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, print_option?: number | boolean): Promise<(U | undefined)[]>;
10
+ iterate<T, U>(inputs: T[], f: (input: T, iterator_id: number, index: number) => Promise<U>, 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;
@@ -21,8 +21,9 @@ export class UtilsMisc {
21
21
  }
22
22
  return undefined;
23
23
  }
24
- async iterate(inputs, f, num_iterators = 1, print_option) {
25
- const print_freq = typeof print_option === "boolean" ? (print_option === true ? 1 : 0) : print_option;
24
+ async iterate(inputs, f, options = {}) {
25
+ const num_iterators = options.num_iterators || 0;
26
+ const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
26
27
  let index = 0;
27
28
  let iterators = [];
28
29
  const outputs = new Array(inputs.length).fill(undefined);
@@ -31,7 +32,7 @@ export class UtilsMisc {
31
32
  while (index < inputs.length) {
32
33
  const local_index = index;
33
34
  index += 1;
34
- if (print_freq !== undefined && (local_index % print_freq === 0)) {
35
+ if (verbosity !== undefined && (local_index % verbosity === 0)) {
35
36
  console.log(iterator_id + ":" + local_index + "/" + inputs.length);
36
37
  }
37
38
  const output = await this.safe_run(() => f(inputs[local_index], iterator_id, local_index));
@@ -42,6 +43,29 @@ export class UtilsMisc {
42
43
  await Promise.all(iterators);
43
44
  return outputs;
44
45
  }
46
+ async batch_iterate(inputs, f, options) {
47
+ const batch_size = options.batch_size || 1;
48
+ const num_iterators = options.num_iterators || 0;
49
+ const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
50
+ let index = 0;
51
+ let iterators = [];
52
+ const outputs = new Array(inputs.length).fill(undefined);
53
+ for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
54
+ iterators.push((async () => {
55
+ while (index < inputs.length) {
56
+ const local_index = index;
57
+ index += batch_size;
58
+ if (verbosity !== undefined && (local_index % verbosity === 0)) {
59
+ console.log(iterator_id + ":" + local_index + "/" + inputs.length);
60
+ }
61
+ const output = await this.safe_run(() => f(inputs.slice(local_index, local_index + batch_size), iterator_id, local_index));
62
+ outputs[local_index] = output;
63
+ }
64
+ })());
65
+ }
66
+ await Promise.all(iterators);
67
+ return outputs;
68
+ }
45
69
  static async wait(duration) {
46
70
  return new Promise(resolve => setTimeout(resolve, duration));
47
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.107",
3
+ "version": "1.4.109",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
package/src/UtilsMisc.ts CHANGED
@@ -31,8 +31,9 @@ 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, print_option? : number | boolean) {
35
- const print_freq : number | undefined = typeof print_option === "boolean" ? (print_option === true ? 1 : 0) : print_option
34
+ async iterate<T, U>(inputs : T[], f : (input : T, iterator_id : number, index : number) => Promise<U>, options : { num_iterators? : number, verbosity? : number | boolean } = {}) {
35
+ const num_iterators = options.num_iterators || 0
36
+ const verbosity : number = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0)
36
37
  let index = 0
37
38
  let iterators = []
38
39
  const outputs : (U | undefined)[] = new Array(inputs.length).fill(undefined)
@@ -41,7 +42,7 @@ export class UtilsMisc {
41
42
  while (index < inputs.length) {
42
43
  const local_index = index
43
44
  index += 1
44
- if (print_freq !== undefined && (local_index % print_freq === 0)) {
45
+ if (verbosity !== undefined && (local_index % verbosity === 0)) {
45
46
  console.log(iterator_id + ":" + local_index + "/" + inputs.length)
46
47
  }
47
48
  const output = await this.safe_run(() => f(inputs[local_index], iterator_id, local_index))
@@ -53,6 +54,31 @@ export class UtilsMisc {
53
54
  return outputs
54
55
  }
55
56
 
57
+ async batch_iterate<T, U>(inputs : T[], f : (inputs : T[], iterator_id : number, index : number) => Promise<U>,
58
+ options : { batch_size? : number, num_iterators? : number, verbosity? : number | boolean }) {
59
+ const batch_size = options.batch_size || 1
60
+ const num_iterators = options.num_iterators || 0
61
+ const verbosity : number = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0)
62
+ let index = 0
63
+ let iterators = []
64
+ const outputs : (U | undefined)[] = new Array(inputs.length).fill(undefined)
65
+ for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
66
+ iterators.push((async () => {
67
+ while (index < inputs.length) {
68
+ const local_index = index
69
+ index += batch_size
70
+ if (verbosity !== undefined && (local_index % verbosity === 0)) {
71
+ console.log(iterator_id + ":" + local_index + "/" + inputs.length)
72
+ }
73
+ const output = await this.safe_run(() => f(inputs.slice(local_index, local_index + batch_size), iterator_id, local_index))
74
+ outputs[local_index] = output
75
+ }
76
+ })())
77
+ }
78
+ await Promise.all(iterators)
79
+ return outputs
80
+ }
81
+
56
82
 
57
83
  static async wait(duration : number) {
58
84
  return new Promise(resolve => setTimeout(resolve, duration))