-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ObjSolver * Add ObjSolver problems * Add ObjSolver rule getters * Add ObjPool::add_conda_dependency * Add ObjSolver::solve test * Add extra header
- Loading branch information
1 parent
17232ca
commit 2dec4ae
Showing
11 changed files
with
389 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) 2023, QuantStack and Mamba Contributors | ||
// | ||
// Distributed under the terms of the BSD 3-Clause License. | ||
// | ||
// The full license is in the file LICENSE, distributed with this software. | ||
|
||
#include <cassert> | ||
|
||
#include <solv/poolid.h> | ||
#include <solv/solvable.h> | ||
#include <solv/solver.h> | ||
// broken headers go last | ||
#include <solv/problems.h> | ||
#include <solv/rules.h> | ||
|
||
#include "solv-cpp/pool.hpp" | ||
#include "solv-cpp/queue.hpp" | ||
#include "solv-cpp/solver.hpp" | ||
|
||
namespace mamba::solv | ||
{ | ||
void ObjSolver::SolverDeleter::operator()(::Solver* ptr) | ||
{ | ||
::solver_free(ptr); | ||
} | ||
|
||
ObjSolver::ObjSolver(const ObjPool& pool) | ||
: m_solver(::solver_create(const_cast<::Pool*>(pool.raw()))) | ||
{ | ||
} | ||
|
||
ObjSolver::~ObjSolver() = default; | ||
|
||
auto ObjSolver::raw() -> ::Solver* | ||
{ | ||
return m_solver.get(); | ||
} | ||
|
||
void ObjSolver::set_flag(SolverFlag flag, bool value) | ||
{ | ||
::solver_set_flag(raw(), flag, value); | ||
} | ||
|
||
auto ObjSolver::get_flag(SolverFlag flag) const -> bool | ||
{ | ||
const auto val = ::solver_get_flag(const_cast<::Solver*>(raw()), flag); | ||
assert((val == 0) || (val == 1)); | ||
return val != 0; | ||
} | ||
|
||
auto ObjSolver::raw() const -> const ::Solver* | ||
{ | ||
return m_solver.get(); | ||
} | ||
|
||
auto ObjSolver::solve(const ObjPool& /* pool */, const ObjQueue& jobs) -> bool | ||
{ | ||
// pool is captured inside solver so we take it as a parameter to be explicit. | ||
const auto n_pbs = ::solver_solve(raw(), const_cast<::Queue*>(jobs.raw())); | ||
return n_pbs == 0; | ||
} | ||
|
||
auto ObjSolver::problem_count() const -> std::size_t | ||
{ | ||
return ::solver_problem_count(const_cast<::Solver*>(raw())); | ||
} | ||
|
||
auto ObjSolver::problem_to_string(const ObjPool& /* pool */, ProblemId id) const -> std::string | ||
{ | ||
// pool is captured inside solver so we take it as a parameter to be explicit. | ||
return ::solver_problem2str(const_cast<::Solver*>(raw()), id); | ||
} | ||
|
||
auto ObjSolver::next_problem(ProblemId id) const -> ProblemId | ||
{ | ||
return ::solver_next_problem(const_cast<::Solver*>(raw()), id); | ||
} | ||
|
||
auto ObjSolver::problem_rules(ProblemId id) const -> ObjQueue | ||
{ | ||
ObjQueue rules = {}; | ||
::solver_findallproblemrules(const_cast<::Solver*>(raw()), id, rules.raw()); | ||
return rules; | ||
} | ||
|
||
auto ObjSolver::get_rule_info(const ObjPool& /* pool */, RuleId id) const -> ObjRuleInfo | ||
{ | ||
// pool is captured inside solver so we take it as a parameter to be explicit. | ||
SolvableId from_id = 0; | ||
SolvableId to_id = 0; | ||
DependencyId dep_id = 0; | ||
const auto type = ::solver_ruleinfo(const_cast<::Solver*>(raw()), id, &from_id, &to_id, &dep_id); | ||
|
||
return { | ||
/* .from_id= */ (from_id != 0) ? std::optional{ from_id } : std::nullopt, | ||
/* .to_id= */ (to_id != 0) ? std::optional{ to_id } : std::nullopt, | ||
/* .dep_id= */ (dep_id != 0) ? std::optional{ dep_id } : std::nullopt, | ||
/* .type= */ type, | ||
/* .klass= */ ::solver_ruleclass(const_cast<::Solver*>(raw()), id), | ||
}; | ||
} | ||
|
||
auto ObjSolver::rule_info_to_string(const ObjPool& /* pool */, ObjRuleInfo ri) const -> std::string | ||
{ | ||
// pool is captured inside solver so we take it as a parameter to be explicit. | ||
return ::solver_ruleinfo2str( | ||
const_cast<::Solver*>(raw()), | ||
ri.type, | ||
ri.from_id.value_or(0), | ||
ri.to_id.value_or(0), | ||
ri.dep_id.value_or(0) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) 2023, QuantStack and Mamba Contributors | ||
// | ||
// Distributed under the terms of the BSD 3-Clause License. | ||
// | ||
// The full license is in the file LICENSE, distributed with this software. | ||
|
||
#ifndef MAMBA_SOLV_SOLVER_HPP | ||
#define MAMBA_SOLV_SOLVER_HPP | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include <string> | ||
|
||
// START Only required for broken header <solv/rule.h> | ||
#include <solv/poolid.h> | ||
extern "C" | ||
{ | ||
typedef struct s_Solvable Solvable; | ||
typedef struct s_Map Map; | ||
typedef struct s_Queue Queue; | ||
} | ||
// END | ||
#include <solv/rules.h> | ||
|
||
#include "solv-cpp/ids.hpp" | ||
#include "solv-cpp/queue.hpp" | ||
|
||
extern "C" | ||
{ | ||
using Solver = struct s_Solver; | ||
} | ||
|
||
namespace mamba::solv | ||
{ | ||
class ObjPool; | ||
class ObjQueue; | ||
|
||
struct ObjRuleInfo | ||
{ | ||
std::optional<SolvableId> from_id; | ||
std::optional<SolvableId> to_id; | ||
std::optional<DependencyId> dep_id; | ||
::SolverRuleinfo type; | ||
::SolverRuleinfo klass; | ||
}; | ||
|
||
class ObjSolver | ||
{ | ||
public: | ||
|
||
ObjSolver(const ObjPool& pool); | ||
~ObjSolver(); | ||
|
||
auto raw() -> ::Solver*; | ||
auto raw() const -> const ::Solver*; | ||
|
||
void set_flag(SolverFlag flag, bool value); | ||
[[nodiscard]] auto get_flag(SolverFlag flag) const -> bool; | ||
|
||
[[nodiscard]] auto solve(const ObjPool& pool, const ObjQueue& jobs) -> bool; | ||
|
||
[[nodiscard]] auto problem_count() const -> std::size_t; | ||
[[nodiscard]] auto problem_to_string(const ObjPool& pool, ProblemId id) const -> std::string; | ||
template <typename UnaryFunc> | ||
void for_each_problem_id(UnaryFunc&& func) const; | ||
|
||
/** | ||
* Return an @ref ObjQueue of @ref RuleId with all rules involved in a current problem. | ||
*/ | ||
[[nodiscard]] auto problem_rules(ProblemId id) const -> ObjQueue; | ||
[[nodiscard]] auto get_rule_info(const ObjPool& pool, RuleId id) const -> ObjRuleInfo; | ||
[[nodiscard]] auto rule_info_to_string(const ObjPool& pool, ObjRuleInfo id) const | ||
-> std::string; | ||
|
||
private: | ||
|
||
struct SolverDeleter | ||
{ | ||
void operator()(::Solver* ptr); | ||
}; | ||
|
||
std::unique_ptr<::Solver, ObjSolver::SolverDeleter> m_solver = nullptr; | ||
|
||
auto next_problem(ProblemId id = 0) const -> ProblemId; | ||
}; | ||
|
||
/********************************* | ||
* Implementation of ObjSolver * | ||
*********************************/ | ||
|
||
template <typename UnaryFunc> | ||
void ObjSolver::for_each_problem_id(UnaryFunc&& func) const | ||
{ | ||
for (ProblemId id = next_problem(); id != 0; id = next_problem(id)) | ||
{ | ||
func(id); | ||
} | ||
} | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.