如何理解 ListT和 DictionaryK,V 的扩容机制 ?
生活随笔
收集整理的這篇文章主要介紹了
如何理解 ListT和 DictionaryK,V 的扩容机制 ?
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
咨詢區(qū)
Royi Namir
為什么 List 是按照 2倍 擴容。
private?void?EnsureCapacity(int?min) {if?(this._items.Length?<?min){int?num?=?(this._items.Length?==?0)???4?:?(this._items.Length?*?2);if?(num?<?min){num?=?min;}this.Capacity?=?num;} }而 Dictionary<K,V> 是按 素數(shù) 擴容。
private?void?Resize() {int?prime?=?HashHelpers.GetPrime(this.count?*?2);int[]?numArray?=?new?int[prime];for?(int?i?=?0;?i?<?numArray.Length;?i++){numArray[i]?=?-1;}Entry<TKey,?TValue>[]?destinationArray?=?new?Entry<TKey,?TValue>[prime];Array.Copy(this.entries,?0,?destinationArray,?0,?this.count);for?(int?j?=?0;?j?<?this.count;?j++){int?index?=?destinationArray[j].hashCode?%?prime;destinationArray[j].next?=?numArray[index];numArray[index]?=?j;}this.buckets?=?numArray;this.entries?=?destinationArray; }為什么不都按照 2倍 來呢?這樣還可以實現(xiàn)代碼復(fù)用。
回答區(qū)
Gman
Dictionary 是啟發(fā)式的,它需要保證 hashcode 必須準(zhǔn)均勻的分布在各個桶中,.NET 的 Dictionary 采用的是素數(shù)來實現(xiàn)這個均衡,下面是計算桶索引的算法。
int?num?=?this.comparer.GetHashCode(key)?&?2147483647;?//?make?hash?code?positive //?get?the?remainder?from?division?-?that's?our?bucket?index int?num2?=?this.buckets[num?%?((int)this.buckets.Length)];不過 Java 采用的和 .NET 中的 List 是一樣的擴容機制。
resize(2?*?table.length);在翻倍之后,java 會重新計算 hashcode 值的。
static?int?hash(int?h)?{//?This?function?ensures?that?hashCodes?that?differ?only?by//?constant?multiples?at?each?bit?position?have?a?bounded//?number?of?collisions?(approximately?8?at?default?load?factor).h?^=?(h?>>>?20)?^?(h?>>>?12);return?h?^?(h?>>>?7)?^?(h?>>>?4); } static?int?indexFor(int?h,?int?length)?{return?h?&?(length-1); }//?from?put()?method int?hash?=?hash(key.hashCode());?//?get?modified?hash int?i?=?indexFor(hash,?table.length);?//?trim?the?hash?to?the?bucket?countList 不是啟發(fā)式的,所以沒有這么煩惱,
點評區(qū)
其實說到底,Dictionary采用素數(shù),本質(zhì)目的就是減少桶掛鏈,減少hashcode沖突,如果掛鏈過長,那就達不到 Contains,Add,Get 等操作的 O(1) 時間復(fù)雜度,這也就失去了 Dictionary 原本的特性。
總結(jié)
以上是生活随笔為你收集整理的如何理解 ListT和 DictionaryK,V 的扩容机制 ?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iNeuOS工业互联网操作系统,顺利从N
- 下一篇: 2. 堪比JMeter的.Net压测工具