簡體   English   中英

我可以使用什么數據結構來表示.Net中的強類型2D數據矩陣?

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

我正在嘗試代表比賽的記分牌,並且正在努力使用最好的數據結構。

我有一個Player對象列表,一個Round對象列表,對於每種組合,我需要存儲一個RoundScore對象(一個回合的得分有不同的部分)。

我想要的是一些總體Scoreboard對象,其中包含以下內容:

1-我可以通過提供Player對象來訪問Round標識的RoundScore對象的集合。 例如,也許類似:

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

2-我可以通過提供Round對象來訪問由Player鍵標識的RoundScore對象的集合。 例如:

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

3-我可以通過提供PlayerRound來訪問單個RoundScore對象

4-我可以添加一個新Round並且所有Players都將為該回合使用默認值獲得一個新的RoundScore

5-同樣,我可以添加一個新的Player並且所有Rounds都將為該播放器提供一個具有默認值的新RoundScore


我猜我真正想要的是一個網格的表示,一個軸上有Rounds ,另一軸上是Players ,中間是RoundScores

.Net中已經有任何可用於此目的的數據結構(或數據結構的組合),還是我必須自己滾動?

我相信您必須自己動手。 您可以將數據矩陣存儲在以下之一中:

List<List<RoundScore>>

然后在回合中,添加一個存儲該回合得分索引的字段。 同樣,在Player中,為該玩家的分數添加一個字段。

如果這些行是一個回合的分數,那么返回該列表是微不足道的。 要返回玩家的分數列表,您可以創建一個實現IList的類,該類知道如何按索引訪問分數。 這樣,您不必每次都將分數復制到新列表中。

例如:

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

}

怎么樣:

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