- "Finished" sequence.h, there's more to do, but the basic functionality is there

- bintree.h is implemented according to the needs of sequence.h at present
This commit is contained in:
2025-08-31 14:38:05 -04:00
parent dbcb50349d
commit 375492ef7b
4 changed files with 231 additions and 5 deletions

View File

@@ -86,6 +86,10 @@ protected:
right = npos;
depth = npos;
}
size_t& operator[](bool d) {
return d ? right : left;
}
};
using table_t = allocation<node, alloc_t>;
@@ -144,6 +148,12 @@ public:
return _size;
}
///
/// \returns `true` when there are no elements in the tree, `false` otherwise.
constexpr bool empty() const {
return _size == 0;
}
///
/// \returns The capacity of the underlying allocation
constexpr size_t capacity() const {
@@ -206,6 +216,27 @@ public:
return i >= _table.size() ? npos : _table[i].right;
}
///
/// \details \f$O(1)\f$
/// \param i The node id
/// \param dir The direction to go `true` for right, `false` for left
/// \returns The child in the direction specified by `dir`
constexpr size_t child(size_t i, bool dir) const {
return dir ? right(i) : left(i);
}
///
/// \details \f$O(1)\f$
/// \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 {
size_t p = parent(i);
if (p >= _table.capacity()) {
return false;
}
return i == right(p);
}
///
/// \brief \f$O(1)\f$
/// \param i The id of the node
@@ -415,6 +446,32 @@ public:
_table[l].right = r;
}
///
/// \brief Perform a Tree Rotation at `i` in the specified direction
/// \param i The root node for the rotation
/// \param dir The direction to rotate, `true` for right, `false` for left
constexpr size_t rotate(size_t sub, bool dir) {
if (sub >=_table.size()) {
return npos;
}
size_t sub_parent = parent(sub);
size_t new_root = child(sub, not dir);
size_t new_child = child(new_root, dir);
child(sub, not dir) = new_child;
parent(new_child) = sub;
child(new_root, dir) = sub;
parent(new_root) = sub_parent;
parent(sub) = new_root;
if (sub_parent != npos) {
child(sub_parent, sub == right(sub_parent)) = new_root;
} else {
_root = new_root;
}
return new_root;
}
///
/// \brief Clears the tree, destroying all elements
constexpr void clear() {
@@ -640,6 +697,10 @@ public:
_order(*tree, root);
}
size_t index() const {
return _n;
}
iterator& operator++() {
return _n = _order[*_tree, _n, traversal_control_continue], *this;
}
@@ -745,6 +806,10 @@ protected:
return i >= _table.size() ? sink : _table[i].right;
}
constexpr size_t& child(size_t i, bool dir) {
return dir ? right(i) : left(i);
}
constexpr size_t& sibling(size_t i) {
size_t p = parent(i);
size_t& l = left(p);