学习 LLVM(13) SmallSet 类

位于 llvm/include/llvm/[[ADT]]/SmallSet.h

注释:'Normally small' sets -- 普通小型集合

这个头文件定义了 SmallSet 类。

SmallSet 类保持(维护)一组唯一值的集合,为集合中元素数量较少的情况(小于 N)进行了优化。这种情况下,Set 可以不用进行内存分配。如果 Set 变得更大,则内部扩展到使用 std::set 来维护以获得理想的检索响应时间。要注意的是,这个类不提供迭代器(iterator)。

参见:http://llvm.org/docs/ProgrammersManual.html#dss_smallset

SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less than N).  In this case, the set can be
maintained with no mallocs.  If the set gets large, we expand to using an std::set to maintain reasonable lookup times.

Note that this set does not provide a way to iterate over members in the
set.

== SmallSet 类概要 ==

template <T, N, C> class SmallSet {  // 注1:T, N, C
  SmallVector<T,N> Vector;  // 内部使用 SmallVector 保存集合数据。
  std::set<T,C> Set;   // 当元素多了使用 std::set 保存集合数据。

  this() // 构造
  empty(), size(), count(), insert(), erase(), clear() 等容器方法。
}

注1:T 是要保存的元素的类型;N 是 in-place 保存的元素的数量,其实际传递给 SmallVector 模板;C 缺省为 std::less<T> 用于比较两个元素的大小,传递给 std::set 做模板参数。

== 实现机理 ==
我们以 insert() 函数为例,研究其实现机理。

* 1. 当 std::set 非空的时候,也即 SmallVector 不足以容纳 N 个元素的时候,直接将新元素插入到 std::set 中。
* 2. 在 SmallVector 中查找(线性搜索),如果已经存在了(== 比较),则返回 false(重复不插入)
* 3. 如果 SmallVector 的现有元素数量 < N,表示还未装满,则 push_back 到 SmallVector 中。
* 4. 否则,SmallVector 已经满了,将 SmallVector 所有内容转入 std::set;然后插入到 std::set 中。这里进行了 SmallVector -> std::set 的转换。

其它的实现机理都很简单,不再说明。

转载于:https://my.oschina.net/u/232554/blog/42215

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值