diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 14:14:09 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 14:14:09 +0100 |
commit | f8d8d7499ba4433678db2a68fb1cae74448ca31e (patch) | |
tree | fc96bef89fc3206277d1fcf1e3df5ad733b7e652 /backend | |
parent | a0e14fa424494ed35da1bd6e5e54bb36178259a7 (diff) |
make ListIterator continue working on a changing list
Diffstat (limited to 'backend')
-rw-r--r-- | backend/List.h | 10 | ||||
-rw-r--r-- | backend/List.hpp | 8 | ||||
-rw-r--r-- | backend/ListIterator.h | 16 | ||||
-rw-r--r-- | backend/ListIterator.hpp | 26 | ||||
-rw-r--r-- | backend/Location.cpp | 6 | ||||
-rw-r--r-- | backend/Location.h | 6 | ||||
-rw-r--r-- | backend/PtrList.hpp | 2 |
7 files changed, 41 insertions, 33 deletions
diff --git a/backend/List.h b/backend/List.h index 1af0b35..3bdbb23 100644 --- a/backend/List.h +++ b/backend/List.h @@ -10,6 +10,7 @@ class ListIterator; template <typename T> struct ListLink { + friend class ListIterator<T>; ListLink<T> * prev; ListLink<T> * next; T value; @@ -24,7 +25,7 @@ public: public: size_t size() const; - void push_back(T & el); + void push_back(const T & el); void remove(const T & val); void pop_back(); @@ -33,11 +34,12 @@ public: T & operator [] (size_t index) const; - ListIterator<T> begin() const; - ListIterator<T> end() const; - ListRange<T> range() const; + ListIterator<T> begin(); + ListIterator<T> end(); + ListRange<T> range(); private: + friend class ListRange<T>; ListLink<T> * head = nullptr; ListLink<T> * tail = nullptr; size_t length = 0; diff --git a/backend/List.hpp b/backend/List.hpp index 25e7a56..634f14b 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -8,7 +8,7 @@ size_t List<T>::size() const { } template <typename T> -void List<T>::push_back(T & el) { +void List<T>::push_back(const T & el) { ListLink<T> * link = new ListLink<T> { .prev = this->head, .next = nullptr, @@ -78,17 +78,17 @@ T & List<T>::operator [] (size_t index) const { } template <typename T> -ListRange<T> List<T>::range() const { +ListRange<T> List<T>::range() { return { *this }; } template <typename T> -ListIterator<T> List<T>::begin() const { +ListIterator<T> List<T>::begin() { return this->range().begin(); } template <typename T> -ListIterator<T> List<T>::end() const { +ListIterator<T> List<T>::end() { return this->range().end(); } diff --git a/backend/ListIterator.h b/backend/ListIterator.h index c7fbf4a..3d11806 100644 --- a/backend/ListIterator.h +++ b/backend/ListIterator.h @@ -10,30 +10,30 @@ class List; template <typename T> class ListIterator { public: - ListIterator(const List<T> & list, size_t index); + ListIterator(ListLink<T> * & here); public: - T operator * () const; + T & operator * () const; ListIterator<T> & operator ++ (); bool operator != (const ListIterator<T> &) const; private: - const List<T> & list; - size_t index; + ListLink<T> * here = nullptr; + ListLink<T> * next = nullptr; }; template <typename T> class ListRange { public: - ListRange(const List<T> & list); + ListRange(List<T> & list); public: - ListIterator<T> begin() const; - ListIterator<T> end() const; + ListIterator<T> begin(); + ListIterator<T> end(); size_t size() const; private: - const List<T> & list; + List<T> & list; }; #include "ListIterator.hpp" diff --git a/backend/ListIterator.hpp b/backend/ListIterator.hpp index b6b7a36..30a556f 100644 --- a/backend/ListIterator.hpp +++ b/backend/ListIterator.hpp @@ -3,16 +3,16 @@ #include "ListIterator.h" template <typename T> -ListRange<T>::ListRange(const List<T> & list) : list(list) { } +ListRange<T>::ListRange(List<T> & list) : list(list) { } template <typename T> -ListIterator<T> ListRange<T>::begin() const { - return { this->list, 0 }; +ListIterator<T> ListRange<T>::begin() { + return { this->list.tail }; } template <typename T> -ListIterator<T> ListRange<T>::end() const { - return { this->list, this->list.size() }; +ListIterator<T> ListRange<T>::end() { + return { this->list.head }; } template <typename T> @@ -21,21 +21,27 @@ size_t ListRange<T>::size() const { } template <typename T> -ListIterator<T>::ListIterator(const List<T> & list, size_t index) : list(list), index(index) { } +ListIterator<T>::ListIterator(ListLink<T> * & here) { + this->here = here; + if (this->here != nullptr) + this->next = this->here->next; +} template <typename T> -T ListIterator<T>::operator * () const { - return this->list[this->index]; +T & ListIterator<T>::operator * () const { + return this->here->value; } template <typename T> ListIterator<T> & ListIterator<T>::operator ++ () { - this->index++; + this->here = this->next; + if (this->here != nullptr) + this->next = this->here->next; return *this; } template <typename T> bool ListIterator<T>::operator != (const ListIterator<T> & rhs) const { - return this->index < rhs.index; + return this->here != nullptr; } diff --git a/backend/Location.cpp b/backend/Location.cpp index bfd2107..c036c7b 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -39,7 +39,7 @@ void Location::add_visible_object(Object * object) { void Location::remove_visible_object(Object * object) { this->visible_objects.remove(object); } -ListRange<Object *> Location::get_visible_objects() const { +ListRange<Object *> Location::get_visible_objects() { return this->visible_objects.range(); } @@ -49,7 +49,7 @@ void Location::add_hidden_object(Object * object) { void Location::remove_hidden_object(Object * object) { this->hidden_objects.remove(object); } -ListRange<Object *> Location::get_hidden_objects() const { +ListRange<Object *> Location::get_hidden_objects() { return this->hidden_objects.range(); } @@ -68,7 +68,7 @@ void Location::add_enemy(Enemy * enemy) { void Location::remove_enemy(Enemy * enemy) { this->enemies.remove(enemy); } -ListRange<Enemy *> Location::get_enemies() const { +ListRange<Enemy *> Location::get_enemies() { return this->enemies.range(); } diff --git a/backend/Location.h b/backend/Location.h index 3a1f5ac..d29c117 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -32,18 +32,18 @@ public: void add_visible_object(Object *); void remove_visible_object(Object *); - ListRange<Object *> get_visible_objects() const; + ListRange<Object *> get_visible_objects(); void add_hidden_object(Object *); void remove_hidden_object(Object *); - ListRange<Object *> get_hidden_objects() const; + ListRange<Object *> get_hidden_objects(); void hide_object(Object *); void unhide_object(Object *); void add_enemy(Enemy *); void remove_enemy(Enemy *); - ListRange<Enemy *> get_enemies() const; + ListRange<Enemy *> get_enemies(); private: friend class LocationFactory; diff --git a/backend/PtrList.hpp b/backend/PtrList.hpp index 96f2643..cbfd6e0 100644 --- a/backend/PtrList.hpp +++ b/backend/PtrList.hpp @@ -4,7 +4,7 @@ template <typename T> PtrList<T>::~PtrList() { - for (T * obj : *this) + for (T * & obj : *this) delete obj; } |