40 template <
class K,
class V,
class C=Comparator<K>,
class A=DefaultAllocator>
53 friend class Map<K,V,C,A>;
84 const K& key()
const {
90 const V& value()
const {
96 const V&
get()
const {
118 _FORCE_INLINE_ _Data() {
119 #ifdef GLOBALNIL_DISABLED 120 _nil = memnew_allocator(
Element, A );
121 _nil->parent=_nil->left=_nil->right=_nil;
124 _nil=(
Element*)&_GlobalNilClass::_nil;
130 void _create_root() {
132 _root = memnew_allocator(
Element,A );
133 _root->parent=_root->left=_root->right=_nil;
140 memdelete_allocator<Element,A>(_root);
149 #ifdef GLOBALNIL_DISABLED 150 memdelete_allocator<Element,A>(_nil);
158 inline void _set_color(
Element *p_node,
int p_color) {
160 ERR_FAIL_COND( p_node == _data._nil && p_color == RED );
161 p_node->color=p_color;
163 inline void _rotate_left(
Element *p_node) {
166 p_node->right=r->left;
167 if (r->left != _data._nil )
168 r->left->parent=p_node;
169 r->parent=p_node->parent;
170 if (p_node==p_node->parent->left)
171 p_node->parent->left=r;
173 p_node->parent->right=r;
180 inline void _rotate_right(
Element *p_node) {
183 p_node->left=l->right;
184 if (l->right != _data._nil)
185 l->right->parent=p_node;
186 l->parent=p_node->parent;
187 if (p_node==p_node->parent->right)
188 p_node->parent->right=l;
190 p_node->parent->left=l;
201 if (node->right != _data._nil) {
204 while(node->left != _data._nil) {
210 while(node == node->parent->right) {
213 if (node->parent == _data._root)
222 if (node->left != _data._nil) {
225 while(node->right != _data._nil) {
231 while(node == node->parent->left) {
232 if (node->parent == _data._root)
241 Element *_find(
const K& p_key)
const {
243 Element *node = _data._root->left;
246 while(node!=_data._nil) {
248 if (less(p_key,node->_key))
250 else if (less(node->_key,p_key))
256 return (node!=_data._nil)?node:NULL;
259 Element *_find_closest(
const K& p_key)
const {
261 Element *node = _data._root->left;
265 while(node!=_data._nil) {
268 if (less(p_key,node->_key))
270 else if (less(node->_key,p_key))
276 if (node==_data._nil) {
279 if (less(p_key,prev->_key)) {
291 Element *_insert(
const K& p_key,
bool& r_exists) {
293 Element *new_parent=_data._root;
294 Element *node = _data._root->left;
297 while (node!=_data._nil) {
301 if (less(p_key,node->_key))
303 else if (less(node->_key,p_key))
314 new_node->parent=new_parent;
315 new_node->right=_data._nil;
316 new_node->left=_data._nil;
317 new_node->_key=p_key;
319 if (new_parent==_data._root || less(p_key,new_parent->_key)) {
321 new_parent->left=new_node;
323 new_parent->right=new_node;
328 new_node->_next=_successor(new_node);
329 new_node->_prev=_predecessor(new_node);
331 new_node->_next->_prev=new_node;
333 new_node->_prev->_next=new_node;
339 Element * _insert_rb(
const K& p_key,
const V& p_value) {
342 Element *new_node = _insert(p_key,exists);
345 new_node->_value=p_value;
353 while(node->parent->color==RED) {
355 if (node->parent == node->parent->parent->left) {
357 Element *aux=node->parent->parent->right;
359 if (aux->color==RED) {
360 _set_color(node->parent,BLACK);
361 _set_color(aux,BLACK);
362 _set_color(node->parent->parent,RED);
363 node=node->parent->parent;
365 if (node == node->parent->right) {
369 _set_color(node->parent,BLACK);
370 _set_color(node->parent->parent,RED);
371 _rotate_right(node->parent->parent);
374 Element *aux=node->parent->parent->left;
376 if (aux->color==RED) {
377 _set_color(node->parent,BLACK);
378 _set_color(aux,BLACK);
379 _set_color(node->parent->parent,RED);
380 node=node->parent->parent;
382 if (node == node->parent->left) {
386 _set_color(node->parent,BLACK);
387 _set_color(node->parent->parent,RED);
388 _rotate_left(node->parent->parent);
392 _set_color(_data._root->left,BLACK);
396 void _erase_fix(
Element *p_node) {
398 Element *root = _data._root->left;
402 while( (node->color==BLACK) && (root != node)) {
403 if (node == node->parent->left) {
404 Element *aux=node->parent->right;
405 if (aux->color==RED) {
406 _set_color(aux,BLACK);
407 _set_color(node->parent,RED);
408 _rotate_left(node->parent);
409 aux=node->parent->right;
411 if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) {
415 if (aux->right->color==BLACK) {
416 _set_color(aux->left,BLACK);
419 aux=node->parent->right;
421 _set_color(aux,node->parent->color);
422 _set_color(node->parent,BLACK);
423 _set_color(aux->right,BLACK);
424 _rotate_left(node->parent);
428 Element *aux=node->parent->left;
429 if (aux->color==RED) {
430 _set_color(aux,BLACK);
431 _set_color(node->parent,RED);;
432 _rotate_right(node->parent);
433 aux=node->parent->left;
435 if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) {
439 if (aux->left->color==BLACK) {
440 _set_color(aux->right,BLACK);
443 aux=node->parent->left;
445 _set_color(aux,node->parent->color);
446 _set_color(node->parent,BLACK);
447 _set_color(aux->left,BLACK);
448 _rotate_right(node->parent);
454 _set_color(node,BLACK);
456 ERR_FAIL_COND(_data._nil->color!=BLACK);
462 Element *rp= ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node);
465 Element *node= (rp->left == _data._nil) ? rp->right : rp->left;
467 if (_data._root == (node->parent=rp->parent) ) {
468 _data._root->left=node;
470 if (rp == rp->parent->left) {
471 rp->parent->left=node;
473 rp->parent->right=node;
479 ERR_FAIL_COND( rp == _data._nil );
481 if (rp->color==BLACK)
485 rp->left=p_node->left;
486 rp->right=p_node->right;
487 rp->parent=p_node->parent;
488 rp->color=p_node->color;
489 p_node->left->parent=rp;
490 p_node->right->parent=rp;
492 if (p_node == p_node->parent->left) {
493 p_node->parent->left=rp;
495 p_node->parent->right=rp;
498 if (p_node->color==BLACK)
505 p_node->_next->_prev=p_node->_prev;
507 p_node->_prev->_next=p_node->_next;
509 memdelete_allocator<Element,A>(p_node);
511 ERR_FAIL_COND( _data._nil->color==RED );
515 void _calculate_depth(
Element *p_element,
int &max_d,
int d)
const {
517 if (p_element==_data._nil) {
520 _calculate_depth(p_element->left,max_d,d+1);
521 _calculate_depth(p_element->right,max_d,d+1);
526 void _cleanup_tree(
Element *p_element) {
528 if (p_element==_data._nil)
531 _cleanup_tree(p_element->left);
532 _cleanup_tree(p_element->right);
533 memdelete_allocator<Element,A>( p_element );
536 void _copy_from(
const Map& p_map) {
540 for(
Element *I=p_map.front();I;I=I->next()) {
542 insert(I->key(),I->value());
547 const Element *find(
const K& p_key)
const {
552 const Element *res=_find(p_key);
556 Element *find(
const K& p_key) {
564 const Element *find_closest(
const K& p_key)
const {
568 const Element *res=_find_closest(p_key);
572 Element *find_closest(
const K& p_key) {
576 Element *res=_find_closest(p_key);
580 Element *insert(
const K& p_key,
const V& p_value) {
583 _data._create_root();
584 return _insert_rb(p_key,p_value);
588 void erase(
Element* p_element) {
593 if (_data.size_cache==0 && _data._root)
597 bool erase(
const K& p_key) {
608 bool has(
const K& p_key)
const {
612 return find(p_key) != NULL;
615 const V& operator[](
const K& p_key)
const {
617 ERR_FAIL_COND_V(!_data._root, *(V*)NULL);
619 ERR_FAIL_COND_V(!e, *(V*)NULL);
622 V& operator[](
const K& p_key) {
625 _data._create_root();
631 ERR_FAIL_COND_V(!e, *(V*)NULL);
644 while(e->left!=_data._nil)
658 while(e->right!=_data._nil)
664 inline bool empty()
const {
return _data.size_cache==0; }
665 inline int size()
const {
return _data.size_cache; }
666 int calculate_depth()
const {
671 _calculate_depth(_data._root->left,max_d,0);
679 _cleanup_tree(_data._root->left);
680 _data._root->left=_data._nil;
682 _data._nil->parent=_data._nil;
686 void operator=(
const Map& p_map) {
696 _FORCE_INLINE_
Map() {
Definition: tween_interpolaters.cpp:356