Skip to content

Commit

Permalink
[vcpkg] ImmutableSortedVector is actually Mutable via move.
Browse files Browse the repository at this point in the history
Use fmap instead of construct/insert.
Don't cache VS2015 instances since it is called once.
Add ParagraphDataMap alias.
  • Loading branch information
ras0219-msft committed Apr 1, 2017
1 parent bb865fb commit b788c2b
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 113 deletions.
73 changes: 0 additions & 73 deletions toolsrc/include/ImmutableSortedVector.h

This file was deleted.

10 changes: 6 additions & 4 deletions toolsrc/include/Paragraphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

namespace vcpkg::Paragraphs
{
expected<std::unordered_map<std::string, std::string>> get_single_paragraph(const fs::path& control_path);
expected<std::vector<std::unordered_map<std::string, std::string>>> get_paragraphs(const fs::path& control_path);
expected<std::unordered_map<std::string, std::string>> parse_single_paragraph(const std::string& str);
expected<std::vector<std::unordered_map<std::string, std::string>>> parse_paragraphs(const std::string& str);
using ParagraphDataMap = std::unordered_map<std::string, std::string>;

expected<ParagraphDataMap> get_single_paragraph(const fs::path& control_path);
expected<std::vector<ParagraphDataMap>> get_paragraphs(const fs::path& control_path);
expected<ParagraphDataMap> parse_single_paragraph(const std::string& str);
expected<std::vector<ParagraphDataMap>> parse_paragraphs(const std::string& str);

expected<SourceParagraph> try_load_port(const fs::path& control_path);

Expand Down
55 changes: 55 additions & 0 deletions toolsrc/include/SortedVector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <vector>
#include <algorithm>

// Add more forwarding functions to the m_data std::vector as needed.
namespace vcpkg
{
template <class T>
class SortedVector
{
public:
using size_type = typename std::vector<T>::size_type;
using iterator = typename std::vector<T>::const_iterator;

explicit SortedVector<T>(std::vector<T> 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 <class Compare>
SortedVector<T>(std::vector<T> 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<T> m_data;
};
}
4 changes: 2 additions & 2 deletions toolsrc/include/vcpkglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "StatusParagraphs.h"
#include "vcpkg_paths.h"
#include "ImmutableSortedVector.h"
#include "SortedVector.h"

namespace vcpkg
{
Expand All @@ -15,7 +15,7 @@ namespace vcpkg
struct StatusParagraph_and_associated_files
{
StatusParagraph pgh;
ImmutableSortedVector<std::string> files;
SortedVector<std::string> files;
};

std::vector<StatusParagraph*> get_installed_ports(const StatusParagraphs& status_db);
Expand Down
18 changes: 9 additions & 9 deletions toolsrc/src/commands_install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> build_list_of_package_files(const fs::path& package_dir)
static SortedVector<std::string> build_list_of_package_files(const fs::path& package_dir)
{
const std::vector<fs::path> 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
Expand All @@ -124,16 +124,16 @@ namespace vcpkg::Commands::Install
return std::move(as_string);
});

return ImmutableSortedVector<std::string>::create(std::move(package_files));
return SortedVector<std::string>(std::move(package_files));
}

static ImmutableSortedVector<std::string> build_list_of_installed_files(const std::vector<StatusParagraph_and_associated_files>& pgh_and_files, const triplet& triplet)
static SortedVector<std::string> build_list_of_installed_files(const std::vector<StatusParagraph_and_associated_files>& pgh_and_files, const triplet& triplet)
{
std::vector<std::string> 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<std::string>::create(std::move(installed_files));
return SortedVector<std::string>(std::move(installed_files));
}

void install_package(const vcpkg_paths& paths, const BinaryParagraph& binary_paragraph, StatusParagraphs* status_db)
Expand All @@ -142,12 +142,12 @@ namespace vcpkg::Commands::Install
const triplet& triplet = binary_paragraph.spec.target_triplet();
const std::vector<StatusParagraph_and_associated_files> pgh_and_files = get_installed_files(paths, *status_db);

const ImmutableSortedVector<std::string> package_files = build_list_of_package_files(package_dir);
const ImmutableSortedVector<std::string> installed_files = build_list_of_installed_files(pgh_and_files, triplet);
const SortedVector<std::string> package_files = build_list_of_package_files(package_dir);
const SortedVector<std::string> installed_files = build_list_of_installed_files(pgh_and_files, triplet);

std::vector<std::string> 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())
Expand Down
2 changes: 1 addition & 1 deletion toolsrc/src/commands_portsdiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "vcpkg_Maps.h"
#include "SourceParagraph.h"
#include "Paragraphs.h"
#include "ImmutableSortedVector.h"
#include "SortedVector.h"

namespace vcpkg::Commands::PortsDiff
{
Expand Down
2 changes: 1 addition & 1 deletion toolsrc/src/commands_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace vcpkg::Commands::Update

const StatusParagraphs status_db = database_load_check(paths);

const auto outdated_packages = ImmutableSortedVector<outdated_package>::create(find_outdated_packages(paths, status_db),
const auto outdated_packages = SortedVector<outdated_package>(find_outdated_packages(paths, status_db),
&outdated_package::compare_by_name);

if (outdated_packages.empty())
Expand Down
29 changes: 11 additions & 18 deletions toolsrc/src/vcpkg_paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "vcpkg_System.h"
#include "package_spec.h"
#include "vcpkg_Files.h"
#include "vcpkg_Util.h"

namespace vcpkg
{
Expand Down Expand Up @@ -62,10 +63,7 @@ namespace vcpkg
return {};
}

const std::vector<std::string> paths_to_add = Strings::split(out.output, "\n");
std::vector<fs::path> 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)
Expand Down Expand Up @@ -252,21 +250,16 @@ namespace vcpkg
return Strings::split(ec_data.output, "\n");
}

static const optional<fs::path>& get_VS2015_installation_instance()
static optional<fs::path> get_VS2015_installation_instance()
{
static const optional<fs::path> vs2015_path = []() -> optional<fs::path>
{
const optional<std::wstring> 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<std::wstring> 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)
Expand Down Expand Up @@ -312,7 +305,7 @@ namespace vcpkg
}

// VS2015
const optional<fs::path>& vs_2015_installation_instance = get_VS2015_installation_instance();
const optional<fs::path> 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";
Expand Down
2 changes: 1 addition & 1 deletion toolsrc/src/vcpkglib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ namespace vcpkg
}
), installed_files_of_current_pgh.end());

