diff --git a/library/include/BitArray.h b/library/include/BitArray.h index 42f6fd431..c703a2554 100644 --- a/library/include/BitArray.h +++ b/library/include/BitArray.h @@ -272,7 +272,7 @@ namespace DFHack { if (!prev->next) { - prev->next = df::allocate(); + prev->next = new L(); if (prev != root) prev->next->prev = prev; } @@ -288,6 +288,17 @@ namespace DFHack bool next; friend struct DfLinkedList; friend class const_iterator; + void ensure_prev() + { + CHECK_NULL_POINTER(root); + if (!prev && !next) + { + for (prev = root; prev->next && prev->next->next; prev = prev->next) + { + next = true; + } + } + } iterator() : root(nullptr), prev(nullptr), next(false) {} public: using difference_type = void; @@ -321,6 +332,8 @@ namespace DFHack { CHECK_NULL_POINTER(root); + ensure_prev(); + if (next) { next = false; @@ -412,6 +425,18 @@ namespace DFHack if (root != other.root) return false; + if (!next && !prev && !other.next && !other.prev) + return true; + + if ((!next && !prev) || (!other.next && !other.prev)) + { + iterator this_copy = *this; + this_copy.ensure_prev(); + iterator other_copy = other; + other_copy.ensure_prev(); + return this_copy == other_copy; + } + if (other.next && !next) return prev && other.prev && prev->next == other.prev; if (next && !other.next) @@ -505,7 +530,7 @@ namespace DFHack } const_iterator begin() const { - return const_iterator(const_cast *>(this)->begin()); + return const_iterator(static_cast(this), static_cast(this)); } const_iterator cbegin() const { @@ -513,16 +538,11 @@ namespace DFHack } iterator end() { - L *it = static_cast(this); - while (it->next && it->next->next) - { - it = it->next; - } - return iterator(static_cast(this), it, true); + return iterator(static_cast(this), nullptr, false); } const_iterator end() const { - return const_iterator(const_cast *>(this)->end()); + return const_iterator(static_cast(this), nullptr, false); } const_iterator cend() const {