diff options
Diffstat (limited to 'core/set.h')
-rw-r--r-- | core/set.h | 264 |
1 files changed, 132 insertions, 132 deletions
diff --git a/core/set.h b/core/set.h index 808ffe5b36..d4d19129d4 100644 --- a/core/set.h +++ b/core/set.h @@ -42,16 +42,16 @@ template <class T,class C=Comparator<T>,class A=DefaultAllocator > class Set { - - enum Color { + + enum Color { RED, BLACK }; - struct _Data; + struct _Data; public: - - + + class Element { private: @@ -72,18 +72,18 @@ public: return _next; } Element *next() { - + return _next; } const Element *prev() const { - + return _prev; } Element *prev() { - + return _prev; } - const T& get() const { + const T& get() const { return value; }; Element() { @@ -96,7 +96,7 @@ public: }; }; - + private: struct _Data { @@ -104,7 +104,7 @@ private: Element* _root; Element* _nil; int size_cache; - + _Data() { #ifdef GLOBALNIL_DISABLED @@ -119,7 +119,7 @@ private: size_cache=0; } - + void _create_root() { _root = memnew_allocator( Element,A ); @@ -136,7 +136,7 @@ private: } ~_Data() { - + _free_root(); #ifdef GLOBALNIL_DISABLED memdelete_allocator<Element,A>(_nil); @@ -144,16 +144,16 @@ private: // memdelete_allocator<Element,A>(_root); } }; - + _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 ) @@ -163,14 +163,14 @@ private: p_node->parent->left=r; 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) @@ -180,25 +180,25 @@ private: p_node->parent->right=l; else p_node->parent->left=l; - + l->right=p_node; p_node->parent=l; - + } - - inline Element* _successor(Element *p_node) const { - + + 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 minium of the right subtree of node */ node=node->left; } return node; } else { - + while(node == node->parent->right) { node=node->parent; } @@ -207,19 +207,19 @@ private: return node->parent; } } - - inline Element* _predecessor(Element *p_node) const { + + inline Element* _predecessor(Element *p_node) const { Element *node=p_node; - + if (node->left != _data._nil) { - + node=node->left; while(node->right != _data._nil) { /* returns the minium of the left subtree of node */ node=node->right; } return node; } else { - + while(node == node->parent->left) { if (node->parent == _data._root) return NULL; @@ -229,23 +229,23 @@ private: return node->parent; } } - - + + Element *_find(const T& p_value) const { - + Element *node = _data._root->left; - C less; - + 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)) node=node->right; else break; // found } - + return (node!=_data._nil)?node:NULL; } @@ -282,68 +282,68 @@ private: Element *_insert(const T& p_value, bool& r_exists) { - + Element *new_parent=_data._root; Element *node = _data._root->left; - C less; - + C less; + while (node!=_data._nil) { - + new_parent=node; - + if (less(p_value,node->value)) node=node->left; else if (less(node->value,p_value)) node=node->right; - else { + else { r_exists=true; return node; - } + } } - + 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; } - + r_exists=false; - + new_node->_next=_successor(new_node); new_node->_prev=_predecessor(new_node); if (new_node->_next) new_node->_next->_prev=new_node; if (new_node->_prev) new_node->_prev->_next=new_node; - + return new_node; } - + Element * _insert_rb(const T& p_value) { - + bool exists=false; Element *new_node = _insert(p_value,exists); if (exists) return new_node; - + Element *node=new_node; _data.size_cache++; - while(node->parent->color==RED) { - + while(node->parent->color==RED) { + if (node->parent == node->parent->parent->left) { - + Element *aux=node->parent->parent->right; - + if (aux->color==RED) { _set_color(node->parent,BLACK); _set_color(aux,BLACK); @@ -357,10 +357,10 @@ private: _set_color(node->parent,BLACK); _set_color(node->parent->parent,RED); _rotate_right(node->parent->parent); - } - } else { + } + } else { Element *aux=node->parent->parent->left; - + if (aux->color==RED) { _set_color(node->parent,BLACK); _set_color(aux,BLACK); @@ -374,19 +374,19 @@ private: _set_color(node->parent,BLACK); _set_color(node->parent->parent,RED); _rotate_left(node->parent->parent); - } + } } } _set_color(_data._root->left,BLACK); - return new_node; - } + return new_node; + } void _erase_fix(Element *p_node) { - + Element *root = _data._root->left; Element *node=p_node; - - + + while( (node->color==BLACK) && (root != node)) { if (node == node->parent->left) { Element *aux=node->parent->right; @@ -396,7 +396,7 @@ private: _rotate_left(node->parent); aux=node->parent->right; } - if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) { + if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) { _set_color(aux,RED); node=node->parent; } else { @@ -420,7 +420,7 @@ private: _rotate_right(node->parent); aux=node->parent->left; } - if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) { + if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) { _set_color(aux,RED); node=node->parent; } else { @@ -434,24 +434,24 @@ private: _set_color(node->parent,BLACK); _set_color(aux->left,BLACK); _rotate_right(node->parent); - node=root; + node=root; } } } - + _set_color(node,BLACK); - + ERR_FAIL_COND(_data._nil->color!=BLACK); } - + void _erase(Element *p_node) { - - + + Element *rp= ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node); if (!rp) rp=_data._nil; Element *node= (rp->left == _data._nil) ? rp->right : rp->left; - + if (_data._root == (node->parent=rp->parent) ) { _data._root->left=node; } else { @@ -461,47 +461,47 @@ private: rp->parent->right=node; } } - - if (rp != p_node) { - + + if (rp != p_node) { + ERR_FAIL_COND( rp == _data._nil ); - + if (rp->color==BLACK) _erase_fix(node); - - + + rp->left=p_node->left; rp->right=p_node->right; rp->parent=p_node->parent; rp->color=p_node->color; p_node->left->parent=rp; p_node->right->parent=rp; - + if (p_node == p_node->parent->left) { - p_node->parent->left=rp; + p_node->parent->left=rp; } else { p_node->parent->right=rp; } } else { - if (p_node->color==BLACK) + if (p_node->color==BLACK) _erase_fix(node); - + } - - + + if (p_node->_next) p_node->_next->_prev=p_node->_prev; if (p_node->_prev) p_node->_prev->_next=p_node->_next; - + memdelete_allocator<Element,A>(p_node); _data.size_cache--; ERR_FAIL_COND( _data._nil->color==RED ); } - - + + void _calculate_depth(Element *p_element,int &max_d,int d) const { - + if (p_element==_data._nil) { return; } @@ -510,30 +510,30 @@ private: if (d>max_d) max_d=d; } - + void _cleanup_tree(Element *p_element) { - + if (p_element==_data._nil) return; - + _cleanup_tree(p_element->left); _cleanup_tree(p_element->right); memdelete_allocator<Element,A>( p_element ); } - + 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) return NULL; @@ -542,30 +542,30 @@ public: } Element *find(const T& p_value) { - + if (!_data._root) return NULL; Element *res=_find(p_value); return res; } - + bool has(const T& p_value) const { - + if (!_data._root) return false; return find(p_value)!=NULL; } - + Element *insert(const T& p_value) { - + if (!_data._root) _data._create_root(); return _insert_rb(p_value); - + } - + void erase(Element* p_element) { - + if (!_data._root) return; _erase(p_element); @@ -574,7 +574,7 @@ public: } bool erase(const T& p_value) { - + if (!_data._root) return false; Element *e=find(p_value); @@ -585,32 +585,32 @@ public: _data._free_root(); return true; } - + Element *front() const { - + if (!_data._root) return NULL; Element *e=_data._root->left; if (e==_data._nil) return NULL; - + while(e->left!=_data._nil) e=e->left; - + return e; } - + Element *back() const { - + if (!_data._root) return NULL; Element *e=_data._root->left; if (e==_data._nil) return NULL; - + while(e->right!=_data._nil) e=e->right; - + return e; } @@ -619,7 +619,7 @@ public: return _lower_bound(p_value); } - + inline int size() const { return _data.size_cache; } int calculate_depth() const { // used for debug mostly @@ -629,9 +629,9 @@ public: _calculate_depth(_data._root->left,max_d,0); return max_d; } - + void clear() { - + if (!_data._root) return; @@ -641,25 +641,25 @@ public: _data._nil->parent=_data._nil; _data._free_root(); } - + void operator=(const Set& p_set) { - + _copy_from( p_set ); } - + Set(const Set& p_set) { - + _copy_from( p_set ); } _FORCE_INLINE_ Set() { - + } - - + + ~Set() { - - clear(); + + clear(); } }; |