StatusParagraph_and_associated_files pgh_and_files = { *pgh, ImmutableSortedVector<std::string>::create(std::move(installed_files_of_current_pgh)) };
StatusParagraph_and_associated_files pgh_and_files = { *pgh, SortedVector<std::string>(std::move(installed_files_of_current_pgh)) };
installed_files.push_back(std::move(pgh_and_files));
}

Expand Down
2 changes: 1 addition & 1 deletion toolsrc/vcpkglib/vcpkglib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<ClInclude Include="..\include\coff_file_reader.h" />
<ClInclude Include="..\include\vcpkg_expected.h" />
<ClInclude Include="..\include\filesystem_fs.h" />
<ClInclude Include="..\include\ImmutableSortedVector.h" />
<ClInclude Include="..\include\SortedVector.h" />
<ClInclude Include="..\include\MachineType.h" />
<ClInclude Include="..\include\metrics.h" />
<ClInclude Include="..\include\opt_bool.h" />
Expand Down
6 changes: 3 additions & 3 deletions toolsrc/vcpkglib/vcpkglib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@
<ClInclude Include="..\include\vcpkglib.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\ImmutableSortedVector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\opt_bool.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -311,5 +308,8 @@
<ClInclude Include="..\include\vcpkg_Util.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\SortedVector.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit b788c2b

Please sign in to comment.