data-structure-typed 1.41.2 → 1.41.4

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.
Files changed (77) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +57 -0
  3. package/benchmark/report.html +73 -0
  4. package/benchmark/report.json +239 -0
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.js +11 -10
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  7. package/dist/mjs/data-structures/binary-tree/binary-tree.js +11 -10
  8. package/dist/umd/data-structure-typed.min.js +1 -1
  9. package/dist/umd/data-structure-typed.min.js.map +1 -1
  10. package/jest.integration.config.js +5 -0
  11. package/package.json +13 -8
  12. package/src/data-structures/binary-tree/binary-tree.ts +12 -9
  13. package/test/config.ts +1 -1
  14. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +24 -0
  15. package/test/performance/data-structures/binary-tree/binary-index-tree.test.ts +0 -0
  16. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +25 -0
  17. package/test/performance/data-structures/binary-tree/bst.test.ts +24 -0
  18. package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
  19. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +0 -0
  20. package/test/performance/data-structures/binary-tree/segment-tree.test.ts +0 -0
  21. package/test/performance/data-structures/binary-tree/tree-multiset.test.ts +0 -0
  22. package/test/performance/data-structures/graph/abstract-graph.test.ts +0 -0
  23. package/test/performance/data-structures/graph/directed-graph.test.ts +0 -0
  24. package/test/performance/data-structures/graph/map-graph.test.ts +0 -0
  25. package/test/performance/data-structures/graph/overall.test.ts +0 -0
  26. package/test/performance/data-structures/graph/undirected-graph.test.ts +0 -0
  27. package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
  28. package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
  29. package/test/performance/data-structures/hash/hash-map.test.ts +0 -0
  30. package/test/performance/data-structures/hash/hash-table.test.ts +0 -0
  31. package/test/performance/data-structures/heap/heap.test.ts +30 -0
  32. package/test/performance/data-structures/heap/max-heap.test.ts +0 -0
  33. package/test/performance/data-structures/heap/min-heap.test.ts +0 -0
  34. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +40 -0
  35. package/test/performance/data-structures/linked-list/linked-list.test.ts +0 -0
  36. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +34 -0
  37. package/test/performance/data-structures/linked-list/skip-linked-list.test.ts +0 -0
  38. package/test/performance/data-structures/linked-list/skip-list.test.ts +0 -0
  39. package/test/performance/data-structures/matrix/matrix.test.ts +0 -0
  40. package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
  41. package/test/performance/data-structures/matrix/navigator.test.ts +0 -0
  42. package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
  43. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +19 -0
  44. package/test/performance/data-structures/priority-queue/min-priority-queue.test.ts +0 -0
  45. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +0 -0
  46. package/test/performance/data-structures/queue/deque.test.ts +21 -0
  47. package/test/performance/data-structures/queue/queue.test.ts +25 -0
  48. package/test/performance/data-structures/stack/stack.test.ts +0 -0
  49. package/test/performance/data-structures/tree/tree.test.ts +0 -0
  50. package/test/performance/data-structures/trie/trie.test.ts +0 -0
  51. package/test/performance/reportor.ts +174 -0
  52. package/test/performance/types/index.ts +1 -0
  53. package/test/performance/types/reportor.ts +3 -0
  54. package/test/types/utils/index.ts +1 -0
  55. package/test/types/utils/json2html.ts +1 -0
  56. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +5 -5
  57. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -23
  58. package/test/unit/data-structures/linked-list/linked-list.test.ts +3 -30
  59. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -21
  60. package/test/unit/data-structures/matrix/matrix2d.test.ts +1 -1
  61. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -32
  62. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +2 -2
  63. package/test/unit/data-structures/queue/queue.test.ts +3 -39
  64. package/test/utils/array.ts +5 -0
  65. package/test/utils/big-o.ts +7 -7
  66. package/test/utils/console.ts +30 -0
  67. package/test/utils/index.ts +4 -0
  68. package/test/utils/is.ts +56 -0
  69. package/test/utils/json2html.ts +322 -0
  70. package/test/utils/number.ts +11 -1
  71. package/test/config.js +0 -4
  72. package/test/types/index.js +0 -29
  73. package/test/types/utils/big-o.js +0 -2
  74. package/test/types/utils/index.js +0 -29
  75. package/test/utils/big-o.js +0 -222
  76. package/test/utils/index.js +0 -30
  77. package/test/utils/number.js +0 -14
