Skip to content

Commit 6b39ed0

Browse files
authored
Merge pull request #1457 from AThousandShips/foreach_list
[Core] Reduce and prevent unnecessary random-access to `List`
2 parents 16cad7b + d0bdd60 commit 6b39ed0

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

include/godot_cpp/templates/list.hpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,54 +139,58 @@ class List {
139139

140140
typedef T ValueType;
141141

142-
struct Iterator {
143-
_FORCE_INLINE_ T &operator*() const {
142+
struct ConstIterator {
143+
_FORCE_INLINE_ const T &operator*() const {
144144
return E->get();
145145
}
146-
_FORCE_INLINE_ T *operator->() const { return &E->get(); }
147-
_FORCE_INLINE_ Iterator &operator++() {
146+
_FORCE_INLINE_ const T *operator->() const { return &E->get(); }
147+
_FORCE_INLINE_ ConstIterator &operator++() {
148148
E = E->next();
149149
return *this;
150150
}
151-
_FORCE_INLINE_ Iterator &operator--() {
151+
_FORCE_INLINE_ ConstIterator &operator--() {
152152
E = E->prev();
153153
return *this;
154154
}
155155

156-
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
157-
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
156+
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
157+
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
158158

159-
Iterator(Element *p_E) { E = p_E; }
160-
Iterator() {}
161-
Iterator(const Iterator &p_it) { E = p_it.E; }
159+
_FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
160+
_FORCE_INLINE_ ConstIterator() {}
161+
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
162162

163163
private:
164-
Element *E = nullptr;
164+
const Element *E = nullptr;
165165
};
166166

167-
struct ConstIterator {
168-
_FORCE_INLINE_ const T &operator*() const {
167+
struct Iterator {
168+
_FORCE_INLINE_ T &operator*() const {
169169
return E->get();
170170
}
171-
_FORCE_INLINE_ const T *operator->() const { return &E->get(); }
172-
_FORCE_INLINE_ ConstIterator &operator++() {
171+
_FORCE_INLINE_ T *operator->() const { return &E->get(); }
172+
_FORCE_INLINE_ Iterator &operator++() {
173173
E = E->next();
174174
return *this;
175175
}
176-
_FORCE_INLINE_ ConstIterator &operator--() {
176+
_FORCE_INLINE_ Iterator &operator--() {
177177
E = E->prev();
178178
return *this;
179179
}
180180

181-
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
182-
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
181+
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
182+
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
183183

184-
_FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
185-
_FORCE_INLINE_ ConstIterator() {}
186-
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
184+
Iterator(Element *p_E) { E = p_E; }
185+
Iterator() {}
186+
Iterator(const Iterator &p_it) { E = p_it.E; }
187+
188+
operator ConstIterator() const {
189+
return ConstIterator(E);
190+
}
187191

188192
private:
189-
const Element *E = nullptr;
193+
Element *E = nullptr;
190194
};
191195

192196
_FORCE_INLINE_ Iterator begin() {
@@ -519,7 +523,14 @@ class List {
519523
}
520524
}
521525

522-
T &operator[](int p_index) {
526+
// Index operator, kept for compatibility.
527+
_FORCE_INLINE_ T &operator[](int p_index) {
528+
return get(p_index);
529+
}
530+
531+
// Random access to elements, use with care,
532+
// do not use for iteration.
533+
T &get(int p_index) {
523534
CRASH_BAD_INDEX(p_index, size());
524535

525536
Element *I = front();
@@ -532,7 +543,14 @@ class List {
532543
return I->get();
533544
}
534545

535-
const T &operator[](int p_index) const {
546+
// Index operator, kept for compatibility.
547+
_FORCE_INLINE_ const T &operator[](int p_index) const {
548+
return get(p_index);
549+
}
550+
551+
// Random access to elements, use with care,
552+
// do not use for iteration.
553+
const T &get(int p_index) const {
536554
CRASH_BAD_INDEX(p_index, size());
537555

538556
const Element *I = front();

0 commit comments

Comments
 (0)