list/sort: use a comparator for is_sorted

This commit is contained in:
Danny Robson 2019-09-12 13:33:32 +10:00
parent b0bf064fdd
commit 8adac4604c

View File

@ -196,10 +196,11 @@ namespace cruft::list {
/// Tests if a singly linked list is sorted. /// Tests if a singly linked list is sorted.
template <typename NodeT> template <typename NodeT, typename ComparatorT>
bool bool
is_sorted [[gnu::nonnull]] ( is_sorted [[gnu::nonnull]] (
NodeT const *head NodeT const *head,
ComparatorT &&cmp
) { ) {
if (!next (head)) if (!next (head))
return true; return true;
@ -208,13 +209,22 @@ namespace cruft::list {
auto b = next (head); auto b = next (head);
while (b) { while (b) {
if (b->data < a->data) if (cmp (b, a))
return false; return false;
a = next (a); a = b;
b = next (b); b = next (b);
} }
return true; return true;
} }
template <typename NodeT>
bool
is_sorted [[gnu::nonnull]] (
NodeT const *head
) {
return is_sorted (head, node::value_comparator {std::less<> {}});
};
} }