您的位置:首页 > 产品设计 > UI/UE

学习 LLVM(19) UniqueVector

2012-03-09 16:22 190 查看
位于 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(应避免使用).

UniqueVector 类产生一个从 1 开始的顺序值作为每个插入的唯一元素的 ID。模板参数 T 是数组元素类型。T 应该实现 ==, < 操作符。插入的项可以通过 [ID] 来访问。

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 来实现底层存储,确实应该避免使用,或选择更合适的数据结构。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LLVM