Skip to content

Commit cc83fc2

Browse files
committed
sync more templates
1 parent 101ae38 commit cc83fc2

8 files changed

Lines changed: 162 additions & 371 deletions

File tree

‎include/godot_cpp/templates/hashfuncs.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ struct HashHasher {
287287
static _FORCE_INLINE_ uint64_t hash(const uint64_t p_hash) { return p_hash; }
288288
};
289289

290+
template <typename T>
291+
struct HashableHasher {
292+
static _FORCE_INLINE_ uint32_t hash(const T &hashable) { return hashable.hash(); }
293+
};
294+
290295
template <typename T>
291296
struct HashMapComparatorDefault {
292297
static bool compare(const T &p_lhs, const T &p_rhs) {

‎include/godot_cpp/templates/rb_map.hpp‎

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@
3737
#include <initializer_list>
3838

3939
namespace godot {
40-
41-
// based on the very nice implementation of rb-trees by:
42-
// https://web.archive.org/web/20120507164830/https://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
43-
40+
/**
41+
* Key-value container (aka dictionary) using a red-black tree for lookup.
42+
*
43+
* Elements are ordered by key (using the < comparison operator).
44+
* Key-Values are pointer-stable until removed.
45+
*
46+
* Implementation based on:
47+
* https://web.archive.org/web/20120507164830/https://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
48+
*
49+
* Core container guidance:
50+
* https://docs.godotengine.org/en/latest/engine_details/architecture/core_types.html#containers
51+
*/
4452
template <typename K, typename V, typename C = Comparator<K>, typename A = DefaultAllocator>
45-
class RBMap {
53+
class _WARN_UNUSED_ RBMap {
4654
enum Color {
4755
RED,
4856
BLACK
@@ -124,7 +132,7 @@ class RBMap {
124132
E = p_it.E;
125133
return *this;
126134
}
127-
Iterator(Element *p_E) { E = p_E; }
135+
Iterator(Element *p_element) { E = p_element; }
128136
Iterator() {}
129137
Iterator(const Iterator &p_it) { E = p_it.E; }
130138

@@ -156,7 +164,7 @@ class RBMap {
156164
E = p_it.E;
157165
return *this;
158166
}
159-
ConstIterator(const Element *p_E) { E = p_E; }
167+
ConstIterator(const Element *p_element) { E = p_element; }
160168
ConstIterator() {}
161169
ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
162170

@@ -360,6 +368,23 @@ class RBMap {
360368
return prev;
361369
}
362370

371+
Element *_lower_bound(const K &p_key) const {
372+
Element *node = _data._root->left;
373+
Element *result = nullptr;
374+
C less;
375+
376+
while (node != _data._nil) {
377+
if (!less(node->_data.key, p_key)) {
378+
result = node;
379+
node = node->left;
380+
} else {
381+
node = node->right;
382+
}
383+
}
384+
385+
return result;
386+
}
387+
363388
void _insert_rb_fix(Element *p_new_node) {
364389
Element *node = p_new_node;
365390
Element *nparent = node->parent;
@@ -568,16 +593,16 @@ class RBMap {
568593
ERR_FAIL_COND(_data._nil->color == RED);
569594
}
570595

571-
void _calculate_depth(Element *p_element, int &max_d, int d) const {
596+
void _calculate_depth(Element *p_element, int &p_max_d, int p_d) const {
572597
if (p_element == _data._nil) {
573598
return;
574599
}
575600

576-
_calculate_depth(p_element->left, max_d, d + 1);
577-
_calculate_depth(p_element->right, max_d, d + 1);
601+
_calculate_depth(p_element->left, p_max_d, p_d + 1);
602+
_calculate_depth(p_element->right, p_max_d, p_d + 1);
578603

579-
if (d > max_d) {
580-
max_d = d;
604+
if (p_d > p_max_d) {
605+
p_max_d = p_d;
581606
}
582607
}
583608

@@ -636,6 +661,20 @@ class RBMap {
636661
return res;
637662
}
638663

664+
Element *lower_bound(const K &p_key) {
665+
if (!_data._root) {
666+
return nullptr;
667+
}
668+
return _lower_bound(p_key);
669+
}
670+
671+
const Element *lower_bound(const K &p_key) const {
672+
if (!_data._root) {
673+
return nullptr;
674+
}
675+
return _lower_bound(p_key);
676+
}
677+
639678
bool has(const K &p_key) const {
640679
return find(p_key) != nullptr;
641680
}
@@ -759,13 +798,34 @@ class RBMap {
759798
}
760799

761800
void operator=(const RBMap &p_map) {
801+
if (this == &p_map) {
802+
return;
803+
}
804+
762805
_copy_from(p_map);
763806
}
764807

765-
RBMap(const RBMap &p_map) {
808+
void operator=(RBMap &&p_map) {
809+
if (this == &p_map) {
810+
return;
811+
}
812+
813+
SWAP(_data._root, p_map._data._root);
814+
SWAP(_data.size_cache, p_map._data.size_cache);
815+
}
816+
817+
explicit RBMap(const RBMap &p_map) {
766818
_copy_from(p_map);
767819
}
768820

821+
RBMap(RBMap &&p_map) {
822+
_data._root = p_map._data._root;
823+
_data.size_cache = p_map._data.size_cache;
824+
825+
p_map._data._root = nullptr;
826+
p_map._data.size_cache = 0;
827+
}
828+
769829
RBMap(std::initializer_list<KeyValue<K, V>> p_init) {
770830
for (const KeyValue<K, V> &E : p_init) {
771831
insert(E.key, E.value);

‎include/godot_cpp/templates/rb_set.hpp‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,21 @@
3434

3535
#include <initializer_list>
3636

37-
// based on the very nice implementation of rb-trees by:
38-
// https://web.archive.org/web/20120507164830/https://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
39-
4037
namespace godot {
41-
38+
/**
39+
* Set container using a red-black tree for lookup.
40+
*
41+
* Elements are ordered (using the < comparison operator).
42+
* Key-Values are pointer-stable until removed.
43+
*
44+
* Implementation based on:
45+
* https://web.archive.org/web/20120507164830/https://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
46+
*
47+
* Core container guidance:
48+
* https://docs.godotengine.org/en/latest/engine_details/architecture/core_types.html#containers
49+
*/
4250
template <typename T, typename C = Comparator<T>, typename A = DefaultAllocator>
43-
class RBSet {
51+
class _WARN_UNUSED_ RBSet {
4452
enum Color {
4553
RED,
4654
BLACK
@@ -79,7 +87,6 @@ class RBSet {
7987
const T &get() const {
8088
return value;
8189
}
82-
Element() {}
8390
};
8491

8592
typedef T ValueType;
@@ -98,11 +105,11 @@ class RBSet {
98105
return *this;
99106
}
100107

101-
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
102-
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
108+
_FORCE_INLINE_ bool operator==(const Iterator &p_other) const { return E == p_other.E; }
109+
_FORCE_INLINE_ bool operator!=(const Iterator &p_other) const { return E != p_other.E; }
103110

104111
explicit operator bool() const { return E != nullptr; }
105-
Iterator(Element *p_E) { E = p_E; }
112+
Iterator(Element *p_element) { E = p_element; }
106113
Iterator() {}
107114
Iterator(const Iterator &p_it) { E = p_it.E; }
108115

@@ -124,10 +131,10 @@ class RBSet {
124131
return *this;
125132
}
126133

127-
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
128-
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
134+
_FORCE_INLINE_ bool operator==(const ConstIterator &p_other) const { return E == p_other.E; }
135+
_FORCE_INLINE_ bool operator!=(const ConstIterator &p_other) const { return E != p_other.E; }
129136

130-
_FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
137+
_FORCE_INLINE_ ConstIterator(const Element *p_element) { E = p_element; }
131138
_FORCE_INLINE_ ConstIterator() {}
132139
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
133140

@@ -536,16 +543,16 @@ class RBSet {
536543
ERR_FAIL_COND(_data._nil->color == RED);
537544
}
538545

539-
void _calculate_depth(Element *p_element, int &max_d, int d) const {
546+
void _calculate_depth(Element *p_element, int &p_max_d, int p_d) const {
540547
if (p_element == _data._nil) {
541548
return;
542549
}
543550

544-
_calculate_depth(p_element->left, max_d, d + 1);
545-
_calculate_depth(p_element->right, max_d, d + 1);
551+
_calculate_depth(p_element->left, p_max_d, p_d + 1);
552+
_calculate_depth(p_element->right, p_max_d, p_d + 1);
546553

547-
if (d > max_d) {
548-
max_d = d;
554+
if (p_d > p_max_d) {
555+
p_max_d = p_d;
549556
}
550557
}
551558

@@ -696,13 +703,34 @@ class RBSet {
696703
}
697704

698705
void operator=(const RBSet &p_set) {
706+
if (this == &p_set) {
707+
return;
708+
}
709+
699710
_copy_from(p_set);
700711
}
701712

702-
RBSet(const RBSet &p_set) {
713+
void operator=(RBSet &&p_set) {
714+
if (this == &p_set) {
715+
return;
716+
}
717+
718+
SWAP(_data._root, p_set._data._root);
719+
SWAP(_data.size_cache, p_set._data.size_cache);
720+
}
721+
722+
explicit RBSet(const RBSet &p_set) {
703723
_copy_from(p_set);
704724
}
705725

726+
RBSet(RBSet &&p_set) {
727+
_data._root = p_set._data._root;
728+
_data.size_cache = p_set._data.size_cache;
729+
730+
p_set._data._root = nullptr;
731+
p_set._data.size_cache = 0;
732+
}
733+
706734
RBSet(std::initializer_list<T> p_init) {
707735
for (const T &E : p_init) {
708736
insert(E);

‎include/godot_cpp/templates/search_array.hpp‎

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)