哈希和比较数组 [英] Hashing and comparing of arrays

查看:67
本文介绍了哈希和比较数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组对象的GetHashCode()如何表现?


它是否结合了元素的GetHashCode(),或者它创建的是
对象的同步块?

我想使用只读数组作为字典键,基于

他们的内容,而不是他们的身份。这是可行的直接使用

数组,还是我需要将它们包装在一个结构中,

处理GetHashCode和Equal?如果是这样,标准类库中是否有这样的包装器?



我的应用程序将有数百甚至数千这些

词典,其中一些只包含一个

arraykey-value条目,而其他一些将有数千个
的条目。特定字典中的条目数

无法提前可靠地预测或估计。与数组关联的

值将是WeakReference

对象,每个对象都指向一个具有终结器的对象,该对象将从b中删除条目。包含字典。


字典< SomeClass [],WeakReference>

泛型集合是否适用于此,或者我需要

加油吗?

解决方案

Ole Nielsby< ol ********* @ snailmail.dk>写道:

数组对象的GetHashCode()如何表现?

它是否结合了其元素的GetHashCode(),或者它是否创建了一个对象的同步块?


据我所知,两者都没有。我相信它根本不会覆盖GetHashCode

,所以使用了Object中的实现。

我想使用readonly数组作为字典键,基于数组,还是我需要将它们包装在一个处理GetHashCode和Equal的结构中?如果是这样,标准类库中是否存在这样的包装器?


我相信你需要包装它们。 (使用结构的任何理由

而不是类?)

我的应用程序将有数百甚至数千这些词典,其中一些将会只包含一个
arraykey-value条目,而其他条目将包含数千个条目。特定字典中的条目数量无法提前预测或估计。与数组关联的
值将是WeakReference
对象,每个对象都指向一个带有终结器的对象,该终结器将从包含的字典中删除该条目。

字典< SomeClass [],WeakReference>
通用集合适用于此,或者我需要对其进行调整吗?




I考虑使用HybridDictionary - 对于较小的那些,它可能会更有效。然而不幸的是,我不相信

