summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Snopek <dsnopek@gmail.com>2024-05-16 08:46:39 -0500
committerGitHub <noreply@github.com>2024-05-16 08:46:39 -0500
commit6b39ed0732116766c667cf509b99a769e2c41776 (patch)
tree2775fd81ad7d9592a8c4a877f171b71807c7ad91
parent16cad7ba24d7688c837e3b64381309b5ace6074f (diff)
parentd0bdd6096c0440542245fb37690e7ab8f3e34b29 (diff)
downloadredot-cpp-6b39ed0732116766c667cf509b99a769e2c41776.tar.gz
Merge pull request #1457 from AThousandShips/foreach_list
[Core] Reduce and prevent unnecessary random-access to `List`
-rw-r--r--include/godot_cpp/templates/list.hpp66
1 files changed, 42 insertions, 24 deletions
diff --git a/include/godot_cpp/templates/list.hpp b/include/godot_cpp/templates/list.hpp
index 376e3ea..fa59605 100644
--- a/include/godot_cpp/templates/list.hpp
+++ b/include/godot_cpp/templates/list.hpp
@@ -139,54 +139,58 @@ public:
typedef T ValueType;
- struct Iterator {
- _FORCE_INLINE_ T &operator*() const {
+ struct ConstIterator {
+ _FORCE_INLINE_ const T &operator*() const {
return E->get();
}
- _FORCE_INLINE_ T *operator->() const { return &E->get(); }
- _FORCE_INLINE_ Iterator &operator++() {
+ _FORCE_INLINE_ const T *operator->() const { return &E->get(); }
+ _FORCE_INLINE_ ConstIterator &operator++() {
E = E->next();
return *this;
}
- _FORCE_INLINE_ Iterator &operator--() {
+ _FORCE_INLINE_ ConstIterator &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; }
+ _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
+ _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
- Iterator(Element *p_E) { E = p_E; }
- Iterator() {}
- Iterator(const Iterator &p_it) { E = p_it.E; }
+ _FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
+ _FORCE_INLINE_ ConstIterator() {}
+ _FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
private:
- Element *E = nullptr;
+ const Element *E = nullptr;
};
- struct ConstIterator {
- _FORCE_INLINE_ const T &operator*() const {
+ struct Iterator {
+ _FORCE_INLINE_ T &operator*() const {
return E->get();
}
- _FORCE_INLINE_ const T *operator->() const { return &E->get(); }
- _FORCE_INLINE_ ConstIterator &operator++() {
+ _FORCE_INLINE_ T *operator->() const { return &E->get(); }
+ _FORCE_INLINE_ Iterator &operator++() {
E = E->next();
return *this;
}
- _FORCE_INLINE_ ConstIterator &operator--() {
+ _FORCE_INLINE_ Iterator &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; }
+ _FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
+ _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
- _FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
- _FORCE_INLINE_ ConstIterator() {}
- _FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
+ Iterator(Element *p_E) { E = p_E; }
+ Iterator() {}
+ Iterator(const Iterator &p_it) { E = p_it.E; }
+
+ operator ConstIterator() const {
+ return ConstIterator(E);
+ }
private:
- const Element *E = nullptr;
+ Element *E = nullptr;
};
_FORCE_INLINE_ Iterator begin() {
@@ -519,7 +523,14 @@ public:
}
}
- T &operator[](int p_index) {
+ // Index operator, kept for compatibility.
+ _FORCE_INLINE_ T &operator[](int p_index) {
+ return get(p_index);
+ }
+
+ // Random access to elements, use with care,
+ // do not use for iteration.
+ T &get(int p_index) {
CRASH_BAD_INDEX(p_index, size());
Element *I = front();
@@ -532,7 +543,14 @@ public:
return I->get();
}
- const T &operator[](int p_index) const {
+ // Index operator, kept for compatibility.
+ _FORCE_INLINE_ const T &operator[](int p_index) const {
+ return get(p_index);
+ }
+
+ // Random access to elements, use with care,
+ // do not use for iteration.
+ const T &get(int p_index) const {
CRASH_BAD_INDEX(p_index, size());
const Element *I = front();