Skip to content

Commit

Permalink
maint: Detemplatize Database::add_repo_from_packages
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>
  • Loading branch information
jjerphan committed Aug 1, 2024
1 parent d662921 commit c003f7e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 61 deletions.
35 changes: 3 additions & 32 deletions libmamba/include/mamba/solver/libsolv/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,15 @@ namespace mamba::solver::libsolv
PipAsPythonDependency add = PipAsPythonDependency::No
) -> expected_t<RepoInfo>;

template <typename Iter>
auto add_repo_from_packages(
Iter first_package,
Iter last_package,
std::vector<specs::PackageInfo>::const_iterator first_package,
std::vector<specs::PackageInfo>::const_iterator last_package,
std::string_view name = "",
PipAsPythonDependency add = PipAsPythonDependency::No
) -> RepoInfo;

template <typename Range>
auto add_repo_from_packages(
const Range& packages,
const std::vector<specs::PackageInfo>& packages,
std::string_view name = "",
PipAsPythonDependency add = PipAsPythonDependency::No
) -> RepoInfo;
Expand Down Expand Up @@ -173,33 +171,6 @@ namespace mamba::solver::libsolv
* Implementation *
********************/

template <typename Iter>
auto Database::add_repo_from_packages(
Iter first_package,
Iter last_package,
std::string_view name,
PipAsPythonDependency add
) -> RepoInfo
{
auto repo = add_repo_from_packages_impl_pre(name);
for (; first_package != last_package; ++first_package)
{
add_repo_from_packages_impl_loop(repo, *first_package);
}
add_repo_from_packages_impl_post(repo, add);
return repo;
}

template <typename Range>
auto Database::add_repo_from_packages(
const Range& packages,
std::string_view name,
PipAsPythonDependency add
) -> RepoInfo
{
return add_repo_from_packages(packages.begin(), packages.end(), name, add);
}

// TODO(C++20): Use ranges::transform
template <typename Func>
void Database::for_each_package_in_repo(RepoInfo repo, Func&& func) const
Expand Down
26 changes: 26 additions & 0 deletions libmamba/src/solver/libsolv/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,30 @@ namespace mamba::solver::libsolv
);
return out;
}

auto Database::add_repo_from_packages(
std::vector<specs::PackageInfo>::const_iterator first_package,
std::vector<specs::PackageInfo>::const_iterator last_package,
std::string_view name,
PipAsPythonDependency add
) -> RepoInfo
{
auto repo = add_repo_from_packages_impl_pre(name);
for (; first_package != last_package; ++first_package)
{
add_repo_from_packages_impl_loop(repo, *first_package);
}
add_repo_from_packages_impl_post(repo, add);
return repo;
}

auto Database::add_repo_from_packages(
const std::vector<specs::PackageInfo>& packages,
std::string_view name,
PipAsPythonDependency add
) -> RepoInfo
{
return add_repo_from_packages(packages.begin(), packages.end(), name, add);
}

}
4 changes: 2 additions & 2 deletions libmamba/tests/src/solver/libsolv/test_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST_SUITE("solver::libsolv::database")