这是一个通用版本:(


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet

如果回复小组,请不要给我发邮件


Jon Skeet [C#MVP ]< sk *** @ pobox.com>写道:

Ole Nielsby< ol ********* @ snailmail.dk>写道:

数组对象的GetHashCode()如何表现?

它是否结合了元素的GetHashCode(),或者它是否为对象创建了一个同步块?
两者都没有,据我所知。我相信它根本不会覆盖GetHashCode,因此使用了Object中的实现。




我有没有错,但我认为Object :: GetHashCode

实现会创建一个同步块,如果还没有
一个。

我想使用readonly数组作为字典键,基于它们的内容,而不是它们的身份。这是可行的直接使用
数组,还是我需要将它们包装在一个处理GetHashCode和Equal的结构中?如果是这样,标准类库中是否存在这样的包装?



我相信你需要包装它们。 (使用
结构而不是类的任何原因虽然?)




将会有大量的这些数组和包装器

将是不必要的内存臃肿。一个通用的集合

与结构一起使用,如果我理解泛型,那么就能够像使用它们一样有效地存储数组指针

直接使用我的结构定义的覆盖,当

哈希和比较时。

我会考虑使用HybridDictionary - 它可能是对于较小的那些,效率更高。然而不幸的是,
我不相信这是一个通用版本:(




那么,我想我会谷歌开源泛型

HybridDictionary一样的东西 - 我很确定它存在于某个地方,

我希望我可以用标准库来处理。


问候/ Ole N.


Ole Nielsby< ol ********* @ snailmail.dk>写道:

它是否结合了其元素的GetHashCode(),还是为对象创建了一个同步块?


根据我的意识,两者都没有。我相信它根本不会覆盖GetHashCode,因此使用了Object中的实现。



我可能有这个错误,但我认为Object :: GetHashCode
实现将创建一个同步块,如果还没有。



同步块和GetHashCode不相关。第一个是用

锁定;第二个是哈希。是什么让你认为他们与b $ b相关?

我相信你需要包装它们。 (使用
结构而不是类的任何原因虽然?)



将会有大量的这些数组,并且包装器将是不必要的内存膨胀。与结构一起使用的通用集合,如果我理解泛型,就能够像我直接使用它们一样有效地存储数组指针,同时使用我定义的覆盖
哈希和比较时的结构。




是的,如果你使用泛型,你应该没事。

< blockquote class =post_quotes>我会考虑使用HybridDictionary - 对于较小的那些,它可能会更有效率。然而不幸的是,
我不相信这是一个通用版本:(



那么,我想我会谷歌一个开源通用类似HybridDictionary的东西 - 我很确定它存在于某个地方,
我只是希望我可以使用标准库。




听起来就像你需要泛型一样。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复对于小组,请不要给我发邮件


How does the GetHashCode() of an array object behave?

Does it combine the GetHashCode() of its elements, or does
it create a sync block for the object?

I want to use readonly arrays as dictionary keys, based on
their content, not their identity. Is this feasible using the
arrays directly, or do I need to wrap them in a struct that
handles GetHashCode and Equal? If so, is such a wrapper
present in the standard class library?

My app will have hundreds or maybe thousands of these
dictionaries, some of which will contain only a single
arraykey-value entry, while others will have thousands
of entries. The number of entries in a particular dictionary
cannot be predicted or estimated reliably in advance. The
values associated with the arrays will be WeakReference
objects, each pointing to an object with a finalizer that will
remove the entry from the containing dictionary.

Would the Dictionary<SomeClass[], WeakReference>
generic collection be suitable for this, or do I need to
spice it up?

解决方案

Ole Nielsby <ol*********@snailmail.dk> wrote:

How does the GetHashCode() of an array object behave?

Does it combine the GetHashCode() of its elements, or does
it create a sync block for the object?
Neither, as far as I''m aware. I believe it doesn''t override GetHashCode
at all, so the implementation in Object is used.
I want to use readonly arrays as dictionary keys, based on
their content, not their identity. Is this feasible using the
arrays directly, or do I need to wrap them in a struct that
handles GetHashCode and Equal? If so, is such a wrapper
present in the standard class library?
You''ll need to wrap them, I believe. (Any reason for using a struct
rather than a class though?)
My app will have hundreds or maybe thousands of these
dictionaries, some of which will contain only a single
arraykey-value entry, while others will have thousands
of entries. The number of entries in a particular dictionary
cannot be predicted or estimated reliably in advance. The
values associated with the arrays will be WeakReference
objects, each pointing to an object with a finalizer that will
remove the entry from the containing dictionary.

Would the Dictionary<SomeClass[], WeakReference>
generic collection be suitable for this, or do I need to
spice it up?



I''d consider using a HybridDictionary - it''s likely to be much more
efficient for the smaller ones. Unfortunately, however, I don''t believe
there''s a generic version :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Jon Skeet [C# MVP] <sk***@pobox.com> wrote:

Ole Nielsby <ol*********@snailmail.dk> wrote:

How does the GetHashCode() of an array object behave?

Does it combine the GetHashCode() of its elements, or does
it create a sync block for the object?
Neither, as far as I''m aware. I believe it doesn''t override
GetHashCode at all, so the implementation in Object is used.



I may have got this wrong but I think the Object::GetHashCode
implementation will create a sync block if there isn''t already
one.

I want to use readonly arrays as dictionary keys, based on
their content, not their identity. Is this feasible using the
arrays directly, or do I need to wrap them in a struct that
handles GetHashCode and Equal? If so, is such a wrapper
present in the standard class library?



You''ll need to wrap them, I believe. (Any reason for using a
struct rather than a class though?)



There will be large numbers of these arrays, and wrappers
would be unnecessary memory bloat. A generic collection
used with a struct will, if I understand generics right, be able
to store the array pointers as efficiently as if I used them
directly, while using the overrides defined by my struct when
hashing and comparing.
I''d consider using a HybridDictionary - it''s likely to be much
more efficient for the smaller ones. Unfortunately, however,
I don''t believe there''s a generic version :(



Well then, I guess I will google up an open source generic
HybridDictionary-like thing - I''m pretty sure it exists somewhere,
I just hoped I could make do with the standard library.

Regards/Ole N.


Ole Nielsby <ol*********@snailmail.dk> wrote:

Does it combine the GetHashCode() of its elements, or does
it create a sync block for the object?



Neither, as far as I''m aware. I believe it doesn''t override
GetHashCode at all, so the implementation in Object is used.



I may have got this wrong but I think the Object::GetHashCode
implementation will create a sync block if there isn''t already
one.



Sync blocks and GetHashCode are unrelated. The first is to do with
locking; the second is to do with hashing. What makes you think they''re
related?

You''ll need to wrap them, I believe. (Any reason for using a
struct rather than a class though?)



There will be large numbers of these arrays, and wrappers
would be unnecessary memory bloat. A generic collection
used with a struct will, if I understand generics right, be able
to store the array pointers as efficiently as if I used them
directly, while using the overrides defined by my struct when
hashing and comparing.



Yes, if you use generics you should be okay.

I''d consider using a HybridDictionary - it''s likely to be much
more efficient for the smaller ones. Unfortunately, however,
I don''t believe there''s a generic version :(



Well then, I guess I will google up an open source generic
HybridDictionary-like thing - I''m pretty sure it exists somewhere,
I just hoped I could make do with the standard library.



It sounds like you do need generics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于哈希和比较数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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