@@ -0,0 +1,322 @@
1
+ import * as _ from './is';
2
+ import {Json2htmlOptions} from '../types';
3
+
4
+ function toggleJS(options?: Json2htmlOptions): string {
5
+ if (options?.plainHtml) {
6
+ return '';
7
+ } else {
8
+ return 'onclick="json-to-html.toggleVisibility(this);return false"';
9
+ }
10
+ }
11
+
12
+ function makeLabelDiv(options: any, level: number, keyName: string | number, datatype?: string): string {
13
+ if (typeof keyName === 'number') {
14
+ return `<div class='index'><span class='json-to-html-label'>${keyName}&nbsp;</span></div>`;
15
+ } else if (typeof keyName === 'string') {
16
+ if (datatype === 'array') {
17
+ return `<div class='collapsible level${level}' ${toggleJS(
18
+ options
19
+ )}><span class='json-to-html-label'>${keyName}</span></div>`;
20
+ } else if (datatype === 'object') {
21
+ return `<div class='attribute collapsible level${level}' ${toggleJS(
22
+ options
23
+ )}><span class='json-to-html-label'>${keyName}:</span></div>`;
24
+ } else {
25
+ return `<div class='leaf level${level}'><span class='json-to-html-label'>${keyName}:</span></div>`;
26
+ }
27
+ } else {
28
+ return '';
29
+ }
30
+ }
31
+
32
+ function getContentClass(keyName: string | number): string {
33
+ if (typeof keyName === 'string') {
34
+ return 'content';
35
+ } else {
36
+ return '';
37
+ }
38
+ }
39
+
40
+ function isPlainObject(val: any): boolean {
41
+ let lastKey: string | undefined;
42
+ let lastOwnKey: string | undefined;
43
+ for (const key in val) {
44
+ if (val.hasOwnProperty(key)) {
45
+ lastOwnKey = key;
46
+ }
47
+ }
48
+ for (const key in val) {
49
+ lastKey = key;
50
+ }
51
+ return lastOwnKey === lastKey;
52
+ }
53
+
54
+ function isLeafValue(val: any): boolean {
55
+ return (
56
+ _.isNumber(val) ||
57
+ _.isString(val) ||
58
+ _.isBoolean(val) ||
59
+ _.isDate(val) ||
60
+ _.isNull(val) ||
61
+ _.isUndefined(val) ||
62
+ isNaN(val) ||
63
+ _.isFunction(val) ||
64
+ !isPlainObject(val)
65
+ );
66
+ }
67
+
68
+ function isLeafObject(obj: any): boolean {
69
+ if (!_.isObject(obj)) {
70
+ return false;
71
+ }
72
+ for (const key in obj) {
73
+ const val = obj[key];
74
+ if (!isLeafValue(val)) {
75
+ return false;
76
+ }
77
+ }
78
+ return true;
79
+ }
80
+
81
+ function isTable(arr: any[]): boolean {
82
+ if (!_.isArray(arr)) {
83
+ return false;
84
+ }
85
+ if (arr.length === 0 || !_.isObject(arr[0])) {
86
+ return false;
87
+ } else {
88
+ let nonCompliant = arr.find(row => !isLeafObject(row));
89
+ if (nonCompliant) {
90
+ return false;
91
+ } else {
92
+ const cols = Object.keys(arr[0]);
93
+ nonCompliant = arr.find((row: object) => !_.isEqual(cols, Object.keys(row)));
94
+ if (nonCompliant) {
95
+ return false;
96
+ } else {
97
+ return true;
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ function drawTable(arr: any[]): string {
104
+ function drawRow(headers: string[], rowObj: any): string {
105
+ return '<td>' + headers.map(header => rowObj[header]).join('</td><td>') + '</td>';
106
+ }
107
+
108
+ const cols = Object.keys(arr[0]);
109
+ const content = arr.map(rowObj => drawRow(cols, rowObj));
110
+ const headingHtml = '<tr><th>' + cols.join('</th><th>') + '</th></tr>';
111
+ const contentHtml = '<tr>' + content.join('</tr><tr>') + '</tr>';
112
+ return '<table>' + headingHtml + contentHtml + '</table>';
113
+ }
114
+
115
+ function _render(name: string, data: any, options: Json2htmlOptions, level: number, altRow: number): string {
116
+ const contentClass = getContentClass(name);
117
+ if (_.isArray(data)) {
118
+ const title = makeLabelDiv(options, level, `${name}`, 'array');
119
+ let subs: string;
120
+ if (isTable(data)) {
121
+ subs = drawTable(data);
122
+ } else {
123
+ subs =
124
+ "<div class='altRows'>" +
125
+ data
126
+ .map((val: any, idx: number) => _render(idx.toString(), val, options, level + 1, idx % 2))
127
+ .join("</div><div class='altRows'>") +
128
+ '</div>';
129
+ }
130
+ return `<div class="json-to-html-collapse clearfix ${altRow}">
131
+ ${title}
132
+ <div class="${contentClass}">${subs}</div>
133
+ </div>`;
134
+ } else if (isLeafValue(data)) {
135
+ const title = makeLabelDiv(options, level, name);
136
+ if (_.isFunction(data)) {
137
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;-function() can't _render-</span>`;
138
+ } else if (!isPlainObject(data)) {
139
+ if (_.isFunction(data.toString)) {
140
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;${data.toString()}</span>`;
141
+ } else {
142
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;-instance object, can't render-</span>`;
143
+ }
144
+ } else {
145
+ return `${title}<span class='json-to-html-value'>&nbsp;&nbsp;${data}</span>`;
146
+ }
147
+ } else {
148
+ const title = makeLabelDiv(options, level, name, 'object');
149
+ let count = 0;
150
+ const subs =
151
+ '<div>' +
152
+ Object.entries(data)
153
+ .map(([key, val]) => _render(key, val, options, level + 1, count++ % 2))
154
+ .join('</div><div>') +
155
+ '</div>';
156
+ const inner = `<div class="json-to-html-expand clearfix ${altRow}">
157
+ ${title}
158
+ <div class="${contentClass}">${subs}</div>
159
+ </div>`;
160
+ return `${level === 0 ? "<div id='json-to-html'>" : ''}
161
+ ${inner}
162
+ ${level === 0 ? '</div>' : ''}`;
163
+ }
164
+ }
165
+
166
+ export function render(name: string, json: any, options: Json2htmlOptions): string {
167
+ // return `${head}${_render('', json, options, 0, 0)}`;
168
+ return `${_render(name, json, options, 0, 0)}`;
169
+ }
170
+
171
+ // const head = `<style>
172
+ // #json-to-html table {
173
+ // border-collapse: collapse;
174
+ // }
175
+ // #json-to-html th {
176
+ // color: #888;
177
+ // }
178
+ // #json-to-html table,th, td {
179
+ // border: 1px solid #DDD;
180
+ // padding: 10px 5px;
181
+ // }
182
+ // #json-to-html th, td {
183
+ // text-align: center;
184
+ // }
185
+ // #json-to-html .content {
186
+ // padding-left: 30px;
187
+ // font-family: Arial;
188
+ // }
189
+ //
190
+ // #json-to-html .index {
191
+ // font-size: 100%;
192
+ // color: #999;
193
+ // float: left;
194
+ // }
195
+ // #json-to-html .clearfix:after {
196
+ // content: ".";
197
+ // display: block;
198
+ // height: 0;
199
+ // clear: both;
200
+ // visibility: hidden;
201
+ // }
202
+ // #json-to-html .json-to-html-label {
203
+ // font-family: Helvetica Neue;
204
+ // color: #333;
205
+ // }
206
+ // #json-to-html .json-to-html-value {
207
+ // font-family: Arial;
208
+ // color: #777;
209
+ // }
210
+ // #json-to-html .collapsible > .json-to-html-label:hover {
211
+ // text-decoration: underline;
212
+ // }
213
+ // #json-to-html .collapsible > .json-to-html-label {
214
+ // color: #15C;
215
+ // }
216
+ // #json-to-html .json-to-html-collapse > div.content {
217
+ // display: none;
218
+ // }
219
+ // #json-to-html .json-to-html-collapse > .json-to-html-label {
220
+ // font-weight: bold;
221
+ // }
222
+ //
223
+ // #json-to-html .json-to-html-expand > div > .json-to-html-label, #json-to-html .json-to-html-collapse > div > .json-to-html-label {
224
+ // background-repeat: no-repeat;
225
+ // background-position: left;
226
+ // padding-left: 25px;
227
+ // margin: 5px 0px 5px 15px;
228
+ // display: inline-block;
229
+ // }
230
+ //
231
+ // #json-to-html .json-to-html-expand > div > .json-to-html-label {
232
+ // width: 30px;
233
+ // height: 30px;
234
+ // background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><polygon points="50,10 10,90 90,90" fill="blue" /></svg>');
235
+ // background-size: cover;
236
+ // background-position: center;
237
+ // }
238
+ //
239
+ // #json-to-html .json-to-html-collapse > div > .json-to-html-label {
240
+ // width: 30px;
241
+ // height: 30px;
242
+ // background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><polygon points="50,10 10,90 90,90" fill="blue" /></svg>');
243
+ // background-size: cover;
244
+ // background-position: center;
245
+ // }
246
+ //
247
+ // #json-to-html .json-to-html-collapse > span.collapsible:before {
248
+ // border-radius: 2px;
249
+ // border-color: #A44;
250
+ // border-style: solid;
251
+ // border-width: 1px;
252
+ // color: #A44;
253
+ // content: '+';
254
+ // display: inline-block;
255
+ // line-height: 7px;
256
+ // margin: 0 2px;
257
+ // overflow: hidden;
258
+ // padding: 1px;
259
+ // font-size: 11px;
260
+ // }
261
+ //
262
+ // #json-to-html .json-to-html-expand > span.collapsible:before {
263
+ // border: none;
264
+ // color: #A44;
265
+ // content: '-';
266
+ // display: inline-block;
267
+ // line-height: 7px;
268
+ // margin: 4px;
269
+ // overflow: hidden;
270
+ // padding: 1px;
271
+ // font-size: 11px;
272
+ // }
273
+ //
274
+ // #json-to-html.level0 {
275
+ // font-size: 25px;
276
+ // }
277
+ // #json-to-html .level1 {
278
+ // font-size: 22px;
279
+ // }
280
+ //
281
+ // #json-to-html .leaf {
282
+ // color: #666;
283
+ // display: inline;
284
+ // }
285
+ //
286
+ // #json-to-html .altRows:nth-child(odd) { background-color:#ddd; }
287
+ // #json-to-html .altRows:nth-child(even) { background-color:#fff; }
288
+ //
289
+ // #json-to-html tr:nth-child(odd) { background-color:#eee; }
290
+ // #json-to-html tr:nth-child(even) { background-color:#fff; }
291
+ // </style>
292
+ // <script type="text/javascript">
293
+ // json-to-html = {
294
+ // toggleVisibility: function(el, name) {
295
+ // json-to-html.toggleClass(el.parentElement,'json-to-html-collapse json-to-html-expand');
296
+ // },
297
+ // classRe: function(name) {
298
+ // return new RegExp('(?:^|\\s)'+name+'(?!\\S)');
299
+ // },
300
+ // addClass: function(el, name) {
301
+ // el.className += " "+name;
302
+ // },
303
+ // removeClass: function(el, name) {
304
+ // var re = json-to-html.classRe(name);
305
+ // el.className = el.className.replace(json-to-html.classRe(name) , '' )
306
+ // },
307
+ // hasClass: function(el, name) {
308
+ // var re = json-to-html.classRe(name);
309
+ // return json-to-html.classRe(name).exec(el.className);
310
+ // },
311
+ // toggleClass: function(el, name) {
312
+ // var names = name.split(/\s+/);
313
+ // for (n in names) {
314
+ // if (json-to-html.hasClass(el, names[n])) {
315
+ // json-to-html.removeClass(el, names[n]);
316
+ // } else {
317
+ // json-to-html.addClass(el, names[n]);
318
+ // }
319
+ // }
320
+ // }
321
+ // };
322
+ // </script>`;
@@ -1,3 +1,13 @@
1
- export function getRandomInt(min: number, max: number) {
1
+ export function randomInt(min: number, max: number) {
2
2
  return Math.floor(Math.random() * (max - min + 1)) + min;
3
3
  }
4
+
5
+ export function numberFix(num: number, decimalPlaces: number): string {
6
+ if (num > 10000 || num < 0.001) {
7
+ const [mantissa, exponent] = num.toExponential().split('e');
8
+ const formattedMantissa = Number(mantissa).toFixed(decimalPlaces);
9
+ return `${formattedMantissa}e${exponent}`;
10
+ } else {
11
+ return num.toFixed(decimalPlaces);
12
+ }
13
+ }
package/test/config.js DELETED
@@ -1,4 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {value: true});
3
- exports.isDebugTest = void 0;
4
- exports.isDebugTest = false;
@@ -1,29 +0,0 @@
1
- 'use strict';
2
- var __createBinding =
3
- (this && this.__createBinding) ||
4
- (Object.create
5
- ? function (o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- var desc = Object.getOwnPropertyDescriptor(m, k);
8
- if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
- desc = {
10
- enumerable: true,
11
- get: function () {
12
- return m[k];
13
- }
14
- };
15
- }
16
- Object.defineProperty(o, k2, desc);
17
- }
18
- : function (o, m, k, k2) {
19
- if (k2 === undefined) k2 = k;
20
- o[k2] = m[k];
21
- });
22
- var __exportStar =
23
- (this && this.__exportStar) ||
24
- function (m, exports) {
25
- for (var p in m)
26
- if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
- };
28
- Object.defineProperty(exports, '__esModule', {value: true});
29
- __exportStar(require('./utils'), exports);
@@ -1,2 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {value: true});
@@ -1,29 +0,0 @@
1
- 'use strict';
2
- var __createBinding =
3
- (this && this.__createBinding) ||
4
- (Object.create
5
- ? function (o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- var desc = Object.getOwnPropertyDescriptor(m, k);
8
- if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
- desc = {
10
- enumerable: true,
11
- get: function () {
12
- return m[k];
13
- }
14
- };
15
- }
16
- Object.defineProperty(o, k2, desc);
17
- }
18
- : function (o, m, k, k2) {
19
- if (k2 === undefined) k2 = k;
20
- o[k2] = m[k];
21
- });
22
- var __exportStar =
23
- (this && this.__exportStar) ||
24
- function (m, exports) {
25
- for (var p in m)
26
- if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
- };
28
- Object.defineProperty(exports, '__esModule', {value: true});
29
- __exportStar(require('./big-o'), exports);
@@ -1,222 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {value: true});
3
- exports.logBigOMetrics = exports.logBigOMetricsWrap = exports.bigO = exports.magnitude = void 0;
4
- var config_1 = require('../config');
5
-
6
- var isDebug = config_1.isDebugTest;
7
- var orderReducedBy = 2; // reduction of bigO's order compared to the baseline bigO
8
- exports.magnitude = {
9
- CONSTANT: Math.floor(Number.MAX_SAFE_INTEGER / Math.pow(10, orderReducedBy)),
10
- LOG_N: Math.pow(10, 9 - orderReducedBy),
11
- LINEAR: Math.pow(10, 6 - orderReducedBy),
12
- N_LOG_N: Math.pow(10, 5 - orderReducedBy),
13
- SQUARED: Math.pow(10, 4 - orderReducedBy),
14
- CUBED: Math.pow(10, 3 - orderReducedBy),
15
- FACTORIAL: 20 - orderReducedBy
16
- };
17
- exports.bigO = {
18
- CONSTANT: exports.magnitude.CONSTANT / 100000,
19
- LOG_N: Math.log2(exports.magnitude.LOG_N) / 1000,
20
- LINEAR: exports.magnitude.LINEAR / 1000,
21
- N_LOG_N: (exports.magnitude.N_LOG_N * Math.log2(exports.magnitude.LOG_N)) / 1000,
22
- SQUARED: Math.pow(exports.magnitude.SQUARED, 2) / 1000,
23
- CUBED: Math.pow(exports.magnitude.SQUARED, 3) / 1000,
24
- FACTORIAL: 10000
25
- };
26
-
27
- function findPotentialN(input) {
28
- var longestArray = [];
29
- var mostProperties = {};
30
-
31
- function recurse(obj) {
32
- if (Array.isArray(obj)) {
33
- if (obj.length > longestArray.length) {
34
- longestArray = obj;
35
- }
36
- } else if (typeof obj === 'object' && obj !== null) {
37
- var keys = Object.keys(obj);
38
- if (keys.length > Object.keys(mostProperties).length) {
39
- mostProperties = obj;
40
- }
41
- keys.forEach(function (key) {
42
- recurse(obj[key]);
43
- });
44
- }
45
- }
46
-
47
- if (Array.isArray(input)) {
48
- input.forEach(function (item) {
49
- recurse(item);
50
- });
51
- } else {
52
- recurse(input);
53
- }
54
- // return [longestArray, mostProperties] : [any[], { [key: string]: any }];
55
- return Math.max(longestArray.length, Object.keys(mostProperties).length);
56
- }
57
-
58
- function linearRegression(x, y) {
59
- var n = x.length;
60
- var sumX = x.reduce(function (acc, val) {
61
- return acc + val;
62
- }, 0);
63
- var sumY = y.reduce(function (acc, val) {
64
- return acc + val;
65
- }, 0);
66
- var sumXSquared = x.reduce(function (acc, val) {
67
- return acc + Math.pow(val, 2);
68
- }, 0);
69
- var sumXY = x.reduce(function (acc, val, i) {
70
- return acc + val * y[i];
71
- }, 0);
72
- var slope = (n * sumXY - sumX * sumY) / (n * sumXSquared - Math.pow(sumX, 2));
73
- var intercept = (sumY - slope * sumX) / n;
74
- var yHat = x.map(function (val) {
75
- return slope * val + intercept;
76
- });
77
- var totalVariation = y
78
- .map(function (val, i) {
79
- return Math.pow(val - yHat[i], 2);
80
- })
81
- .reduce(function (acc, val) {
82
- return acc + val;
83
- }, 0);
84
- var explainedVariation = y
85
- .map(function (val) {
86
- return Math.pow(val - sumY / n, 2);
87
- })
88
- .reduce(function (acc, val) {
89
- return acc + val;
90
- }, 0);
91
- var rSquared = 1 - totalVariation / explainedVariation;
92
- return {slope: slope, intercept: intercept, rSquared: rSquared};
93
- }
94
-
95
- function estimateBigO(runtimes, dataSizes) {
96
- // Make sure the input runtimes and data sizes have the same length
97
- if (runtimes.length !== dataSizes.length) {
98
- return 'Lengths of input arrays do not match';
99
- }
100
- // Create an array to store the computational complexity of each data point
101
- var complexities = [];
102
- // Traverse different possible complexities
103
- var complexitiesToCheck = [
104
- 'O(1)',
105
- 'O(log n)',
106
- 'O(n)',
107
- 'O(n log n)',
108
- 'O(n^2)' // squared time complexity
109
- ];
110
- var _loop_1 = function (complexity) {
111
- // Calculate data points for fitting
112
- var fittedData = dataSizes.map(function (size) {
113
- if (complexity === 'O(1)') {
114
- return 1; // constant time complexity
115
- } else if (complexity === 'O(log n)') {
116
- return Math.log(size);
117
- } else if (complexity === 'O(n)') {
118
- return size;
119
- } else if (complexity === 'O(n log n)') {
120
- return size * Math.log(size);
121
- } else if (complexity === 'O(n^2)') {
122
- return Math.pow(size, 2);
123
- } else {
124
- return Math.pow(size, 10);
125
- }
126
- });
127
- // Fit the data points using linear regression analysis
128
- var regressionResult = linearRegression(fittedData, runtimes);
129
- // Check the R-squared value of the fit. It is usually considered a valid fit if it is greater than 0.9.
130
- if (regressionResult.rSquared >= 0.9) {
131
- complexities.push(complexity);
132
- }
133
- };
134
- for (var _i = 0, complexitiesToCheck_1 = complexitiesToCheck; _i < complexitiesToCheck_1.length; _i++) {
135
- var complexity = complexitiesToCheck_1[_i];
136
- _loop_1(complexity);
137
- }
138
- // If there is no valid fitting result, return "cannot estimate", otherwise return the estimated time complexity
139
- if (complexities.length === 0) {
140
- return 'Unable to estimate';
141
- } else {
142
- return complexities.join(' or ');
143
- }
144
- }
145
-
146
- var methodLogs = new Map();
147
-
148
- function logBigOMetricsWrap(fn, args, fnName) {
149
- var startTime = performance.now();
150
- var result = fn(args);
151
- var endTime = performance.now();
152
- var runTime = endTime - startTime;
153
- var methodName = ''.concat(fnName);
154
- if (!methodLogs.has(methodName)) {
155
- methodLogs.set(methodName, []);
156
- }
157
- var methodLog = methodLogs.get(methodName);
158
- var maxDataSize = args.length === 1 && typeof args[0] === 'number' ? args[0] : findPotentialN(args);
159
- if (methodLog) {
160
- methodLog.push([runTime, maxDataSize]);
161
- if (methodLog.length >= 20) {
162
- isDebug && console.log('triggered', methodName, methodLog);
163
- var bigO_1 = estimateBigO(
164
- methodLog.map(function (_a) {
165
- var runTime = _a[0];
166
- return runTime;
167
- }),
168
- methodLog.map(function (_a) {
169
- var runTime = _a[0];
170
- return runTime;
171
- })
172
- );
173
- isDebug && console.log('Estimated Big O: '.concat(bigO_1));
174
- methodLogs.delete(methodName);
175
- }
176
- }
177
- return result;
178
- }
179
-
180
- exports.logBigOMetricsWrap = logBigOMetricsWrap;
181
-
182
- function logBigOMetrics(target, propertyKey, descriptor) {
183
- var originalMethod = descriptor.value;
184
- descriptor.value = function () {
185
- var args = [];
186
- for (var _i = 0; _i < arguments.length; _i++) {
187
- args[_i] = arguments[_i];
188
- }
189
- var startTime = performance.now();
190
- var result = originalMethod.apply(this, args);
191
- var endTime = performance.now();
192
- var runTime = endTime - startTime;
193
- var methodName = ''.concat(target.constructor.name, '.').concat(propertyKey);
194
- if (!methodLogs.has(methodName)) {
195
- methodLogs.set(methodName, []);
196
- }
197
- var methodLog = methodLogs.get(methodName);
198
- var maxDataSize = args.length === 1 && typeof args[0] === 'number' ? args[0] : findPotentialN(args);
199
- if (methodLog) {
200
- methodLog.push([runTime, maxDataSize]);
201
- if (methodLog.length >= 20) {
202
- isDebug && console.log('triggered', methodName, methodLog);
203
- var bigO_2 = estimateBigO(
204
- methodLog.map(function (_a) {
205
- var runTime = _a[0];
206
- return runTime;
207
- }),
208
- methodLog.map(function (_a) {
209
- var runTime = _a[0];
210
- return runTime;
211
- })
212
- );
213
- isDebug && console.log('Estimated Big O: '.concat(bigO_2));
214
- methodLogs.delete(methodName);
215
- }
216
- }
217
- return result;
218
- };
219
- return descriptor;
220
- }
221
-
222
- exports.logBigOMetrics = logBigOMetrics;
@@ -1,30 +0,0 @@
1
- 'use strict';
2
- var __createBinding =
3
- (this && this.__createBinding) ||
4
- (Object.create
5
- ? function (o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- var desc = Object.getOwnPropertyDescriptor(m, k);
8
- if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
- desc = {
10
- enumerable: true,
11
- get: function () {
12
- return m[k];
13
- }
14
- };
15
- }
16
- Object.defineProperty(o, k2, desc);
17
- }
18
- : function (o, m, k, k2) {
19
- if (k2 === undefined) k2 = k;
20
- o[k2] = m[k];
21
- });
22
- var __exportStar =
23
- (this && this.__exportStar) ||
24
- function (m, exports) {
25
- for (var p in m)
26
- if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
- };
28
- Object.defineProperty(exports, '__esModule', {value: true});
29
- __exportStar(require('./number'), exports);
30
- __exportStar(require('./big-o'), exports);
@@ -1,14 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {value: true});
3
- exports.getMSB = exports.getRandomInt = void 0;
4
- function getRandomInt(min, max) {
5
- return Math.floor(Math.random() * (max - min + 1)) + min;
6
- }
7
- exports.getRandomInt = getRandomInt;
8
- var getMSB = function (value) {
9
- if (value <= 0) {
10
- return 0;
11
- }
12
- return 1 << (31 - Math.clz32(value));
13
- };
14
- exports.getMSB = getMSB;