简体   繁体   English

我可以使用什么数据结构来表示.Net中的强类型2D数据矩阵?

[英]What data structures can I use to represent a strongly-typed 2D matrix of data in .Net?

I'm trying to represent a scoreboard for a competition and am struggling with the best data structures to use. 我正在尝试代表比赛的记分牌,并且正在努力使用最好的数据结构。

I have a list of Player objects, a list of Round objects, and for each combination, I need to store a RoundScore object (there are various parts to the score for a round). 我有一个Player对象列表,一个Round对象列表,对于每种组合,我需要存储一个RoundScore对象(一个回合的得分有不同的部分)。

What I'd like is some overall Scoreboard object where the following holds: 我想要的是一些总体Scoreboard对象,其中包含以下内容:

1 - I can access a collection of RoundScore objects identified by Round keys by providing a Player object. 1-我可以通过提供Player对象来访问Round标识的RoundScore对象的集合。 For example, maybe something like: 例如,也许类似:

public IDictionary<Round,RoundScore> PlayerScores(Player player) { ... }

2 - I can access a collection of RoundScore objects identified by Player keys by providing a Round object. 2-我可以通过提供Round对象来访问由Player键标识的RoundScore对象的集合。 eg: 例如:

public IDictionary<Player,RoundScore> RoundScores(Round round) { ... }

3 - I can access a single RoundScore object by providing a Player and a Round 3-我可以通过提供PlayerRound来访问单个RoundScore对象

4 - I can add a new Round and all Players will get a new RoundScore for that round with default values 4-我可以添加一个新Round并且所有Players都将为该回合使用默认值获得一个新的RoundScore

5 - Similarly, I can add a new Player and all Rounds will have a new RoundScore for that player with default values 5-同样,我可以添加一个新的Player并且所有Rounds都将为该播放器提供一个具有默认值的新RoundScore


I guess what I'm really looking for is a representation of a grid with Rounds on one axis, Players on the other, and RoundScores in the middle. 我猜我真正想要的是一个网格的表示,一个轴上有Rounds ,另一轴上是Players ,中间是RoundScores

Is there any data structure (or combination of data structures) already in .Net that I can use for this or will I have to roll my own? .Net中已经有任何可用于此目的的数据结构(或数据结构的组合),还是我必须自己滚动?

I believe you'll have to roll your own. 我相信您必须自己动手。 You could store your matrix of data in one of these: 您可以将数据矩阵存储在以下之一中:

List<List<RoundScore>>

Then in Round, add a field that stores the index of that Round's scores. 然后在回合中,添加一个存储该回合得分索引的字段。 Likewise, in Player, add a field for that player's scores. 同样,在Player中,为该玩家的分数添加一个字段。

If the rows are the scores for a round, then returning that list is trivial. 如果这些行是一个回合的分数,那么返回该列表是微不足道的。 To return the list of scores for a player, you could create a class that implements IList, which knows how to access the scores by index. 要返回玩家的分数列表,您可以创建一个实现IList的类,该类知道如何按索引访问分数。 By doing this, you don't have to copy the scores into a new list each time they are requested. 这样,您不必每次都将分数复制到新列表中。

For example: 例如:

List<Player> Players;
List<Round> Rounds;
List<List<RoundScore>> Scores;


List<RoundScore> GetRoundScores(Round round)
{
    return Scores[round.Index];
}

IList<RoundScore> GetRoundScores(Player player)
{
    return new PlayerScoreList(Scores, player.Index); // or better yet, cache this
}


public class PlayerScoreList : IList<RoundScore>
{
    private List<List<RoundScore>> _scores;
    private int _playerIndex;

    public RoundScore this[int index]
    {
        get
        {
            return _scores[_playerIndex][index];
        }
        set
        {
            _scores[_playerIndex][index] = value;
        }
    }

    public PlayerScoreList(List<List<RoundScore>> scores, int playerIndex)
    {
        _scores = scores;
        _playerIndex = playerIndex;
    }

    public void Add(RoundScore item)
    {
        throw new NotSupportedException();
    }

    public void Clear()
    {
        throw new NotSupportedException();
    }

    public bool Contains(RoundScore item)
    {            
        for (int i = 0; i < Count; i++)
        {
            if (this[i].Equals(item))
            {
                return true;
            }
        }

        return false;
    }

    public int Count
    {
        get { return _scores[0].Count; }
    }

    public IEnumerator<RoundScore> GetEnumerator()
    {
        for (int i = 0; i < Count; i++)
        {
            yield return this[i];
        }
    }

    // ... more methods

}

How about something like: 怎么样:

public class Matrix
{
    public List<Player> Players;
    public List<Round> Rounds;
    public Dictionary<Tuple<Player, Round>, RoundScore> RoundScores;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM