Merge pull request #1457 from AThousandShips/foreach_list

[Core] Reduce and prevent unnecessary random-access to `List`
pull/1439/head
David Snopek 2024-05-16 08:46:39 -05:00 committed by GitHub
commit 6b39ed0732
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 45 additions and 27 deletions

View File

@ -139,31 +139,6 @@ public:
typedef T ValueType; typedef T ValueType;
struct Iterator {
_FORCE_INLINE_ T &operator*() const {
return E->get();
}
_FORCE_INLINE_ T *operator->() const { return &E->get(); }
_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 { struct ConstIterator {
_FORCE_INLINE_ const T &operator*() const { _FORCE_INLINE_ const T &operator*() const {
return E->get(); return E->get();
@ -189,6 +164,35 @@ public:
const Element *E = nullptr; const Element *E = nullptr;
}; };
struct Iterator {
_FORCE_INLINE_ T &operator*() const {
return E->get();
}
_FORCE_INLINE_ T *operator->() const { return &E->get(); }
_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; }
operator ConstIterator() const {
return ConstIterator(E);
}
private:
Element *E = nullptr;
};
_FORCE_INLINE_ Iterator begin() { _FORCE_INLINE_ Iterator begin() {
return Iterator(front()); return Iterator(front());
} }
@ -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()); CRASH_BAD_INDEX(p_index, size());
Element *I = front(); Element *I = front();
@ -532,7 +543,14 @@ public:
return I->get(); 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()); CRASH_BAD_INDEX(p_index, size());
const Element *I = front(); const Element *I = front();