DotNet源碼學習-HASHSET(初探)

来源:https://www.cnblogs.com/xtt321/archive/2020/02/15/12310345.html
-Advertisement-
Play Games

命名空間:System.Collections.Generic 先看一下官方說明:類提供了高級的設置操作。集是不包含重覆元素的集合,其元素無特定順序。 HashSet <T>對象的容量是對象可以容納的元素數。當向對象添加元素時,HashSet <T>對象的容量會自動增加。 HashSet<Strin ...


命名空間:System.Collections.Generic

先看一下官方說明:類提供了高級的設置操作。集是不包含重覆元素的集合,其元素無特定順序 

HashSet <T>對象的容量是對象可以容納的元素數。當向對象添加元素時,HashSet <T>對象的容量會自動增加。

HashSet<String> hashSet = new HashSet<string>();
hashSet.Add("test");
hashSet.Add("test");
Console.WriteLine(hashSet.Count);

列印結果:1

HashSet的預設構造方法:

public HashSet()
            : this((IEqualityComparer<T>?)null)
        { }

註:: this語法為調用自身對象的其他構造方法。

public HashSet(IEqualityComparer<T>? comparer)
{
     if (comparer == EqualityComparer<T>.Default)
     {
          comparer = null;
     }
     _comparer = comparer;
     _lastIndex = 0;
     _count = 0;
     _freeList = -1;
     _version = 0;
}

第二中創建方式,將集合作為參數。

List<string> list = new List<string>();
list.Add("test");
list.Add("test");
HashSet<string> hSet = new HashSet<string>(list);
Console.WriteLine(hSet.Count);

此時控台輸出:1

此時調用的構造方法為:

public HashSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer)
            : this(comparer)
{
    if (collection == null)
    {
        throw new ArgumentNullException(nameof(collection));
    }
    var otherAsHashSet = collection as HashSet<T>;
    if (otherAsHashSet != null && AreEqualityComparersEqual(this, otherAsHashSet))
    {
        CopyFrom(otherAsHashSet);
    }
    else
    {
        // to avoid excess resizes, first set size based on collection's count. Collection
        // may contain duplicates, so call TrimExcess if resulting hashset is larger than
        // threshold
        ICollection<T>? coll = collection as ICollection<T>;
        int suggestedCapacity = coll == null ? 0 : coll.Count;
        Initialize(suggestedCapacity);
        UnionWith(collection);
        if (_count > 0 && _slots.Length / _count > ShrinkThreshold)
        {
            TrimExcess();
        }
    }
}

在該構造方法中若存在重覆值則通過查找大於或等於容量的下一個質數來使用建議的容量。

private int Initialize(int capacity)
{
    Debug.Assert(_buckets == null, "Initialize was called but _buckets was non-null");
    int size = HashHelpers.GetPrime(capacity);
    _buckets = new int[size];
    _slots = new Slot[size];
    return size;
}

下麵為生成質數方法:

public static int GetPrime(int min)
{
    if (min < 0)
        throw new ArgumentException(SR.Arg_HTCapacityOverflow);
    foreach (int prime in s_primes)
    {
        if (prime >= min)
            return prime;
    }
    // Outside of our predefined table. Compute the hard way.
    for (int i = (min | 1); i < int.MaxValue; i += 2)
    {
        if (IsPrime(i) && ((i - 1) % HashPrime != 0))
            return i;
    }
    return min;
}

再次擴展-》

public static bool IsPrime(int candidate)
{
  if ((candidate & 1) != 0)
  {
    int limit = (int)Math.Sqrt(candidate);//取平方
    for (int divisor = 3; divisor <= limit; divisor += 2)
    {
      if ((candidate % divisor) == 0)
        return false;
    }
    return true;
  }
  return candidate == 2;
}
internal struct Slot
{
  internal int hashCode;      // Lower 31 bits of hash code, -1 if unused
  internal int next;          // Index of next entry, -1 if last
  internal T value;
}

存儲LIst集合:

public void UnionWith(IEnumerable<T> other)
{
  if (other == null)
  {
    throw new ArgumentNullException(nameof(other));
  }
  foreach (T item in other)
  {
    AddIfNotPresent(item);
  }
}

繼續往下追蹤:

