diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2024-05-07 09:04:44 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2024-05-07 09:04:44 +0200 |
commit | e63252b4219680d109bfa41c24f483e97b37f40e (patch) | |
tree | 8b8af6d7efcab9b2692d45dec65449fd83d2c8b9 /core/templates | |
parent | 570220ba9b127325f1a5aa7bb17d5c6f76ccf62c (diff) | |
parent | 955d5affa857ec1f358c56da8fb1ff4ab6590704 (diff) | |
download | redot-engine-e63252b4219680d109bfa41c24f483e97b37f40e.tar.gz |
Merge pull request #90705 from AThousandShips/foreach_list
Reduce and prevent unnecessary random-access to `List`
Diffstat (limited to 'core/templates')
-rw-r--r-- | core/templates/list.h | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/core/templates/list.h b/core/templates/list.h index b4d4beb930..6663f06c30 100644 --- a/core/templates/list.h +++ b/core/templates/list.h @@ -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,9 @@ public: } } - T &operator[](int 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 +538,9 @@ public: return I->get(); } - const T &operator[](int p_index) const { + // 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(); |