godot/core/templates/rb_set.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

712 lines
16 KiB
C++
Raw Normal View History

/**************************************************************************/
/* rb_set.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef RB_SET_H
#define RB_SET_H
2014-02-10 01:10:30 +00:00
#include "core/os/memory.h"
#include "core/typedefs.h"
2014-02-10 01:10:30 +00:00
// based on the very nice implementation of rb-trees by:
// https://web.archive.org/web/20120507164830/https://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
2014-02-10 01:10:30 +00:00
template <class T, class C = Comparator<T>, class A = DefaultAllocator>
class RBSet {
2016-03-08 23:00:52 +00:00
enum Color {
2014-02-10 01:10:30 +00:00
RED,
BLACK
};
2016-03-08 23:00:52 +00:00
struct _Data;
public:
2014-02-10 01:10:30 +00:00
class Element {
private:
friend class RBSet<T, C, A>;
int color = RED;
Element *right = nullptr;
Element *left = nullptr;
Element *parent = nullptr;
Element *_next = nullptr;
Element *_prev = nullptr;
2014-02-10 01:10:30 +00:00
T value;
//_Data *data;
public:
const Element *next() const {
return _next;
}
Element *next() {
return _next;
}
const Element *prev() const {
return _prev;
}
Element *prev() {
return _prev;
}
2021-07-27 00:44:58 +00:00
T &get() {
return value;
}
2016-03-08 23:00:52 +00:00
const T &get() const {
2014-02-10 01:10:30 +00:00
return value;
};
Element() {}
2014-02-10 01:10:30 +00:00
};
typedef T ValueType;
struct Iterator {
_FORCE_INLINE_ T &operator*() const {
return E->get();
}
_FORCE_INLINE_ T *operator->() const { return &E->get(); }
_FORCE_INLINE_ Iterator &operator++() {
E = E->next();
return *this;
}
_FORCE_INLINE_ Iterator &operator--() {
E = E->prev();
return *this;
}
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
explicit operator bool() const { return E != nullptr; }
Iterator(Element *p_E) { E = p_E; }
Iterator() {}
Iterator(const Iterator &p_it) { E = p_it.E; }
private:
Element *E = nullptr;
};
struct ConstIterator {
_FORCE_INLINE_ const T &operator*() const {
return E->get();
}
_FORCE_INLINE_ const T *operator->() const { return &E->get(); }
_FORCE_INLINE_ ConstIterator &operator++() {
E = E->next();
return *this;
}
_FORCE_INLINE_ ConstIterator &operator--() {
E = E->prev();
return *this;
}
2021-07-27 00:44:58 +00:00
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
_FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
_FORCE_INLINE_ ConstIterator() {}
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
explicit operator bool() const { return E != nullptr; }
private:
const Element *E = nullptr;
};
_FORCE_INLINE_ Iterator begin() {
return Iterator(front());
}
_FORCE_INLINE_ Iterator end() {
return Iterator(nullptr);
}
#if 0
//to use when replacing find()
_FORCE_INLINE_ Iterator find(const K &p_key) {
return Iterator(find(p_key));
}
#endif
_FORCE_INLINE_ ConstIterator begin() const {
return ConstIterator(front());
}
_FORCE_INLINE_ ConstIterator end() const {
return ConstIterator(nullptr);
}
#if 0
//to use when replacing find()
_FORCE_INLINE_ ConstIterator find(const K &p_key) const {
return ConstIterator(find(p_key));
}
#endif
2014-02-10 01:10:30 +00:00
private:
struct _Data {
Element *_root = nullptr;
Element *_nil = nullptr;
int size_cache = 0;
2016-03-08 23:00:52 +00:00
2017-09-20 17:52:40 +00:00
_FORCE_INLINE_ _Data() {
2014-02-10 01:10:30 +00:00
#ifdef GLOBALNIL_DISABLED
_nil = memnew_allocator(Element, A);
_nil->parent = _nil->left = _nil->right = _nil;
_nil->color = BLACK;
#else
_nil = (Element *)&_GlobalNilClass::_nil;
#endif
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
void _create_root() {
_root = memnew_allocator(Element, A);
_root->parent = _root->left = _root->right = _nil;
_root->color = BLACK;
}
void _free_root() {
if (_root) {
memdelete_allocator<Element, A>(_root);
2020-04-01 23:20:12 +00:00
_root = nullptr;
2014-02-10 01:10:30 +00:00
}
}
~_Data() {
_free_root();
2017-09-20 17:52:40 +00:00
2014-02-10 01:10:30 +00:00
#ifdef GLOBALNIL_DISABLED
memdelete_allocator<Element, A>(_nil);
#endif
}
};
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
_Data _data;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
inline void _set_color(Element *p_node, int p_color) {
ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
p_node->color = p_color;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
inline void _rotate_left(Element *p_node) {
Element *r = p_node->right;
p_node->right = r->left;
if (r->left != _data._nil) {
2014-02-10 01:10:30 +00:00
r->left->parent = p_node;
}
2014-02-10 01:10:30 +00:00
r->parent = p_node->parent;
if (p_node == p_node->parent->left) {
2014-02-10 01:10:30 +00:00
p_node->parent->left = r;
} else {
2014-02-10 01:10:30 +00:00
p_node->parent->right = r;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
r->left = p_node;
p_node->parent = r;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
inline void _rotate_right(Element *p_node) {
Element *l = p_node->left;
p_node->left = l->right;
if (l->right != _data._nil) {
2014-02-10 01:10:30 +00:00
l->right->parent = p_node;
}
2014-02-10 01:10:30 +00:00
l->parent = p_node->parent;
if (p_node == p_node->parent->right) {
2014-02-10 01:10:30 +00:00
p_node->parent->right = l;
} else {
2014-02-10 01:10:30 +00:00
p_node->parent->left = l;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
l->right = p_node;
p_node->parent = l;
}
2016-03-08 23:00:52 +00:00
inline Element *_successor(Element *p_node) const {
2014-02-10 01:10:30 +00:00
Element *node = p_node;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (node->right != _data._nil) {
node = node->right;
while (node->left != _data._nil) { /* returns the minimum of the right subtree of node */
2014-02-10 01:10:30 +00:00
node = node->left;
}
return node;
} else {
while (node == node->parent->right) {
node = node->parent;
}
2017-08-30 00:18:32 +00:00
if (node->parent == _data._root) {
2020-04-01 23:20:12 +00:00
return nullptr; // No successor, as p_node = last node
}
2014-02-10 01:10:30 +00:00
return node->parent;
}
}
2016-03-08 23:00:52 +00:00
inline Element *_predecessor(Element *p_node) const {
2014-02-10 01:10:30 +00:00
Element *node = p_node;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (node->left != _data._nil) {
node = node->left;
while (node->right != _data._nil) { /* returns the minimum of the left subtree of node */
2014-02-10 01:10:30 +00:00
node = node->right;
}
return node;
} else {
while (node == node->parent->left) {
node = node->parent;
}
2017-08-30 00:18:32 +00:00
if (node == _data._root) {
2020-04-01 23:20:12 +00:00
return nullptr; // No predecessor, as p_node = first node.
}
2014-02-10 01:10:30 +00:00
return node->parent;
}
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
Element *_find(const T &p_value) const {
Element *node = _data._root->left;
2016-03-08 23:00:52 +00:00
C less;
2014-02-10 01:10:30 +00:00
while (node != _data._nil) {
if (less(p_value, node->value)) {
2014-02-10 01:10:30 +00:00
node = node->left;
} else if (less(node->value, p_value)) {
2014-02-10 01:10:30 +00:00
node = node->right;
} else {
2017-08-30 00:18:32 +00:00
return node; // found
}
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
2020-04-01 23:20:12 +00:00
return nullptr;
2014-02-10 01:10:30 +00:00
}
Element *_lower_bound(const T &p_value) const {
Element *node = _data._root->left;
2020-04-01 23:20:12 +00:00
Element *prev = nullptr;
C less;
while (node != _data._nil) {
prev = node;
if (less(p_value, node->value)) {
node = node->left;
} else if (less(node->value, p_value)) {
node = node->right;
} else {
2017-08-30 00:18:32 +00:00
return node; // found
}
}
if (prev == nullptr) {
2020-04-01 23:20:12 +00:00
return nullptr; // tree empty
}
if (less(prev->value, p_value)) {
2017-08-30 00:18:32 +00:00
prev = prev->_next;
}
2017-08-30 00:18:32 +00:00
return prev;
}
2017-09-20 17:52:40 +00:00
void _insert_rb_fix(Element *p_new_node) {
Element *node = p_new_node;
2017-08-30 00:18:32 +00:00
Element *nparent = node->parent;
Element *ngrand_parent = nullptr;
2014-02-10 01:10:30 +00:00
2017-08-30 00:18:32 +00:00
while (nparent->color == RED) {
ngrand_parent = nparent->parent;
2016-03-08 23:00:52 +00:00
2017-08-30 00:18:32 +00:00
if (nparent == ngrand_parent->left) {
if (ngrand_parent->right->color == RED) {
_set_color(nparent, BLACK);
_set_color(ngrand_parent->right, BLACK);
_set_color(ngrand_parent, RED);
node = ngrand_parent;
nparent = node->parent;
2014-02-10 01:10:30 +00:00
} else {
2017-08-30 00:18:32 +00:00
if (node == nparent->right) {
_rotate_left(nparent);
node = nparent;
nparent = node->parent;
2014-02-10 01:10:30 +00:00
}
2017-08-30 00:18:32 +00:00
_set_color(nparent, BLACK);
_set_color(ngrand_parent, RED);
_rotate_right(ngrand_parent);
2016-03-08 23:00:52 +00:00
}
} else {
2017-08-30 00:18:32 +00:00
if (ngrand_parent->left->color == RED) {
_set_color(nparent, BLACK);
_set_color(ngrand_parent->left, BLACK);
_set_color(ngrand_parent, RED);
node = ngrand_parent;
nparent = node->parent;
2014-02-10 01:10:30 +00:00
} else {
2017-08-30 00:18:32 +00:00
if (node == nparent->left) {
_rotate_right(nparent);
node = nparent;
nparent = node->parent;
2014-02-10 01:10:30 +00:00
}
2017-08-30 00:18:32 +00:00
_set_color(nparent, BLACK);
_set_color(ngrand_parent, RED);
_rotate_left(ngrand_parent);
2016-03-08 23:00:52 +00:00
}
2014-02-10 01:10:30 +00:00
}
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
_set_color(_data._root->left, BLACK);
2017-09-20 17:52:40 +00:00
}
Element *_insert(const T &p_value) {
Element *new_parent = _data._root;
Element *node = _data._root->left;
C less;
while (node != _data._nil) {
new_parent = node;
if (less(p_value, node->value)) {
2017-09-20 17:52:40 +00:00
node = node->left;
} else if (less(node->value, p_value)) {
2017-09-20 17:52:40 +00:00
node = node->right;
} else {
2017-09-20 17:52:40 +00:00
return node; // Return existing node
}
}
2017-08-30 00:18:32 +00:00
2017-09-20 17:52:40 +00:00
Element *new_node = memnew_allocator(Element, A);
new_node->parent = new_parent;
new_node->right = _data._nil;
new_node->left = _data._nil;
new_node->value = p_value;
//new_node->data=_data;
if (new_parent == _data._root || less(p_value, new_parent->value)) {
new_parent->left = new_node;
} else {
new_parent->right = new_node;
}
new_node->_next = _successor(new_node);
new_node->_prev = _predecessor(new_node);
if (new_node->_next) {
2017-09-20 17:52:40 +00:00
new_node->_next->_prev = new_node;
}
if (new_node->_prev) {
2017-09-20 17:52:40 +00:00
new_node->_prev->_next = new_node;
}
2017-09-20 17:52:40 +00:00
_data.size_cache++;
_insert_rb_fix(new_node);
2016-03-08 23:00:52 +00:00
return new_node;
}
2014-02-10 01:10:30 +00:00
2017-09-20 17:52:40 +00:00
void _erase_fix_rb(Element *p_node) {
2014-02-10 01:10:30 +00:00
Element *root = _data._root->left;
2017-08-30 00:18:32 +00:00
Element *node = _data._nil;
Element *sibling = p_node;
Element *parent = sibling->parent;
while (node != root) { // If red node found, will exit at a break
if (sibling->color == RED) {
_set_color(sibling, BLACK);
_set_color(parent, RED);
if (sibling == parent->right) {
sibling = sibling->left;
_rotate_left(parent);
2014-02-10 01:10:30 +00:00
} else {
2017-08-30 00:18:32 +00:00
sibling = sibling->right;
_rotate_right(parent);
2014-02-10 01:10:30 +00:00
}
2017-08-30 00:18:32 +00:00
}
if ((sibling->left->color == BLACK) && (sibling->right->color == BLACK)) {
_set_color(sibling, RED);
if (parent->color == RED) {
_set_color(parent, BLACK);
break;
} else { // loop: haven't found any red nodes yet
node = parent;
parent = node->parent;
sibling = (node == parent->left) ? parent->right : parent->left;
2014-02-10 01:10:30 +00:00
}
2017-08-30 00:18:32 +00:00
} else {
if (sibling == parent->right) {
if (sibling->right->color == BLACK) {
_set_color(sibling->left, BLACK);
_set_color(sibling, RED);
_rotate_right(sibling);
sibling = sibling->parent;
}
_set_color(sibling, parent->color);
_set_color(parent, BLACK);
_set_color(sibling->right, BLACK);
_rotate_left(parent);
break;
2014-02-10 01:10:30 +00:00
} else {
2017-08-30 00:18:32 +00:00
if (sibling->left->color == BLACK) {
_set_color(sibling->right, BLACK);
_set_color(sibling, RED);
_rotate_left(sibling);
sibling = sibling->parent;
2014-02-10 01:10:30 +00:00
}
2017-08-30 00:18:32 +00:00
_set_color(sibling, parent->color);
_set_color(parent, BLACK);
_set_color(sibling->left, BLACK);
_rotate_right(parent);
break;
2014-02-10 01:10:30 +00:00
}
}
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(_data._nil->color != BLACK);
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
void _erase(Element *p_node) {
2017-08-30 00:18:32 +00:00
Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : p_node->_next;
2014-02-10 01:10:30 +00:00
Element *node = (rp->left == _data._nil) ? rp->right : rp->left;
2016-03-08 23:00:52 +00:00
Element *sibling = nullptr;
2017-08-30 00:18:32 +00:00
if (rp == rp->parent->left) {
rp->parent->left = node;
sibling = rp->parent->right;
2014-02-10 01:10:30 +00:00
} else {
2017-08-30 00:18:32 +00:00
rp->parent->right = node;
sibling = rp->parent->left;
}
if (node->color == RED) {
node->parent = rp->parent;
_set_color(node, BLACK);
} else if (rp->color == BLACK && rp->parent != _data._root) {
2017-09-20 17:52:40 +00:00
_erase_fix_rb(sibling);
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
if (rp != p_node) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(rp == _data._nil);
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
rp->left = p_node->left;
rp->right = p_node->right;
rp->parent = p_node->parent;
rp->color = p_node->color;
if (p_node->left != _data._nil) {
2017-08-30 00:18:32 +00:00
p_node->left->parent = rp;
}
if (p_node->right != _data._nil) {
2017-08-30 00:18:32 +00:00
p_node->right->parent = rp;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (p_node == p_node->parent->left) {
2016-03-08 23:00:52 +00:00
p_node->parent->left = rp;
2014-02-10 01:10:30 +00:00
} else {
p_node->parent->right = rp;
}
}
2016-03-08 23:00:52 +00:00
if (p_node->_next) {
2014-02-10 01:10:30 +00:00
p_node->_next->_prev = p_node->_prev;
}
if (p_node->_prev) {
2014-02-10 01:10:30 +00:00
p_node->_prev->_next = p_node->_next;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
memdelete_allocator<Element, A>(p_node);
_data.size_cache--;
ERR_FAIL_COND(_data._nil->color == RED);
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
void _calculate_depth(Element *p_element, int &max_d, int d) const {
if (p_element == _data._nil) {
2014-02-10 01:10:30 +00:00
return;
}
2017-09-20 17:52:40 +00:00
2014-02-10 01:10:30 +00:00
_calculate_depth(p_element->left, max_d, d + 1);
_calculate_depth(p_element->right, max_d, d + 1);
2017-09-20 17:52:40 +00:00
if (d > max_d) {
2014-02-10 01:10:30 +00:00
max_d = d;
}
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
void _cleanup_tree(Element *p_element) {
if (p_element == _data._nil) {
2014-02-10 01:10:30 +00:00
return;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
_cleanup_tree(p_element->left);
_cleanup_tree(p_element->right);
memdelete_allocator<Element, A>(p_element);
}
2016-03-08 23:00:52 +00:00
void _copy_from(const RBSet &p_set) {
2014-02-10 01:10:30 +00:00
clear();
// not the fastest way, but safeset to write.
for (Element *I = p_set.front(); I; I = I->next()) {
insert(I->get());
}
}
public:
const Element *find(const T &p_value) const {
if (!_data._root) {
2020-04-01 23:20:12 +00:00
return nullptr;
}
2014-02-10 01:10:30 +00:00
const Element *res = _find(p_value);
return res;
}
Element *find(const T &p_value) {
if (!_data._root) {
2020-04-01 23:20:12 +00:00
return nullptr;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
Element *res = _find(p_value);
return res;
}
2016-03-08 23:00:52 +00:00
2017-09-20 17:52:40 +00:00
Element *lower_bound(const T &p_value) const {
2022-01-10 09:19:47 +00:00
if (!_data._root) {
return nullptr;
}
2017-09-20 17:52:40 +00:00
return _lower_bound(p_value);
}
2014-02-10 01:10:30 +00:00
bool has(const T &p_value) const {
2020-04-01 23:20:12 +00:00
return find(p_value) != nullptr;
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
Element *insert(const T &p_value) {
if (!_data._root) {
2014-02-10 01:10:30 +00:00
_data._create_root();
}
2017-09-20 17:52:40 +00:00
return _insert(p_value);
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
void erase(Element *p_element) {
if (!_data._root || !p_element) {
2014-02-10 01:10:30 +00:00
return;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
_erase(p_element);
if (_data.size_cache == 0 && _data._root) {
2014-02-10 01:10:30 +00:00
_data._free_root();
}
2014-02-10 01:10:30 +00:00
}
bool erase(const T &p_value) {
if (!_data._root) {
2014-02-10 01:10:30 +00:00
return false;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
Element *e = find(p_value);
if (!e) {
2014-02-10 01:10:30 +00:00
return false;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
_erase(e);
if (_data.size_cache == 0 && _data._root) {
2014-02-10 01:10:30 +00:00
_data._free_root();
}
2014-02-10 01:10:30 +00:00
return true;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
Element *front() const {
if (!_data._root) {
2020-04-01 23:20:12 +00:00
return nullptr;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
Element *e = _data._root->left;
if (e == _data._nil) {
2020-04-01 23:20:12 +00:00
return nullptr;
}
2016-03-08 23:00:52 +00:00
while (e->left != _data._nil) {
2014-02-10 01:10:30 +00:00
e = e->left;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return e;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
Element *back() const {
if (!_data._root) {
2020-04-01 23:20:12 +00:00
return nullptr;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
Element *e = _data._root->left;
if (e == _data._nil) {
2020-04-01 23:20:12 +00:00
return nullptr;
}
2016-03-08 23:00:52 +00:00
while (e->right != _data._nil) {
2014-02-10 01:10:30 +00:00
e = e->right;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return e;
}
inline bool is_empty() const {
return _data.size_cache == 0;
}
inline int size() const {
return _data.size_cache;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
int calculate_depth() const {
// used for debug mostly
if (!_data._root) {
2014-02-10 01:10:30 +00:00
return 0;
}
2017-08-30 00:18:32 +00:00
2014-02-10 01:10:30 +00:00
int max_d = 0;
_calculate_depth(_data._root->left, max_d, 0);
return max_d;
}
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
void clear() {
if (!_data._root) {
2014-02-10 01:10:30 +00:00
return;
}
2014-02-10 01:10:30 +00:00
_cleanup_tree(_data._root->left);
_data._root->left = _data._nil;
_data.size_cache = 0;
_data._free_root();
}
2016-03-08 23:00:52 +00:00
void operator=(const RBSet &p_set) {
2014-02-10 01:10:30 +00:00
_copy_from(p_set);
}
2016-03-08 23:00:52 +00:00
RBSet(const RBSet &p_set) {
2014-02-10 01:10:30 +00:00
_copy_from(p_set);
}
2017-08-30 00:18:32 +00:00
_FORCE_INLINE_ RBSet() {}
2016-03-08 23:00:52 +00:00
~RBSet() {
2016-03-08 23:00:52 +00:00
clear();
2014-02-10 01:10:30 +00:00
}
};
#endif // RB_SET_H