我有一个四/八叉树数据结构。我将一个单元格的子索引/ptrs 存储在一个数组中。数组中的每个位置都表示一个孩子相对于其父母的位置,例如在 2D 中:
// _____________
// | | |
// | 2 | 3 |
// |_____|_____|
// | | |
// | 0 | 1 |
// |_____|_____|
// for each cell, 4 children are always stored in row-major order
std::vector<std::array<Integer,4>> children;
我知道最大子节点数是Integer
类型可以表示的值的子集。-1
因此,我可以通过使用诸如forInteger = int
或std::numeric_limits<unsigned>::max()
for之类的“魔术”值来确定单元格是否缺少孩子Integer = unsigned
。这是std::optional<Integer>
不能假设的。
据我了解,这种魔法值的使用是std::optional
. 尽管如此,我还是担心std::vector<std::optional<int>>
内部循环的性能。
所以,
的性能
std::vector<std::optional<int>>
会比 的差std::vector<int>
吗?(我已经在比较“不存在”的值了)。或者,是否可以
std::optional
优化实现以提供与 raw 相同的性能int
?如何?
在我的数据结构中混合std::optional
函数的返回类型和魔法值听起来是个非常糟糕的主意。我更喜欢保持一致并使用其中一个(至少在相同的上下文中)。虽然我可以重载执行与幻数比较的函数:
template<T> bool is_valid(const T& t) {
return /* comparison with magic value for t */;
}
对于可选类型。