tree-multimap-typed
Version:
108 lines (107 loc) • 3.8 kB
JavaScript
;
/**
* data-structure-typed
*
* @author Pablo Zeng
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
* @license MIT License
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MapGraph = exports.MapEdge = exports.MapVertex = void 0;
const directed_graph_1 = require("./directed-graph");
class MapVertex extends directed_graph_1.DirectedVertex {
constructor(key, value, lat, long) {
super(key, value);
this.lat = lat;
this.long = long;
}
}
exports.MapVertex = MapVertex;
class MapEdge extends directed_graph_1.DirectedEdge {
constructor(src, dest, weight, value) {
super(src, dest, weight, value);
}
}
exports.MapEdge = MapEdge;
/**
* Directed graph variant carrying geospatial coordinates.
* @template V - Vertex value type.
* @template E - Edge value type.
* @template VO - Concrete vertex class (MapVertex<V>).
* @template EO - Concrete edge class (MapEdge<E>).
* @remarks Time O(1), Space O(1)
* @example examples will be generated by unit test
*/
class MapGraph extends directed_graph_1.DirectedGraph {
/**
* Construct a MapGraph.
* @param originCoord - Origin coordinate `[lat, long]` used as default.
* @param bottomRight - Optional bottom-right coordinate for bounding boxes.
* @remarks Time O(1), Space O(1)
*/
constructor(originCoord, bottomRight) {
super();
this._originCoord = [0, 0];
this._originCoord = originCoord;
this._bottomRight = bottomRight;
}
get originCoord() {
return this._originCoord;
}
get bottomRight() {
return this._bottomRight;
}
/**
* Create a map vertex with optional coordinates.
* @param key - Vertex identifier.
* @param value - Optional payload.
* @param lat - Latitude (defaults to `originCoord[0]`).
* @param long - Longitude (defaults to `originCoord[1]`).
* @returns MapVertex instance.
* @remarks Time O(1), Space O(1)
*/
createVertex(key, value, lat = this.originCoord[0], long = this.originCoord[1]) {
return new MapVertex(key, value, lat, long);
}
/**
* Create a map edge (directed) with optional weight/value.
* @param src - Source key.
* @param dest - Destination key.
* @param weight - Edge weight.
* @param value - Edge payload.
* @returns MapEdge instance.
* @remarks Time O(1), Space O(1)
*/
createEdge(src, dest, weight, value) {
return new MapEdge(src, dest, weight, value);
}
/**
* Deep clone as the same concrete class.
* @returns A new graph of the same concrete class (`this` type).
* @remarks Time O(V + E), Space O(V + E)
*/
clone() {
return super.clone();
}
/**
* Include `originCoord` and `bottomRight` so `clone()/filter()` preserve geospatial settings.
* @returns Options bag extending super snapshot.
* @remarks Time O(1), Space O(1)
*/
_snapshotOptions() {
return Object.assign(Object.assign({}, super._snapshotOptions()), { originCoord: this.originCoord, bottomRight: this.bottomRight });
}
/**
* Re-create a same-species MapGraph instance from snapshot options.
* @param options - Snapshot options providing `originCoord`/`bottomRight`.
* @returns Empty MapGraph instance of `this` type.
* @remarks Time O(1), Space O(1)
*/
_createInstance(options) {
const { originCoord, bottomRight } = (options || {});
const oc = (originCoord !== null && originCoord !== void 0 ? originCoord : this.originCoord);
const br = (bottomRight !== null && bottomRight !== void 0 ? bottomRight : this.bottomRight);
return new MapGraph(oc, br);
}
}
exports.MapGraph = MapGraph;