diff options
Diffstat (limited to 'core/set.h')
-rw-r--r-- | core/set.h | 196 |
1 files changed, 89 insertions, 107 deletions
diff --git a/core/set.h b/core/set.h index c17ee15350..1bc0a3f41e 100644 --- a/core/set.h +++ b/core/set.h @@ -39,7 +39,6 @@ template <class T, class C = Comparator<T>, class A = DefaultAllocator> class Set { - enum Color { RED, BLACK @@ -48,54 +47,41 @@ class Set { public: class Element { - private: friend class Set<T, C, A>; - int color; - Element *right; - Element *left; - Element *parent; - Element *_next; - Element *_prev; + int color = RED; + Element *right = nullptr; + Element *left = nullptr; + Element *parent = nullptr; + Element *_next = nullptr; + Element *_prev = nullptr; 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; } const T &get() const { return value; }; - Element() { - color = RED; - right = nullptr; - left = nullptr; - parent = nullptr; - _next = nullptr; - _prev = nullptr; - }; + Element() {} }; private: struct _Data { - - Element *_root; + Element *_root = nullptr; Element *_nil; - int size_cache; + int size_cache = 0; _FORCE_INLINE_ _Data() { #ifdef GLOBALNIL_DISABLED @@ -105,19 +91,15 @@ private: #else _nil = (Element *)&_GlobalNilClass::_nil; #endif - _root = nullptr; - size_cache = 0; } 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); _root = nullptr; @@ -125,7 +107,6 @@ private: } ~_Data() { - _free_root(); #ifdef GLOBALNIL_DISABLED @@ -137,62 +118,61 @@ private: _Data _data; 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; } inline void _rotate_left(Element *p_node) { - Element *r = p_node->right; p_node->right = r->left; - if (r->left != _data._nil) + if (r->left != _data._nil) { r->left->parent = p_node; + } r->parent = p_node->parent; - if (p_node == p_node->parent->left) + if (p_node == p_node->parent->left) { p_node->parent->left = r; - else + } else { p_node->parent->right = r; + } r->left = p_node; p_node->parent = r; } inline void _rotate_right(Element *p_node) { - Element *l = p_node->left; p_node->left = l->right; - if (l->right != _data._nil) + if (l->right != _data._nil) { l->right->parent = p_node; + } l->parent = p_node->parent; - if (p_node == p_node->parent->right) + if (p_node == p_node->parent->right) { p_node->parent->right = l; - else + } else { p_node->parent->left = l; + } l->right = p_node; p_node->parent = l; } inline Element *_successor(Element *p_node) const { - Element *node = p_node; if (node->right != _data._nil) { - node = node->right; while (node->left != _data._nil) { /* returns the minimum of the right subtree of node */ node = node->left; } return node; } else { - while (node == node->parent->right) { node = node->parent; } - if (node->parent == _data._root) + if (node->parent == _data._root) { return nullptr; // No successor, as p_node = last node + } return node->parent; } } @@ -201,43 +181,41 @@ private: Element *node = p_node; if (node->left != _data._nil) { - node = node->left; while (node->right != _data._nil) { /* returns the minimum of the left subtree of node */ node = node->right; } return node; } else { - while (node == node->parent->left) { node = node->parent; } - if (node == _data._root) + if (node == _data._root) { return nullptr; // No predecessor, as p_node = first node. + } return node->parent; } } Element *_find(const T &p_value) const { - Element *node = _data._root->left; C less; while (node != _data._nil) { - if (less(p_value, node->value)) + if (less(p_value, node->value)) { node = node->left; - else if (less(node->value, p_value)) + } else if (less(node->value, p_value)) { node = node->right; - else + } else { return node; // found + } } return nullptr; } Element *_lower_bound(const T &p_value) const { - Element *node = _data._root->left; Element *prev = nullptr; C less; @@ -245,25 +223,27 @@ private: while (node != _data._nil) { prev = node; - if (less(p_value, node->value)) + if (less(p_value, node->value)) { node = node->left; - else if (less(node->value, p_value)) + } else if (less(node->value, p_value)) { node = node->right; - else + } else { return node; // found + } } - if (prev == nullptr) + if (prev == nullptr) { return nullptr; // tree empty + } - if (less(prev->value, p_value)) + if (less(prev->value, p_value)) { prev = prev->_next; + } return prev; } void _insert_rb_fix(Element *p_new_node) { - Element *node = p_new_node; Element *nparent = node->parent; Element *ngrand_parent; @@ -312,20 +292,18 @@ private: } 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)) + if (less(p_value, node->value)) { node = node->left; - else if (less(node->value, p_value)) + } else if (less(node->value, p_value)) { node = node->right; - else { + } else { return node; // Return existing node } } @@ -345,10 +323,12 @@ private: new_node->_next = _successor(new_node); new_node->_prev = _predecessor(new_node); - if (new_node->_next) + if (new_node->_next) { new_node->_next->_prev = new_node; - if (new_node->_prev) + } + if (new_node->_prev) { new_node->_prev->_next = new_node; + } _data.size_cache++; _insert_rb_fix(new_node); @@ -356,7 +336,6 @@ private: } void _erase_fix_rb(Element *p_node) { - Element *root = _data._root->left; Element *node = _data._nil; Element *sibling = p_node; @@ -418,7 +397,6 @@ private: } void _erase(Element *p_node) { - Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : p_node->_next; Element *node = (rp->left == _data._nil) ? rp->right : rp->left; @@ -439,17 +417,18 @@ private: } if (rp != p_node) { - ERR_FAIL_COND(rp == _data._nil); 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) + if (p_node->left != _data._nil) { p_node->left->parent = rp; - if (p_node->right != _data._nil) + } + if (p_node->right != _data._nil) { p_node->right->parent = rp; + } if (p_node == p_node->parent->left) { p_node->parent->left = rp; @@ -458,10 +437,12 @@ private: } } - if (p_node->_next) + if (p_node->_next) { p_node->_next->_prev = p_node->_prev; - if (p_node->_prev) + } + if (p_node->_prev) { p_node->_prev->_next = p_node->_next; + } memdelete_allocator<Element, A>(p_node); _data.size_cache--; @@ -469,21 +450,22 @@ private: } void _calculate_depth(Element *p_element, int &max_d, int d) const { - - if (p_element == _data._nil) + if (p_element == _data._nil) { return; + } _calculate_depth(p_element->left, max_d, d + 1); _calculate_depth(p_element->right, max_d, d + 1); - if (d > max_d) + if (d > max_d) { max_d = d; + } } void _cleanup_tree(Element *p_element) { - - if (p_element == _data._nil) + if (p_element == _data._nil) { return; + } _cleanup_tree(p_element->left); _cleanup_tree(p_element->right); @@ -491,102 +473,105 @@ private: } void _copy_from(const Set &p_set) { - 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) + if (!_data._root) { return nullptr; + } const Element *res = _find(p_value); return res; } Element *find(const T &p_value) { - - if (!_data._root) + if (!_data._root) { return nullptr; + } Element *res = _find(p_value); return res; } Element *lower_bound(const T &p_value) const { - return _lower_bound(p_value); } bool has(const T &p_value) const { - return find(p_value) != nullptr; } Element *insert(const T &p_value) { - - if (!_data._root) + if (!_data._root) { _data._create_root(); + } return _insert(p_value); } void erase(Element *p_element) { - - if (!_data._root || !p_element) + if (!_data._root || !p_element) { return; + } _erase(p_element); - if (_data.size_cache == 0 && _data._root) + if (_data.size_cache == 0 && _data._root) { _data._free_root(); + } } bool erase(const T &p_value) { - - if (!_data._root) + if (!_data._root) { return false; + } Element *e = find(p_value); - if (!e) + if (!e) { return false; + } _erase(e); - if (_data.size_cache == 0 && _data._root) + if (_data.size_cache == 0 && _data._root) { _data._free_root(); + } return true; } Element *front() const { - - if (!_data._root) + if (!_data._root) { return nullptr; + } Element *e = _data._root->left; - if (e == _data._nil) + if (e == _data._nil) { return nullptr; + } - while (e->left != _data._nil) + while (e->left != _data._nil) { e = e->left; + } return e; } Element *back() const { - - if (!_data._root) + if (!_data._root) { return nullptr; + } Element *e = _data._root->left; - if (e == _data._nil) + if (e == _data._nil) { return nullptr; + } - while (e->right != _data._nil) + while (e->right != _data._nil) { e = e->right; + } return e; } @@ -596,8 +581,9 @@ public: int calculate_depth() const { // used for debug mostly - if (!_data._root) + if (!_data._root) { return 0; + } int max_d = 0; _calculate_depth(_data._root->left, max_d, 0); @@ -605,9 +591,9 @@ public: } void clear() { - - if (!_data._root) + if (!_data._root) { return; + } _cleanup_tree(_data._root->left); _data._root->left = _data._nil; @@ -616,20 +602,16 @@ public: } void operator=(const Set &p_set) { - _copy_from(p_set); } Set(const Set &p_set) { - _copy_from(p_set); } - _FORCE_INLINE_ Set() { - } + _FORCE_INLINE_ Set() {} ~Set() { - clear(); } }; |