@softsky/utils 2.7.3 → 2.8.0
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/README.md +6 -0
- package/dist/arrays.js +1 -1
- package/dist/graphs.d.ts +20 -0
- package/dist/graphs.js +33 -0
- package/dist/time.js +0 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -300,6 +300,12 @@ If isTarget is omitted becomes floodfill.
|
|
|
300
300
|
Returns a target and map of parents.
|
|
301
301
|
You can use `unfoldPathfindingResult()` to get array of nodes.
|
|
302
302
|
|
|
303
|
+
---
|
|
304
|
+
__function__ `dijkstra` - Dijkstra search. Like bfs but supports weight and target is required.
|
|
305
|
+
If isTarget is omitted becomes floodfill (WITH WEIGHT).
|
|
306
|
+
Returns a target and map of parents.
|
|
307
|
+
You can use `unfoldPathfindingResult()` to get array of nodes.
|
|
308
|
+
|
|
303
309
|
---
|
|
304
310
|
__function__ `knapsack` - Knapsack find best way to get maximum value in limited capacity
|
|
305
311
|
|
package/dist/arrays.js
CHANGED
|
@@ -48,7 +48,7 @@ export function binarySearch(size, compare, returnClosest) {
|
|
|
48
48
|
else
|
|
49
49
|
low = mid + 1;
|
|
50
50
|
}
|
|
51
|
-
return returnClosest ?
|
|
51
|
+
return returnClosest ? low : -1;
|
|
52
52
|
}
|
|
53
53
|
/** Split array into sub arrays of spicified size */
|
|
54
54
|
export function chunk(array, chunkSize) {
|
package/dist/graphs.d.ts
CHANGED
|
@@ -59,6 +59,26 @@ export declare function dfs<T>(options: {
|
|
|
59
59
|
parents: Map<T, T>;
|
|
60
60
|
target?: T;
|
|
61
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* Dijkstra search. Like bfs but supports weight and target is required.
|
|
64
|
+
* If isTarget is omitted becomes floodfill (WITH WEIGHT).
|
|
65
|
+
* Returns a target and map of parents.
|
|
66
|
+
* You can use `unfoldPathfindingResult()` to get array of nodes.
|
|
67
|
+
*/
|
|
68
|
+
export declare function dijkstra<T>(options: {
|
|
69
|
+
/** Start node */
|
|
70
|
+
start: T;
|
|
71
|
+
/** Get weight of node */
|
|
72
|
+
getWeight: (node: T) => number;
|
|
73
|
+
/** Get neighbors of node */
|
|
74
|
+
getNeighbors: (node: T) => T[];
|
|
75
|
+
/** Check if node is a target */
|
|
76
|
+
isTarget?: (node: T) => boolean;
|
|
77
|
+
}): {
|
|
78
|
+
parents: Map<T, T>;
|
|
79
|
+
target?: T;
|
|
80
|
+
distance: Map<T, number>;
|
|
81
|
+
};
|
|
62
82
|
/** Knapsack find best way to get maximum value in limited capacity */
|
|
63
83
|
export declare function knapsack(weights: number[], values: number[], capacity: number): {
|
|
64
84
|
weight: number;
|
package/dist/graphs.js
CHANGED
|
@@ -109,6 +109,39 @@ export function dfs(options) {
|
|
|
109
109
|
parents,
|
|
110
110
|
};
|
|
111
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Dijkstra search. Like bfs but supports weight and target is required.
|
|
114
|
+
* If isTarget is omitted becomes floodfill (WITH WEIGHT).
|
|
115
|
+
* Returns a target and map of parents.
|
|
116
|
+
* You can use `unfoldPathfindingResult()` to get array of nodes.
|
|
117
|
+
*/
|
|
118
|
+
export function dijkstra(options) {
|
|
119
|
+
const parents = new Map();
|
|
120
|
+
const distance = new Map();
|
|
121
|
+
const visited = new Set();
|
|
122
|
+
const queue = [[0, options.start]];
|
|
123
|
+
distance.set(options.start, 0);
|
|
124
|
+
while (queue.length > 0) {
|
|
125
|
+
const [distribution, node] = queue.shift();
|
|
126
|
+
if (visited.has(node))
|
|
127
|
+
continue;
|
|
128
|
+
visited.add(node);
|
|
129
|
+
if (options.isTarget?.(node))
|
|
130
|
+
return { parents, target: node, distance };
|
|
131
|
+
const neighbors = options.getNeighbors(node);
|
|
132
|
+
for (let index = 0; index < neighbors.length; index++) {
|
|
133
|
+
const neighbor = neighbors[index];
|
|
134
|
+
const cost = options.getWeight(neighbor);
|
|
135
|
+
const newDistribution = distribution + cost;
|
|
136
|
+
if (newDistribution < (distance.get(neighbor) ?? Infinity)) {
|
|
137
|
+
distance.set(neighbor, newDistribution);
|
|
138
|
+
parents.set(neighbor, node);
|
|
139
|
+
pushToSorted(queue, [newDistribution, neighbor], ([x]) => x - newDistribution);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { parents, distance };
|
|
144
|
+
}
|
|
112
145
|
/** Knapsack find best way to get maximum value in limited capacity */
|
|
113
146
|
export function knapsack(weights, values, capacity) {
|
|
114
147
|
const n = weights.length;
|
package/dist/time.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softsky/utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"description": "JavaScript/TypeScript utilities",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"homepage": "https://github.com/SoundOfTheSky/utils#readme",
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@softsky/configs": "^1.3.4",
|
|
24
|
-
"@types/bun": "^1.2.
|
|
24
|
+
"@types/bun": "^1.2.23"
|
|
25
25
|
},
|
|
26
26
|
"files": ["dist/**/*"]
|
|
27
27
|
}
|