typescript-ds-lib 0.4.5 → 0.4.6
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/LICENSE +1 -1
- package/README.md +6 -2
- package/dist/algorithms/binary-search.d.ts +14 -0
- package/dist/algorithms/binary-search.js +36 -0
- package/dist/algorithms/binary-search.js.map +1 -0
- package/dist/algorithms/shuffle.d.ts +1 -0
- package/dist/algorithms/shuffle.js +20 -0
- package/dist/algorithms/shuffle.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# TypeScript Data Structure Library
|
|
2
2
|
[](https://www.npmjs.com/package/typescript-ds-lib)
|
|
3
3
|
|
|
4
|
-
TypeScript data structure implementations without external dependencies.
|
|
4
|
+
TypeScript data structure and algorithm implementations without external dependencies.
|
|
5
5
|
|
|
6
|
+
**Why to use this library?**
|
|
6
7
|
- Fully Tested
|
|
7
8
|
- Fast
|
|
8
9
|
- 0 Dependencies
|
|
@@ -36,7 +37,7 @@ console.log(stack.size()); // 2
|
|
|
36
37
|
|
|
37
38
|
|
|
38
39
|
## Documentation and Examples
|
|
39
|
-
See the [documentation](https://github.com/baloian/typescript-ds-lib/blob/master/doc/
|
|
40
|
+
See the [documentation](https://github.com/baloian/typescript-ds-lib/blob/master/doc/README.md) for more details and examples.
|
|
40
41
|
|
|
41
42
|
|
|
42
43
|
## Data Structures
|
|
@@ -54,6 +55,9 @@ See the [documentation](https://github.com/baloian/typescript-ds-lib/blob/master
|
|
|
54
55
|
- Set
|
|
55
56
|
- Stack
|
|
56
57
|
|
|
58
|
+
## Algorithms
|
|
59
|
+
- Binary Search (with custom comparator)
|
|
60
|
+
- Shuffle (shuffles the array in-place)
|
|
57
61
|
|
|
58
62
|
## `Map-Set` vs Native JavaScript `Map-Set`
|
|
59
63
|
The library's `Map` and `Set` data structures are implemented as Red-Black Tree and Binary Search Tree respectively.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs a binary search on a sorted array to locate a target element.
|
|
3
|
+
*
|
|
4
|
+
* This function assumes that the input array is sorted in ascending order with respect to the comparison logic.
|
|
5
|
+
* It accepts an optional comparison function for custom sorting logic; if not provided,
|
|
6
|
+
* native < and > operators are used.
|
|
7
|
+
*
|
|
8
|
+
* @param arr - A sorted array of elements of type T.
|
|
9
|
+
* @param target - The element to search for within the array.
|
|
10
|
+
* @param compare - (Optional) A comparison function that returns a negative value if the first argument is less
|
|
11
|
+
* than the second, a positive value if the first is greater, and zero if they are considered equal.
|
|
12
|
+
* @returns The index of the target element if it is found in the array; otherwise, returns -1.
|
|
13
|
+
*/
|
|
14
|
+
export declare function binarySearch<T>(arr: T[], target: T, compare?: (a: T, b: T) => number): number;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.binarySearch = binarySearch;
|
|
4
|
+
/**
|
|
5
|
+
* Performs a binary search on a sorted array to locate a target element.
|
|
6
|
+
*
|
|
7
|
+
* This function assumes that the input array is sorted in ascending order with respect to the comparison logic.
|
|
8
|
+
* It accepts an optional comparison function for custom sorting logic; if not provided,
|
|
9
|
+
* native < and > operators are used.
|
|
10
|
+
*
|
|
11
|
+
* @param arr - A sorted array of elements of type T.
|
|
12
|
+
* @param target - The element to search for within the array.
|
|
13
|
+
* @param compare - (Optional) A comparison function that returns a negative value if the first argument is less
|
|
14
|
+
* than the second, a positive value if the first is greater, and zero if they are considered equal.
|
|
15
|
+
* @returns The index of the target element if it is found in the array; otherwise, returns -1.
|
|
16
|
+
*/
|
|
17
|
+
function binarySearch(arr, target, compare) {
|
|
18
|
+
let left = 0;
|
|
19
|
+
let right = arr.length - 1;
|
|
20
|
+
const cmp = compare ? compare : (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
21
|
+
while (left <= right) {
|
|
22
|
+
const mid = left + Math.floor((right - left) / 2);
|
|
23
|
+
const comparison = cmp(arr[mid], target);
|
|
24
|
+
if (comparison === 0) {
|
|
25
|
+
return mid;
|
|
26
|
+
}
|
|
27
|
+
if (comparison < 0) {
|
|
28
|
+
left = mid + 1;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
right = mid - 1;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return -1;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=binary-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary-search.js","sourceRoot":"","sources":["../../algorithms/binary-search.ts"],"names":[],"mappings":";;AAaA,oCAiBC;AA9BD;;;;;;;;;;;;GAYG;AACH,SAAgB,YAAY,CAAI,GAAQ,EAAE,MAAS,EAAE,OAAgC;IACnF,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,KAAK,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;IAC3B,MAAM,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7E,OAAO,IAAI,IAAI,KAAK,EAAE,CAAC;QACrB,MAAM,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAClD,MAAM,UAAU,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC;QACzC,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;YACrB,OAAO,GAAG,CAAC;QACb,CAAC;QACD,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;YACnB,IAAI,GAAG,GAAG,GAAG,CAAC,CAAC;QACjB,CAAC;aAAM,CAAC;YACN,KAAK,GAAG,GAAG,GAAG,CAAC,CAAC;QAClB,CAAC;IACH,CAAC;IACD,OAAO,CAAC,CAAC,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function shuffle<T>(array: T[]): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.shuffle = shuffle;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
/*
|
|
9
|
+
* Fisher-Yates shuffle algorithm, Durstenfeld's variant as described in
|
|
10
|
+
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
|
|
11
|
+
*
|
|
12
|
+
* This function shuffles the given array in-place.
|
|
13
|
+
*/
|
|
14
|
+
function shuffle(array) {
|
|
15
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
16
|
+
const j = crypto_1.default.randomInt(0, i + 1);
|
|
17
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=shuffle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../algorithms/shuffle.ts"],"names":[],"mappings":";;;;;AAQA,0BAKC;AAbD,oDAAgC;AAEhC;;;;;EAKE;AACF,SAAgB,OAAO,CAAI,KAAU;IACnC,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1C,MAAM,CAAC,GAAG,gBAAU,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACzC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -13,3 +13,5 @@ export { Queue } from './lib/queue';
|
|
|
13
13
|
export { RedBlackTree } from './lib/red-black-tree';
|
|
14
14
|
export { Set } from './lib/set';
|
|
15
15
|
export { Stack } from './lib/stack';
|
|
16
|
+
export { binarySearch } from './algorithms/binary-search';
|
|
17
|
+
export { shuffle } from './algorithms/shuffle';
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.Stack = exports.Set = exports.RedBlackTree = exports.Queue = exports.PriorityQueue = exports.Matrix = exports.Map = exports.LinkedList = exports.Heap = exports.HashTable = exports.LinkedListGraph = exports.MatrixGraph = exports.Deque = exports.BinarySearchTree = void 0;
|
|
17
|
+
exports.shuffle = exports.binarySearch = exports.Stack = exports.Set = exports.RedBlackTree = exports.Queue = exports.PriorityQueue = exports.Matrix = exports.Map = exports.LinkedList = exports.Heap = exports.HashTable = exports.LinkedListGraph = exports.MatrixGraph = exports.Deque = exports.BinarySearchTree = void 0;
|
|
18
18
|
__exportStar(require("./types"), exports);
|
|
19
19
|
var binary_search_tree_1 = require("./lib/binary-search-tree");
|
|
20
20
|
Object.defineProperty(exports, "BinarySearchTree", { enumerable: true, get: function () { return binary_search_tree_1.BinarySearchTree; } });
|
|
@@ -44,4 +44,8 @@ var set_1 = require("./lib/set");
|
|
|
44
44
|
Object.defineProperty(exports, "Set", { enumerable: true, get: function () { return set_1.Set; } });
|
|
45
45
|
var stack_1 = require("./lib/stack");
|
|
46
46
|
Object.defineProperty(exports, "Stack", { enumerable: true, get: function () { return stack_1.Stack; } });
|
|
47
|
+
var binary_search_1 = require("./algorithms/binary-search");
|
|
48
|
+
Object.defineProperty(exports, "binarySearch", { enumerable: true, get: function () { return binary_search_1.binarySearch; } });
|
|
49
|
+
var shuffle_1 = require("./algorithms/shuffle");
|
|
50
|
+
Object.defineProperty(exports, "shuffle", { enumerable: true, get: function () { return shuffle_1.shuffle; } });
|
|
47
51
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+DAA4D;AAAnD,sHAAA,gBAAgB,OAAA;AACzB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,yDAAuD;AAA9C,2GAAA,WAAW,OAAA;AACpB,mEAAgE;AAAvD,oHAAA,eAAe,OAAA;AACxB,+CAA6C;AAApC,uGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,iDAA+C;AAAtC,yGAAA,UAAU,OAAA;AACnB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,uDAAqD;AAA5C,+GAAA,aAAa,OAAA;AACtB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,uDAAoD;AAA3C,8GAAA,YAAY,OAAA;AACrB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,qCAAoC;AAA3B,8FAAA,KAAK,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+DAA4D;AAAnD,sHAAA,gBAAgB,OAAA;AACzB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,yDAAuD;AAA9C,2GAAA,WAAW,OAAA;AACpB,mEAAgE;AAAvD,oHAAA,eAAe,OAAA;AACxB,+CAA6C;AAApC,uGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,iDAA+C;AAAtC,yGAAA,UAAU,OAAA;AACnB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,uDAAqD;AAA5C,+GAAA,aAAa,OAAA;AACtB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,uDAAoD;AAA3C,8GAAA,YAAY,OAAA;AACrB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,4DAA0D;AAAjD,6GAAA,YAAY,OAAA;AACrB,gDAA+C;AAAtC,kGAAA,OAAO,OAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-ds-lib",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
4
4
|
"description": "A collection of TypeScript data structure implementations",
|
|
5
5
|
"author": "Artiom Baloian <artiom.baloian@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/jest": "^29.5.14",
|
|
27
|
-
"@types/node": "^
|
|
28
|
-
"ts-jest": "^29.
|
|
27
|
+
"@types/node": "^25.0.3",
|
|
28
|
+
"ts-jest": "^29.4.1",
|
|
29
29
|
"ts-node": "^10.9.2",
|
|
30
|
-
"typescript": "^5.
|
|
30
|
+
"typescript": "^5.9.3"
|
|
31
31
|
},
|
|
32
32
|
"keywords": [
|
|
33
33
|
"typescript",
|