diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2020-01-20 17:58:09 -0500 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2020-02-04 03:33:20 -0500 |
| commit | 9c9de680c6d53755d3bf99a0c3af2e2d440f86f3 (patch) | |
| tree | c5bf8bbae10cb1daa1af39333f0cf89c027a95d0 /src/filetreeitem.h | |
| parent | 8e3a360cd427f0f653846a0afc627a39cbf16276 (diff) | |
faster parent() and indexFromItem() with index guess
implemented files
Diffstat (limited to 'src/filetreeitem.h')
| -rw-r--r-- | src/filetreeitem.h | 27 |
1 files changed, 27 insertions, 0 deletions
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<FileTreeItem> child) { + child->m_indexGuess = m_children.size(); m_children.push_back(std::move(child)); } @@ -37,6 +38,11 @@ public: template <class Itor> 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<int>(item.m_indexGuess); + } + } + + for (std::size_t i=0; i<m_children.size(); ++i) { + if (m_children[i].get() == &item) { + item.m_indexGuess = i; + return static_cast<int>(i); + } + } + + return -1; + } FileTreeItem* parent() { @@ -171,7 +194,11 @@ public: QString debugName() const; private: + static constexpr std::size_t NoIndexGuess = + std::numeric_limits<std::size_t>::max(); + FileTreeItem* m_parent; + mutable std::size_t m_indexGuess; const int m_originID; const QString m_virtualParentPath; |
