From 9c9de680c6d53755d3bf99a0c3af2e2d440f86f3 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 20 Jan 2020 17:58:09 -0500 Subject: faster parent() and indexFromItem() with index guess implemented files --- src/filetreeitem.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/filetreeitem.h') diff --git a/src/filetreeitem.h b/src/filetreeitem.h index 2819afbd..e060f63a 100644 --- a/src/filetreeitem.h +++ b/src/filetreeitem.h @@ -29,6 +29,7 @@ public: void add(std::unique_ptr child) { + child->m_indexGuess = m_children.size(); m_children.push_back(std::move(child)); } @@ -37,6 +38,11 @@ public: template void insert(Itor begin, Itor end, std::size_t at) { + std::size_t nextRowGuess = m_children.size(); + for (auto itor=begin; itor!=end; ++itor) { + (*itor)->m_indexGuess = nextRowGuess++; + } + m_children.insert(m_children.begin() + at, begin, end); } @@ -54,6 +60,23 @@ public: return m_children; } + int childIndex(const FileTreeItem& item) const + { + if (item.m_indexGuess < m_children.size()) { + if (m_children[item.m_indexGuess].get() == &item) { + return static_cast(item.m_indexGuess); + } + } + + for (std::size_t i=0; i(i); + } + } + + return -1; + } FileTreeItem* parent() { @@ -171,7 +194,11 @@ public: QString debugName() const; private: + static constexpr std::size_t NoIndexGuess = + std::numeric_limits::max(); + FileTreeItem* m_parent; + mutable std::size_t m_indexGuess; const int m_originID; const QString m_virtualParentPath; -- cgit v1.3.1