ts-structures
Advanced tools
Comparing version 0.5.1 to 0.5.2
import ListGraph from './list-graph'; | ||
interface Graph { | ||
addVertex(vertex: string): void; | ||
removeVertex(vertex: string): void; | ||
addEdge(vertexA: string, vertexB: string, weight: number): void; | ||
removeEdge(vertexA: string, vertexB: string): void; | ||
addDirectedEdge(from: string, to: string, weight: number): void; | ||
removeDirectedEdge(from: string, to: string): void; | ||
interface Graph<T extends number | string> { | ||
get(vertex: Vertex<T>): EdgeSet | undefined; | ||
getEdge(vertexA: Vertex<T>, vertexB: Vertex<T>): Edge | undefined; | ||
addVertex(vertex: string): boolean; | ||
removeVertex(vertex: string): boolean; | ||
addEdge(vertexA: string, vertexB: string, weight: number): boolean; | ||
removeEdge(vertexA: string, vertexB: string): boolean; | ||
addDirectedEdge(from: string, to: string, weight: number): boolean; | ||
removeDirectedEdge(from: string, to: string): boolean; | ||
} | ||
export { Graph, ListGraph, }; | ||
declare type Vertex<T extends string | number> = T; | ||
declare class Edge { | ||
vertex: Vertex<string>; | ||
weight: number; | ||
constructor(vertex: Vertex<string>, weight: number); | ||
setWeight(weight: number): boolean; | ||
} | ||
declare class EdgeSet extends Set<Edge> { | ||
hasVertex(vertex: Vertex<string>): boolean; | ||
getEdge(vertex: Vertex<string>): Edge | undefined; | ||
deleteVertex(vertex: Vertex<string>): boolean; | ||
private actionOnMatch; | ||
} | ||
export type { Vertex, }; | ||
export { Graph, ListGraph, Edge, EdgeSet, }; |
import ListGraph from './list-graph'; | ||
export { ListGraph, }; | ||
class Edge { | ||
constructor(vertex, weight) { | ||
this.vertex = vertex; | ||
this.weight = weight; | ||
} | ||
setWeight(weight) { | ||
this.weight = weight; | ||
return true; | ||
} | ||
} | ||
class EdgeSet extends Set { | ||
hasVertex(vertex) { | ||
var _a; | ||
return (_a = this.actionOnMatch(vertex, () => true)) !== null && _a !== void 0 ? _a : false; | ||
} | ||
getEdge(vertex) { | ||
return this.actionOnMatch(vertex, (edge) => edge); | ||
} | ||
deleteVertex(vertex) { | ||
var _a; | ||
return (_a = this.actionOnMatch(vertex, (edge) => this.delete(edge))) !== null && _a !== void 0 ? _a : false; | ||
} | ||
actionOnMatch(vertex, action) { | ||
for (const edge of this) | ||
if (edge.vertex === vertex) | ||
return action(edge); | ||
return undefined; | ||
} | ||
} | ||
export { ListGraph, Edge, EdgeSet, }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,17 +0,8 @@ | ||
import { Graph } from '.'; | ||
declare type Vertex<T extends string | number> = T; | ||
declare class Edge { | ||
vertex: Vertex<string>; | ||
weight: number; | ||
constructor(vertex: Vertex<string>, weight: number); | ||
setWeight(weight: number): boolean; | ||
} | ||
declare class EdgeSet extends Set<Edge> { | ||
hasVertex(vertex: Vertex<string>): boolean; | ||
getEdge(vertex: Vertex<string>): Edge | undefined; | ||
deleteVertex(vertex: Vertex<string>): boolean; | ||
private actionOnMatch; | ||
} | ||
declare class ListGraph implements Graph { | ||
import type { Vertex } from '.'; | ||
import { Graph, EdgeSet, Edge } from '.'; | ||
declare class ListGraph implements Graph<string> { | ||
data: Map<string, EdgeSet>; | ||
get size(): number; | ||
get(vertex: Vertex<string>): EdgeSet | undefined; | ||
getEdge(from: Vertex<string>, to: Vertex<string>): Edge | undefined; | ||
addVertex(vertex: Vertex<string>): boolean; | ||
@@ -26,5 +17,3 @@ removeVertex(vertex: Vertex<string>): boolean; | ||
setEdgeWeight(from: Vertex<string>, to: Vertex<string>, weight: number): boolean; | ||
private _addEdge; | ||
private _removeEdge; | ||
} | ||
export default ListGraph; |
@@ -1,34 +0,16 @@ | ||
class Edge { | ||
constructor(vertex, weight) { | ||
this.vertex = vertex; | ||
this.weight = weight; | ||
import { EdgeSet, Edge, } from '.'; | ||
class ListGraph { | ||
constructor() { | ||
this.data = new Map(); | ||
} | ||
setWeight(weight) { | ||
this.weight = weight; | ||
return true; | ||
get size() { | ||
return this.data.size; | ||
} | ||
} | ||
class EdgeSet extends Set { | ||
hasVertex(vertex) { | ||
var _a; | ||
return (_a = this.actionOnMatch(vertex, () => true)) !== null && _a !== void 0 ? _a : false; | ||
get(vertex) { | ||
return this.data.get(vertex); | ||
} | ||
getEdge(vertex) { | ||
return this.actionOnMatch(vertex, (edge) => edge); | ||
} | ||
deleteVertex(vertex) { | ||
getEdge(from, to) { | ||
var _a; | ||
return (_a = this.actionOnMatch(vertex, (edge) => this.delete(edge))) !== null && _a !== void 0 ? _a : false; | ||
return (_a = this.get(from)) === null || _a === void 0 ? void 0 : _a.getEdge(to); | ||
} | ||
actionOnMatch(vertex, action) { | ||
for (const edge of this) | ||
if (edge.vertex === vertex) | ||
return action(edge); | ||
return undefined; | ||
} | ||
} | ||
class ListGraph { | ||
constructor() { | ||
this.data = new Map(); | ||
} | ||
addVertex(vertex) { | ||
@@ -51,12 +33,24 @@ if (this.data.has(vertex)) | ||
addEdge(vertexA, vertexB, weight = 1) { | ||
return this._addEdge(false, vertexA, vertexB, weight); | ||
const edgeAddedA = this.addDirectedEdge(vertexA, vertexB, weight); | ||
const edgeAddedB = this.addDirectedEdge(vertexB, vertexA, weight); | ||
return edgeAddedA || edgeAddedB; | ||
} | ||
removeEdge(vertexA, vertexB) { | ||
return this._removeEdge(false, vertexA, vertexB); | ||
const edgeRemovedA = this.removeDirectedEdge(vertexA, vertexB); | ||
const edgeRemovedB = this.removeDirectedEdge(vertexB, vertexA); | ||
return edgeRemovedA || edgeRemovedB; | ||
} | ||
addDirectedEdge(from, to, weight = 1) { | ||
return this._addEdge(true, from, to, weight); | ||
const srcEdges = this.data.get(from); | ||
const dstEdges = this.data.get(to); | ||
if (!srcEdges || !dstEdges || srcEdges.hasVertex(to)) | ||
return false; | ||
return !!srcEdges.add(new Edge(to, weight)); | ||
} | ||
removeDirectedEdge(from, to) { | ||
return this._removeEdge(true, from, to); | ||
const srcEdges = this.data.get(from); | ||
const dstEdges = this.data.get(to); | ||
if (!srcEdges || !dstEdges || !srcEdges.hasVertex(to)) | ||
return false; | ||
return srcEdges.deleteVertex(to); | ||
} | ||
@@ -71,31 +65,4 @@ getEdgeWeight(from, to) { | ||
} | ||
_addEdge(isDirected, from, to, weight) { | ||
const srcEdges = this.data.get(from); | ||
const dstEdges = this.data.get(to); | ||
if (!srcEdges || !dstEdges) | ||
return false; | ||
if (srcEdges.hasVertex(to)) | ||
return false; | ||
if (!isDirected && dstEdges.hasVertex(from)) | ||
return false; | ||
srcEdges.add(new Edge(to, weight)); | ||
if (!isDirected) | ||
dstEdges.add(new Edge(from, weight)); | ||
return true; | ||
} | ||
_removeEdge(isDirected, from, to) { | ||
const srcEdges = this.data.get(from); | ||
const dstEdges = this.data.get(to); | ||
if (!srcEdges || !dstEdges) | ||
return false; | ||
if (!srcEdges.hasVertex(to)) | ||
return false; | ||
if (!isDirected && !dstEdges.hasVertex(from)) | ||
return false; | ||
const vertexIsDeleted = srcEdges.deleteVertex(to); | ||
const otherIsDeleted = isDirected ? true : dstEdges.deleteVertex(from); | ||
return vertexIsDeleted && otherIsDeleted; | ||
} | ||
} | ||
export default ListGraph; | ||
//# sourceMappingURL=list-graph.js.map |
{ | ||
"name": "ts-structures", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "TypeScript implementation of common data structures", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60482
930