summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/City.cs
blob: 1990094f1ef312d40b795b38032a2910fd8895c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using Graph;
using Interfaces;

namespace TSP
{

    public class City : IVertex<Road, double>, IComparable<INode<double>>
    {
        public int Id { get; set; }
        public double Key { get; set; }

        public int Parent { get; set; } = -1;
        public List<Road> Edges { get; set; } = new List<Road>();

        // indexer for accessing edge with v of index 
        public Road this[int index] { get => Edges.Find(edge => edge.V == index); }

        public City() { }

        public City(int id) =>
            Id = id;

        public City(int id, List<Road> edges) : this(id) =>
            Edges = edges;

        public City(City vertex)
        {
            Id = vertex.Id;
            Parent = vertex.Parent;
            Key = vertex.Key;
            foreach (Road edge in vertex.Edges)
            {
                Road newEdge = new Road(edge.U, edge.V, edge.Weight);
                Edges.Add(newEdge);
            }
        }
        public City(Vertex<Edge<double>, double> vertex)
        {
            Id = vertex.Id;
            Parent = vertex.Parent;
            Key = vertex.Key;
            foreach (Edge<double> edge in vertex.Edges)
            {
                Road newEdge = new Road(edge.U, edge.V, edge.Weight);
                Edges.Add(newEdge);
            }
        }

        public int CompareTo(INode<double> node) =>
            Key.CompareTo(node.Key);

        public override string ToString() => $"{Id} {String.Join(" ", Edges.Select(e => (e.V, e.Weight)))}";
    }
}