學習 LLVM(19) UniqueVector

位於 llvm/include/llvm/[[ADT]]/UniqueVector.h數組

UniqueVector is similar to SetVector, but it retains(容納) a unique ID for each element inserted into the set. It internally contains a map and a vector(內部由 map 和 vector 構成), and it assigns a unique ID for each value inserted into the set.數據結構

UniqueVector is very expensive(昂貴的): its cost is the sum of the cost of maintaining both the map and vector, it has high complexity, high constant factors, and produces a lot of malloc traffic. It should be avoided(應避免使用).ide

UniqueVector 類產生一個從 1 開始的順序值做爲每一個插入的惟一元素的 ID。模板參數 T 是數組元素類型。T 應該實現 ==, < 操做符。插入的項能夠經過 [ID] 來訪問。code

template <T> class UniqueVector {
  std::map<T, unsigned_ID> Map;  // 從 entry 映射到 ID
  std::vector<T> Vector;         // 從 ID 映射到 entry

  insert(T&) // 新增一個 entry,若是存在則返回已存在項的 ID
  idFor(T&)  // 根據 entry 在 Map 中找到其對應的 ID,返回 0 表示沒找到
  []   // 數組訪問符,經過 ID 訪問 entry
  size(), empty(), reset() 通常容器方法。
}

 這個類提供的對外方法很少。內部使用了昂貴的 Map, Vector 來實現底層存儲,確實應該避免使用,或選擇更合適的數據結構。
element

相關文章
相關標籤/搜索