如何判断TBucketList的存储桶数 [英] How to judge number of buckets for TBucketList
问题描述
我一直在使用TBucketList和TObjectBucketList满足我的所有哈希需求,但从未尝试过切换存储桶数.我隐约记得在Data Structures类中这是什么意思,但是有人可以在Delphi中详细说明该特定类的细微差别
I've been using the TBucketList and TObjectBucketList for all my hashing needs, but never experiemented with switching the number of buckets. I vaguely remember what this means from Data Structures class, but could someone elaborated on the nuances of this particular class in Delphi
The following table lists the possible values:
Value Number of buckets
bl2 2
bl4 4
bl8 8
bl16 16
bl32 32
bl64 64
bl128 128
bl256 256
推荐答案
TBucketList
和TObjectBucketList
存储指针.他们使用的哈希函数只是掩盖了地址的高位.掩盖多少位取决于对象具有多少个存储桶.例如,如果使用bl2
,则会屏蔽掉31位,只有地址的一位确定存储桶.使用bl256
,将使用指针的整个字节.它是中间两个字节之一.权衡只是您将拥有的存储桶数.一个存储桶仅占用8个字节,因此拥有256个字节并不是很大的代价.
TBucketList
and TObjectBucketList
store pointers. The hash function they use simply masks out the upper bits of the address. How many bits get masked out depends on how many buckets the object has. If you use bl2
, for example, 31 bits get masked out and only one bit of the address determines the bucket. With bl256
, an entire byte of the pointer gets used. It's one of the middle two bytes. The trade-off is simply the number of buckets you'll have. A bucket only takes eight bytes, so having 256 of them isn't a huge cost.
除此之外,TBucketList
只是一个普通的哈希表,就像您在数据结构类中学到的一样.
Aside from that, TBucketList
is just an ordinary hash table like you learned about in your data-structure class.
TIntegerBucketList
使用与其他函数相同的哈希函数.如果要使用更复杂的哈希函数,请编写TCustomBucketList
的后代并覆盖BucketFor
方法.在后代类中,您还可以分配受保护的BucketCount
属性以使用除TBucketList
提供的计数以外的其他内容.请注意,由于存储桶计数的变化,该类不进行任何重新分配项目的操作,因此,除非已计划自己进行重新分配,否则不要在将项目添加到列表后重新分配BucketCount
.
TIntegerBucketList
uses the same hash function as the others. If you want a more sophisticated hash function, write a descendant of TCustomBucketList
and override the BucketFor
method. In your descendant class, you can also assign the protected BucketCount
property to use something other than the counts provided by TBucketList
. Note that the class makes no effort to redistribute items due to a change in the bucket count, so don't re-assign BucketCount
after items have already been added to the list unless you plan to do the redistribution yourself.
这篇关于如何判断TBucketList的存储桶数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!