我正在寻找一个集合。
我需要能够像使用 2D 整数键一样添加元素,例如 .Add(3, 4, element)
。如果我在集合范围之外添加,我需要扩展集合,这包括负面影响,尽管它可以有限制,例如 Int16 的范围就很好。集合中的每个元素都可以具有相同的类型,但我需要指定它是什么,例如 Set; s;
我还需要避免缓慢的操作,例如查找元素时进行搜索,添加到集合时性能不太重要。
有谁知道使用什么方法或最好的方法可以为课程提供答案。
I'm looking for a collection.
I need to be able to add elements as if using a 2D integer key, for example .Add(3, 4, element)
. If I add outside the range of the collection I need the collection to expand, this include negatively, although it can have a limit, for example the range of an Int16 would be good. Every element in the collection can have the same type as each other but I need to specify what that is, for example Set<type> s;
I also need to avoid slow operations such as searching when looking up an element, performance is less important when adding to the collection.
Does anyone have any ideas about what approach to use or best could provide the class in there answer.
发布评论
评论(3)
如果您想要复合键,可以使用
Tuple ;
类:Dictionary, TItem>
。如果语法太奇怪,你可以像这样包装类:
使用 Tuple 类的好处是,相等性和哈希码比较是本地处理的,所以即使它是一个类,具有相同值的两个不同的 tuple 实例也会被处理。视为平等。
If you want a compound key, you can use the
Tuple<T1,T2>
class in a :Dictionary<Tuple<T1,T2>, TItem>
.If the syntax is too weird, you may wrap the class like this :
The benefits of using Tuple class, is that the equality and hashcode comparison is natively handled, so even if it's a class, two differents instances of tuple with same values will be considered equals.
听起来您想要一个
Dictionary
。It sounds like you want a
Dictionary<int, T>
.您可以通过将其数据存储在
Dictionary>
类型的私有变量中来实现此Set
。然后您可以使用存储
You can implement this
Set<T>
by storing its data in a private variable of typeDictionary<int, Dictionary<int, T>>
.You can then store using