From b788c2b2098093843d4521d12a617199af39e7dc Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Sat, 1 Apr 2017 03:30:52 -0700 Subject: [vcpkg] ImmutableSortedVector is actually Mutable via move. Use fmap instead of construct/insert. Don't cache VS2015 instances since it is called once. Add ParagraphDataMap alias. --- toolsrc/include/ImmutableSortedVector.h | 73 ------------------------------- toolsrc/include/Paragraphs.h | 10 +++-- toolsrc/include/SortedVector.h | 55 +++++++++++++++++++++++ toolsrc/include/vcpkglib.h | 4 +- toolsrc/src/commands_install.cpp | 18 ++++---- toolsrc/src/commands_portsdiff.cpp | 2 +- toolsrc/src/commands_update.cpp | 2 +- toolsrc/src/vcpkg_paths.cpp | 29 +++++------- toolsrc/src/vcpkglib.cpp | 2 +- toolsrc/vcpkglib/vcpkglib.vcxproj | 2 +- toolsrc/vcpkglib/vcpkglib.vcxproj.filters | 6 +-- 11 files changed, 90 insertions(+), 113 deletions(-) delete mode 100644 toolsrc/include/ImmutableSortedVector.h create mode 100644 toolsrc/include/SortedVector.h diff --git a/toolsrc/include/ImmutableSortedVector.h b/toolsrc/include/ImmutableSortedVector.h deleted file mode 100644 index 0756068eb..000000000 --- a/toolsrc/include/ImmutableSortedVector.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include -#include - -// Add more forwarding functions to the delegate std::vector as needed. -namespace vcpkg -{ - template - class ImmutableSortedVector - { - typedef typename std::vector::size_type size_type; - - public: - static ImmutableSortedVector create(std::vector vector) - { - ImmutableSortedVector out; - out.delegate = std::move(vector); - if (!std::is_sorted(out.delegate.cbegin(), out.delegate.cend())) - { - std::sort(out.delegate.begin(), out.delegate.end()); - } - - return out; - } - - template - static ImmutableSortedVector create(std::vector vector, Compare comp) - { - ImmutableSortedVector out; - out.delegate = std::move(vector); - if (!std::is_sorted(out.delegate.cbegin(), out.delegate.cend(), comp)) - { - std::sort(out.delegate.begin(), out.delegate.end(), comp); - } - - return out; - } - - typename std::vector::const_iterator begin() const - { - return this->delegate.cbegin(); - } - - typename std::vector::const_iterator end() const - { - return this->delegate.cend(); - } - - typename std::vector::const_iterator cbegin() const - { - return this->delegate.cbegin(); - } - - typename std::vector::const_iterator cend() const - { - return this->delegate.cend(); - } - - bool empty() const - { - return this->delegate.empty(); - } - - size_type size() const - { - return this->delegate.size(); - } - - private: - std::vector delegate; - }; -} diff --git a/toolsrc/include/Paragraphs.h b/toolsrc/include/Paragraphs.h index 39880192c..26f81ffad 100644 --- a/toolsrc/include/Paragraphs.h +++ b/toolsrc/include/Paragraphs.h @@ -9,10 +9,12 @@ namespace vcpkg::Paragraphs { - expected> get_single_paragraph(const fs::path& control_path); - expected>> get_paragraphs(const fs::path& control_path); - expected> parse_single_paragraph(const std::string& str); - expected>> parse_paragraphs(const std::string& str); + using ParagraphDataMap = std::unordered_map; + + expected get_single_paragraph(const fs::path& control_path); + expected> get_paragraphs(const fs::path& control_path); + expected parse_single_paragraph(const std::string& str); + expected> parse_paragraphs(const std::string& str); expected try_load_port(const fs::path& control_path); diff --git a/toolsrc/include/SortedVector.h b/toolsrc/include/SortedVector.h new file mode 100644 index 000000000..d11c3544a --- /dev/null +++ b/toolsrc/include/SortedVector.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include + +// Add more forwarding functions to the m_data std::vector as needed. +namespace vcpkg +{ + template + class SortedVector + { + public: + using size_type = typename std::vector::size_type; + using iterator = typename std::vector::const_iterator; + + explicit SortedVector(std::vector v) : m_data(std::move(v)) + { + if (!std::is_sorted(m_data.begin(), m_data.end())) + { + std::sort(m_data.begin(), m_data.end()); + } + } + template + SortedVector(std::vector v, Compare comp) : m_data(std::move(v)) + { + if (!std::is_sorted(m_data.cbegin(), m_data.cend(), comp)) + { + std::sort(m_data.begin(), m_data.end(), comp); + } + } + + iterator begin() const + { + return this->m_data.cbegin(); + } + + iterator end() const + { + return this->m_data.cend(); + } + + bool empty() const + { + return this->m_data.empty(); + } + + size_type size() const + { + return this->m_data.size(); + } + + private: + std::vector m_data; + }; +} diff --git a/toolsrc/include/vcpkglib.h b/toolsrc/include/vcpkglib.h index b589c1617..bab1c28a1 100644 --- a/toolsrc/include/vcpkglib.h +++ b/toolsrc/include/vcpkglib.h @@ -2,7 +2,7 @@ #include "StatusParagraphs.h" #include "vcpkg_paths.h" -#include "ImmutableSortedVector.h" +#include "SortedVector.h" namespace vcpkg { @@ -15,7 +15,7 @@ namespace vcpkg struct StatusParagraph_and_associated_files { StatusParagraph pgh; - ImmutableSortedVector files; + SortedVector files; }; std::vector get_installed_ports(const StatusParagraphs& status_db); diff --git a/toolsrc/src/commands_install.cpp b/toolsrc/src/commands_install.cpp index e504cf8ee..bba3c665f 100644 --- a/toolsrc/src/commands_install.cpp +++ b/toolsrc/src/commands_install.cpp @@ -106,14 +106,14 @@ namespace vcpkg::Commands::Install continue; } - output.insert(output.end(), t.files.cbegin(), t.files.cend()); + output.insert(output.end(), t.files.begin(), t.files.end()); } std::sort(output.begin(), output.end()); return output; } - static ImmutableSortedVector build_list_of_package_files(const fs::path& package_dir) + static SortedVector build_list_of_package_files(const fs::path& package_dir) { const std::vector package_file_paths = Files::recursive_find_all_files_in_dir(package_dir); const size_t package_remove_char_count = package_dir.generic_string().size() + 1; // +1 for the slash @@ -124,16 +124,16 @@ namespace vcpkg::Commands::Install return std::move(as_string); }); - return ImmutableSortedVector::create(std::move(package_files)); + return SortedVector(std::move(package_files)); } - static ImmutableSortedVector build_list_of_installed_files(const std::vector& pgh_and_files, const triplet& triplet) + static SortedVector build_list_of_installed_files(const std::vector& pgh_and_files, const triplet& triplet) { std::vector installed_files = extract_files_in_triplet(pgh_and_files, triplet); const size_t installed_remove_char_count = triplet.canonical_name().size() + 1; // +1 for the slash remove_first_n_chars(&installed_files, installed_remove_char_count); - return ImmutableSortedVector::create(std::move(installed_files)); + return SortedVector(std::move(installed_files)); } void install_package(const vcpkg_paths& paths, const BinaryParagraph& binary_paragraph, StatusParagraphs* status_db) @@ -142,12 +142,12 @@ namespace vcpkg::Commands::Install const triplet& triplet = binary_paragraph.spec.target_triplet(); const std::vector pgh_and_files = get_installed_files(paths, *status_db); - const ImmutableSortedVector package_files = build_list_of_package_files(package_dir); - const ImmutableSortedVector installed_files = build_list_of_installed_files(pgh_and_files, triplet); + const SortedVector package_files = build_list_of_package_files(package_dir); + const SortedVector installed_files = build_list_of_installed_files(pgh_and_files, triplet); std::vector intersection; - std::set_intersection(package_files.cbegin(), package_files.cend(), - installed_files.cbegin(), installed_files.cend(), + std::set_intersection(package_files.begin(), package_files.end(), + installed_files.begin(), installed_files.end(), std::back_inserter(intersection)); if (!intersection.empty()) diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index d3c8b29e1..ce1e6a4c7 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -4,7 +4,7 @@ #include "vcpkg_Maps.h" #include "SourceParagraph.h" #include "Paragraphs.h" -#include "ImmutableSortedVector.h" +#include "SortedVector.h" namespace vcpkg::Commands::PortsDiff { diff --git a/toolsrc/src/commands_update.cpp b/toolsrc/src/commands_update.cpp index 701302dfc..b6c933521 100644 --- a/toolsrc/src/commands_update.cpp +++ b/toolsrc/src/commands_update.cpp @@ -44,7 +44,7 @@ namespace vcpkg::Commands::Update const StatusParagraphs status_db = database_load_check(paths); - const auto outdated_packages = ImmutableSortedVector::create(find_outdated_packages(paths, status_db), + const auto outdated_packages = SortedVector(find_outdated_packages(paths, status_db), &outdated_package::compare_by_name); if (outdated_packages.empty()) diff --git a/toolsrc/src/vcpkg_paths.cpp b/toolsrc/src/vcpkg_paths.cpp index dd26e89e4..eb2478f2e 100644 --- a/toolsrc/src/vcpkg_paths.cpp +++ b/toolsrc/src/vcpkg_paths.cpp @@ -5,6 +5,7 @@ #include "vcpkg_System.h" #include "package_spec.h" #include "vcpkg_Files.h" +#include "vcpkg_Util.h" namespace vcpkg { @@ -62,10 +63,7 @@ namespace vcpkg return {}; } - const std::vector paths_to_add = Strings::split(out.output, "\n"); - std::vector v; - v.insert(v.end(), paths_to_add.cbegin(), paths_to_add.cend()); - return v; + return Util::fmap(Strings::split(out.output, "\n"), [](auto&& s) { return fs::path(s); }); } static fs::path fetch_dependency(const fs::path scripts_folder, const std::wstring& tool_name, const fs::path& expected_downloaded_path) @@ -252,21 +250,16 @@ namespace vcpkg return Strings::split(ec_data.output, "\n"); } - static const optional& get_VS2015_installation_instance() + static optional get_VS2015_installation_instance() { - static const optional vs2015_path = []() -> optional - { - const optional vs2015_cmntools_optional = System::get_environmental_variable(L"VS140COMNTOOLS"); - if (auto v = vs2015_cmntools_optional.get()) - { - static const fs::path vs2015_cmntools = fs::path(*v).parent_path(); // The call to parent_path() is needed because the env variable has a trailing backslash - static const fs::path vs2015_path = vs2015_cmntools.parent_path().parent_path(); - return vs2015_path; - } + const optional vs2015_cmntools_optional = System::get_environmental_variable(L"VS140COMNTOOLS"); + if (auto v = vs2015_cmntools_optional.get()) + { + const fs::path vs2015_cmntools = fs::path(*v).parent_path(); // The call to parent_path() is needed because the env variable has a trailing backslash + return vs2015_cmntools.parent_path().parent_path(); + } - return nullopt; - }(); - return vs2015_path; + return nullopt; } static toolset_t find_toolset_instance(const vcpkg_paths& paths) @@ -312,7 +305,7 @@ namespace vcpkg } // VS2015 - const optional& vs_2015_installation_instance = get_VS2015_installation_instance(); + const optional vs_2015_installation_instance = get_VS2015_installation_instance(); if (auto v = vs_2015_installation_instance.get()) { const fs::path vs2015_vcvarsall_bat = *v / "VC" / "vcvarsall.bat"; diff --git a/toolsrc/src/vcpkglib.cpp b/toolsrc/src/vcpkglib.cpp index 47592ea1d..152b9ac2b 100644 --- a/toolsrc/src/vcpkglib.cpp +++ b/toolsrc/src/vcpkglib.cpp @@ -208,7 +208,7 @@ namespace vcpkg } ), installed_files_of_current_pgh.end()); - StatusParagraph_and_associated_files pgh_and_files = { *pgh, ImmutableSortedVector::create(std::move(installed_files_of_current_pgh)) }; + StatusParagraph_and_associated_files pgh_and_files = { *pgh, SortedVector(std::move(installed_files_of_current_pgh)) }; installed_files.push_back(std::move(pgh_and_files)); } diff --git a/toolsrc/vcpkglib/vcpkglib.vcxproj b/toolsrc/vcpkglib/vcpkglib.vcxproj index 664c33dce..d79c77a94 100644 --- a/toolsrc/vcpkglib/vcpkglib.vcxproj +++ b/toolsrc/vcpkglib/vcpkglib.vcxproj @@ -146,7 +146,7 @@ - + diff --git a/toolsrc/vcpkglib/vcpkglib.vcxproj.filters b/toolsrc/vcpkglib/vcpkglib.vcxproj.filters index 914efbb91..acfecbcc5 100644 --- a/toolsrc/vcpkglib/vcpkglib.vcxproj.filters +++ b/toolsrc/vcpkglib/vcpkglib.vcxproj.filters @@ -236,9 +236,6 @@ Header Files - - Header Files - Header Files @@ -311,5 +308,8 @@ Header Files + + Header Files + \ No newline at end of file -- cgit v1.2.3