From 3634925c8c003631b0438b7a13fdc437a7c7fdbb Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Wed, 2 Mar 2016 11:06:24 +0300 Subject: [PATCH] [new downloader] Using unique_ptr and raw pointer instead of shared_ptr. Forwarding some functions args. --- storage/country_tree.hpp | 16 ++++++++-------- storage/country_tree_facade.hpp | 29 ++++++++++++++++++----------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/storage/country_tree.hpp b/storage/country_tree.hpp index 5e7f62f9f3..69a2fae5a9 100644 --- a/storage/country_tree.hpp +++ b/storage/country_tree.hpp @@ -3,12 +3,12 @@ #include "base/assert.hpp" #include "std/algorithm.hpp" -#include "std/shared_ptr.hpp" +#include "std/unique_ptr.hpp" #include "std/vector.hpp" -/// This class is developed for using in Storage. It's a implementation of a tree. +/// This class is developed for using in CountryTreeFacade. It's a implementation of a tree. /// It should be filled with AddAtDepth method. -/// This class is used in Storage and filled based on countries.txt (countries_migrate.txt). +/// This class is used in filled based on countries.txt (countries_migrate.txt). /// While filling CountryTree nodes in countries.txt should be visited in DFS order. template class CountryTree @@ -18,14 +18,14 @@ class CountryTree /// \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; + vector>> m_children; CountryTree * m_parent; /// @return reference is valid only up to the next tree structure modification - shared_ptr> Add(T const & value) + CountryTree * Add(T const & value) { - m_children.emplace_back(make_shared>(value, this)); - return m_children.back(); + m_children.emplace_back(make_unique>(value, this)); + return m_children.back().get(); } public: @@ -41,7 +41,7 @@ public: T & Value() { return m_value; } /// @return reference is valid only up to the next tree structure modification - shared_ptr> AddAtDepth(int level, T const & value) + CountryTree * AddAtDepth(int level, T const & value) { CountryTree * node = this; while (level-- > 0 && !node->m_children.empty()) diff --git a/storage/country_tree_facade.hpp b/storage/country_tree_facade.hpp index a89b8301cb..22d3df4138 100644 --- a/storage/country_tree_facade.hpp +++ b/storage/country_tree_facade.hpp @@ -21,14 +21,15 @@ private: hash m_hash; }; -/// This class is developed for using in Storage. It's a implementation of a tree. +/// This class is developed for using in Storage. It's a implementation of a tree with ability +/// of access to its nodes in constant time with the help of hash table. /// It should be filled with AddAtDepth method. /// This class is used in Storage and filled based on countries.txt (countries_migrate.txt). /// While filling CountryTree nodes in countries.txt should be visited in DFS order. template class CountryTreeFacade { - using TCountryTreeHashTable = unordered_multimap>, CountryTreeKeyHasher>; + using TCountryTreeHashTable = unordered_multimap *, CountryTreeKeyHasher>; CountryTree m_countryTree; TCountryTreeHashTable m_countryTreeHashTable; @@ -46,7 +47,7 @@ public: /// @return reference is valid only up to the next tree structure modification T & AddAtDepth(int level, T const & value) { - shared_ptr> const added = m_countryTree.AddAtDepth(level, value); + CountryTree * const added = m_countryTree.AddAtDepth(level, value); m_countryTreeHashTable.insert(make_pair(value, added)); return added->Value(); } @@ -112,29 +113,35 @@ public: /// \brief Calls functor f for each first generation descendant of the node. template - void ForEachChild(TFunctor && f) { return m_countryTree.ForEachChild(f); } + void ForEachChild(TFunctor && f) { return m_countryTree.ForEachChild(forward(f)); } template - void ForEachChild(TFunctor && f) const { return m_countryTree.ForEachChild(f); } + void ForEachChild(TFunctor && f) const { return m_countryTree.ForEachChild(forward(f)); } /// \brief Calls functor f for all nodes (add descendant) in the tree. template - void ForEachDescendant(TFunctor && f) { return m_countryTree.ForEachDescendant(f); } + void ForEachDescendant(TFunctor && f) { return m_countryTree.ForEachDescendant(forward(f)); } template - void ForEachDescendant(TFunctor && f) const { return m_countryTree.ForEachDescendant(f); } + void ForEachDescendant(TFunctor && f) const { return m_countryTree.ForEachDescendant(forward(f)); } template - void ForEachInSubtree(TFunctor && f) { return m_countryTree.ForEachInSubtree(f); } + void ForEachInSubtree(TFunctor && f) { return m_countryTree.ForEachInSubtree(forward(f)); } template - void ForEachInSubtree(TFunctor && f) const { return m_countryTree.ForEachInSubtree(f); } + void ForEachInSubtree(TFunctor && f) const { return m_countryTree.ForEachInSubtree(forward(f)); } template - void ForEachAncestorExceptForTheRoot(TFunctor && f) { return m_countryTree.ForEachAncestorExceptForTheRoot(f); } + void ForEachAncestorExceptForTheRoot(TFunctor && f) + { + return m_countryTree.ForEachAncestorExceptForTheRoot(forward(f)); + } template - void ForEachAncestorExceptForTheRoot(TFunctor && f) const { return m_countryTree.ForEachAncestorExceptForTheRoot(f); } + void ForEachAncestorExceptForTheRoot(TFunctor && f) const + { + return m_countryTree.ForEachAncestorExceptForTheRoot(forward(f)); + } private: static bool IsEqual(T const & v1, T const & v2) { return !(v1 < v2) && !(v2 < v1); }