是否可以使用哈希码作为位掩码来有效地存储/跟踪集合成员身份? [英] Is It Possible to Use Hashcodes as a Bitmask to Efficiently Store/Track Collection Membership?

查看:179
本文介绍了是否可以使用哈希码作为位掩码来有效地存储/跟踪集合成员身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个解决方案,可以通过Object.GetHashCode获取其哈希码,然后将其存储在HashSet<int>中,从而跟踪我感兴趣的对象.

Currently I have a solution where I keep track of objects I am interested in by getting their hashcode via Object.GetHashCode and then storing them in a HashSet<int>.

但是,我也一直在学习有关位掩码和按位操作的知识,我对它们很感兴趣. 这是一个很好的问题,我发现与我的问题很接近我想做.但是,我似乎无法使哈希码有效地工作.

However, I have also been learning about bitmasks and bitwise operations and I am quite intrigued by them. Here is a great question that I found close to what I am looking to do. However, I cannot seem to make this work efficiently for hash codes.

也有这个问题,但似乎当哈希码为int(32位)时,可以处理5位数字.

There is also this question, but it seems to deal with 5-bit numbers, when hash codes are int's (32-bits).

我确实得到了BitArray解决方案(来自第一个引用的问题),但是它的速度不如我当前的HashSet方法快.这是由于BitArray中需要创建的大量项目来说明所有已知的哈希码(int.MaxValue).

I do get the BitArray solution (from the first referenced question) working, but it is not as fast as my current HashSet approach is. This is due to the sheer number of items in the BitArray that need to be created to account for all known hash codes (int.MaxValue).

在使用BitArray进行这项工作时,应该考虑一些位掩码/按位操作吗?还是我完全走开了人迹罕至的地方,应该坚持使用我的HashSet<int>解决方案?

Is there some a bitmask/bitwise operation I should consider in making this work with a BitArray? Or have I totally gone off the beaten path here and should simply stick with my HashSet<int> solution?

推荐答案

这确实是走人迹罕至的例子,加上对引用/内存管理的误解.就我而言,我正在使用Dictionary<TKey, TValue>对象存储实例上方法调用的缓存结果.我不能使用ConditionalWeakTable,因为键至少包含三个部分:

This was indeed an instance of going off the beaten path, paired with a misunderstanding of references/memory management. In my case I was using a Dictionary<TKey, TValue> object to store cached results from method calls on an instance. I couldn't use a ConditionalWeakTable because there are at least three parts to a key:

  1. 包含要在其中缓存结果的方法的实例.
  2. 所说的方法.
  3. 参数被传递到方法中.

如果使用ConditionalWeakTable,则必须创建还需要另一个分配"的另一个对象"(类),但更糟糕的是,该对象将不会被任何内容引用(除了ConditionalWeakTable之外),并且会导致垃圾回收初次收集(确实是

If I used a ConditionalWeakTable I would have to create "yet another object" (class) that would require yet another allocation, but worse yet would not be referenced by anything (besides the ConditionalWeakTable) and would get garbage collected on the first pass (this is indeed related to another open -- but now also solved -- question of mine).

经过一番思考之后,我意识到答案确实在ConditionalWeakTable中.答案不是使用上面的3个项目作为键,而是使用由第1部分和第2部分组成的Delegate,然后将Dictionary与该部分配对.

After much thought around this, I have realized that the answer does indeed lie in ConditionalWeakTable. Rather than use the 3 items above as a key, the answer is to use a Delegate made up from parts 1 and 2, and then pair a Dictionary to that.

或者将其作为字段初始化:

Or to put as a field initialization:

readonly ConditionalWeakTable<Delegate, Dictionary<object[], object>> cache = new ConditionalWeakTable<Delegate, Dictionary<object[], object>>();

(还要注意的是使用 与上面的词典一起使用)

(Also of note is the use of a StructuralEqualityComparer with the above dictionary to make this work).

这当然可以满足我的所有需求.它将跟踪对象,直到从内存中清除了包含方法(并且包含Delegate的方法)的引用对象.此时,缓存的字典也将被释放,清除其所有内容.

This of course does everything I want. It will keep track of objects until the referenced object which contains the method (and from which the Delegate is comprised) is cleaned from memory. At this point, the cached dictionary will also be released as well, clearing out all of its contents.

这篇关于是否可以使用哈希码作为位掩码来有效地存储/跟踪集合成员身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