106 lines
2.4 KiB
C#
106 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace SmartNav.Graph;
|
|
|
|
public sealed class NavGraph
|
|
{
|
|
private readonly Dictionary<string, NavNode> _nodes = new Dictionary<string, NavNode>();
|
|
|
|
private readonly Dictionary<string, List<NavEdge>> _adjacency = new Dictionary<string, List<NavEdge>>();
|
|
|
|
private readonly Dictionary<uint, List<string>> _territoryIndex = new Dictionary<uint, List<string>>();
|
|
|
|
public IReadOnlyDictionary<string, NavNode> Nodes => _nodes;
|
|
|
|
public IReadOnlyDictionary<string, List<NavEdge>> Adjacency => _adjacency;
|
|
|
|
public IReadOnlyDictionary<uint, List<string>> TerritoryIndex => _territoryIndex;
|
|
|
|
public int NodeCount => _nodes.Count;
|
|
|
|
public int EdgeCount
|
|
{
|
|
get
|
|
{
|
|
int num = 0;
|
|
foreach (List<NavEdge> value in _adjacency.Values)
|
|
{
|
|
num += value.Count;
|
|
}
|
|
return num;
|
|
}
|
|
}
|
|
|
|
public void AddNode(NavNode node)
|
|
{
|
|
bool num = !_nodes.ContainsKey(node.Id);
|
|
_nodes[node.Id] = node;
|
|
if (num)
|
|
{
|
|
_adjacency[node.Id] = new List<NavEdge>();
|
|
if (!_territoryIndex.TryGetValue(node.TerritoryId, out List<string> value))
|
|
{
|
|
value = new List<string>();
|
|
_territoryIndex[node.TerritoryId] = value;
|
|
}
|
|
value.Add(node.Id);
|
|
}
|
|
}
|
|
|
|
public void AddEdge(NavEdge edge)
|
|
{
|
|
if (!_adjacency.TryGetValue(edge.FromNodeId, out List<NavEdge> value))
|
|
{
|
|
value = new List<NavEdge>();
|
|
_adjacency[edge.FromNodeId] = value;
|
|
}
|
|
value.Add(edge);
|
|
}
|
|
|
|
public void ReplaceEdge(NavEdge edge)
|
|
{
|
|
if (!_adjacency.TryGetValue(edge.FromNodeId, out List<NavEdge> value))
|
|
{
|
|
value = new List<NavEdge>();
|
|
_adjacency[edge.FromNodeId] = value;
|
|
}
|
|
value.RemoveAll((NavEdge e) => e.ToNodeId == edge.ToNodeId && e.EdgeType == edge.EdgeType);
|
|
value.Add(edge);
|
|
}
|
|
|
|
public void RemoveEdges(string fromNodeId, Predicate<NavEdge> predicate)
|
|
{
|
|
if (_adjacency.TryGetValue(fromNodeId, out List<NavEdge> value))
|
|
{
|
|
value.RemoveAll(predicate);
|
|
}
|
|
}
|
|
|
|
public NavNode? GetNode(string nodeId)
|
|
{
|
|
if (!_nodes.TryGetValue(nodeId, out NavNode value))
|
|
{
|
|
return null;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public IReadOnlyList<NavEdge> GetEdges(string nodeId)
|
|
{
|
|
if (!_adjacency.TryGetValue(nodeId, out List<NavEdge> value))
|
|
{
|
|
return new List<NavEdge>();
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public IReadOnlyList<string> GetNodesInTerritory(uint territoryId)
|
|
{
|
|
if (!_territoryIndex.TryGetValue(territoryId, out List<string> value))
|
|
{
|
|
return new List<string>();
|
|
}
|
|
return value;
|
|
}
|
|
}
|