import * as $N from "./BaseNode" ; import { TypedEdge } from "../typed/TypedEdge" ; import * as $SU from "@/utils/StructUtils" ; export interface IConnectedNodes { a : $N . IBaseNode ; b : $N . IBaseNode ; } export type EdgeFeatures = { [ k ] : any ; } ; param _id param _label export interface IBaseEdge { /**
     * Getters
     */ readonly id : string ; readonly label : string ; readonly features : EdgeFeatures ; getID ( ) : string ; getLabel ( ) : string ; setLabel ( label ) : void ; getFeatures ( ) : EdgeFeatures ; getFeature ( key ) : any ; f ( key ) : any | undefined ; setFeatures ( features ) : void ; setFeature ( key , value ) : void ; deleteFeature ( key ) : any ; clearFeatures ( ) : void ; isDirected ( ) : boolean ; isWeighted ( ) : boolean ; getWeight ( ) : number ; setWeight ( w ) : void ; getNodes ( ) : IConnectedNodes ; clone ( node_a , node_b ) : IBaseEdge ; } export interface BaseEdgeConfig { directed ? : boolean ; weighted ? : boolean ; weight ? : number ; label ? : string ; features ? : EdgeFeatures ; } class BaseEdge implements IBaseEdge { protected _directed : boolean ; protected _weighted : boolean ; protected _weight : number ; protected _label : string ; protected _features : EdgeFeatures ; constructor ( protected _id , protected _node_a , protected _node_b , config ? ) { if ( ! ( _node_a instanceof $N . BaseNode ) || ! ( _node_b instanceof $N . BaseNode ) ) { throw new Error ( "cannot instantiate edge without two valid node objects" ) ; } config = config || { } ; this . _directed = config . directed || false ; this . _weighted = config . weighted || false ; this . _weight = this . _weighted ? isNaN ( config . weight ) ? 1 : config . weight : undefined ; this . _label = config . label || this . _id ; this . _features = config . features != null ? $SU . clone ( config . features ) : { } ; } get id ( ) : string { return this . _id ; } get label ( ) : string { return this . _label ; } get features ( ) : EdgeFeatures { return this . _features ; } getID ( ) { return this . _id ; } getLabel ( ) { return this . _label ; } setLabel ( label ) { this . _label = label ; } getFeatures ( ) { return this . _features ; } getFeature ( key ) { return this . _features [ key ] ; } f ( key ) { return this . getFeature ( key ) ; } setFeatures ( features ) { this . _features = $SU . clone ( features ) ; } setFeature ( key , value ) { this . _features [ key ] = value ; } deleteFeature ( key ) { let feat = this . _features [ key ] ; delete this . _features [ key ] ; return feat ; } clearFeatures ( ) { this . _features = { } ; } isDirected ( ) { return this . _directed ; } isWeighted ( ) { return this . _weighted ; } getWeight ( ) { return this . _weight ; } setWeight ( w ) { if ( ! this . _weighted ) { throw new Error ( "Cannot set weight on unweighted edge." ) ; } this . _weight = w ; } getNodes ( ) { return { a : this . _node_a , b : this . _node_b } ; } clone ( new_node_a , new_node_b ) { if ( ! ( new_node_a instanceof $N . BaseNode ) || ! ( new_node_b instanceof $N . BaseNode ) ) { throw new Error ( "refusing to clone edge if any new node is invalid" ) ; } return new BaseEdge ( this . _id , new_node_a , new_node_b , { directed : this . _directed , weighted : this . _weighted , weight : this . _weight , label : this . _label , } ) ; } static isTyped ( arg ) { return ! ! arg . type ; } } export { BaseEdge } ;