data-structure-typed 1.41.4 → 1.41.5

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.
@@ -1,25 +1,45 @@
1
1
  import {BinaryTree} from '../../../../src';
2
2
  import * as Benchmark from 'benchmark';
3
- import {magnitude, randomInt, randomIntArray} from '../../../utils';
3
+ import {getRandomIntArray, magnitude} from '../../../utils';
4
4
 
5
5
  const suite = new Benchmark.Suite();
6
6
  const biTree = new BinaryTree<number>();
7
7
  const {N_LOG_N} = magnitude;
8
+ const arr = getRandomIntArray(N_LOG_N, 0, N_LOG_N, true);
8
9
 
9
10
  suite
10
- .add(`add ${N_LOG_N}`, () => {
11
- for (let i = 0; i < N_LOG_N; i++) biTree.add(randomInt(-N_LOG_N, N_LOG_N));
11
+ .add(`${N_LOG_N} add randomly`, () => {
12
+ biTree.clear();
13
+ for (let i = 0; i < arr.length; i++) {
14
+ biTree.add(arr[i]);
15
+ }
12
16
  })
13
- .add(`delete ${N_LOG_N}`, () => {
14
- for (let i = 0; i < N_LOG_N; i++) biTree.delete(randomInt(-N_LOG_N, N_LOG_N));
17
+ .add(`${N_LOG_N} add & delete randomly`, () => {
18
+ biTree.clear();
19
+ for (let i = 0; i < arr.length; i++) {
20
+ biTree.add(arr[i]);
21
+ }
22
+ for (let i = 0; i < arr.length; i++) {
23
+ biTree.delete(arr[i]);
24
+ }
15
25
  })
16
- .add(`addMany ${N_LOG_N}`, () => {
26
+ .add(`${N_LOG_N} addMany`, () => {
17
27
  biTree.clear();
18
- const arr = randomIntArray(N_LOG_N);
19
28
  biTree.addMany(arr);
20
29
  })
21
- .add(`get ${N_LOG_N}`, () => {
22
- for (let i = 0; i < N_LOG_N; i++) biTree.get(randomInt(-N_LOG_N, N_LOG_N));
30
+ .add(`${N_LOG_N} get`, () => {
31
+ for (let i = 0; i < arr.length; i++) {
32
+ biTree.get(arr[i]);
33
+ }
34
+ })
35
+ .add(`${N_LOG_N} dfs`, () => {
36
+ for (let i = 0; i < N_LOG_N; i++) biTree.dfs();
37
+ })
38
+ .add(`${N_LOG_N} bfs`, () => {
39
+ for (let i = 0; i < N_LOG_N; i++) biTree.bfs();
40
+ })
41
+ .add(`${N_LOG_N} morris`, () => {
42
+ for (let i = 0; i < N_LOG_N; i++) biTree.morris(n => n, 'pre');
23
43
  });
24
44
 
25
45
  export {suite};
@@ -1,24 +1,36 @@
1
1
  import {BST} from '../../../../src';
2
2
  import * as Benchmark from 'benchmark';
3
- import {magnitude, randomInt, randomIntArray} from '../../../utils';
3
+ import {getRandomIntArray, magnitude} from '../../../utils';
4
4
 
5
5
  const suite = new Benchmark.Suite();
6
6
  const bst = new BST<number>();
7
7
  const {N_LOG_N} = magnitude;
8
+ const arr = getRandomIntArray(N_LOG_N, 0, N_LOG_N, true);
8
9
 
9
10
  suite
10
- .add(`add ${N_LOG_N} randomly`, () => {
11
- for (let i = 0; i < N_LOG_N; i++) bst.add(randomInt(0, N_LOG_N));
11
+ .add(`${N_LOG_N} add randomly`, () => {
12
+ bst.clear();
13
+ for (let i = 0; i < arr.length; i++) {
14
+ bst.add(arr[i]);
15
+ }
12
16
  })
13
- .add(`delete ${N_LOG_N} randomly`, () => {
14
- for (let i = 0; i < N_LOG_N; i++) bst.delete(randomInt(0, N_LOG_N));
17
+ .add(`${N_LOG_N} add & delete randomly`, () => {
18
+ bst.clear();
19
+ for (let i = 0; i < arr.length; i++) {
20
+ bst.add(arr[i]);
21
+ }
22
+ for (let i = 0; i < arr.length; i++) {
23
+ bst.delete(arr[i]);
24
+ }
15
25
  })
16
- .add(`addMany ${N_LOG_N} balanced`, () => {
17
- const arr = randomIntArray(N_LOG_N);
26
+ .add(`${N_LOG_N} addMany`, () => {
27
+ bst.clear();
18
28
  bst.addMany(arr);
19
29
  })
20
- .add(`get ${N_LOG_N}`, () => {
21
- for (let i = 0; i < N_LOG_N; i++) bst.get(randomInt(-N_LOG_N, N_LOG_N));
30
+ .add(`${N_LOG_N} get`, () => {
31
+ for (let i = 0; i < arr.length; i++) {
32
+ bst.get(arr[i]);
33
+ }
22
34
  });
23
35
 
24
36
  export {suite};
@@ -0,0 +1,49 @@
1
+ import {DirectedGraph} from '../../../../src';
2
+ import * as Benchmark from 'benchmark';
3
+ import {getRandomIndex, getRandomWords, magnitude} from '../../../utils';
4
+
5
+ const suite = new Benchmark.Suite();
6
+ const {THOUSAND} = magnitude;
7
+ const graph = new DirectedGraph<number, number>();
8
+ const vertexes = getRandomWords(THOUSAND);
9
+
10
+ suite
11
+ .add(`${THOUSAND} addVertex`, () => {
12
+ for (const v of vertexes) {
13
+ graph.addVertex(v);
14
+ }
15
+ })
16
+ .add(`${THOUSAND} addEdge`, () => {
17
+ for (let i = 0; i < THOUSAND; i++) {
18
+ const v1 = vertexes[getRandomIndex(vertexes)];
19
+ const v2 = vertexes[getRandomIndex(vertexes)];
20
+ graph.addEdge(v1, v2);
21
+ }
22
+ })
23
+ .add(`${THOUSAND} getVertex`, () => {
24
+ for (let i = 0; i < THOUSAND; i++) {
25
+ graph.getVertex(vertexes[getRandomIndex(vertexes)]);
26
+ }
27
+ })
28
+ .add(`${THOUSAND} getEdge`, () => {
29
+ for (let i = 0; i < THOUSAND; i++) {
30
+ graph.getEdge(vertexes[getRandomIndex(vertexes)], vertexes[getRandomIndex(vertexes)]);
31
+ }
32
+ })
33
+ .add(`tarjan`, () => {
34
+ // for (let i = 0; i < THOUSAND; i++) {
35
+ graph.tarjan(true);
36
+ // }
37
+ })
38
+ .add(`tarjan all`, () => {
39
+ // for (let i = 0; i < THOUSAND; i++) {
40
+ graph.tarjan(true, true, true, true);
41
+ // }
42
+ })
43
+ .add(`topologicalSort`, () => {
44
+ // for (let i = 0; i < THOUSAND; i++) {
45
+ graph.topologicalSort('key');
46
+ // }
47
+ });
48
+
49
+ export {suite};
@@ -6,7 +6,7 @@ const suite = new Benchmark.Suite();
6
6
  const {N_LOG_N} = magnitude;
7
7
 
8
8
  suite
9
- .add(`add & ${N_LOG_N}`, () => {
9
+ .add(`${N_LOG_N} add & pop`, () => {
10
10
  const heap = new Heap<number>({comparator: (a, b) => b - a});
11
11
 
12
12
  for (let i = 0; i < N_LOG_N; i++) {
@@ -17,7 +17,7 @@ suite
17
17
  heap.pop();
18
18
  }
19
19
  })
20
- .add(`fib add & pop ${N_LOG_N}`, () => {
20
+ .add(`${N_LOG_N} fib add & pop`, () => {
21
21
  const fbHeap = new FibonacciHeap<number>();
22
22
  for (let i = 1; i <= N_LOG_N; i++) {
23
23
  fbHeap.push(i);
@@ -6,14 +6,14 @@ const suite = new Benchmark.Suite();
6
6
  const {LINEAR, N_LOG_N} = magnitude;
7
7
 
8
8
  suite
9
- .add(`unshift ${LINEAR}`, () => {
9
+ .add(`${LINEAR} unshift`, () => {
10
10
  const list = new DoublyLinkedList<number>();
11
11
 
12
12
  for (let i = 0; i < LINEAR; i++) {
13
13
  list.unshift(i);
14
14
  }
15
15
  })
16
- .add(`unshift & shift ${LINEAR}`, () => {
16
+ .add(`${LINEAR} unshift & shift`, () => {
17
17
  const list = new DoublyLinkedList<number>();
18
18
 
19
19
  for (let i = 0; i < LINEAR; i++) {
@@ -23,7 +23,7 @@ suite
23
23
  list.shift();
24
24
  }
25
25
  })
26
- .add(`insertBefore ${N_LOG_N}`, () => {
26
+ .add(`${N_LOG_N} insertBefore`, () => {
27
27
  const doublyList = new DoublyLinkedList<number>();
28
28
  let midNode: DoublyLinkedListNode | null = null;
29
29
  const midIndex = Math.floor(N_LOG_N / 2);
@@ -6,7 +6,7 @@ const suite = new Benchmark.Suite();
6
6
  const {N_LOG_N} = magnitude;
7
7
 
8
8
  suite
9
- .add(`push & pop ${N_LOG_N}`, () => {
9
+ .add(`${N_LOG_N} push & pop`, () => {
10
10
  const list = new SinglyLinkedList<number>();
11
11
 
12
12
  for (let i = 0; i < N_LOG_N; i++) {
@@ -17,7 +17,7 @@ suite
17
17
  list.pop();
18
18
  }
19
19
  })
20
- .add(`insertBefore ${N_LOG_N}`, () => {
20
+ .add(`${N_LOG_N} insertBefore`, () => {
21
21
  const singlyList = new SinglyLinkedList<number>();
22
22
  let midSinglyNode: SinglyLinkedListNode | null = null;
23
23
  const midIndex = Math.floor(N_LOG_N / 2);
@@ -3,11 +3,11 @@ import * as Benchmark from 'benchmark';
3
3
  import {magnitude} from '../../../utils';
4
4
 
5
5
  const suite = new Benchmark.Suite();
6
- const {LINEAR} = magnitude;
6
+ const {TEN_THOUSAND} = magnitude;
7
7
 
8
- suite.add(`refill & poll ${LINEAR}`, () => {
8
+ suite.add(`${TEN_THOUSAND} refill & poll`, () => {
9
9
  const nodes = Array.from(
10
- new Set<number>(Array.from(new Array(LINEAR), () => Math.floor(Math.random() * LINEAR * 100)))
10
+ new Set<number>(Array.from(new Array(TEN_THOUSAND), () => Math.floor(Math.random() * TEN_THOUSAND * 100)))
11
11
  );
12
12
  const maxPQ = new MaxPriorityQueue<number>();
13
13
  maxPQ.refill(nodes);
@@ -6,13 +6,13 @@ export const suite = new Benchmark.Suite();
6
6
  const {LINEAR} = magnitude;
7
7
 
8
8
  suite
9
- .add(`push ${LINEAR}`, () => {
9
+ .add(`${LINEAR} push`, () => {
10
10
  const deque = new Deque<number>();
11
11
  for (let i = 0; i < LINEAR; i++) {
12
12
  deque.push(i);
13
13
  }
14
14
  })
15
- .add(`shift ${LINEAR}`, () => {
15
+ .add(`${LINEAR} shift`, () => {
16
16
  const deque = new Deque<number>();
17
17
  for (let i = 0; i < LINEAR; i++) {
18
18
  deque.push(i);
@@ -6,14 +6,14 @@ const suite = new Benchmark.Suite();
6
6
  const {LINEAR} = magnitude;
7
7
 
8
8
  suite
9
- .add(`push ${LINEAR}`, () => {
9
+ .add(`${LINEAR} push`, () => {
10
10
  const queue = new Queue<number>();
11
11
 
12
12
  for (let i = 0; i < LINEAR; i++) {
13
13
  queue.push(i);
14
14
  }
15
15
  })
16
- .add(`push & shift ${LINEAR}`, () => {
16
+ .add(`${LINEAR} push & shift`, () => {
17
17
  const queue = new Queue<number>();
18
18
 
19
19
  for (let i = 0; i < LINEAR; i++) {
@@ -0,0 +1,22 @@
1
+ import {Trie} from '../../../../src';
2
+ import * as Benchmark from 'benchmark';
3
+ import {getRandomWords, magnitude} from '../../../utils';
4
+
5
+ const suite = new Benchmark.Suite();
6
+ const {HUNDRED_THOUSAND} = magnitude;
7
+ const trie = new Trie();
8
+ const randomWords = getRandomWords(HUNDRED_THOUSAND, false);
9
+
10
+ suite
11
+ .add(`${HUNDRED_THOUSAND} push`, () => {
12
+ for (let i = 0; i < randomWords.length; i++) {
13
+ trie.add(randomWords[i]);
14
+ }
15
+ })
16
+ .add(`${HUNDRED_THOUSAND} getWords`, () => {
17
+ for (let i = 0; i < randomWords.length; i++) {
18
+ trie.getWords(randomWords[i]);
19
+ }
20
+ });
21
+
22
+ export {suite};
@@ -11,14 +11,12 @@ const testFiles = fastGlob.sync(path.join(testDir, '**', '*.test.ts'));
11
11
 
12
12
  const report: {[key: string]: any} = {};
13
13
 
14
- let testFileCount = 0,
15
- completedCount = 0;
14
+ let completedCount = 0;
16
15
 
17
16
  const performanceTests: PerformanceTest[] = [];
18
- const {GREEN, BOLD, RED, END, YELLOW, CYAN, BG_YELLOW} = Color;
17
+ const {GREEN, BOLD, END, YELLOW, GRAY, CYAN, BG_YELLOW} = Color;
19
18
 
20
19
  testFiles.forEach((file: string) => {
21
- testFileCount++;
22
20
  const testName = path.basename(file, '.test.ts');
23
21
  const testFunction = require(file);
24
22
  const {suite} = testFunction;
@@ -133,7 +131,7 @@ function writeIntoMarkdown(html: string) {
133
131
  if (err) {
134
132
  console.error('Unable to write to README.md file:', err);
135
133
  } else {
136
- console.log('The text has been successfully inserted into the README.md file!');
134
+ console.log('The tables have been successfully inserted into the README.md file!');
137
135
  }
138
136
  });
139
137
  });
@@ -141,29 +139,40 @@ function writeIntoMarkdown(html: string) {
141
139
 
142
140
  performanceTests.forEach(item => {
143
141
  const {suite, testName, file} = item;
144
- console.log(`${BG_YELLOW}Running in${END}: ${CYAN}${file}${END}`);
142
+ const relativeFilePath = path.relative(__dirname, file);
143
+ const directory = path.dirname(relativeFilePath);
144
+ const fileName = path.basename(relativeFilePath);
145
+ console.log(`${BG_YELLOW}Running in${END}: ${GRAY}${directory}/${END}${CYAN}${fileName}${END}`);
145
146
 
146
147
  if (suite) {
148
+ let runTime = 0;
147
149
  suite
148
150
  .on('complete', function (this: Benchmark.Suite) {
149
151
  completedCount++;
150
152
  report[testName] = {};
151
- report[testName].benchmarks = this.map((benchmark: Benchmark) => ({
152
- 'test name': benchmark.name,
153
- 'time taken (ms)': numberFix(benchmark.times.period * 1000, 2),
154
- 'executions per sec': numberFix(benchmark.hz, 2),
155
- 'executed times': numberFix(benchmark.count, 0),
156
- 'sample mean (secs)': numberFix(benchmark.stats.mean, 2),
157
- 'sample deviation': numberFix(benchmark.stats.deviation, 2)
158
- }));
153
+ report[testName].benchmarks = this.map((benchmark: Benchmark) => {
154
+ runTime += benchmark.times.elapsed;
155
+ return {
156
+ 'test name': benchmark.name,
157
+ 'time taken (ms)': numberFix(benchmark.times.period * 1000, 2),
158
+ 'executions per sec': numberFix(benchmark.hz, 2),
159
+ 'executed times': numberFix(benchmark.count, 0),
160
+ 'sample mean (secs)': numberFix(benchmark.stats.mean, 2),
161
+ 'sample deviation': numberFix(benchmark.stats.deviation, 2)
162
+ };
163
+ });
164
+
159
165
  report[testName].testName = testName;
160
166
  const isDone = completedCount === performanceTests.length;
167
+ runTime = Number(runTime.toFixed(2));
168
+ const isTimeWarn = runTime > 120;
161
169
  console.log(
162
- `Files: ${GREEN}${testFileCount}${END} `,
163
- `Suites: ${GREEN}${performanceTests.length}${END} `,
164
- `Progress: ${isDone ? GREEN : YELLOW}${completedCount}${END}/${isDone ? GREEN : RED}${
170
+ // `Files: ${GREEN}${testFileCount}${END} `,
171
+ // `Suites: ${GREEN}${performanceTests.length}${END} `,
172
+ `Suites Progress: ${isDone ? GREEN : YELLOW}${completedCount}${END}/${isDone ? GREEN : YELLOW}${
165
173
  performanceTests.length
166
- }${END}`
174
+ }${END}`,
175
+ `Time: ${isTimeWarn ? YELLOW : GREEN}${runTime}s${END}`
167
176
  );
168
177
  if (isDone) {
169
178
  composeReport();
@@ -1,5 +1,5 @@
1
1
  import {NIL, RBTNColor, RBTreeNode, RedBlackTree} from '../../../../src';
2
- import {randomInt} from '../../../utils';
2
+ import {getRandomInt} from '../../../utils';
3
3
  import {isDebugTest} from '../../../config';
4
4
 
5
5
  const isDebug = isDebugTest;
@@ -428,8 +428,8 @@ describe('RedBlackTree', () => {
428
428
 
429
429
  it('should fix the tree after insertion and deletion', () => {
430
430
  for (let i = 0; i < 100; i++) {
431
- tree.insert(randomInt(-100, 1000));
432
- tree.delete(randomInt(-100, 1000));
431
+ tree.insert(getRandomInt(-100, 1000));
432
+ tree.delete(getRandomInt(-100, 1000));
433
433
  }
434
434
  });
435
435
  });
@@ -235,10 +235,11 @@ describe('FibonacciHeap Stress Test', () => {
235
235
  // 10, 100, 1000, 5000, 10000, 20000, 50000, 75000, 100000,
236
236
  // 150000, 200000, 250000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000
237
237
  // ].forEach(m => logBigOMetricsWrap<typeof testByMagnitude>(testByMagnitude, [m]));
238
- [
239
- 10, 100, 1000, 5000, 10000, 20000, 50000, 75000, 100000, 150000, 200000, 250000, 300000, 400000, 500000, 600000,
240
- 700000, 800000, 900000, 1000000
241
- ].forEach(m =>
238
+ // [
239
+ // 10, 100, 1000, 5000, 10000, 20000, 50000, 75000, 100000, 150000, 200000, 250000, 300000, 400000, 500000, 600000,
240
+ // 700000, 800000, 900000, 1000000
241
+ // ]
242
+ [10, 100, 1000, 5000].forEach(m =>
242
243
  logBigOMetricsWrap(
243
244
  (c: number) => {
244
245
  const result: number[] = [];
@@ -1,5 +1,5 @@
1
1
  import {PriorityQueue} from '../../../../src';
2
- import {randomInt} from '../../../utils';
2
+ import {getRandomInt} from '../../../utils';
3
3
 
4
4
  describe('PriorityQueue Operation Test', () => {
5
5
  it('should PriorityQueue poll, pee, heapify, toArray work well', function () {
@@ -44,7 +44,7 @@ describe('PriorityQueue Operation Test', () => {
44
44
 
45
45
  describe('Priority Queue Performance Test', () => {
46
46
  it('should numeric heap work well', function () {
47
- const values = Array.from(new Array(10000), () => randomInt(1, 10000000));
47
+ const values = Array.from(new Array(10000), () => getRandomInt(1, 10000000));
48
48
  const minPriorityQueue = new PriorityQueue<number>({comparator: (a, b) => a - b});
49
49
  minPriorityQueue.refill(values);
50
50
  const sorted = minPriorityQueue.sort();