[英]What Strongly-Typed Data-Structure can I use to hold a collection of multiple RecordSets with different shape
[英]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-我可以通過提供Player
和Round
來訪問單個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.