2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* map.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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 MAP_H
|
|
|
|
#define MAP_H
|
|
|
|
|
|
|
|
#include "set.h"
|
|
|
|
/**
|
|
|
|
@author Juan Linietsky <reduzio@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
// based on the very nice implementation of rb-trees by:
|
|
|
|
// http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
template <class K, class V, class C = Comparator<K>, class A = DefaultAllocator>
|
2014-02-10 01:10:30 +00:00
|
|
|
class Map {
|
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;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
public:
|
2014-02-10 01:10:30 +00:00
|
|
|
class Element {
|
|
|
|
|
|
|
|
private:
|
2017-03-18 23:36:26 +00:00
|
|
|
friend class Map<K, V, C, A>;
|
2014-02-10 01:10:30 +00:00
|
|
|
//Color color;
|
|
|
|
int color;
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *right;
|
|
|
|
Element *left;
|
|
|
|
Element *parent;
|
|
|
|
Element *_next;
|
|
|
|
Element *_prev;
|
2014-02-10 01:10:30 +00:00
|
|
|
K _key;
|
|
|
|
V _value;
|
|
|
|
|
|
|
|
//_Data *data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
const Element *next() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return _next;
|
|
|
|
}
|
|
|
|
Element *next() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return _next;
|
|
|
|
}
|
|
|
|
const Element *prev() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return _prev;
|
|
|
|
}
|
|
|
|
Element *prev() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return _prev;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
const K &key() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return _key;
|
|
|
|
};
|
2017-03-18 23:36:26 +00:00
|
|
|
V &value() {
|
2014-02-10 01:10:30 +00:00
|
|
|
return _value;
|
|
|
|
};
|
2017-03-18 23:36:26 +00:00
|
|
|
const V &value() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return _value;
|
|
|
|
};
|
2017-03-18 23:36:26 +00:00
|
|
|
V &get() {
|
2014-02-10 01:10:30 +00:00
|
|
|
return _value;
|
|
|
|
};
|
2017-03-18 23:36:26 +00:00
|
|
|
const V &get() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return _value;
|
|
|
|
};
|
|
|
|
Element() {
|
2017-03-18 23:36:26 +00:00
|
|
|
color = RED;
|
|
|
|
right = NULL;
|
|
|
|
left = NULL;
|
|
|
|
parent = NULL;
|
|
|
|
_next = NULL;
|
|
|
|
_prev = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct _Data {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *_root;
|
|
|
|
Element *_nil;
|
2014-02-10 01:10:30 +00:00
|
|
|
int size_cache;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
_FORCE_INLINE_ _Data() {
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifdef GLOBALNIL_DISABLED
|
2017-03-18 23:36:26 +00:00
|
|
|
_nil = memnew_allocator(Element, A);
|
|
|
|
_nil->parent = _nil->left = _nil->right = _nil;
|
|
|
|
_nil->color = BLACK;
|
2014-02-10 01:10:30 +00:00
|
|
|
#else
|
2017-03-18 23:36:26 +00:00
|
|
|
_nil = (Element *)&_GlobalNilClass::_nil;
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|
2017-03-18 23:36:26 +00:00
|
|
|
_root = NULL;
|
|
|
|
size_cache = 0;
|
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 _create_root() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_root = memnew_allocator(Element, A);
|
|
|
|
_root->parent = _root->left = _root->right = _nil;
|
|
|
|
_root->color = BLACK;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _free_root() {
|
|
|
|
|
|
|
|
if (_root) {
|
2017-03-18 23:36:26 +00:00
|
|
|
memdelete_allocator<Element, A>(_root);
|
|
|
|
_root = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~_Data() {
|
|
|
|
|
|
|
|
_free_root();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifdef GLOBALNIL_DISABLED
|
2017-03-18 23:36:26 +00:00
|
|
|
memdelete_allocator<Element, A>(_nil);
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|
2017-03-18 23:36:26 +00:00
|
|
|
// memdelete_allocator<Element,A>(_root);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
};
|
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) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
|
|
|
|
p_node->color = p_color;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
inline void _rotate_left(Element *p_node) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *r = p_node->right;
|
|
|
|
p_node->right = r->left;
|
|
|
|
if (r->left != _data._nil)
|
|
|
|
r->left->parent = p_node;
|
|
|
|
r->parent = p_node->parent;
|
|
|
|
if (p_node == p_node->parent->left)
|
|
|
|
p_node->parent->left = r;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
p_node->parent->right = r;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
r->left = p_node;
|
|
|
|
p_node->parent = r;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
inline void _rotate_right(Element *p_node) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *l = p_node->left;
|
|
|
|
p_node->left = l->right;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (l->right != _data._nil)
|
2017-03-18 23:36:26 +00:00
|
|
|
l->right->parent = p_node;
|
|
|
|
l->parent = p_node->parent;
|
|
|
|
if (p_node == p_node->parent->right)
|
|
|
|
p_node->parent->right = l;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
p_node->parent->left = l;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
l->right = p_node;
|
|
|
|
p_node->parent = l;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
inline Element *_successor(Element *p_node) const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +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) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
node = node->right;
|
|
|
|
while (node->left != _data._nil) { /* returns the minium of the right subtree of node */
|
|
|
|
node = node->left;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
} else {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (node == node->parent->right) {
|
|
|
|
node = node->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
if (node->parent == _data._root)
|
|
|
|
return NULL;
|
|
|
|
return node->parent;
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
inline Element *_predecessor(Element *p_node) const {
|
|
|
|
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) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
node = node->left;
|
|
|
|
while (node->right != _data._nil) { /* returns the minium of the left subtree of node */
|
|
|
|
node = node->right;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
} else {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (node == node->parent->left) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (node->parent == _data._root)
|
2016-03-08 23:00:52 +00:00
|
|
|
return NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
node = node->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return node->parent;
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *_find(const K &p_key) const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Element *node = _data._root->left;
|
2016-03-08 23:00:52 +00:00
|
|
|
C less;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (node != _data._nil) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (less(p_key, node->_key))
|
|
|
|
node = node->left;
|
|
|
|
else if (less(node->_key, p_key))
|
|
|
|
node = node->right;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
break; // found
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return (node != _data._nil) ? node : NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *_find_closest(const K &p_key) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Element *node = _data._root->left;
|
|
|
|
Element *prev = NULL;
|
|
|
|
C less;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (node != _data._nil) {
|
|
|
|
prev = node;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (less(p_key, node->_key))
|
|
|
|
node = node->left;
|
|
|
|
else if (less(node->_key, p_key))
|
|
|
|
node = node->right;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
break; // found
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (node == _data._nil) {
|
|
|
|
if (prev == NULL)
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (less(p_key, prev->_key)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
prev = prev->_prev;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prev;
|
|
|
|
|
|
|
|
} else
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *_insert(const K &p_key, bool &r_exists) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *new_parent = _data._root;
|
2014-02-10 01:10:30 +00:00
|
|
|
Element *node = _data._root->left;
|
2016-03-08 23:00:52 +00:00
|
|
|
C less;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (node != _data._nil) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
new_parent = node;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (less(p_key, node->_key))
|
|
|
|
node = node->left;
|
|
|
|
else if (less(node->_key, p_key))
|
|
|
|
node = node->right;
|
2016-03-08 23:00:52 +00:00
|
|
|
else {
|
2017-03-18 23:36:26 +00:00
|
|
|
r_exists = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
return node;
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *new_node = memnew_allocator(Element, A);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
new_node->parent = new_parent;
|
|
|
|
new_node->right = _data._nil;
|
|
|
|
new_node->left = _data._nil;
|
|
|
|
new_node->_key = p_key;
|
2014-02-10 01:10:30 +00:00
|
|
|
//new_node->data=_data;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (new_parent == _data._root || less(p_key, new_parent->_key)) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
new_parent->left = new_node;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
new_parent->right = new_node;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
r_exists = false;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
new_node->_next = _successor(new_node);
|
|
|
|
new_node->_prev = _predecessor(new_node);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (new_node->_next)
|
2017-03-18 23:36:26 +00:00
|
|
|
new_node->_next->_prev = new_node;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (new_node->_prev)
|
2017-03-18 23:36:26 +00:00
|
|
|
new_node->_prev->_next = new_node;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return new_node;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *_insert_rb(const K &p_key, const V &p_value) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool exists = false;
|
|
|
|
Element *new_node = _insert(p_key, exists);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (new_node) {
|
2017-03-18 23:36:26 +00:00
|
|
|
new_node->_value = p_value;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
if (exists)
|
|
|
|
return new_node;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *node = new_node;
|
2014-02-10 01:10:30 +00:00
|
|
|
_data.size_cache++;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (node->parent->color == RED) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (node->parent == node->parent->parent->left) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *aux = node->parent->parent->right;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (aux->color == RED) {
|
|
|
|
_set_color(node->parent, BLACK);
|
|
|
|
_set_color(aux, BLACK);
|
|
|
|
_set_color(node->parent->parent, RED);
|
|
|
|
node = node->parent->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
if (node == node->parent->right) {
|
2017-03-18 23:36:26 +00:00
|
|
|
node = node->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_left(node);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
_set_color(node->parent, BLACK);
|
|
|
|
_set_color(node->parent->parent, RED);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_right(node->parent->parent);
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *aux = node->parent->parent->left;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (aux->color == RED) {
|
|
|
|
_set_color(node->parent, BLACK);
|
|
|
|
_set_color(aux, BLACK);
|
|
|
|
_set_color(node->parent->parent, RED);
|
|
|
|
node = node->parent->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
if (node == node->parent->left) {
|
2017-03-18 23:36:26 +00:00
|
|
|
node = node->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_right(node);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
_set_color(node->parent, BLACK);
|
|
|
|
_set_color(node->parent->parent, RED);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_left(node->parent->parent);
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
_set_color(_data._root->left, BLACK);
|
2016-03-08 23:00:52 +00:00
|
|
|
return new_node;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void _erase_fix(Element *p_node) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Element *root = _data._root->left;
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *node = p_node;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while ((node->color == BLACK) && (root != node)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (node == node->parent->left) {
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *aux = node->parent->right;
|
|
|
|
if (aux->color == RED) {
|
|
|
|
_set_color(aux, BLACK);
|
|
|
|
_set_color(node->parent, RED);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_left(node->parent);
|
2017-03-18 23:36:26 +00:00
|
|
|
aux = node->parent->right;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((aux->right->color == BLACK) && (aux->left->color == BLACK)) {
|
|
|
|
_set_color(aux, RED);
|
|
|
|
node = node->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (aux->right->color == BLACK) {
|
|
|
|
_set_color(aux->left, BLACK);
|
|
|
|
_set_color(aux, RED);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_right(aux);
|
2017-03-18 23:36:26 +00:00
|
|
|
aux = node->parent->right;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
_set_color(aux, node->parent->color);
|
|
|
|
_set_color(node->parent, BLACK);
|
|
|
|
_set_color(aux->right, BLACK);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_left(node->parent);
|
2017-03-18 23:36:26 +00:00
|
|
|
node = root; /* this is to exit while loop */
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else { /* the code below is has left and right switched from above */
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *aux = node->parent->left;
|
|
|
|
if (aux->color == RED) {
|
|
|
|
_set_color(aux, BLACK);
|
|
|
|
_set_color(node->parent, RED);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_right(node->parent);
|
2017-03-18 23:36:26 +00:00
|
|
|
aux = node->parent->left;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((aux->right->color == BLACK) && (aux->left->color == BLACK)) {
|
|
|
|
_set_color(aux, RED);
|
|
|
|
node = node->parent;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (aux->left->color == BLACK) {
|
|
|
|
_set_color(aux->right, BLACK);
|
|
|
|
_set_color(aux, RED);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_left(aux);
|
2017-03-18 23:36:26 +00:00
|
|
|
aux = node->parent->left;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
_set_color(aux, node->parent->color);
|
|
|
|
_set_color(node->parent, BLACK);
|
|
|
|
_set_color(aux->left, BLACK);
|
2014-02-10 01:10:30 +00:00
|
|
|
_rotate_right(node->parent);
|
2017-03-18 23:36:26 +00:00
|
|
|
node = root;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_set_color(node, BLACK);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(_data._nil->color != BLACK);
|
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_node) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!rp)
|
2017-03-18 23:36:26 +00:00
|
|
|
rp = _data._nil;
|
|
|
|
Element *node = (rp->left == _data._nil) ? rp->right : rp->left;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (_data._root == (node->parent = rp->parent)) {
|
|
|
|
_data._root->left = node;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
if (rp == rp->parent->left) {
|
2017-03-18 23:36:26 +00:00
|
|
|
rp->parent->left = node;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
rp->parent->right = node;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
if (rp != p_node) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(rp == _data._nil);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (rp->color == BLACK)
|
2014-02-10 01:10:30 +00:00
|
|
|
_erase_fix(node);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_node == p_node->parent->left) {
|
2017-03-18 23:36:26 +00:00
|
|
|
p_node->parent->left = rp;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
p_node->parent->right = rp;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_node->color == BLACK)
|
2014-02-10 01:10:30 +00:00
|
|
|
_erase_fix(node);
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_node->_next)
|
2017-03-18 23:36:26 +00:00
|
|
|
p_node->_next->_prev = p_node->_prev;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_node->_prev)
|
2017-03-18 23:36:26 +00:00
|
|
|
p_node->_prev->_next = p_node->_next;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
memdelete_allocator<Element, A>(p_node);
|
2014-02-10 01:10:30 +00:00
|
|
|
_data.size_cache--;
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(_data._nil->color == RED);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void _calculate_depth(Element *p_element, int &max_d, int d) const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_element == _data._nil) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
_calculate_depth(p_element->left, max_d, d + 1);
|
|
|
|
_calculate_depth(p_element->right, max_d, d + 1);
|
|
|
|
if (d > max_d)
|
|
|
|
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) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2017-03-18 23:36:26 +00:00
|
|
|
memdelete_allocator<Element, A>(p_element);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void _copy_from(const Map &p_map) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
clear();
|
|
|
|
// not the fastest way, but safeset to write.
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Element *I = p_map.front(); I; I = I->next()) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
insert(I->key(), I->value());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
public:
|
|
|
|
const Element *find(const K &p_key) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!_data._root)
|
|
|
|
return NULL;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Element *res = _find(p_key);
|
2014-02-10 01:10:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *find(const K &p_key) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
return NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *res = _find(p_key);
|
2014-02-10 01:10:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Element *find_closest(const K &p_key) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!_data._root)
|
|
|
|
return NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
const Element *res = _find_closest(p_key);
|
2014-02-10 01:10:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *find_closest(const K &p_key) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!_data._root)
|
|
|
|
return NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *res = _find_closest(p_key);
|
2014-02-10 01:10:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *insert(const K &p_key, const V &p_value) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
_data._create_root();
|
2017-03-18 23:36:26 +00:00
|
|
|
return _insert_rb(p_key, p_value);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void erase(Element *p_element) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
return;
|
|
|
|
_erase(p_element);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (_data.size_cache == 0 && _data._root)
|
2014-02-10 01:10:30 +00:00
|
|
|
_data._free_root();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool erase(const K &p_key) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
return false;
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *e = find(p_key);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!e)
|
|
|
|
return false;
|
2016-03-08 23:00:52 +00:00
|
|
|
_erase(e);
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool has(const K &p_key) const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
return false;
|
|
|
|
return find(p_key) != NULL;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const V &operator[](const K &p_key) const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!_data._root, *(V *)NULL); // crash on purpose
|
|
|
|
const Element *e = find(p_key);
|
|
|
|
ERR_FAIL_COND_V(!e, *(V *)NULL); // crash on purpose
|
2014-02-10 01:10:30 +00:00
|
|
|
return e->_value;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
V &operator[](const K &p_key) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
_data._create_root();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *e = find(p_key);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!e)
|
2017-03-18 23:36:26 +00:00
|
|
|
e = insert(p_key, V());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!e, *(V *)NULL); // crash on purpose
|
2014-02-10 01:10:30 +00:00
|
|
|
return e->_value;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Element *front() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
return NULL;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *e = _data._root->left;
|
|
|
|
if (e == _data._nil)
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (e->left != _data._nil)
|
|
|
|
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 {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
return NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
Element *e = _data._root->left;
|
|
|
|
if (e == _data._nil)
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (e->right != _data._nil)
|
|
|
|
e = e->right;
|
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
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
inline bool empty() const { return _data.size_cache == 0; }
|
2014-02-10 01:10:30 +00:00
|
|
|
inline int size() const { return _data.size_cache; }
|
|
|
|
int calculate_depth() const {
|
|
|
|
// used for debug mostly
|
|
|
|
if (!_data._root)
|
|
|
|
return 0;
|
2017-03-18 23:36:26 +00:00
|
|
|
int max_d = 0;
|
|
|
|
_calculate_depth(_data._root->left, max_d, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
return max_d;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void clear() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!_data._root)
|
|
|
|
return;
|
|
|
|
_cleanup_tree(_data._root->left);
|
2017-03-18 23:36:26 +00:00
|
|
|
_data._root->left = _data._nil;
|
|
|
|
_data.size_cache = 0;
|
|
|
|
_data._nil->parent = _data._nil;
|
2014-02-10 01:10:30 +00:00
|
|
|
_data._free_root();
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void operator=(const Map &p_map) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_copy_from(p_map);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map(const Map &p_map) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_copy_from(p_map);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Map() {
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
~Map() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
clear();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|