33 #include "os/memory.h" 43 template <
class T,
class C=Comparator<T>,
class A=DefaultAllocator >
58 friend class Set<T,C,A>;
86 const T&
get()
const {
110 #ifdef GLOBALNIL_DISABLED 111 _nil = memnew_allocator(
Element,A );
112 _nil->parent=_nil->left=_nil->right=_nil;
116 _nil=(
Element*)&_GlobalNilClass::_nil;
123 void _create_root() {
125 _root = memnew_allocator(
Element,A );
126 _root->parent=_root->left=_root->right=_nil;
133 memdelete_allocator<Element,A>(_root);
141 #ifdef GLOBALNIL_DISABLED 142 memdelete_allocator<Element,A>(_nil);
150 inline void _set_color(
Element *p_node,
int p_color) {
152 ERR_FAIL_COND( p_node == _data._nil && p_color == RED );
153 p_node->color=p_color;
155 inline void _rotate_left(
Element *p_node) {
158 p_node->right=r->left;
159 if (r->left != _data._nil )
160 r->left->parent=p_node;
161 r->parent=p_node->parent;
162 if (p_node==p_node->parent->left)
163 p_node->parent->left=r;
165 p_node->parent->right=r;
172 inline void _rotate_right(
Element *p_node) {
175 p_node->left=l->right;
176 if (l->right != _data._nil)
177 l->right->parent=p_node;
178 l->parent=p_node->parent;
179 if (p_node==p_node->parent->right)
180 p_node->parent->right=l;
182 p_node->parent->left=l;
193 if (node->right != _data._nil) {
196 while(node->left != _data._nil) {
202 while(node == node->parent->right) {
205 if (node->parent == _data._root)
214 if (node->left != _data._nil) {
217 while(node->right != _data._nil) {
223 while(node == node->parent->left) {
224 if (node->parent == _data._root)
234 Element *_find(
const T& p_value)
const {
236 Element *node = _data._root->left;
239 while(node!=_data._nil) {
241 if (less(p_value,node->value))
243 else if (less(node->value,p_value))
249 return (node!=_data._nil)?node:NULL;
252 Element *_lower_bound(
const T& p_value)
const {
254 Element *node = _data._root->left;
258 while(node!=_data._nil) {
261 if (less(p_value,node->value))
263 else if (less(node->value,p_value))
269 if (node==_data._nil) {
272 if (less(prev->value,p_value)) {
284 Element *_insert(
const T& p_value,
bool& r_exists) {
286 Element *new_parent=_data._root;
287 Element *node = _data._root->left;
290 while (node!=_data._nil) {
294 if (less(p_value,node->value))
296 else if (less(node->value,p_value))
306 new_node->parent=new_parent;
307 new_node->right=_data._nil;
308 new_node->left=_data._nil;
309 new_node->value=p_value;
311 if (new_parent==_data._root || less(p_value,new_parent->value)) {
313 new_parent->left=new_node;
315 new_parent->right=new_node;
320 new_node->_next=_successor(new_node);
321 new_node->_prev=_predecessor(new_node);
323 new_node->_next->_prev=new_node;
325 new_node->_prev->_next=new_node;
331 Element * _insert_rb(
const T& p_value) {
334 Element *new_node = _insert(p_value,exists);
341 while(node->parent->color==RED) {
343 if (node->parent == node->parent->parent->left) {
345 Element *aux=node->parent->parent->right;
347 if (aux->color==RED) {
348 _set_color(node->parent,BLACK);
349 _set_color(aux,BLACK);
350 _set_color(node->parent->parent,RED);
351 node=node->parent->parent;
353 if (node == node->parent->right) {
357 _set_color(node->parent,BLACK);
358 _set_color(node->parent->parent,RED);
359 _rotate_right(node->parent->parent);
362 Element *aux=node->parent->parent->left;
364 if (aux->color==RED) {
365 _set_color(node->parent,BLACK);
366 _set_color(aux,BLACK);
367 _set_color(node->parent->parent,RED);
368 node=node->parent->parent;
370 if (node == node->parent->left) {
374 _set_color(node->parent,BLACK);
375 _set_color(node->parent->parent,RED);
376 _rotate_left(node->parent->parent);
380 _set_color(_data._root->left,BLACK);
384 void _erase_fix(
Element *p_node) {
386 Element *root = _data._root->left;
390 while( (node->color==BLACK) && (root != node)) {
391 if (node == node->parent->left) {
392 Element *aux=node->parent->right;
393 if (aux->color==RED) {
394 _set_color(aux,BLACK);
395 _set_color(node->parent,RED);
396 _rotate_left(node->parent);
397 aux=node->parent->right;
399 if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) {
403 if (aux->right->color==BLACK) {
404 _set_color(aux->left,BLACK);
407 aux=node->parent->right;
409 _set_color(aux,node->parent->color);
410 _set_color(node->parent,BLACK);
411 _set_color(aux->right,BLACK);
412 _rotate_left(node->parent);
416 Element *aux=node->parent->left;
417 if (aux->color==RED) {
418 _set_color(aux,BLACK);
419 _set_color(node->parent,RED);;
420 _rotate_right(node->parent);
421 aux=node->parent->left;
423 if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) {
427 if (aux->left->color==BLACK) {
428 _set_color(aux->right,BLACK);
431 aux=node->parent->left;
433 _set_color(aux,node->parent->color);
434 _set_color(node->parent,BLACK);
435 _set_color(aux->left,BLACK);
436 _rotate_right(node->parent);
442 _set_color(node,BLACK);
444 ERR_FAIL_COND(_data._nil->color!=BLACK);
450 Element *rp= ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node);
453 Element *node= (rp->left == _data._nil) ? rp->right : rp->left;
455 if (_data._root == (node->parent=rp->parent) ) {
456 _data._root->left=node;
458 if (rp == rp->parent->left) {
459 rp->parent->left=node;
461 rp->parent->right=node;
467 ERR_FAIL_COND( rp == _data._nil );
469 if (rp->color==BLACK)
473 rp->left=p_node->left;
474 rp->right=p_node->right;
475 rp->parent=p_node->parent;
476 rp->color=p_node->color;
477 p_node->left->parent=rp;
478 p_node->right->parent=rp;
480 if (p_node == p_node->parent->left) {
481 p_node->parent->left=rp;
483 p_node->parent->right=rp;
486 if (p_node->color==BLACK)
493 p_node->_next->_prev=p_node->_prev;
495 p_node->_prev->_next=p_node->_next;
497 memdelete_allocator<Element,A>(p_node);
499 ERR_FAIL_COND( _data._nil->color==RED );
503 void _calculate_depth(
Element *p_element,
int &max_d,
int d)
const {
505 if (p_element==_data._nil) {
508 _calculate_depth(p_element->left,max_d,d+1);
509 _calculate_depth(p_element->right,max_d,d+1);
514 void _cleanup_tree(
Element *p_element) {
516 if (p_element==_data._nil)
519 _cleanup_tree(p_element->left);
520 _cleanup_tree(p_element->right);
521 memdelete_allocator<Element,A>( p_element );
524 void _copy_from(
const Set& p_set) {
528 for(
Element *I=p_set.front();I;I=I->next()) {
535 const Element *find(
const T& p_value)
const {
540 const Element *res=_find(p_value);
544 Element *find(
const T& p_value) {
552 bool has(
const T& p_value)
const {
556 return find(p_value)!=NULL;
559 Element *insert(
const T& p_value) {
562 _data._create_root();
563 return _insert_rb(p_value);
567 void erase(
Element* p_element) {
572 if (_data.size_cache==0 && _data._root)
576 bool erase(
const T& p_value) {
584 if (_data.size_cache==0 && _data._root)
597 while(e->left!=_data._nil)
611 while(e->right!=_data._nil)
617 Element *lower_bound(
const T& p_value)
const {
619 return _lower_bound(p_value);
623 inline int size()
const {
return _data.size_cache; }
624 int calculate_depth()
const {
629 _calculate_depth(_data._root->left,max_d,0);
638 _cleanup_tree(_data._root->left);
639 _data._root->left=_data._nil;
641 _data._nil->parent=_data._nil;
645 void operator=(
const Set& p_set) {
654 _FORCE_INLINE_
Set() {
Definition: tween_interpolaters.cpp:356