SUBCASE("Add repo from packages")
{
auto pkgs = std::array{
auto pkgs = std::vector{
mkpkg("x", "1.0"),
mkpkg("x", "2.0"),
mkpkg("z", "1.0", { "x>=1.0" }),
Expand Down Expand Up @@ -115,7 +115,7 @@ TEST_SUITE("solver::libsolv::database")

SUBCASE("Iterate over packages")
{
auto repo2 = db.add_repo_from_packages(std::array{ mkpkg("z", "2.0") }, "repo1");
auto repo2 = db.add_repo_from_packages(std::vector{ mkpkg("z", "2.0") }, "repo1");

SUBCASE("In a given repo")
{
Expand Down
37 changes: 18 additions & 19 deletions libmamba/tests/src/solver/libsolv/test_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include <array>
#include <type_traits>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -359,7 +358,7 @@ TEST_SUITE("solver::libsolv::solver")

SUBCASE("numpy 1.0 is installed")
{
const auto installed = db.add_repo_from_packages(std::array{
const auto installed = db.add_repo_from_packages(std::vector{
specs::PackageInfo("numpy", "1.0.0", "phony", 0),
});
db.set_installed_repo(installed);
Expand Down Expand Up @@ -423,7 +422,7 @@ TEST_SUITE("solver::libsolv::solver")
{
auto pkg_numpy = specs::PackageInfo("numpy", "1.0.0", "phony", 0);
pkg_numpy.dependencies = { "python=2.0", "foo" };
const auto installed = db.add_repo_from_packages(std::array{
const auto installed = db.add_repo_from_packages(std::vector{
pkg_numpy,
specs::PackageInfo("python", "2.0.0", "phony", 0),
specs::PackageInfo("foo"),
Expand Down Expand Up @@ -548,7 +547,7 @@ TEST_SUITE("solver::libsolv::solver")
pkg_numpy.dependencies = { "python=4.0", "foo" };
auto pkg_foo = specs::PackageInfo("foo", "1.0.0", "phony", 0);
pkg_foo.constrains = { "numpy=1.0.0", "foo" };
const auto installed = db.add_repo_from_packages(std::array{
const auto installed = db.add_repo_from_packages(std::vector{
pkg_numpy,
pkg_foo,
specs::PackageInfo("python", "4.0.0", "phony", 0),
Expand Down Expand Up @@ -614,10 +613,10 @@ TEST_SUITE("solver::libsolv::solver")
{
auto db = libsolv::Database({});

const auto repo1 = db.add_repo_from_packages(std::array{
const auto repo1 = db.add_repo_from_packages(std::vector{
specs::PackageInfo("numpy", "1.0.0", "repo1", 0),
});
const auto repo2 = db.add_repo_from_packages(std::array{
const auto repo2 = db.add_repo_from_packages(std::vector{
specs::PackageInfo("numpy", "2.0.0", "repo2", 0),
});
db.set_repo_priority(repo1, { 2, 0 });
Expand Down Expand Up @@ -675,7 +674,7 @@ TEST_SUITE("solver::libsolv::solver")

SUBCASE("Pins are respected")
{
db.add_repo_from_packages(std::array{
db.add_repo_from_packages(std::vector{
mkfoo("1.0.0", 0, { "feat" }, 0),
mkfoo("2.0.0", 1, {}, 1),
});
Expand All @@ -698,7 +697,7 @@ TEST_SUITE("solver::libsolv::solver")

SUBCASE("Track features has highest priority")
{
db.add_repo_from_packages(std::array{
db.add_repo_from_packages(std::vector{
mkfoo("1.0.0", 0, {}, 0),
mkfoo("2.0.0", 1, { "feat" }, 1),
});
Expand All @@ -720,7 +719,7 @@ TEST_SUITE("solver::libsolv::solver")

SUBCASE("Version has second highest priority")
{
db.add_repo_from_packages(std::array{
db.add_repo_from_packages(std::vector{
mkfoo("2.0.0", 0, {}, 0),
mkfoo("1.0.0", 1, {}, 1),
});
Expand All @@ -742,7 +741,7 @@ TEST_SUITE("solver::libsolv::solver")

SUBCASE("Build number has third highest priority")
{
db.add_repo_from_packages(std::array{
db.add_repo_from_packages(std::vector{
mkfoo("2.0.0", 1, {}, 0),
mkfoo("2.0.0", 0, {}, 1),
});
Expand All @@ -764,7 +763,7 @@ TEST_SUITE("solver::libsolv::solver")

SUBCASE("Timestamp has lowest priority")
{
db.add_repo_from_packages(std::array{
db.add_repo_from_packages(std::vector{
mkfoo("2.0.0", 0, {}, 0),
mkfoo("2.0.0", 0, {}, 1),
});
Expand Down Expand Up @@ -796,10 +795,10 @@ TEST_SUITE("solver::libsolv::solver")
{
auto pkg1 = specs::PackageInfo("foo", "1.0.0", "conda", 0);
pkg1.package_url = "https://conda.anaconda.org/conda-forge/linux-64/foo-1.0.0-phony.conda";
db.add_repo_from_packages(std::array{ pkg1 });
db.add_repo_from_packages(std::vector{ pkg1 });
auto pkg2 = specs::PackageInfo("foo", "1.0.0", "mamba", 0);
pkg2.package_url = "https://conda.anaconda.org/mamba-forge/linux-64/foo-1.0.0-phony.conda";
db.add_repo_from_packages(std::array{ pkg2 });
db.add_repo_from_packages(std::vector{ pkg2 });

SUBCASE("conda-forge::foo")
{
Expand Down Expand Up @@ -938,7 +937,7 @@ TEST_SUITE("solver::libsolv::solver")
auto pkg2 = PackageInfo("foo");
pkg2.version = "2.0";

db.add_repo_from_packages(std::array{ pkg1, pkg2 });
db.add_repo_from_packages(std::vector{ pkg1, pkg2 });

auto request = Request{
/* .flags= */ {},
Expand Down Expand Up @@ -969,7 +968,7 @@ TEST_SUITE("solver::libsolv::solver")
pkg4.version = "2.0";
pkg4.dependencies = { "foo=2.0" };

db.add_repo_from_packages(std::array{ pkg1, pkg2, pkg3, pkg4 });
db.add_repo_from_packages(std::vector{ pkg1, pkg2, pkg3, pkg4 });

auto request = Request{
/* .flags= */ {},
Expand Down Expand Up @@ -1006,7 +1005,7 @@ TEST_SUITE("solver::libsolv::solver")
auto pkg2 = PackageInfo("foo");
pkg2.md5 = "bad";

db.add_repo_from_packages(std::array{ pkg1, pkg2 });
db.add_repo_from_packages(std::vector{ pkg1, pkg2 });

auto request = Request{
/* .flags= */ {},
Expand All @@ -1031,7 +1030,7 @@ TEST_SUITE("solver::libsolv::solver")
auto pkg1 = PackageInfo("foo");
pkg1.md5 = "0bab699354cbd66959550eb9b9866620";

db.add_repo_from_packages(std::array{ pkg1 });
db.add_repo_from_packages(std::vector{ pkg1 });

auto request = Request{
/* .flags= */ {},
Expand All @@ -1050,7 +1049,7 @@ TEST_SUITE("solver::libsolv::solver")
auto pkg2 = PackageInfo("foo");
pkg2.build_string = "bld";

db.add_repo_from_packages(std::array{ pkg1, pkg2 });
db.add_repo_from_packages(std::vector{ pkg1, pkg2 });

auto request = Request{
/* .flags= */ {},
Expand Down Expand Up @@ -1079,7 +1078,7 @@ TEST_SUITE("solver::libsolv::solver")
pkg3.build_string = "bld";
pkg3.build_number = 4;

db.add_repo_from_packages(std::array{ pkg1, pkg2, pkg3 });
db.add_repo_from_packages(std::vector{ pkg1, pkg2, pkg3 });

auto request = Request{
/* .flags= */ {},
Expand Down
16 changes: 8 additions & 8 deletions libmamba/tests/src/solver/test_problems_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ namespace
*
* The underlying packages do not exist, we are only interested in the conflict.
*/
template <typename PkgRange>
auto create_pkgs_database(ChannelContext& channel_context, const PkgRange& packages)
auto
create_pkgs_database(ChannelContext& channel_context, const std::vector<specs::PackageInfo>& packages)
{
solver::libsolv::Database db{ channel_context.params() };
db.add_repo_from_packages(packages);
Expand All @@ -116,7 +116,7 @@ TEST_CASE("Test create_pkgs_database utility")
{
auto& ctx = mambatests::context();
auto channel_context = ChannelContext::make_conda_compatible(ctx);
auto db = create_pkgs_database(channel_context, std::array{ mkpkg("foo", "0.1.0", {}) });
auto db = create_pkgs_database(channel_context, std::vector{ mkpkg("foo", "0.1.0", {}) });
auto request = Request{ {}, { Request::Install{ "foo"_ms } } };
const auto outcome = solver::libsolv::Solver().solve(db, request).value();
REQUIRE(std::holds_alternative<solver::Solution>(outcome));
Expand All @@ -128,7 +128,7 @@ TEST_CASE("Test empty specs")
auto channel_context = ChannelContext::make_conda_compatible(ctx);
auto db = create_pkgs_database(
channel_context,
std::array{ mkpkg("foo", "0.1.0", {}), mkpkg("", "", {}) }
std::vector{ mkpkg("foo", "0.1.0", {}), mkpkg("", "", {}) }
);
auto request = Request{ {}, { Request::Install{ "foo"_ms } } };
const auto outcome = solver::libsolv::Solver().solve(db, request).value();
Expand All @@ -142,7 +142,7 @@ namespace
return std::pair(
create_pkgs_database(
channel_context,
std::array{
std::vector{
mkpkg("A", "0.1.0"),
mkpkg("A", "0.2.0"),
mkpkg("A", "0.3.0"),
Expand All @@ -163,7 +163,7 @@ namespace
return std::pair(
create_pkgs_database(
channel_context,
std::array{
std::vector{
mkpkg("menu", "1.5.0", { "dropdown=2.*" }),
mkpkg("menu", "1.4.0", { "dropdown=2.*" }),
mkpkg("menu", "1.3.0", { "dropdown=2.*" }),
Expand Down Expand Up @@ -281,7 +281,7 @@ namespace
return std::pair(
create_pkgs_database(
channel_context,
std::array{
std::vector{
mkpkg("foo", "2.0.0", { "bar=2.0" }),
mkpkg("bar", "1.0.0"),
mkpkg("bar", "2.0.0"),
Expand Down Expand Up @@ -600,7 +600,7 @@ TEST_CASE("Create problem graph")
using PbGr = ProblemsGraph;
using CpPbGr = CompressedProblemsGraph;

const auto issues = std::array{
const auto issues = std::vector{
std::pair{ "Basic conflict", &create_basic_conflict },
std::pair{ "PubGrub example", &create_pubgrub },
std::pair{ "Harder PubGrub example", &create_pubgrub_hard },
Expand Down

0 comments on commit c003f7e

Please sign in to comment.