- "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:
@@ -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);
|
||||
|
||||
@@ -92,18 +92,25 @@ protected:
|
||||
|
||||
using base_t::left;
|
||||
using base_t::right;
|
||||
using base_t::child;
|
||||
using base_t::direction;
|
||||
using base_t::parent;
|
||||
using base_t::grandparent;
|
||||
using base_t::sibling;
|
||||
using base_t::parsib;
|
||||
|
||||
using base_t::left_most;
|
||||
using base_t::right_most;
|
||||
|
||||
using base_t::insert_left;
|
||||
using base_t::insert_right;
|
||||
|
||||
using base_t::rotate;
|
||||
using base_t::rotate_left;
|
||||
using base_t::rotate_right;
|
||||
|
||||
using base_t::_table;
|
||||
using base_t::_freed;
|
||||
using base_t::_root;
|
||||
using base_t::_size;
|
||||
|
||||
@@ -179,6 +186,10 @@ public:
|
||||
/// \returns The capacity of the underlying allocation
|
||||
using base_t::capacity;
|
||||
|
||||
///
|
||||
/// \returns `true` when there are no elements in the sequence, `false` otherwise.
|
||||
using base_t::empty;
|
||||
|
||||
/// @}
|
||||
|
||||
// Modifiers ===========================================================================================================
|
||||
@@ -213,8 +224,7 @@ public:
|
||||
}
|
||||
|
||||
constexpr void erase(const value_t& val) {
|
||||
size_t i = find(val)._n;
|
||||
|
||||
_erase_bst(val);
|
||||
}
|
||||
|
||||
///
|
||||
@@ -289,11 +299,11 @@ protected:
|
||||
}
|
||||
|
||||
constexpr bool& _color(size_t i) {
|
||||
return i >= _table.capacity() ? color_sink : _table[i].value.second;
|
||||
return i >= _table.capacity() ? color_sink = false : _table[i].value.second;
|
||||
}
|
||||
|
||||
constexpr bool _color(size_t i) const {
|
||||
return i >= _table.capacity() ? color_sink : _table[i].value.second;
|
||||
return i >= _table.capacity() ? color_sink = false : _table[i].value.second;
|
||||
}
|
||||
|
||||
template<typename...ArgsT>
|
||||
@@ -352,6 +362,92 @@ protected:
|
||||
}
|
||||
_color(_root) = black;
|
||||
}
|
||||
|
||||
constexpr void _shift(size_t u, size_t v) {
|
||||
if (parent(u) == npos) {
|
||||
_root = v;
|
||||
} else {
|
||||
child(parent(u), direction(u)) = v;
|
||||
}
|
||||
if (v != npos) {
|
||||
parent(v) = parent(u);
|
||||
}
|
||||
}
|
||||
|
||||
constexpr void _erase_bst(const value_t& val) {
|
||||
size_t z = find(val).index();
|
||||
size_t y = z;
|
||||
size_t x = npos;
|
||||
bool c = _color(y);
|
||||
size_t p = npos;
|
||||
|
||||
if (left(z) == npos) {
|
||||
x = right(z);
|
||||
p = parent(z);
|
||||
_shift(z, x);
|
||||
} else if (right(z) == npos) {
|
||||
x = left(z);
|
||||
p = parent(z);
|
||||
_shift(z, x);
|
||||
} else {
|
||||
y = left_most(right(z));
|
||||
c = _color(y);
|
||||
x = right(y);
|
||||
p = (parent(y) == z) ? y : parent(y);
|
||||
if (parent(y) != z) {
|
||||
_shift(y, right(y));
|
||||
right(y) = right(z);
|
||||
parent(right(y)) = y;
|
||||
}
|
||||
_shift(z, y);
|
||||
left(y) = left(z);
|
||||
if (left(y))
|
||||
parent(left(y)) = y;
|
||||
_color(y) = _color(z);
|
||||
}
|
||||
|
||||
fennec::destruct(&_table[z]);
|
||||
--_size;
|
||||
|
||||
if (c == black) {
|
||||
_fix_erase(x, p);
|
||||
}
|
||||
}
|
||||
|
||||
constexpr void _fix_erase(size_t x, size_t p) {
|
||||
while (x != _root && _color(x) == black) {
|
||||
bool dir = direction(x);
|
||||
size_t w = child(p, not dir);
|
||||
|
||||
if (_color(w) == red) {
|
||||
_color(w) = black;
|
||||
_color(p) = red;
|
||||
w = rotate(p, dir);
|
||||
}
|
||||
|
||||
if (w == npos || (_color(left(w)) == black && _color(right(w)) == black)) {
|
||||
_color(w) = red;
|
||||
x = p;
|
||||
p = parent(x);
|
||||
} else {
|
||||
if (_color(child(w, not dir)) == black) {
|
||||
_color(child(w, dir)) = black;
|
||||
_color(w) = red;
|
||||
rotate(w, not dir);
|
||||
w = child(p, not dir);
|
||||
}
|
||||
|
||||
_color(w) = _color(p);
|
||||
_color(p) = black;
|
||||
_color(child(w, not dir)) = black;
|
||||
rotate(p, dir);
|
||||
x = _root;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_color(x) = black;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user