- Some last minute performance improvements I noticed.

This commit is contained in:
2025-09-18 21:56:46 -04:00
parent 18c8171847
commit 1a9a80e37f
2 changed files with 9 additions and 42 deletions

View File

@@ -226,7 +226,7 @@ public:
/// \param i The node id
/// \returns `true` if `i` is the right node of `parent(i)`, `false` otherwise
constexpr bool direction(size_t i) const {
return i == npos ? false : i == right(parent(i));
return i == npos ? false : i == right(_parent(i));
}
///
@@ -242,7 +242,7 @@ public:
}
size_t p = _parent(i);
bool d = i == _right(p);
return _child(p, d);
return _child(p, !d);
}
///
@@ -405,7 +405,7 @@ public:
_parent(new_root) = sub_parent;
_parent(sub) = new_root;
if (sub_parent != npos) {
_child(sub_parent, sub == right(sub_parent)) = new_root;
_child(sub_parent, sub == _right(sub_parent)) = new_root;
} else {
_root = new_root;
}
@@ -579,8 +579,8 @@ public:
};
struct in_order {
list<size_t> visit;
size_t head;
list<size_t> visit;
constexpr size_t operator()(const bintree& tree, size_t start) {
head = start;
@@ -683,9 +683,8 @@ public:
constexpr iterator(bintree* tree, size_t root, size_t node)
: _tree(tree)
, _order()
, _order(root)
, _n(node) {
_order(*tree, root);
}
size_t index() const {
@@ -805,7 +804,7 @@ protected:
}
constexpr size_t& _grandparent(size_t i) {
return _parent(parent(i));
return _parent(_parent(i));
}
constexpr size_t& _left(size_t i) {
@@ -821,10 +820,9 @@ protected:
}
constexpr size_t& _sibling(size_t i) {
size_t p = _parent(i);
size_t& l = _left(p);
size_t& r = _right(p);
return i == l ? l : r;
size_t p = _parent(i);
bool d = i == _right(p);
return _child(p, !d);
}
};

View File

@@ -299,7 +299,6 @@ protected:
using base_t::_left;
using base_t::_right;
using base_t::_parent;
using base_t::_sibling;
using base_t::_child;
constexpr value_t& _value(size_t i) {
@@ -390,40 +389,10 @@ protected:
_color(_root) = black;
}
constexpr void _transplant(size_t u, size_t v) {
size_t p = parent(u);
if (p == npos) {
_root = v;
} else if (u == left(p)) {
_left(p) = v;
} else {
_right(p) = v;
}
_parent(v) = _parent(u);
}
constexpr void _swap_val(size_t a, size_t b) {
fennec::swap(_value(a), _value(b));
}
constexpr size_t _replace(size_t x) {
size_t l = left(x);
size_t r = right(x);
// Both are null
if (l == r) {
return npos;
}
if (l == npos) {
return r;
} else if (r == npos) {
return l;
} else {
return left_most(right(x));
}
}
constexpr size_t _red_child(size_t x) {
size_t l = _left(x);
size_t r = _right(x);