ln.skyscanner/entities/HopMap.cs

93 lines
2.1 KiB
C#

// /**
// * File: HopMap.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
using System.Collections;
using Castle.Core.Logging;
using ln.types.odb;
using System.Linq;
namespace ln.skyscanner.entities
{
public class HopMap : Persistent
{
public Node Node { get; private set; }
public GlobalNetwork GlobalNetwork => SkyScanner.Instance.Entities.GlobalNetwork;
public int MaxDepth { get; set; } = 2;
Dictionary<Node, int> hops = new Dictionary<Node, int>();
public HopMap()
{
}
public HopMap(Node node)
{
Node = node;
AddNode(node, 0);
}
public void Reset(Node node)
{
hops.Clear();
Node = node;
AddNode(node, 0);
}
private void AddNodes(IEnumerable<Node> nodes,int level)
{
foreach (Node node in nodes)
AddNode(node, level);
}
public void AddNode(Node node,int level)
{
if (Node == null)
Node = node;
if (!hops.ContainsKey(node) || (hops[node] > level))
{
hops[node] = level;
if (level < MaxDepth)
AddNodes(GlobalNetwork.FindNeighbors(node), level + 1);
}
}
public int GetHopCount(Node node)
{
if (!hops.ContainsKey(node))
return -1;
return hops[node];
}
public HopNode[] HopNodes
{
get
{
return hops.Select((h) => new HopNode(h.Key,h.Value)).ToArray();
}
}
public struct HopNode
{
public Node Node;
public int HopCount;
public HopNode(Node node,int hopCount)
{
Node = node;
HopCount = hopCount;
}
}
}
}