From 476395ef7220b1fa3201e79ed42e2b2f37a941e9 Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 16 Nov 2024 23:07:53 +1300 Subject: [PATCH] chore: Add source map support for ESModule. Add a 'browser' field to package.json. Performance optimization of getRandomIntArray method --- CHANGELOG.md | 2 +- package-lock.json | 52 +++++++++---------- package.json | 13 ++--- .../binary-tree/avl-tree-multi-map.ts | 4 +- .../data-structures/binary-tree/avl-tree.ts | 4 +- .../binary-tree/binary-tree.ts | 6 +-- src/types/data-structures/binary-tree/bst.ts | 6 +-- .../data-structures/binary-tree/rb-tree.ts | 4 +- .../binary-tree/tree-multi-map.ts | 4 +- .../binary-tree/avl-tree.test.ts | 2 +- .../binary-tree/binary-tree-overall.test.ts | 4 +- .../binary-tree/binary-tree.test.ts | 2 +- .../data-structures/binary-tree/bst.test.ts | 2 +- .../binary-tree/rb-tree.test.ts | 10 +++- .../graph/directed-graph.test.ts | 14 +++++ test/utils/array.ts | 27 +++++----- tsconfig-mjs.json | 2 +- 17 files changed, 92 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e566c8b..470b4d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. - [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - [`auto-changelog`](https://github.com/CookPete/auto-changelog) -## [v1.53.0](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming) +## [v1.53.1](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming) ### Changes diff --git a/package-lock.json b/package-lock.json index 848a4d9..eae3ca6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "data-structure-typed", - "version": "1.53.0", + "version": "1.53.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.53.0", + "version": "1.53.1", "license": "MIT", "devDependencies": { "@eslint/compat": "^1.2.2", @@ -19,11 +19,11 @@ "@typescript-eslint/eslint-plugin": "^8.12.1", "@typescript-eslint/parser": "^8.12.1", "auto-changelog": "^2.5.0", - "avl-tree-typed": "^1.52.9", + "avl-tree-typed": "^1.53.0", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.52.9", - "bst-typed": "^1.52.9", - "data-structure-typed": "^1.52.9", + "binary-tree-typed": "^1.53.0", + "bst-typed": "^1.53.0", + "data-structure-typed": "^1.53.0", "dependency-cruiser": "^16.5.0", "doctoc": "^2.2.1", "eslint": "^9.13.0", @@ -32,7 +32,7 @@ "eslint-import-resolver-typescript": "^3.6.3", "eslint-plugin-import": "^2.31.0", "fast-glob": "^3.3.2", - "heap-typed": "^1.52.9", + "heap-typed": "^1.53.0", "istanbul-badges-readme": "^1.9.0", "jest": "^29.7.0", "js-sdsl": "^4.4.2", @@ -3437,13 +3437,13 @@ } }, "node_modules/avl-tree-typed": { - "version": "1.52.9", - "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.52.9.tgz", - "integrity": "sha512-TfapTNrlIquhf1iwrzhc5pEejEhE0CogxiNlaI0nqkv2Qz2OkhvfmAtJOG5ndiFBp4btNZYFGHb+S/zXkEX82A==", + "version": "1.53.0", + "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.53.0.tgz", + "integrity": "sha512-SVk6zEvzrB2GhHVFOhVO2yaB7p9iLGy2aml0j7UW1rWp+VACw4SZQhKuldl2qC7ECb9AOIbajD7Y9ott8Qfy8Q==", "dev": true, "license": "MIT", "dependencies": { - "data-structure-typed": "^1.52.9" + "data-structure-typed": "^1.53.0" } }, "node_modules/babel-jest": { @@ -3602,13 +3602,13 @@ } }, "node_modules/binary-tree-typed": { - "version": "1.52.9", - "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.52.9.tgz", - "integrity": "sha512-/q5l3wftn485Px+L40NKjKBPukL1XjotSQVCYB+wmz1S3P4H8FobBlEFo5j0UqaW9+TgzhSwecoBbIPsvshXAQ==", + "version": "1.53.0", + "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.53.0.tgz", + "integrity": "sha512-5EmlwbR4Kaphual+wF2rJwpZFhhK2u6zOFiA+yagWDbH6tzBCojT5o2DN+1tmW1Pj6bGJxz7PHcDBIdBkQruMw==", "dev": true, "license": "MIT", "dependencies": { - "data-structure-typed": "^1.52.9" + "data-structure-typed": "^1.53.0" } }, "node_modules/brace-expansion": { @@ -3691,13 +3691,13 @@ } }, "node_modules/bst-typed": { - "version": "1.52.9", - "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.52.9.tgz", - "integrity": "sha512-7jgOhrYST+HBZRGvmKx3UZRt6ddBr9Ib807gfJ9+jRVuXQIK9m3hW1MSwXMovWs8tKpF0JZMCqj+uk+S8B85qg==", + "version": "1.53.0", + "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.53.0.tgz", + "integrity": "sha512-f1f+RlbIA6J2reFO/DwLRLaZVBmMNNyKCi5eWd+r5ojV9bDC/IZ34nzID0zSJUwVqtAtIpaK9x63XAo42+5SoQ==", "dev": true, "license": "MIT", "dependencies": { - "data-structure-typed": "^1.52.9" + "data-structure-typed": "^1.53.0" } }, "node_modules/buffer-from": { @@ -4069,9 +4069,9 @@ } }, "node_modules/data-structure-typed": { - "version": "1.52.9", - "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.52.9.tgz", - "integrity": "sha512-tIAf4CqOdDz5XJZBtTjWwhOgLOwiMS+1kH9dN44x12klBcKavpUDUjncvrcAqLsVP5xTZSeehJ5teNa6YNgWQQ==", + "version": "1.53.0", + "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.53.0.tgz", + "integrity": "sha512-bukacSwbWyypNTdQCt51rwNcqM1hacHG6mMJ2RDBqsz8jJ5N4oMOgQhDyA1GkvMvdq83mcj9ilCFZ6lDcwShqA==", "dev": true, "license": "MIT" }, @@ -5946,13 +5946,13 @@ } }, "node_modules/heap-typed": { - "version": "1.52.9", - "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.52.9.tgz", - "integrity": "sha512-YdCJFzKob73n5sDovGkL2Y/jqVe+vY9dXcDyx4ToZoneJqJ83IMVDl/qlGb/ujDMlTXqJR4/jiIT5s00jxS7jQ==", + "version": "1.53.0", + "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.53.0.tgz", + "integrity": "sha512-MtbVdNGb2mvLrv67j0ajaMDr/u0HpR0tMaCj1MUrmXKK+IhVqniq7I8ADUlDrEPpKXwES8RBa0v7lA5umKR9hg==", "dev": true, "license": "MIT", "dependencies": { - "data-structure-typed": "^1.52.9" + "data-structure-typed": "^1.53.0" } }, "node_modules/html-escaper": { diff --git a/package.json b/package.json index eff79ae..e2351df 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "data-structure-typed", - "version": "1.53.0", + "version": "1.53.2", "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python", "main": "dist/cjs/index.js", "module": "dist/mjs/index.js", + "browser": "dist/umd/data-structure-typed.min.js", "types": "dist/mjs/index.d.ts", "umd:main": "dist/umd/data-structure-typed.min.js", "exports": { @@ -68,11 +69,11 @@ "@typescript-eslint/eslint-plugin": "^8.12.1", "@typescript-eslint/parser": "^8.12.1", "auto-changelog": "^2.5.0", - "avl-tree-typed": "^1.52.9", + "avl-tree-typed": "^1.53.0", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.52.9", - "bst-typed": "^1.52.9", - "data-structure-typed": "^1.52.9", + "binary-tree-typed": "^1.53.0", + "bst-typed": "^1.53.0", + "data-structure-typed": "^1.53.0", "dependency-cruiser": "^16.5.0", "doctoc": "^2.2.1", "eslint": "^9.13.0", @@ -81,7 +82,7 @@ "eslint-import-resolver-typescript": "^3.6.3", "eslint-plugin-import": "^2.31.0", "fast-glob": "^3.3.2", - "heap-typed": "^1.52.9", + "heap-typed": "^1.53.0", "istanbul-badges-readme": "^1.9.0", "jest": "^29.7.0", "js-sdsl": "^4.4.2", diff --git a/src/types/data-structures/binary-tree/avl-tree-multi-map.ts b/src/types/data-structures/binary-tree/avl-tree-multi-map.ts index 8ebad8a..0f418b0 100644 --- a/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +++ b/src/types/data-structures/binary-tree/avl-tree-multi-map.ts @@ -1,5 +1,5 @@ -import {AVLTreeMultiMap, AVLTreeMultiMapNode} from '../../../data-structures'; -import type {AVLTreeOptions} from './avl-tree'; +import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures'; +import type { AVLTreeOptions } from './avl-tree'; export type AVLTreeMultiMapNodeNested = AVLTreeMultiMapNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/src/types/data-structures/binary-tree/avl-tree.ts b/src/types/data-structures/binary-tree/avl-tree.ts index 1576651..b84b3b6 100644 --- a/src/types/data-structures/binary-tree/avl-tree.ts +++ b/src/types/data-structures/binary-tree/avl-tree.ts @@ -1,5 +1,5 @@ -import {AVLTree, AVLTreeNode} from '../../../data-structures'; -import {BSTOptions} from './bst'; +import { AVLTree, AVLTreeNode } from '../../../data-structures'; +import { BSTOptions } from './bst'; export type AVLTreeNodeNested = AVLTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/src/types/data-structures/binary-tree/binary-tree.ts b/src/types/data-structures/binary-tree/binary-tree.ts index cca130d..5c80c40 100644 --- a/src/types/data-structures/binary-tree/binary-tree.ts +++ b/src/types/data-structures/binary-tree/binary-tree.ts @@ -1,6 +1,6 @@ -import {BinaryTree, BinaryTreeNode} from '../../../data-structures'; -import {IterationType, OptValue} from '../../common'; -import {DFSOperation} from '../../../constants'; +import { BinaryTree, BinaryTreeNode } from '../../../data-structures'; +import { IterationType, OptValue } from '../../common'; +import { DFSOperation } from '../../../constants'; export type BinaryTreeNodeNested = BinaryTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/src/types/data-structures/binary-tree/bst.ts b/src/types/data-structures/binary-tree/bst.ts index b9d974e..37e7e12 100644 --- a/src/types/data-structures/binary-tree/bst.ts +++ b/src/types/data-structures/binary-tree/bst.ts @@ -1,6 +1,6 @@ -import {BST, BSTNode} from '../../../data-structures'; -import type {BinaryTreeOptions} from './binary-tree'; -import {Comparator} from '../../common'; +import { BST, BSTNode } from '../../../data-structures'; +import type { BinaryTreeOptions } from './binary-tree'; +import { Comparator } from '../../common'; export type BSTNodeNested = BSTNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/src/types/data-structures/binary-tree/rb-tree.ts b/src/types/data-structures/binary-tree/rb-tree.ts index b08002c..3822199 100644 --- a/src/types/data-structures/binary-tree/rb-tree.ts +++ b/src/types/data-structures/binary-tree/rb-tree.ts @@ -1,5 +1,5 @@ -import {RedBlackTree, RedBlackTreeNode} from '../../../data-structures'; -import type {BSTOptions} from "./bst"; +import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures'; +import type { BSTOptions } from "./bst"; export type RBTNColor = 'RED' | 'BLACK'; diff --git a/src/types/data-structures/binary-tree/tree-multi-map.ts b/src/types/data-structures/binary-tree/tree-multi-map.ts index 7498ff3..43ccc38 100644 --- a/src/types/data-structures/binary-tree/tree-multi-map.ts +++ b/src/types/data-structures/binary-tree/tree-multi-map.ts @@ -1,5 +1,5 @@ -import {TreeMultiMap, TreeMultiMapNode} from '../../../data-structures'; -import type {RBTreeOptions} from './rb-tree'; +import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures'; +import type { RBTreeOptions } from './rb-tree'; export type TreeMultiMapNodeNested = TreeMultiMapNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/test/performance/data-structures/binary-tree/avl-tree.test.ts b/test/performance/data-structures/binary-tree/avl-tree.test.ts index 23c83e2..1fb736f 100644 --- a/test/performance/data-structures/binary-tree/avl-tree.test.ts +++ b/test/performance/data-structures/binary-tree/avl-tree.test.ts @@ -3,7 +3,7 @@ import * as Benchmark from 'benchmark'; import { getRandomIntArray, magnitude } from '../../../utils'; const suite = new Benchmark.Suite(); -const avlTree = new AVLTree([], { isMapMode: true }); +const avlTree = new AVLTree(); const { HUNDRED_THOUSAND } = magnitude; const randomArray = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSAND - 1, true); diff --git a/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts b/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts index 0461fa6..ad115fa 100644 --- a/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +++ b/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts @@ -3,8 +3,8 @@ import * as Benchmark from 'benchmark'; import { getRandomIntArray, magnitude } from '../../../utils'; const suite = new Benchmark.Suite(); -const rbTree = new RedBlackTree([], { isMapMode: true }); -const avlTree = new AVLTree([], { isMapMode: true }); +const rbTree = new RedBlackTree(); +const avlTree = new AVLTree(); const { TEN_THOUSAND } = magnitude; const arr = getRandomIntArray(TEN_THOUSAND, 0, TEN_THOUSAND - 1, true); diff --git a/test/performance/data-structures/binary-tree/binary-tree.test.ts b/test/performance/data-structures/binary-tree/binary-tree.test.ts index 3b1359b..f35a081 100644 --- a/test/performance/data-structures/binary-tree/binary-tree.test.ts +++ b/test/performance/data-structures/binary-tree/binary-tree.test.ts @@ -3,7 +3,7 @@ import * as Benchmark from 'benchmark'; import { getRandomIntArray, magnitude } from '../../../utils'; const suite = new Benchmark.Suite(); -const biTree = new BinaryTree([], { isMapMode: true }); +const biTree = new BinaryTree(); const { THOUSAND } = magnitude; const arr = getRandomIntArray(THOUSAND, 0, THOUSAND, true); diff --git a/test/performance/data-structures/binary-tree/bst.test.ts b/test/performance/data-structures/binary-tree/bst.test.ts index b485603..a00db81 100644 --- a/test/performance/data-structures/binary-tree/bst.test.ts +++ b/test/performance/data-structures/binary-tree/bst.test.ts @@ -3,7 +3,7 @@ import * as Benchmark from 'benchmark'; import { getRandomIntArray, magnitude } from '../../../utils'; const suite = new Benchmark.Suite(); -const bst = new BST([], { isMapMode: true }); +const bst = new BST(); const { TEN_THOUSAND } = magnitude; const arr = getRandomIntArray(TEN_THOUSAND, 0, TEN_THOUSAND, true); diff --git a/test/performance/data-structures/binary-tree/rb-tree.test.ts b/test/performance/data-structures/binary-tree/rb-tree.test.ts index 5e18043..f11867b 100644 --- a/test/performance/data-structures/binary-tree/rb-tree.test.ts +++ b/test/performance/data-structures/binary-tree/rb-tree.test.ts @@ -5,7 +5,8 @@ import { OrderedMap } from 'js-sdsl'; import { isCompetitor } from '../../../config'; const suite = new Benchmark.Suite(); -const rbTree = new RedBlackTree([], { isMapMode: true }); +const rbTree = new RedBlackTree(); +const rbTreeNodeMode = new RedBlackTree([], { isMapMode: false }); const { HUNDRED_THOUSAND } = magnitude; const randomArray = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSAND - 1, true); const cOrderedMap = new OrderedMap(); @@ -22,6 +23,13 @@ suite .add(`${HUNDRED_THOUSAND.toLocaleString()} get`, () => { for (let i = 0; i < randomArray.length; i++) rbTree.get(randomArray[i]); }) + .add(`${HUNDRED_THOUSAND.toLocaleString()} node mode add randomly`, () => { + rbTreeNodeMode.clear(); + for (let i = 0; i < randomArray.length; i++) rbTree.add(randomArray[i]); + }) + .add(`${HUNDRED_THOUSAND.toLocaleString()} node mode get`, () => { + for (let i = 0; i < randomArray.length; i++) rbTreeNodeMode.get(randomArray[i]); + }) .add(`${HUNDRED_THOUSAND.toLocaleString()} iterator`, () => { const entries = [...rbTree]; return entries.length === HUNDRED_THOUSAND; diff --git a/test/unit/data-structures/graph/directed-graph.test.ts b/test/unit/data-structures/graph/directed-graph.test.ts index 59dcec3..f59f548 100644 --- a/test/unit/data-structures/graph/directed-graph.test.ts +++ b/test/unit/data-structures/graph/directed-graph.test.ts @@ -1035,4 +1035,18 @@ describe('delete', () => { // even though A no longer has any knowledge of C linking to it. expect(graph.outgoingEdgesOf('C').map(e => e.dest)).toEqual(['A']); }); + + // it('should print', () => { + // const graph = new DirectedGraph(); + // graph.addVertex('A'); + // graph.addVertex('B'); + // graph.addVertex('C'); + // + // graph.addEdge('A', 'B'); + // graph.addEdge('A', 'C'); + // graph.addEdge('B', 'C'); + // graph.addEdge('C', 'B'); + // graph.print(); + // + // }) }); diff --git a/test/utils/array.ts b/test/utils/array.ts index 798bbd3..7e39ad9 100644 --- a/test/utils/array.ts +++ b/test/utils/array.ts @@ -1,19 +1,22 @@ import { getRandomInt } from './number'; -export function getRandomIntArray(length: number = 1000, min: number = -1000, max: number = 1000, isDistinct = true) { - if (isDistinct) { - const set = new Set(); - const ans: number[] = []; - while (ans.length < length) { - const num = getRandomInt(min, max); - if (!set.has(num)) { - ans.push(num); - set.add(num); - } +export function getRandomIntArray(length: number = 1000, min: number = -1000, max: number = 1000, unique = true) { + if (unique) { + if (max - min + 1 < length) { + throw new Error('Range too small for unique values with the specified length'); } - return ans; + + const allNumbers = Array.from({ length: max - min + 1 }, (_, i) => i + min); + + for (let i = allNumbers.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [allNumbers[i], allNumbers[j]] = [allNumbers[j], allNumbers[i]]; + } + + return allNumbers.slice(0, length); + } else { + return Array.from({ length }, () => Math.floor(Math.random() * (max - min + 1)) + min); } - return new Array(length).fill(0).map(() => getRandomInt(min, max)); } const words = [ diff --git a/tsconfig-mjs.json b/tsconfig-mjs.json index 8f8178e..afdba48 100644 --- a/tsconfig-mjs.json +++ b/tsconfig-mjs.json @@ -6,7 +6,7 @@ "module": "ESNext", "target": "ESNext", "moduleResolution": "Node", - "sourceMap": false + "sourceMap": true }, "include": [ "./src/**/*.ts"