private bool AddIfNotPresent(T value)
{
  if (_buckets == null)
  {
    Initialize(0);
  }

  int hashCode;
  int bucket;
  int collisionCount = 0;
  Slot[] slots = _slots;

  IEqualityComparer<T>? comparer = _comparer;

  if (comparer == null)
  {
  //取HASHCODE
    hashCode = value == null ? 0 : InternalGetHashCode(value.GetHashCode());
    bucket = hashCode % _buckets!.Length;

    if (default(T)! != null) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
    {
      for (int i = _buckets[bucket] - 1; i >= 0; i = slots[i].next)
      {
        if (slots[i].hashCode == hashCode && EqualityComparer<T>.Default.Equals(slots[i].value, value))
        {
          return false;
        }

        if (collisionCount >= slots.Length)
        {
          // The chain of entries forms a loop, which means a concurrent update has happened.
          throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
        }
        collisionCount++;
      }
    }
    else
    {
      // Object type: Shared Generic, EqualityComparer<TValue>.Default won't devirtualize
      // https://github.com/dotnet/coreclr/issues/17273
      // So cache in a local rather than get EqualityComparer per loop iteration
      EqualityComparer<T> defaultComparer = EqualityComparer<T>.Default;

      for (int i = _buckets[bucket] - 1; i >= 0; i = slots[i].next)
      {
        if (slots[i].hashCode == hashCode && defaultComparer.Equals(slots[i].value, value))
        {
          return false;
        }

        if (collisionCount >= slots.Length)
        {
          // The chain of entries forms a loop, which means a concurrent update has happened.
          throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
        }
        collisionCount++;
      }
    }
  }
  else
  {
    hashCode = value == null ? 0 : InternalGetHashCode(comparer.GetHashCode(value));
    bucket = hashCode % _buckets!.Length;

    for (int i = _buckets[bucket] - 1; i >= 0; i = slots[i].next)
    {
      if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, value))
      {
        return false;
      }

      if (collisionCount >= slots.Length)
      {
        // The chain of entries forms a loop, which means a concurrent update has happened.
        throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
      }
      collisionCount++;
    }
  }

  int index;
  if (_freeList >= 0)
  {
    index = _freeList;
    _freeList = slots[index].next;
  }
  else
  {
    if (_lastIndex == slots.Length)
    {
      IncreaseCapacity();
      // this will change during resize
      slots = _slots;
      bucket = hashCode % _buckets.Length;
    }
    index = _lastIndex;
    _lastIndex++;
  }
  slots[index].hashCode = hashCode;
  slots[index].value = value;
  slots[index].next = _buckets[bucket] - 1;
  _buckets[bucket] = index + 1;
  _count++;
  _version++;

  return true;
}
private const int Lower31BitMask = 0x7FFFFFFF;

獲取內部的HASHCODE

private static int InternalGetHashCode(T item, IEqualityComparer<T>? comparer)
{
  if (item == null)
  {
    return 0;
  }
  int hashCode = comparer?.GetHashCode(item) ?? item.GetHashCode();
  return hashCode & Lower31BitMask;
}

劃重點-》

slots[index].hashCode = hashCode;
slots[index].value = value;
slots[index].next = _buckets[bucket] - 1;

最終列表中的值存儲到結構中。

使用對象初始化HASHSET時,如果相同

 

HashSet<string> hashSet = new HashSet<string>();
 hashSet.Add("test");
 hashSet.Add("test");
 HashSet<string> hSet = new HashSet<string>(hashSet);
