最佳C#数据结构的随机秩序? [英] Best C# data structure for random order population?
本文介绍了最佳C#数据结构的随机秩序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中,我有一个用例,从 int
s到集合。
In C# I have a use case where I have a mapping from int
s to collections.
-
int
s是从1到n的密集(但不是压缩)集合,其中n不为人知。 - 这些单元格将以随机顺序加载。
- 每个单元格的边际成本应该是理想的(与
List< T>
或T []
) - 我希望按需要填充单元格
- the
int
s are a dense (but not packed) set from 1 to n where n is not known. - The cells will be loaded in random order.
- the marginal cost of each cell should be ideal (as good as a
List<T>
orT[]
) - I'd like to have the cells default filled on demand
这是最好的结构是什么?
What is the best structure for this?
A 列表< T& ;
会工作得很好(在空间比在 Dictionary<>
更好),并通过从中得到我可以得到我想要的大部分,有什么更好的吗?
A List<T>
would work well (better in space than a Dictionary<>
) and by deriving from it I can get much of what I want, but is there something better? As in the best code is code you don't write.
推荐答案
A Dictionary< int,Cell> ;
听起来像是一个很好的匹配。或者您可以很容易地使用 List< Cell>
,只需确保在必要时展开它:
A Dictionary<int, Cell>
sounds like a good match to me. Or you could use a List<Cell>
quite easily, and just make sure that you expand it where necessary:
public static void EnsureCount<T>(List<T> list, int count)
{
if (list.Count > count)
{
return;
}
if (list.Capacity < count)
{
// Always at least double the capacity, to reduce
// the number of expansions required
list.Capacity = Math.Max(list.Capacity*2, count);
}
list.AddRange(Enumerable.Repeat(default(T), list.Capacity-list.Count));
}
这篇关于最佳C#数据结构的随机秩序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文