-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Solver solution #2584
Merged
Merged
Create Solver solution #2584
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
00a473a
Replace MTransaction to_remove with PackageInfo
AntoinePrv a06ce08
Add a solution class
AntoinePrv f852987
Fix Solution for_each
AntoinePrv e881e1a
Add Solution empty and size
AntoinePrv 6ce86f5
Make MTransaction::filter const
AntoinePrv 24a3a30
Remove unused MTransaction::m_force_reinstall
AntoinePrv 02ad666
Rename MSolver::set_flags
AntoinePrv 8c74e95
Refactor MSolver flags
AntoinePrv cfba796
Call solver.flag() once in MTransaction
AntoinePrv b212db0
Implement Solution as alias with free functions
AntoinePrv 94c8745
Remove as many MSolver flag setters as possible
AntoinePrv 5eeb99a
Make Soltion a struct to possibly add more members
AntoinePrv 384353e
Remove useless function call
AntoinePrv db577a6
Add Solution::Omit
AntoinePrv 863e574
Rename template typenames and parameters in Solution
AntoinePrv 37c4c63
Fix MSolver const parameter
AntoinePrv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
// 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_CORE_SOLUTION_HPP | ||
#define MAMBA_CORE_SOLUTION_HPP | ||
|
||
#include <variant> | ||
#include <vector> | ||
|
||
#include "package_info.hpp" | ||
|
||
namespace mamba | ||
{ | ||
namespace detail | ||
{ | ||
template <typename T, typename... U> | ||
inline constexpr bool is_any_of_v = std::disjunction_v<std::is_same<T, U>...>; | ||
} | ||
|
||
struct Solution | ||
{ | ||
struct Omit | ||
{ | ||
PackageInfo what; | ||
}; | ||
struct Upgrade | ||
{ | ||
PackageInfo remove; | ||
PackageInfo install; | ||
}; | ||
struct Downgrade | ||
{ | ||
PackageInfo remove; | ||
PackageInfo install; | ||
}; | ||
struct Change | ||
{ | ||
PackageInfo remove; | ||
PackageInfo install; | ||
}; | ||
struct Reinstall | ||
{ | ||
PackageInfo what; | ||
}; | ||
struct Remove | ||
{ | ||
PackageInfo remove; | ||
}; | ||
struct Install | ||
{ | ||
PackageInfo install; | ||
}; | ||
|
||
template <typename T> | ||
inline static constexpr bool has_remove_v = detail::is_any_of_v<T, Upgrade, Downgrade, Change, Remove>; | ||
|
||
template <typename T> | ||
inline static constexpr bool has_install_v = detail::is_any_of_v<T, Upgrade, Downgrade, Change, Install>; | ||
|
||
using Action = std::variant<Omit, Upgrade, Downgrade, Change, Reinstall, Remove, Install>; | ||
using action_list = std::vector<Action>; | ||
|
||
action_list actions = {}; | ||
}; | ||
|
||
template <typename Iter, typename UnaryFunc> | ||
void for_each_to_remove(Iter first, Iter last, UnaryFunc&& func); | ||
template <typename Range, typename UnaryFunc> | ||
void for_each_to_remove(Range&& actions, UnaryFunc&& func); | ||
|
||
template <typename Iter, typename UnaryFunc> | ||
void for_each_to_install(Iter first, Iter last, UnaryFunc&& func); | ||
template <typename Range, typename UnaryFunc> | ||
void for_each_to_install(Range&& actions, UnaryFunc&& func); | ||
|
||
template <typename Iter, typename UnaryFunc> | ||
void for_each_to_omit(Iter first, Iter last, UnaryFunc&& func); | ||
template <typename Range, typename UnaryFunc> | ||
void for_each_to_omit(Range&& actions, UnaryFunc&& func); | ||
} | ||
|
||
#include <type_traits> | ||
|
||
namespace mamba | ||
{ | ||
/******************************** | ||
* Implementation of Solution * | ||
********************************/ | ||
|
||
namespace detail | ||
{ | ||
template <typename Action> | ||
auto to_remove_ptr(Action& action) | ||
{ | ||
using PackageInfoPtr = std::conditional_t<std::is_const_v<Action>, const PackageInfo*, PackageInfo*>; | ||
return std::visit( | ||
[](auto& a) -> PackageInfoPtr | ||
{ | ||
using A = std::decay_t<decltype(a)>; | ||
if constexpr (Solution::has_remove_v<A>) | ||
{ | ||
return &(a.remove); | ||
} | ||
else if constexpr (std::is_same_v<A, Solution::Reinstall>) | ||
{ | ||
return &(a.what); | ||
} | ||
return nullptr; | ||
}, | ||
action | ||
); | ||
} | ||
} | ||
|
||
template <typename Iter, typename UnaryFunc> | ||
void for_each_to_remove(Iter first, Iter last, UnaryFunc&& func) | ||
{ | ||
for (; first != last; ++first) | ||
{ | ||
if (auto* const ptr = detail::to_remove_ptr(*first)) | ||
{ | ||
func(*ptr); | ||
} | ||
} | ||
} | ||
|
||
template <typename Range, typename UnaryFunc> | ||
void for_each_to_remove(Range&& actions, UnaryFunc&& func) | ||
{ | ||
return for_each_to_remove(actions.begin(), actions.end(), std::forward<UnaryFunc>(func)); | ||
} | ||
|
||
namespace detail | ||
{ | ||
template <typename Action> | ||
auto to_install_ptr(Action& action) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark here. |
||
{ | ||
using PackageInfoPtr = std::conditional_t<std::is_const_v<Action>, const PackageInfo*, PackageInfo*>; | ||
return std::visit( | ||
[](auto& a) -> PackageInfoPtr | ||
{ | ||
using A = std::decay_t<decltype(a)>; | ||
if constexpr (Solution::has_install_v<A>) | ||
{ | ||
return &(a.install); | ||
} | ||
else if constexpr (std::is_same_v<A, Solution::Reinstall>) | ||
{ | ||
return &(a.what); | ||
} | ||
return nullptr; | ||
}, | ||
action | ||
); | ||
} | ||
} | ||
|
||
template <typename Iter, typename UnaryFunc> | ||
void for_each_to_install(Iter first, Iter last, UnaryFunc&& func) | ||
{ | ||
for (; first != last; ++first) | ||
{ | ||
if (auto* const ptr = detail::to_install_ptr(*first)) | ||
{ | ||
func(*ptr); | ||
} | ||
} | ||
} | ||
|
||
template <typename Range, typename UnaryFunc> | ||
void for_each_to_install(Range&& actions, UnaryFunc&& func) | ||
{ | ||
return for_each_to_install(actions.begin(), actions.end(), std::forward<UnaryFunc>(func)); | ||
} | ||
|
||
namespace detail | ||
{ | ||
template <typename Action> | ||
auto to_omit_ptr(Action& action) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark as previous regarding the template |
||
{ | ||
using PackageInfoPtr = std::conditional_t<std::is_const_v<Action>, const PackageInfo*, PackageInfo*>; | ||
return std::visit( | ||
[](auto& a) -> PackageInfoPtr | ||
{ | ||
using A = std::decay_t<decltype(a)>; | ||
if constexpr (std::is_same_v<A, Solution::Omit>) | ||
{ | ||
return &(a.what); | ||
} | ||
return nullptr; | ||
}, | ||
action | ||
); | ||
} | ||
} | ||
|
||
template <typename Iter, typename UnaryFunc> | ||
void for_each_to_omit(Iter first, Iter last, UnaryFunc&& func) | ||
{ | ||
for (; first != last; ++first) | ||
{ | ||
if (auto* const ptr = detail::to_omit_ptr(*first)) | ||
{ | ||
func(*ptr); | ||
} | ||
} | ||
} | ||
|
||
template <typename Range, typename UnaryFunc> | ||
void for_each_to_omit(Range&& actions, UnaryFunc&& func) | ||
{ | ||
return for_each_to_omit(actions.begin(), actions.end(), std::forward<UnaryFunc>(func)); | ||
} | ||
} | ||
#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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a template here? From the
std::visit
in the implementation, it looks like you are operating on the typeAction
defined hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is not the idiomatic way of doing this, but this was to deduce the constness of Action, used to deduce the constness of the returned pointer.