如何引用已传递给Map的类实例作为键JavaScript [英] How can I reference class instances that I have passed into a Map as keys JavaScript

查看:81
本文介绍了如何引用已传递给Map的类实例作为键JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Map作为邻接表来实现一个图类,并使用一个简单的Vertex类来表示图中的每个节点:

I am implementing a graph class using a Map as an adjacency list, with a simple class Vertex that I am using to represent each node in the graph:

export class Vertex {
    constructor(value) {
        if (value) this.value = value;
        else throw new Error("Value must be specified");
    }

    getValue() {
        return this.value;
    }

    setValue(value) {
        if (value) this.value = value;
        else throw new Error("Value must be specified");
    }
}

然后在我的图形类中实现了一个构造函数,两种添加顶点和边的方法:

Then in my graph class I have implemented a constructor and 2 methods for adding vertices and edges:

export class UndirectedGraph {
    constructor() {
        this.adjacencyList = new Map();
    }

    addVertex(value) {
        if (value) {
            const vertex = new Vertex(value);
            this.adjacencyList.set(vertex, []);
        }
    }

    addEdge(to, from) {
        if (
            !to ||
            !from ||
            !(to.constructor === Vertex && from.constructor === Vertex)
        ) {
            throw new Error("Arguments must be of type Vertex");
        }
        if (
            !this.adjacencyList.get(to) ||
            !this.adjacencyList.get(from)
        ) {
            throw new Error(
                "Both arguments must already be nodes in this undirected graph"
            );
        }
        this.adjacencyList.get(to).push(from);
        this.adjacencyList.get(from).push(to);
    }

    getAdjacencyList() {
        return this.adjacencyList;
    }
}

然后我要呼叫 addEdge()函数在两个Vertex类型的实例之间创建边线:

Then I want to call the addEdge() function to create an edge between 2 instances of type Vertex:

const graph = new UndirectedGraph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("B");
graph.addEdge(..., ...);

我应该如何传递给 addEdge()函数在 A和 B的特定实例之间创建边?我没有可供参考的Vertex实例变量。

What do I pass to the addEdge() function to create an edge between "A" and a particular instance of "B"? I have no variable for the Vertex instances that I can reference.

我希望图形能够存储重复的值(例如名称),因此使用类实例似乎是显而易见的选择,但现在我被困在如何访问它们包含的值,因为我不确定如何在Map中搜索类实例,即 graph.getAdjacencyList()。get(...)。感谢所有帮助

I want the graph to be able to store duplicate values, such as names, so using class instances seems like the obvious choice but now I am stuck on how to access the values they contain because I am unsure how to search for class instances within the Map i.e. graph.getAdjacencyList().get(...). All help appreciated

推荐答案

鉴于您的 addVertex 方法创建了 Vertex 实例和 addEdge 方法希望将该实例作为参数,您需要使这些实例可供这些方法的调用者使用-通过返回

Given your addVertex method creates the Vertex instance and the addEdge method expects that very instance as a parameter, you need to make it available to the caller of these methods - by returning it:

…
addVertex(value) {
    if (value) {
        const vertex = new Vertex(value);
        this.adjacencyList.set(vertex, []);
        return vertex;
    }
    // else throw new Error("no value given")?
}
…

然后您可以像使用它

const graph = new UndirectedGraph();
const vertex1 = graph.addVertex("A");
const vertex2 = graph.addVertex("B");
const vertex3 = graph.addVertex("B");
graph.addEdge(vertex1, vertex2);
graph.addEdge(vertex1, vertex3);
graph.addEdge(vertex2, vertex3);

这篇关于如何引用已传递给Map的类实例作为键JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