如何使用键的自定义比较器从通用字典中获取值?

发布于 2024-12-19 21:05:06 字数 963 浏览 1 评论 0 原文

我有一个通用的对象字典,想要使用自定义比较器来更新字典中的值。

myObjects 包含对象的字典,其值为该对象存在的次数。请注意,该值可能会使用不同的比较器多次增加或完全删除。

testObject 是我的自定义对象。

customComparer 是一个基于 testObject 类型动态变化的比较器。但所有比较器的类型都是 IEqualityComparer

IDictionary<MyObject, int> myObjects;
var testObject;
var customComparer;

if (myObjects.Keys.Contains(testObject, customComparer))
{
    //get the value, if its > 1 then decrement the value
    //else remove the entry entirely
    //not sure how to get the value based on my custom comparer??

    //this code below does not work because it requires the custom comparer
    //var occurrences = myObjects[testObject];
    //if (occurrences > 1)
    //    myObjects[testObject]--;
    //else
    //    myObjects.Remove(testObject);
}
else
{
    myObjects.Add(testObject, 1);
}

我可以使用 Keys.Contains 来确定对象是否存在于自定义比较器中,但我不确定如何更新该值?

I have a generic dictionary of objects and want to use a custom comparer to update the value in the dictionary.

myObjects contains a dictionary of objects and the value is the number of times that object exists. Note that the value may be incremented numerous times using different comparators or removed altogether.

testObject is my custom object.

customComparer is a dynamically changing comparer based on the type of testObject. but all comparers are of the type IEqualityComparer<MyObject>

IDictionary<MyObject, int> myObjects;
var testObject;
var customComparer;

if (myObjects.Keys.Contains(testObject, customComparer))
{
    //get the value, if its > 1 then decrement the value
    //else remove the entry entirely
    //not sure how to get the value based on my custom comparer??

    //this code below does not work because it requires the custom comparer
    //var occurrences = myObjects[testObject];
    //if (occurrences > 1)
    //    myObjects[testObject]--;
    //else
    //    myObjects.Remove(testObject);
}
else
{
    myObjects.Add(testObject, 1);
}

I can use Keys.Contains to determine if the object exists with custom comparer but then i'm not sure how to update the value?

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

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

发布评论

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

评论(2

守望孤独 2024-12-26 21:05:06

创建字典时,您应该在 IEqualityComparer >构造函数。字典构建后,您无法更改相等比较器。

可以迭代键值对,直到找到根据自定义比较器匹配的键,但这样您就无法利用字典提供的功能。

When you create the dictionary you should provide your custom IEqualityComparer in the constructor. You can't change the equality comparer after the dictionary is constructed.

You could iterate over the key value pairs until you find a key that matches according to your custom comparer, but then you aren't taking advantage of the features that a Dictionary provides.

草莓酥 2024-12-26 21:05:06

对此没有快速实施。相反,我创建了一个包装器 IEqualityComparer,其中包含许多内部相等比较器。包装器重写了 Equals 和 GetHashCode 方法,这些方法根据比较对象的属性选择适当的相等比较器内部字典。

public class WrapperComparer : IEqualityComparer<MyObject>
{
    private IDictionary<string, IEqualityComparer<MyObject>> _myComparerList;

    public bool Equals(MyObject x, MyObject y)
    {
        var comparer = _myComparerList[x.SomeProperty];
        return comparer.Equals(x, y);
    }

    public bool GetHashCode(MyObject obj)
    {
        var comparer = _myComparerList[obj.SomeProperty];
        return comparer.GetHashCode(obj);
    }
}

然后比较就可以了...

var testObject;
var customComparer = new WrapperComparer(list of comparers here);
IDictionary<MyObject, int> myObjects = 
    new Dictionary<MyObject, int>(customComparer);

if (myObjects.ContainsKey(testObject))
{
    var occurrences = myObjects[testObject];
    if (occurrences > 1)
      myObjects[testObject]--;
    else
      myObjects.Remove(testObject);
}
else
{
    myObjects.Add(testObject, 1);
}

There was no quick imlpementation for this. Instead, I created a wrapper IEqualityComparer that housed numerous internal equality comparers. The wrapper overwrote the Equals and GetHashCode methods which picked the appropriate internal dictionary of equality comparers based on a property from the comparison object.

public class WrapperComparer : IEqualityComparer<MyObject>
{
    private IDictionary<string, IEqualityComparer<MyObject>> _myComparerList;

    public bool Equals(MyObject x, MyObject y)
    {
        var comparer = _myComparerList[x.SomeProperty];
        return comparer.Equals(x, y);
    }

    public bool GetHashCode(MyObject obj)
    {
        var comparer = _myComparerList[obj.SomeProperty];
        return comparer.GetHashCode(obj);
    }
}

Then the comparison works...

var testObject;
var customComparer = new WrapperComparer(list of comparers here);
IDictionary<MyObject, int> myObjects = 
    new Dictionary<MyObject, int>(customComparer);

if (myObjects.ContainsKey(testObject))
{
    var occurrences = myObjects[testObject];
    if (occurrences > 1)
      myObjects[testObject]--;
    else
      myObjects.Remove(testObject);
}
else
{
    myObjects.Add(testObject, 1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文