From a8b8379efbcf152fa5c87a15aca0ad455db292e3 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Tue, 16 Feb 2016 15:23:17 +0300 Subject: [PATCH] [new downloader] Keeping parent in SimpleTree and removing Sort form Simple Tree. --- storage/simple_tree.hpp | 42 ++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/storage/simple_tree.hpp b/storage/simple_tree.hpp index 23b810ef0e..537c572de0 100644 --- a/storage/simple_tree.hpp +++ b/storage/simple_tree.hpp @@ -9,10 +9,12 @@ template class SimpleTree { T m_value; - // @TODO(bykoianko) Remove on unnecessary methods of SimpleTree if any. /// \brief m_children contains all first generation descendants of the node. + /// Note. Once created the order of elements of |m_children| should not be changed. + /// See implementation of AddAtDepth and Add methods for details. vector> m_children; + SimpleTree * m_parent; static bool IsEqual(T const & v1, T const & v2) { @@ -20,7 +22,7 @@ class SimpleTree } public: - SimpleTree(T const & value = T()) : m_value(value) + SimpleTree(T const & value = T(), SimpleTree * parent = nullptr) : m_value(value), m_parent(parent) { } @@ -48,7 +50,7 @@ public: /// @return reference is valid only up to the next tree structure modification T & Add(T const & value) { - m_children.emplace_back(SimpleTree(value)); + m_children.emplace_back(SimpleTree(value, this)); return m_children.back().Value(); } @@ -63,14 +65,6 @@ public: return Value() < other.Value(); } - /// sorts children independently on each level by default - void Sort() - { - sort(m_children.begin(), m_children.end()); - for (auto & child : m_children) - child.Sort(); - } - /// \brief Checks all nodes in tree to find an equal one. If there're several equal nodes /// returns the first found. /// \returns a poiter item in the tree if found and nullptr otherwise. @@ -118,6 +112,14 @@ public: return nullptr; } + bool HasParent() const { return m_parent != nullptr; } + + SimpleTree const & Parent() const + { + CHECK(HasParent(), ()); + return *m_parent; + } + SimpleTree const & Child(size_t index) const { ASSERT_LESS(index, m_children.size(), ()); @@ -180,4 +182,22 @@ public: for (auto const & child: m_children) child.ForEachInSubtree(f); } + + template + void ForEachParent(TFunctor && f) + { + if (m_parent == nullptr) + return; + f(*m_parent); + m_parent->ForEachParent(f); + } + + template + void ForEachParent(TFunctor && f) const + { + if (m_parent == nullptr) + return; + f(*m_parent); + m_parent->ForEachParent(f); + } };