English 中文(简体)
HashSet。 不与平等委员会合作
原标题:HashSet.Remove not working with EqualityComparer
  • 时间:2010-09-01 21:52:31
  •  标签:
  • c#
  • hashset

我期待与某个特定的平等委员会共同创建的哈希斯特公司在驱逐行动中使用该比方。 尤其是因为“康涅斯”行动确实恢复。

我使用的是:

public virtual IEnumerable<Allocation> Allocations { get { return _allocations; } }
private ICollection<Allocation> _allocations; 

public Activity(IActivitySubject subject) {    // constructor
    ....
    _allocations = new HashSet<Allocation>(new DurationExcludedEqualityComparer());
}

public virtual void ClockIn(Allocation a)
{
    ...
    if (_allocations.Contains(a)) 
        _allocations.Remove(a);
    _allocations.Add(a);
}

下面是一些迅速和 d脏的LINQ,使我想到了我所希望的逻辑,但我猜测,在平等委员会的基础上,哈希特人会大大加快。

public virtual void ClockIn(Allocation a)
{
    ...
    var found = _allocations.Where(x => x.StartTime.Equals(a.StartTime) && x.Resource.Equals(a.Resource)).FirstOrDefault();
    if (found != null)
    {
            if (!Equals(found.Duration, a.Duration))
            {
                found.UpdateDurationTo(a.Duration);
            }
    }
    else
    {
            _allocations.Add(a);
    }

谁会说,如果被拘留者成功,驱逐会失败?

Cheers,
Berryl

= = EDIT = = = 比较器

public class DurationExcludedEqualityComparer : EqualityComparer<Allocation>
{
    public override bool Equals(Allocation lhs, Allocation rhs)
    {
        if (ReferenceEquals(null, rhs)) return false;
        if (ReferenceEquals(lhs, null)) return false;
        if (ReferenceEquals(lhs, rhs)) return true;

        return 
            lhs.StartTime.Equals(rhs.StartTime) &&
            lhs.Resource.Equals(rhs.Resource) && 
            lhs.Activity.Equals(rhs.Activity);
    }

    public override int GetHashCode(Allocation obj) {
        if (ReferenceEquals(obj, null)) return 0;
        unchecked
        {
            var result = 17;
            result = (result * 397) ^ obj.StartTime.GetHashCode();
            result = (result * 397) ^ (obj.Resource != null ? obj.Resource.GetHashCode() : 0);
            result = (result * 397) ^ (obj.Activity != null ? obj.Activity.GetHashCode() : 0);
            return result;
        }
    }
}

== UPDATE - FIXED ==

好消息是,哈希特人没有被打断,工作确实应该做到。 对我来说,坏消息是,在研究树木的树叶时,我无法看到森林,怎么会令人难以置信。

如果你看上“创造和安放”的类别;拥有哈希特,然后再看比较师,以找出与其不相干之处,那么答案实际上就在上面的代码中。 方便第一人查找。

感谢所有关注该守则的人!

最佳回答

您的代码“工程”似乎在<条码>、Starttime和Resource <>/code>中忽略<条码>的,而您的<条码>IEComparer<Alposition> 执行则看所有三条。 贵问题是否与这个问题有关?

另外,您的<代码>Starttime,Resources unchangeing? 否则,因为它们影响到你的<代码>。 GetHashCode 因此,我认为你冒着打破你的<代码>HashSet<Alposition>的风险。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签