时间:2019-03-17 标签:c#2Dautoexpandablecollection

发布于 2024-12-25 08:27:14 字数 285 浏览 2 评论 0 原文

我正在寻找一个集合。

我需要能够像使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

衣神在巴黎 2025-01-01 08:27:14

如果您想要复合键,可以使用 Tuple ; 类:Dictionary, TItem>

var coll = new Dictionary<Tuple<int,int>, AnyClass>();
coll.Add(new Tuple<int,int>(2, 3), new AnyClass("foo"));
coll.Add(new Tuple<int,int>(4, 2), new AnyClass("bar"));

var foo = coll[new Tuple<int,int>(2,3)];
var bar = coll[new Tuple<int,int>(4,2)];

如果语法太奇怪,你可以像这样包装类:

public class Dictionary2d<TKey1, TKey2, TItem> : Dictionary<Tuple<TKey1, TKey2>,TItem>
{
    public void Add(TKey1 k1, TKey2, TItem item) {
        this.Add(Tuple.Create(k1,k2), item);
    }

    public TItem this[TKey1 k1, TKey2 k2] {
        get { return this[Tuple.Create(k1,k2)]; }
    }
}

public class Program
{
    static void Main() {
        var coll = new Dictionary2d<int,int, AnyClass>();
        coll.Add(2, 3, new AnyClass("foo"));
        coll.Add(4, 2, new AnyClass("bar"));

        var foo = coll[2,3];
        var bar = coll[4,2];
    }
}

使用 Tuple 类的好处是,相等性和哈希码比较是本地处理的,所以即使它是一个类,具有相同值的两个不同的 tuple 实例也会被处理。视为平等。

If you want a compound key, you can use the Tuple<T1,T2> class in a : Dictionary<Tuple<T1,T2>, TItem>.

var coll = new Dictionary<Tuple<int,int>, AnyClass>();
coll.Add(new Tuple<int,int>(2, 3), new AnyClass("foo"));
coll.Add(new Tuple<int,int>(4, 2), new AnyClass("bar"));

var foo = coll[new Tuple<int,int>(2,3)];
var bar = coll[new Tuple<int,int>(4,2)];

If the syntax is too weird, you may wrap the class like this :

public class Dictionary2d<TKey1, TKey2, TItem> : Dictionary<Tuple<TKey1, TKey2>,TItem>
{
    public void Add(TKey1 k1, TKey2, TItem item) {
        this.Add(Tuple.Create(k1,k2), item);
    }

    public TItem this[TKey1 k1, TKey2 k2] {
        get { return this[Tuple.Create(k1,k2)]; }
    }
}

public class Program
{
    static void Main() {
        var coll = new Dictionary2d<int,int, AnyClass>();
        coll.Add(2, 3, new AnyClass("foo"));
        coll.Add(4, 2, new AnyClass("bar"));

        var foo = coll[2,3];
        var bar = coll[4,2];
    }
}

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.

浅黛梨妆こ 2025-01-01 08:27:14

听起来您想要一个 Dictionary

It sounds like you want a Dictionary<int, T>.

娇纵 2025-01-01 08:27:14

您可以通过将其数据存储在 Dictionary> 类型的私有变量中来实现此 Set

然后您可以使用存储

public void Add(int key1, int key2, T value)
{
    _storage[key1][key2] = value;
}

You can implement this Set<T> by storing its data in a private variable of type Dictionary<int, Dictionary<int, T>>.

You can then store using

public void Add(int key1, int key2, T value)
{
    _storage[key1][key2] = value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文