Skip to content

Commit

Permalink
Merge pull request #1457 from AThousandShips/foreach_list
Browse files Browse the repository at this point in the history
[Core] Reduce and prevent unnecessary random-access to `List`
  • Loading branch information
dsnopek authored May 16, 2024
2 parents 16cad7b + d0bdd60 commit 6b39ed0
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions include/godot_cpp/templates/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,54 +139,58 @@ class List {

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() {
Expand Down Expand Up @@ -519,7 +523,14 @@ class List {
}
}

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();
Expand All @@ -532,7 +543,14 @@ class List {
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();
Expand Down

0 comments on commit 6b39ed0

Please sign in to comment.