Run DfLinkedList::end's computations only when needed.

develop
Ben Lubar 2018-06-18 12:57:36 -05:00
parent 31d22967f8
commit 0b9d46712f
No known key found for this signature in database
GPG Key ID: 018BAB45DB2D2B24
1 changed files with 29 additions and 9 deletions

@ -272,7 +272,7 @@ namespace DFHack
{ {
if (!prev->next) if (!prev->next)
{ {
prev->next = df::allocate<L>(); prev->next = new L();
if (prev != root) if (prev != root)
prev->next->prev = prev; prev->next->prev = prev;
} }
@ -288,6 +288,17 @@ namespace DFHack
bool next; bool next;
friend struct DfLinkedList<L, I>; friend struct DfLinkedList<L, I>;
friend class const_iterator; 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) {} iterator() : root(nullptr), prev(nullptr), next(false) {}
public: public:
using difference_type = void; using difference_type = void;
@ -321,6 +332,8 @@ namespace DFHack
{ {
CHECK_NULL_POINTER(root); CHECK_NULL_POINTER(root);
ensure_prev();
if (next) if (next)
{ {
next = false; next = false;
@ -412,6 +425,18 @@ namespace DFHack
if (root != other.root) if (root != other.root)
return false; 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) if (other.next && !next)
return prev && other.prev && prev->next == other.prev; return prev && other.prev && prev->next == other.prev;
if (next && !other.next) if (next && !other.next)
@ -505,7 +530,7 @@ namespace DFHack
} }
const_iterator begin() const const_iterator begin() const
{ {
return const_iterator(const_cast<DfLinkedList<L, I> *>(this)->begin()); return const_iterator(static_cast<L *>(this), static_cast<L *>(this));
} }
const_iterator cbegin() const const_iterator cbegin() const
{ {
@ -513,16 +538,11 @@ namespace DFHack
} }
iterator end() iterator end()
{ {
L *it = static_cast<L *>(this); return iterator(static_cast<L *>(this), nullptr, false);
while (it->next && it->next->next)
{
it = it->next;
}
return iterator(static_cast<L *>(this), it, true);
} }
const_iterator end() const const_iterator end() const
{ {
return const_iterator(const_cast<DfLinkedList<L, I> *>(this)->end()); return const_iterator(static_cast<L *>(this), nullptr, false);
} }
const_iterator cend() const const_iterator cend() const
{ {