Как реализовать обобщенную структуру данных Таблица <Строка, Столбец, Значение> c#
Что я пытаюсь сделать: на основе общих коллекций платформы .NET реализовать обобщенную структуру данных Table <R, C, V>, где:
R - клавиша строки
C - ключ столбца
V - значение
Эта структура данных должна обеспечивать доступ к хранящимся в ней значениям на ключах «R» и «C» со сложностью O (1).
Продемонстрируйте работу с этой структурой данных, создав объект коллекции Table <FootballTeam, Tournament, HashSet >.
FootballTeam (название, город, год основания)
Tournament (название, международный: логическое, FoundationYear)
HashSet <int> - содержит годы, когда данная FootballTeam выиграла турнир.
Также приведите несколько собственных примеров использования этой структуры данных.
Я не знаю, как это сделать дальше, и если я все делаю правильно, я хочу создать словарь в словаре.
using System;
using System.Collections.Generic;
using System.Linq;
namespace task5_virobnicha
{
public class FootballTeam
{
public string title
{
get;
set;
}
public string city
{
get;
set;
}
public int foundationYear
{
get;
set;
}
public FootballTeam(string title, string city, int foundationYear)
{
this.title = title;
this.city = city;
this.foundationYear = foundationYear;
}
public override string ToString()
{
return $"{this.title} {this.city} {this.foundationYear}";
}
public override bool Equals(object obj)
{
if (obj != null && obj is FootballTeam)
{
FootballTeam someone = (FootballTeam)obj;
if (this.title == someone.title)
{
if (this.city == someone.city)
{
if (this.foundationYear == someone.foundationYear)
{
return true;
}
}
}
}
return false;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
public class Tournament
{
public string title
{
get;
set;
}
public bool international
{
get;
set;
}
public int foundationYear
{
get;
set;
}
public Tournament(string title, bool international, int foundationYear)
{
this.title = title;
this.international = international;
this.foundationYear = foundationYear;
}
public override string ToString()
{
return $"{this.title} {this.international} {this.foundationYear}";
}
public override bool Equals(object obj)
{
if (obj != null && obj is Tournament)
{
Tournament someone = (Tournament)obj;
if (this.title == someone.title)
{
if (this.international == someone.international)
{
if (this.foundationYear == someone.foundationYear)
{
return true;
}
}
}
}
return false;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
FootballTeam team1 = new FootballTeam("Vorskla", "Poltava", 1955);
FootballTeam team2 = new FootballTeam("Desna", "Chernihiv", 1960);
FootballTeam team3 = new FootballTeam("Dinamo", "Kyiv", 1927);
FootballTeam team4 = new FootballTeam("Zorya", "Luhansk", 1923);
FootballTeam team5 = new FootballTeam("Karpati", "Lviv", 1963);
Tournament tourn1 = new Tournament("EURO2012", true, 2012);
Tournament tourn2 = new Tournament("UEFA", true, 2010);
Tournament tourn3 = new Tournament("OblastTourn", false, 2014);
Tournament tourn4 = new Tournament("ChampUkr", false, 2005);
Tournament tourn5 = new Tournament("SyhivChamp", false, 2019);
HashSet<int> myHash1 = new HashSet<int>();
myHash1.Add(2012);
myHash1.Add(2013);
HashSet<int> myHash2 = new HashSet<int>();
myHash2.Add(2019);
myHash2.Add(2010);
myHash2.Add(2015);
HashSet<int> myHash3 = new HashSet<int>();
myHash3.Add(2015);
HashSet<int> myHash4 = new HashSet<int>();
myHash4.Add(2005);
myHash4.Add(2015);
HashSet<int> myHash5 = new HashSet<int>();
myHash5.Add(2019);
myHash5.Add(2020);
FootballTeam[] listOfTeams = new FootballTeam[] { team1, team2, team3, team4, team5 };
for (int i = 0; i < listOfTeams.Length; i++)
{
Console.WriteLine(listOfTeams[i]);
}
Console.WriteLine("---------------------------");
Tournament[] listOfTornaments = new Tournament[] { tourn1, tourn2, tourn3, tourn4, tourn5 };
for (int i = 0; i < listOfTornaments.Length; i++)
{
Console.WriteLine(listOfTornaments[i]);
}
Console.WriteLine("---------------------------");
HashSet <int>[] listOfHashSet = new HashSet <int>[] { myHash1, myHash2, myHash3, myHash4, myHash5 };
foreach (int a in myHash1)
{
Console.Write(a);
Console.Write(" ");
}
Console.WriteLine();
foreach (int b in myHash2)
{
Console.Write(b);
Console.Write(" ");
}
Console.WriteLine();
foreach (int c in myHash3)
{
Console.Write(c);
Console.Write(" ");
}
Console.WriteLine();
foreach (int d in myHash4)
{
Console.Write(d);
Console.Write(" ");
}
Console.WriteLine();
foreach (int e in myHash5)
{
Console.Write(e);
Console.Write(" ");
}
Console.WriteLine();
Console.WriteLine("---------------------------");
Console.WriteLine("Work with dictionary");
Dictionary<FootballTeam, Tournament> My_dict1 =
new Dictionary<FootballTeam, Tournament>();
for(int i =0;i< listOfTeams.Length;i++)
{
My_dict1.Add(listOfTeams[i], listOfTornaments[i]);
}
foreach(var item in My_dict1)
{
Console.WriteLine(item);
}
Console.WriteLine("---------------------------");
Dictionary<Dictionary<FootballTeam , Tournament>, HashSet<int>> My_dict2 =
new Dictionary<Dictionary<FootballTeam , Tournament>, HashSet<int>>();
//My_dict2.Add(My_dict1, listOfHashSet[]);
foreach (var item in listOfHashSet)
{
foreach(var item2 in item)
{
Console.Write(item2);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine("---------------------------");
Console.WriteLine();
Console.WriteLine(My_dict1.ToList()[0]);
}
}
}