From fdcff7183b035a27ff960f347b9975ab8e67a3cd Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 9 Nov 2010 12:43:23 +0000 Subject: optimized ui:ListView::sort(): replaced bubblesort routine with std::list::sort() --- src/ui/listview.cc | 30 ++++++++++++++++-------------- src/ui/listview.h | 6 +++++- 2 files changed, 21 insertions(+), 15 deletions(-) (limited to 'src/ui') diff --git a/src/ui/listview.cc b/src/ui/listview.cc index 70ecf8f..32d86dd 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -97,22 +97,24 @@ bool ListView::on_emit(Widget *sender, const Event event, void *data) return false; } -void ListView::sort() +bool compare_listitems(const Widget *first, const Widget *second) { - // bubble sort - there's a reason for using it here - for (Children::iterator low = children().begin(); low != children().end(); low++) { - Children::iterator high = low; - for (high++; high != children().end(); high++) { - ListItem *lowitem = dynamic_cast(*low); - ListItem *highitem = dynamic_cast(*high); - assert(lowitem && highitem); - if (highitem->sortkey() < lowitem->sortkey()) { - Widget *t = (*low); - (*low) = (*high); - (*high) = t; - } - } + const ListItem *firstitem = dynamic_cast(first); + const ListItem *seconditem = dynamic_cast(second); + assert(firstitem && seconditem); + + if (firstitem->sortkey() < seconditem->sortkey()) { + return true; + } else if (firstitem->sortkey() > seconditem->sortkey()) { + return false; + } else { + return (firstitem->sortkey().length() < seconditem->sortkey().length()); } } +void ListView::sort() +{ + children().sort(compare_listitems); +} + } diff --git a/src/ui/listview.h b/src/ui/listview.h index f752ac2..81dd5a5 100644 --- a/src/ui/listview.h +++ b/src/ui/listview.h @@ -59,7 +59,11 @@ public: /// set selection to nothing void deselect(); - /// sort child ListItems according to their sortkey + /** + * @brief sort child ListItems according to their sortkey + * Sort will fail with an assert error if the ListView + * contains any non-ListItem child widgets + */ void sort(); protected: -- cgit v1.2.3