diff --git a/headers/private/kernel/util/DoublyLinkedList.h b/headers/private/kernel/util/DoublyLinkedList.h index 23216894d3..f796bfa3ea 100644 --- a/headers/private/kernel/util/DoublyLinkedList.h +++ b/headers/private/kernel/util/DoublyLinkedList.h @@ -365,6 +365,10 @@ public: inline int32 Count() const; // O(n)! + template + void Sort(const Less& less); + // O(n^2) + inline Iterator GetIterator() { return Iterator(this); } inline ConstIterator GetIterator() const { return ConstIterator(this); } @@ -645,6 +649,31 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Count() const return count; } + +DOUBLY_LINKED_LIST_TEMPLATE_LIST +template +void +DOUBLY_LINKED_LIST_CLASS_NAME::Sort(const Less& less) +{ + // selection sort + Element* tail = Head(); + while (tail != NULL) { + Element* leastElement = tail; + Element* element = tail; + while ((element = GetNext(element)) != NULL) { + if (less(element, leastElement)) + leastElement = element; + } + + if (leastElement != tail) { + Remove(leastElement); + InsertBefore(tail, leastElement); + } else + tail = GetNext(tail); + } +} + + // sGetLink DOUBLY_LINKED_LIST_TEMPLATE_LIST GetLink DOUBLY_LINKED_LIST_CLASS_NAME::sGetLink;