Skip to content

Commit

Permalink
Make stable_adapter work with mutable sorters (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Sep 7, 2024
1 parent fa61902 commit 7220f67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/Sorter-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ struct stable_adapter;

Specializations of `stable_adapter` must provide an `is_always_stable` member type aliasing [`std::true_type`][std-true-type]. Additionally, they might expose a `type` member type aliasing either the *adapted sorter* or some intermediate sorter which is guaranteed to always be stable (it can also alias the `stable_adapter` specialization itself, but it is generally useless). Its goal is to provide the least nested type that is known to always be stable in order to sometimes skip some template nesting. When present, this `::type` must be constructible from an instance of the *adapted sorter* (note however that `stable_adapter<T>::type` does not have to be constructible from an instance of `stable_adapter<T>`!).

The main `stable_adapter` template uses [`is_stable`][is-stable] when called to check whether the *adapted sorter* produces a stable sorter when called with a given set of parameters. If the call is already stable then th *adapted sorter* is used directly otherwise `make_stable` is used to artificially turn it into a stable sort.
The main `stable_adapter` template uses [`is_stable`][is-stable] when called to check whether the *adapted sorter* produces a stable sorter when called with a given set of parameters. If the call is already stable then the *adapted sorter* is used directly otherwise `make_stable` is used to artificially turn it into a stable sort.

![Visual explanation of what stable_adapter does](images/stable_adapter.png)

Expand Down
14 changes: 11 additions & 3 deletions include/cpp-sort/adapters/stable_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,20 @@ namespace cppsort
return std::forward<Self>(self).get()(std::forward<Args>(args)...);
}

template<typename... Args>
requires (not is_stable_v<Sorter(Args...)> && std::is_empty_v<Sorter>)
auto operator()(Args&&... args)
-> decltype(make_stable<Sorter>{}(std::forward<Args>(args)...))
{
return make_stable<Sorter>{}(std::forward<Args>(args)...);
}

template<typename Self, typename... Args>
requires (not is_stable_v<Sorter(Args...)>)
requires (not is_stable_v<Sorter(Args...)> && not std::is_empty_v<Sorter>)
auto operator()(this Self&& self, Args&&... args)
-> decltype(make_stable<Sorter>(std::forward<Self>(self).get())(std::forward<Args>(args)...))
-> decltype(make_stable(std::ref(self.get()))(std::forward<Args>(args)...))
{
return make_stable<Sorter>(std::forward<Self>(self).get())(std::forward<Args>(args)...);
return make_stable(std::ref(self.get()))(std::forward<Args>(args)...);
}

////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 7220f67

Please sign in to comment.