data-structure-typed 1.47.8 → 1.47.9
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +5 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +41 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +5 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +41 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +1 -1
- package/dist/umd/data-structure-typed.js +42 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/graph/abstract-graph.ts +46 -0
- package/src/data-structures/graph/directed-graph.ts +1 -1
- package/test/integration/index.html +2 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +41 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.47.
|
|
3
|
+
"version": "1.47.9",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, RedBlack 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.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -1159,6 +1159,52 @@ export abstract class AbstractGraph<
|
|
|
1159
1159
|
return this.tarjan(false, true, false, false).bridges;
|
|
1160
1160
|
}
|
|
1161
1161
|
|
|
1162
|
+
* [Symbol.iterator](): Iterator<[VertexKey, V | undefined]> {
|
|
1163
|
+
for (const vertex of this._vertices.values()) {
|
|
1164
|
+
yield [vertex.key, vertex.value];
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void {
|
|
1169
|
+
let index = 0;
|
|
1170
|
+
for (const vertex of this) {
|
|
1171
|
+
callback(vertex, index, this._vertices);
|
|
1172
|
+
index++;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][] {
|
|
1177
|
+
const filtered: [VertexKey, V | undefined][] = [];
|
|
1178
|
+
let index = 0;
|
|
1179
|
+
for (const entry of this) {
|
|
1180
|
+
if (predicate(entry, index, this._vertices)) {
|
|
1181
|
+
filtered.push(entry);
|
|
1182
|
+
}
|
|
1183
|
+
index++;
|
|
1184
|
+
}
|
|
1185
|
+
return filtered;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[] {
|
|
1189
|
+
const mapped: T[] = [];
|
|
1190
|
+
let index = 0;
|
|
1191
|
+
for (const entry of this) {
|
|
1192
|
+
mapped.push(callback(entry, index, this._vertices));
|
|
1193
|
+
index++;
|
|
1194
|
+
}
|
|
1195
|
+
return mapped;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T {
|
|
1199
|
+
let accumulator: T = initialValue;
|
|
1200
|
+
let index = 0;
|
|
1201
|
+
for (const entry of this) {
|
|
1202
|
+
accumulator = callback(accumulator, entry, index, this._vertices);
|
|
1203
|
+
index++;
|
|
1204
|
+
}
|
|
1205
|
+
return accumulator;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1162
1208
|
protected abstract _addEdgeOnly(edge: EO): boolean;
|
|
1163
1209
|
|
|
1164
1210
|
protected _addVertexOnly(newVertex: VO): boolean {
|
|
@@ -87,7 +87,7 @@ export class DirectedGraph<
|
|
|
87
87
|
* @returns a new instance of a DirectedVertex object, casted as type VO.
|
|
88
88
|
*/
|
|
89
89
|
createVertex(key: VertexKey, value?: V): VO {
|
|
90
|
-
return new DirectedVertex(key, value
|
|
90
|
+
return new DirectedVertex(key, value) as VO;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
@@ -349,7 +349,7 @@
|
|
|
349
349
|
// 1 _4 6 _9
|
|
350
350
|
// / /
|
|
351
351
|
// 3 8
|
|
352
|
-
|
|
352
|
+
|
|
353
353
|
|
|
354
354
|
const trie2 = new Trie(orgStrArr);
|
|
355
355
|
trie2.print(); // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
|
|
@@ -367,7 +367,7 @@
|
|
|
367
367
|
// 0 2 _5_ 8_
|
|
368
368
|
// / \ \
|
|
369
369
|
// 4 6 9
|
|
370
|
-
|
|
370
|
+
|
|
371
371
|
} catch (e) {
|
|
372
372
|
console.error(e);
|
|
373
373
|
}
|
|
@@ -595,3 +595,44 @@ describe('cycles, strongly connected components, bridges, articular points in Di
|
|
|
595
595
|
expect(dfnMap.size).toBe(8);
|
|
596
596
|
expect(lowMap.size).toBe(8);
|
|
597
597
|
});
|
|
598
|
+
|
|
599
|
+
describe('DirectedGraph iterative Methods', () => {
|
|
600
|
+
let graph: DirectedGraph<string>;
|
|
601
|
+
let vertices: string[];
|
|
602
|
+
|
|
603
|
+
beforeEach(() => {
|
|
604
|
+
graph = new DirectedGraph();
|
|
605
|
+
vertices = ['A', 'B', 'C', 'D'];
|
|
606
|
+
vertices.forEach(vertex => graph.addVertex(vertex));
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
test('[Symbol.iterator] should iterate over all vertices', () => {
|
|
610
|
+
const iteratedVertices = [];
|
|
611
|
+
for (const vertex of graph) {
|
|
612
|
+
iteratedVertices.push(vertex[0]);
|
|
613
|
+
}
|
|
614
|
+
expect(iteratedVertices).toEqual(vertices);
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
test('forEach should apply a function to each vertex', () => {
|
|
618
|
+
const result: VertexKey[] = [];
|
|
619
|
+
graph.forEach(vertex => result.push(vertex[0]));
|
|
620
|
+
expect(result).toEqual(vertices);
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
test('filter should return vertices that satisfy the condition', () => {
|
|
624
|
+
const filtered = graph.filter(vertex => vertex[0] === 'A' || vertex[0] === 'B');
|
|
625
|
+
expect(filtered).toEqual([["A", undefined], ["B", undefined]]);
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
test('map should apply a function to each vertex and return a new array', () => {
|
|
629
|
+
const mapped = graph.map(vertex => vertex[0] + '_mapped');
|
|
630
|
+
expect(mapped).toEqual(vertices.map(v => v + '_mapped'));
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
test('reduce should accumulate a value based on each vertex', () => {
|
|
634
|
+
const concatenated = graph.reduce((acc, vertex) => acc + vertex[0], '');
|
|
635
|
+
expect(concatenated).toBe(vertices.join(''));
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
|