private void CopyFrom(HashSet<T> source)
{
  int count = source._count;
  if (count == 0)
  {
    // As well as short-circuiting on the rest of the work done,
    // this avoids errors from trying to access otherAsHashSet._buckets
    // or otherAsHashSet._slots when they aren't initialized.
    return;
  }

  int capacity = source._buckets!.Length;
  int threshold = HashHelpers.ExpandPrime(count + 1);

  if (threshold >= capacity)
  {
    _buckets = (int[])source._buckets.Clone();
    _slots = (Slot[])source._slots.Clone();

    _lastIndex = source._lastIndex;
    _freeList = source._freeList;
  }
  else
  {
    int lastIndex = source._lastIndex;
    Slot[] slots = source._slots;
    Initialize(count);
    int index = 0;
    for (int i = 0; i < lastIndex; ++i)
    {
      int hashCode = slots[i].hashCode;
      if (hashCode >= 0)
      {
        AddValue(index, hashCode, slots[i].value);
        ++index;
      }
    }
    Debug.Assert(index == count);
    _lastIndex = index;
  }
  _count = count;
}
public static int ExpandPrime(int oldSize)//返回要增長到的哈希表大小
{
  int newSize = 2 * oldSize;

  // Allow the hashtables to grow to maximum possible size (~2G elements) before encountering capacity overflow.
  // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
  if ((uint)newSize > MaxPrimeArrayLength && MaxPrimeArrayLength > oldSize)
  {
    Debug.Assert(MaxPrimeArrayLength == GetPrime(MaxPrimeArrayLength), "Invalid MaxPrimeArrayLength");
    return MaxPrimeArrayLength;
  }

  return GetPrime(newSize);
}

這裡只貼出HashSet聲明創建對象的兩種方式。

下篇再研究具體實現〜


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在使用Eclipse過程中可能想更換下界面主題,此處介紹的是一款主題插件 Eclipse Color Theme 打開Eclipse,Help --> Eclipse Marketplace 在打開的視窗中 搜索 theme 在搜索結果中選擇 Eclipse Color Theme 並安裝,安裝過程 ...
  • 史上最水的 dp 題,沒有之一(By rxz) 確實很簡單,就算是我這個 dp 萌新也一眼看出來了轉移方程 首先考慮狀態,設 $f_{i,j}$ 表示選擇第 $i$ 層第 $j$ 個數時獲得的最大值,那麼可以發現,對於數字 $a_{i,j}$ ,只有從 $a_{i 1,j}$ 和 $a_{i 1,j ...
  • 數轉換成二叉樹:使用孩子兄弟表示法。 二叉樹轉換成樹:將二叉樹的右孩子轉換成兄弟。 森林轉換成二叉樹:將森林中的每一棵樹都轉換成二叉樹,然後把森林中每個結點連起來,調整角度,使其成為二叉樹形狀。 二叉樹轉換成森林:將二叉樹分成n個互不相交、沒有右子樹的二叉樹,然後將每個二叉樹都轉換成樹。 ...
  • 題目大意:給定 $n$ 個數,每次可以 任意 選兩個數 $a_i,a_j$ 相加,把相加的結果作為一個新數繼續執行此操作,直到只剩一個數為止。現要求使最後得出的這個數最小。 一個顯然的貪心策略:每次選最小的兩個數相加,得到一個新數,然後繼續。但是,如果按照這樣的思路,那麼每次得到新數後這個序列的 單 ...
  • 讀題易得:對於有邊的兩個點 $u,v$ ,能且僅能其中一點對這條邊進行封鎖。 什麼意思呢?假設給這張圖上的點進行染色,那麼對於上述的兩個點 $u,v$ , $u,v$ 必須異色 (理解這一點很重要)。 那麼,也就是說,在這張圖上,如果要把這張圖“完全封鎖”且兩隻河蟹不能封鎖相鄰的兩個點,換而言之,把 ...
  • 非嚴格定義:在一棵帶權樹上, 相聚距離最大的兩個點 或 最長鏈 的長度,稱之為 樹的直徑 樣例輸入: 樣例輸出 似乎並沒有什麼難理解的地方。 解法1:DP 咕著 解法2:DFS 經過思考,發現一個重要的性質: 離樹上的某一結點最遠的那個結點,定是直徑的一個端點。 那麼就好辦了!找到任一點的最遠點,再 ...
  • 使用SpringCloud做集群,開發、測試階段,經常要運行一個模塊的多個實例,要修改埠號。 有3種方式。 方式一:配置文件 server.port=9001 方式二、修改引導類,控制台輸入參數值 @SpringBootApplication @EnableEurekaServer //作為Eur ...
  • 1,程式集載入 弱的程式集可以載入強簽名的程式集,但是不可相反.否則引用會報錯!(但是,反射是沒問題的) //獲取當前類的Assembly Assembly.GetEntryAssembly() //通過Load方法載入程式集 Assembly.Load //通過LoadFrom載入指定路徑名的程式 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...