list/sort: use a comparator for is_sorted
This commit is contained in:
parent
b0bf064fdd
commit
8adac4604c
@ -196,10 +196,11 @@ namespace cruft::list {
|
||||
|
||||
|
||||
/// Tests if a singly linked list is sorted.
|
||||
template <typename NodeT>
|
||||
template <typename NodeT, typename ComparatorT>
|
||||
bool
|
||||
is_sorted [[gnu::nonnull]] (
|
||||
NodeT const *head
|
||||
NodeT const *head,
|
||||
ComparatorT &&cmp
|
||||
) {
|
||||
if (!next (head))
|
||||
return true;
|
||||
@ -208,13 +209,22 @@ namespace cruft::list {
|
||||
auto b = next (head);
|
||||
|
||||
while (b) {
|
||||
if (b->data < a->data)
|
||||
if (cmp (b, a))
|
||||
return false;
|
||||
|
||||
a = next (a);
|
||||
a = b;
|
||||
b = next (b);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template <typename NodeT>
|
||||
bool
|
||||
is_sorted [[gnu::nonnull]] (
|
||||
NodeT const *head
|
||||
) {
|
||||
return is_sorted (head, node::value_comparator {std::less<> {}});
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user