2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* map.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef MAP_H
|
|
|
|
#define MAP_H
|
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/error/error_macros.h"
|
2021-04-14 11:43:45 +02:00
|
|
|
#include "core/os/memory.h"
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
#include "core/templates/pair.h"
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// based on the very nice implementation of rb-trees by:
|
2019-07-24 02:06:12 +02:00
|
|
|
// https://web.archive.org/web/20120507164830/http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class K, class V, class C = Comparator<K>, class A = DefaultAllocator>
|
2014-02-10 02:10:30 +01:00
|
|
|
class Map {
|
2016-03-09 00:00:52 +01:00
|
|
|
enum Color {
|
2014-02-10 02:10:30 +01:00
|
|
|
RED,
|
|
|
|
BLACK
|
|
|
|
};
|
2016-03-09 00:00:52 +01:00
|
|
|
struct _Data;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
2014-02-10 02:10:30 +01:00
|
|
|
class Element {
|
|
|
|
private:
|
2017-03-05 16:44:50 +01:00
|
|
|
friend class Map<K, V, C, A>;
|
2020-05-12 17:01:17 +02:00
|
|
|
int color = RED;
|
|
|
|
Element *right = nullptr;
|
|
|
|
Element *left = nullptr;
|
|
|
|
Element *parent = nullptr;
|
|
|
|
Element *_next = nullptr;
|
|
|
|
Element *_prev = nullptr;
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
KeyValue<K, V> _data;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
public:
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
KeyValue<K, V> &key_value() { return _data; }
|
|
|
|
const KeyValue<K, V> &key_value() const { return _data; }
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
const Element *next() const {
|
|
|
|
return _next;
|
|
|
|
}
|
|
|
|
Element *next() {
|
|
|
|
return _next;
|
|
|
|
}
|
|
|
|
const Element *prev() const {
|
|
|
|
return _prev;
|
|
|
|
}
|
|
|
|
Element *prev() {
|
|
|
|
return _prev;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
const K &key() const {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
return _data.key;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
V &value() {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
return _data.value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
const V &value() const {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
return _data.value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
V &get() {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
return _data.value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
const V &get() const {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
return _data.value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
Element(const KeyValue<K, V> &p_data) :
|
|
|
|
_data(p_data) {}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
typedef KeyValue<K, V> ValueType;
|
|
|
|
|
|
|
|
struct Iterator {
|
|
|
|
_FORCE_INLINE_ KeyValue<K, V> &operator*() const {
|
|
|
|
return E->key_value();
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ KeyValue<K, V> *operator->() const { return &E->key_value(); }
|
|
|
|
_FORCE_INLINE_ Iterator &operator++() {
|
|
|
|
E = E->next();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator &operator--() {
|
|
|
|
E = E->prev();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
|
|
|
|
|
|
|
|
Iterator(Element *p_E) { E = p_E; }
|
|
|
|
Iterator() {}
|
|
|
|
Iterator(const Iterator &p_it) { E = p_it.E; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Element *E = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstIterator {
|
|
|
|
_FORCE_INLINE_ const KeyValue<K, V> &operator*() const {
|
|
|
|
return E->key_value();
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const KeyValue<K, V> *operator->() const { return &E->key_value(); }
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator++() {
|
|
|
|
E = E->next();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator--() {
|
|
|
|
E = E->prev();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
|
|
|
|
|
|
|
|
ConstIterator(const Element *p_E) { E = p_E; }
|
|
|
|
ConstIterator() {}
|
|
|
|
ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Element *E = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Iterator begin() {
|
|
|
|
return Iterator(front());
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator end() {
|
|
|
|
return Iterator(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
//to use when replacing find()
|
|
|
|
_FORCE_INLINE_ Iterator find(const K &p_key) {
|
|
|
|
return Iterator(find(p_key));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
_FORCE_INLINE_ void remove(const Iterator &p_iter) {
|
|
|
|
return erase(p_iter.E);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ ConstIterator begin() const {
|
|
|
|
return ConstIterator(front());
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator end() const {
|
|
|
|
return ConstIterator(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
//to use when replacing find()
|
|
|
|
_FORCE_INLINE_ ConstIterator find(const K &p_key) const {
|
|
|
|
return ConstIterator(find(p_key));
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
private:
|
|
|
|
struct _Data {
|
2020-05-12 17:01:17 +02:00
|
|
|
Element *_root = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *_nil;
|
2020-05-12 17:01:17 +02:00
|
|
|
int size_cache = 0;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ _Data() {
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef GLOBALNIL_DISABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
_nil = memnew_allocator(Element, A);
|
|
|
|
_nil->parent = _nil->left = _nil->right = _nil;
|
|
|
|
_nil->color = BLACK;
|
2014-02-10 02:10:30 +01:00
|
|
|
#else
|
2017-03-05 16:44:50 +01:00
|
|
|
_nil = (Element *)&_GlobalNilClass::_nil;
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void _create_root() {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
_root = memnew_allocator(Element(KeyValue<K, V>(K(), V())), A);
|
2017-03-05 16:44:50 +01:00
|
|
|
_root->parent = _root->left = _root->right = _nil;
|
|
|
|
_root->color = BLACK;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void _free_root() {
|
|
|
|
if (_root) {
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete_allocator<Element, A>(_root);
|
2020-04-02 01:20:12 +02:00
|
|
|
_root = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~_Data() {
|
|
|
|
_free_root();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef GLOBALNIL_DISABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete_allocator<Element, A>(_nil);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_Data _data;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
inline void _set_color(Element *p_node, int p_color) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
|
|
|
|
p_node->color = p_color;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
inline void _rotate_left(Element *p_node) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *r = p_node->right;
|
|
|
|
p_node->right = r->left;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r->left != _data._nil) {
|
2017-03-05 16:44:50 +01:00
|
|
|
r->left->parent = p_node;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
r->parent = p_node->parent;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_node == p_node->parent->left) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->parent->left = r;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->parent->right = r;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
r->left = p_node;
|
|
|
|
p_node->parent = r;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
inline void _rotate_right(Element *p_node) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *l = p_node->left;
|
|
|
|
p_node->left = l->right;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (l->right != _data._nil) {
|
2017-03-05 16:44:50 +01:00
|
|
|
l->right->parent = p_node;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
l->parent = p_node->parent;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_node == p_node->parent->right) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->parent->right = l;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->parent->left = l;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
l->right = p_node;
|
|
|
|
p_node->parent = l;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
inline Element *_successor(Element *p_node) const {
|
|
|
|
Element *node = p_node;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (node->right != _data._nil) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->right;
|
2018-02-21 17:30:55 +01:00
|
|
|
while (node->left != _data._nil) { /* returns the minimum of the right subtree of node */
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->left;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (node == node->parent->right) {
|
|
|
|
node = node->parent;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (node->parent == _data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr; // No successor, as p_node = last node
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return node->parent;
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
inline Element *_predecessor(Element *p_node) const {
|
|
|
|
Element *node = p_node;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (node->left != _data._nil) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->left;
|
2018-02-21 17:30:55 +01:00
|
|
|
while (node->right != _data._nil) { /* returns the minimum of the left subtree of node */
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->right;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (node == node->parent->left) {
|
|
|
|
node = node->parent;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (node == _data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr; // No predecessor, as p_node = first node
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return node->parent;
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *_find(const K &p_key) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *node = _data._root->left;
|
2016-03-09 00:00:52 +01:00
|
|
|
C less;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (node != _data._nil) {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
if (less(p_key, node->_data.key)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->left;
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
} else if (less(node->_data.key, p_key)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->right;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-09-20 19:53:18 +02:00
|
|
|
return node; // found
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *_find_closest(const K &p_key) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *node = _data._root->left;
|
2020-04-02 01:20:12 +02:00
|
|
|
Element *prev = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
C less;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (node != _data._nil) {
|
|
|
|
prev = node;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
if (less(p_key, node->_data.key)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->left;
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
} else if (less(node->_data.key, p_key)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->right;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-09-20 19:53:18 +02:00
|
|
|
return node; // found
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (prev == nullptr) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr; // tree empty
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
if (less(p_key, prev->_data.key)) {
|
2017-09-20 19:53:18 +02:00
|
|
|
prev = prev->_prev;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-20 19:53:18 +02:00
|
|
|
return prev;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-20 19:53:18 +02:00
|
|
|
void _insert_rb_fix(Element *p_new_node) {
|
|
|
|
Element *node = p_new_node;
|
|
|
|
Element *nparent = node->parent;
|
|
|
|
Element *ngrand_parent;
|
|
|
|
|
|
|
|
while (nparent->color == RED) {
|
|
|
|
ngrand_parent = nparent->parent;
|
|
|
|
|
|
|
|
if (nparent == ngrand_parent->left) {
|
|
|
|
if (ngrand_parent->right->color == RED) {
|
|
|
|
_set_color(nparent, BLACK);
|
|
|
|
_set_color(ngrand_parent->right, BLACK);
|
|
|
|
_set_color(ngrand_parent, RED);
|
|
|
|
node = ngrand_parent;
|
|
|
|
nparent = node->parent;
|
|
|
|
} else {
|
|
|
|
if (node == nparent->right) {
|
|
|
|
_rotate_left(nparent);
|
|
|
|
node = nparent;
|
|
|
|
nparent = node->parent;
|
|
|
|
}
|
|
|
|
_set_color(nparent, BLACK);
|
|
|
|
_set_color(ngrand_parent, RED);
|
|
|
|
_rotate_right(ngrand_parent);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ngrand_parent->left->color == RED) {
|
|
|
|
_set_color(nparent, BLACK);
|
|
|
|
_set_color(ngrand_parent->left, BLACK);
|
|
|
|
_set_color(ngrand_parent, RED);
|
|
|
|
node = ngrand_parent;
|
|
|
|
nparent = node->parent;
|
|
|
|
} else {
|
|
|
|
if (node == nparent->left) {
|
|
|
|
_rotate_right(nparent);
|
|
|
|
node = nparent;
|
|
|
|
nparent = node->parent;
|
|
|
|
}
|
|
|
|
_set_color(nparent, BLACK);
|
|
|
|
_set_color(ngrand_parent, RED);
|
|
|
|
_rotate_left(ngrand_parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_set_color(_data._root->left, BLACK);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-20 19:53:18 +02:00
|
|
|
Element *_insert(const K &p_key, const V &p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *new_parent = _data._root;
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *node = _data._root->left;
|
2016-03-09 00:00:52 +01:00
|
|
|
C less;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (node != _data._nil) {
|
|
|
|
new_parent = node;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
if (less(p_key, node->_data.key)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->left;
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
} else if (less(node->_data.key, p_key)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
node = node->right;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
node->_data.value = p_value;
|
2017-09-20 19:53:18 +02:00
|
|
|
return node; // Return existing node with new value
|
2016-03-09 00:00:52 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
typedef KeyValue<K, V> KV;
|
|
|
|
Element *new_node = memnew_allocator(Element(KV(p_key, p_value)), A);
|
2017-03-05 16:44:50 +01:00
|
|
|
new_node->parent = new_parent;
|
|
|
|
new_node->right = _data._nil;
|
|
|
|
new_node->left = _data._nil;
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//new_node->data=_data;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
if (new_parent == _data._root || less(p_key, new_parent->_data.key)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_parent->left = new_node;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_parent->right = new_node;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
new_node->_next = _successor(new_node);
|
|
|
|
new_node->_prev = _predecessor(new_node);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (new_node->_next) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_node->_next->_prev = new_node;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (new_node->_prev) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_node->_prev->_next = new_node;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_data.size_cache++;
|
2017-09-20 19:53:18 +02:00
|
|
|
_insert_rb_fix(new_node);
|
2016-03-09 00:00:52 +01:00
|
|
|
return new_node;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-20 19:53:18 +02:00
|
|
|
void _erase_fix_rb(Element *p_node) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *root = _data._root->left;
|
2017-09-20 19:53:18 +02:00
|
|
|
Element *node = _data._nil;
|
|
|
|
Element *sibling = p_node;
|
|
|
|
Element *parent = sibling->parent;
|
|
|
|
|
|
|
|
while (node != root) { // If red node found, will exit at a break
|
|
|
|
if (sibling->color == RED) {
|
|
|
|
_set_color(sibling, BLACK);
|
|
|
|
_set_color(parent, RED);
|
|
|
|
if (sibling == parent->right) {
|
|
|
|
sibling = sibling->left;
|
|
|
|
_rotate_left(parent);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-09-20 19:53:18 +02:00
|
|
|
sibling = sibling->right;
|
|
|
|
_rotate_right(parent);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
}
|
|
|
|
if ((sibling->left->color == BLACK) && (sibling->right->color == BLACK)) {
|
|
|
|
_set_color(sibling, RED);
|
|
|
|
if (parent->color == RED) {
|
|
|
|
_set_color(parent, BLACK);
|
|
|
|
break;
|
|
|
|
} else { // loop: haven't found any red nodes yet
|
|
|
|
node = parent;
|
|
|
|
parent = node->parent;
|
|
|
|
sibling = (node == parent->left) ? parent->right : parent->left;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
} else {
|
|
|
|
if (sibling == parent->right) {
|
|
|
|
if (sibling->right->color == BLACK) {
|
|
|
|
_set_color(sibling->left, BLACK);
|
|
|
|
_set_color(sibling, RED);
|
|
|
|
_rotate_right(sibling);
|
|
|
|
sibling = sibling->parent;
|
|
|
|
}
|
|
|
|
_set_color(sibling, parent->color);
|
|
|
|
_set_color(parent, BLACK);
|
|
|
|
_set_color(sibling->right, BLACK);
|
|
|
|
_rotate_left(parent);
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-09-20 19:53:18 +02:00
|
|
|
if (sibling->left->color == BLACK) {
|
|
|
|
_set_color(sibling->right, BLACK);
|
|
|
|
_set_color(sibling, RED);
|
|
|
|
_rotate_left(sibling);
|
|
|
|
sibling = sibling->parent;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
|
|
|
_set_color(sibling, parent->color);
|
|
|
|
_set_color(parent, BLACK);
|
|
|
|
_set_color(sibling->left, BLACK);
|
|
|
|
_rotate_right(parent);
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(_data._nil->color != BLACK);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void _erase(Element *p_node) {
|
2017-09-20 19:53:18 +02:00
|
|
|
Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : p_node->_next;
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *node = (rp->left == _data._nil) ? rp->right : rp->left;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-09-20 19:53:18 +02:00
|
|
|
Element *sibling;
|
|
|
|
if (rp == rp->parent->left) {
|
|
|
|
rp->parent->left = node;
|
|
|
|
sibling = rp->parent->right;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-09-20 19:53:18 +02:00
|
|
|
rp->parent->right = node;
|
|
|
|
sibling = rp->parent->left;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->color == RED) {
|
|
|
|
node->parent = rp->parent;
|
|
|
|
_set_color(node, BLACK);
|
|
|
|
} else if (rp->color == BLACK && rp->parent != _data._root) {
|
|
|
|
_erase_fix_rb(sibling);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
if (rp != p_node) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(rp == _data._nil);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
rp->left = p_node->left;
|
|
|
|
rp->right = p_node->right;
|
|
|
|
rp->parent = p_node->parent;
|
|
|
|
rp->color = p_node->color;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_node->left != _data._nil) {
|
2017-09-20 19:53:18 +02:00
|
|
|
p_node->left->parent = rp;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (p_node->right != _data._nil) {
|
2017-09-20 19:53:18 +02:00
|
|
|
p_node->right->parent = rp;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_node == p_node->parent->left) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->parent->left = rp;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->parent->right = rp;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_node->_next) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->_next->_prev = p_node->_prev;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (p_node->_prev) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_node->_prev->_next = p_node->_next;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete_allocator<Element, A>(p_node);
|
2014-02-10 02:10:30 +01:00
|
|
|
_data.size_cache--;
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(_data._nil->color == RED);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void _calculate_depth(Element *p_element, int &max_d, int d) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_element == _data._nil) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_calculate_depth(p_element->left, max_d, d + 1);
|
|
|
|
_calculate_depth(p_element->right, max_d, d + 1);
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (d > max_d) {
|
2017-03-05 16:44:50 +01:00
|
|
|
max_d = d;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void _cleanup_tree(Element *p_element) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_element == _data._nil) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_cleanup_tree(p_element->left);
|
|
|
|
_cleanup_tree(p_element->right);
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete_allocator<Element, A>(p_element);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void _copy_from(const Map &p_map) {
|
2014-02-10 02:10:30 +01:00
|
|
|
clear();
|
|
|
|
// not the fastest way, but safeset to write.
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Element *I = p_map.front(); I; I = I->next()) {
|
|
|
|
insert(I->key(), I->value());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
|
|
|
const Element *find(const K &p_key) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Element *res = _find(p_key);
|
2014-02-10 02:10:30 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *find(const K &p_key) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *res = _find(p_key);
|
2014-02-10 02:10:30 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Element *find_closest(const K &p_key) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Element *res = _find_closest(p_key);
|
2014-02-10 02:10:30 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *find_closest(const K &p_key) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *res = _find_closest(p_key);
|
2014-02-10 02:10:30 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-09-20 19:53:18 +02:00
|
|
|
bool has(const K &p_key) const {
|
2020-04-02 01:20:12 +02:00
|
|
|
return find(p_key) != nullptr;
|
2017-09-20 19:53:18 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *insert(const K &p_key, const V &p_value) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_data._create_root();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
return _insert(p_key, p_value);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void erase(Element *p_element) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root || !p_element) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_erase(p_element);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_data.size_cache == 0 && _data._root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_data._free_root();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool erase(const K &p_key) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *e = find(p_key);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!e) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
_erase(e);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_data.size_cache == 0 && _data._root) {
|
2017-09-20 19:53:18 +02:00
|
|
|
_data._free_root();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const V &operator[](const K &p_key) const {
|
2017-05-26 21:11:16 +02:00
|
|
|
CRASH_COND(!_data._root);
|
2017-03-05 16:44:50 +01:00
|
|
|
const Element *e = find(p_key);
|
2017-05-26 21:11:16 +02:00
|
|
|
CRASH_COND(!e);
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
return e->_data.value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
V &operator[](const K &p_key) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_data._create_root();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *e = find(p_key);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!e) {
|
2017-03-05 16:44:50 +01:00
|
|
|
e = insert(p_key, V());
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 17:31:19 +02:00
|
|
|
return e->_data.value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *front() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *e = _data._root->left;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (e == _data._nil) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
while (e->left != _data._nil) {
|
2017-03-05 16:44:50 +01:00
|
|
|
e = e->left;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return e;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *back() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element *e = _data._root->left;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (e == _data._nil) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
while (e->right != _data._nil) {
|
2017-03-05 16:44:50 +01:00
|
|
|
e = e->right;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return e;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
inline bool is_empty() const { return _data.size_cache == 0; }
|
2014-02-10 02:10:30 +01:00
|
|
|
inline int size() const { return _data.size_cache; }
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int calculate_depth() const {
|
|
|
|
// used for debug mostly
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int max_d = 0;
|
|
|
|
_calculate_depth(_data._root->left, max_d, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
return max_d;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void clear() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data._root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-20 19:53:18 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_cleanup_tree(_data._root->left);
|
2017-03-05 16:44:50 +01:00
|
|
|
_data._root->left = _data._nil;
|
|
|
|
_data.size_cache = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
_data._free_root();
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void operator=(const Map &p_map) {
|
|
|
|
_copy_from(p_map);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map(const Map &p_map) {
|
|
|
|
_copy_from(p_map);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
_FORCE_INLINE_ Map() {}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
~Map() {
|
2016-03-09 00:00:52 +01:00
|
|
|
clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-25 11:10:34 +01:00
|
|
|
#endif // MAP_H
|