package com.example.portfolio3;

// origin: <https://moodle.ruc.dk/course/section.php?id=211877>

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/// Adjecency Map Graph - A map from vertices to map of target vertex to edge
class AdjMapGraph extends AbstractGraph {

	/// foo
	AdjMapGraph() { }

	/// foo
	private Map<Vertex, Map<Vertex, Edge>> outEdge = new HashMap<>();

	/// foo
	public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
		Edge e = new Edge(v1,v2, w);
		if (!outEdge.containsKey(e.from()))
			outEdge.put(e.from(), new HashMap<Vertex, Edge>());
		outEdge.get(e.from()).put(e.to(), e);
	}

	/// foo
	public Collection<Edge> edges() {
		Set<Edge> edges = new HashSet<>();
		for (Vertex v : outEdge.keySet())
			for (Vertex w : outEdge.get(v).keySet())
				edges.add(outEdge.get(v).get(w));

		return edges;
	}

	/// foo
	public Collection<Edge> outEdge(final Vertex v) {
		return outEdge.get(v).values();
	}

	/// foo
	public Integer getWeight(final Vertex v1, final Vertex v2) {
		// constant time operation
		if (!outEdge.containsKey(v1))
			return null;
		if (!outEdge.get(v1).containsKey(v2))
			return null;

		return outEdge.get(v1).get(v2).weight();
	}
}