[英]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.