Skip to content

Commit

Permalink
Refactor|C++11|libappfw: Sorting data items with lambda functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 1, 2016
1 parent ae846bd commit 7c92586
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
4 changes: 3 additions & 1 deletion doomsday/sdk/libappfw/include/de/framework/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class LIBAPPFW_PUBLIC Data

virtual void sort(SortMethod method = Ascending);

typedef bool (*LessThanFunc)(Item const &, Item const &);
typedef std::function<bool (Item const &, Item const &)> LessThanFunc;

virtual void sort(LessThanFunc func) = 0;

Expand All @@ -131,6 +131,8 @@ class LIBAPPFW_PUBLIC Data
*/
virtual dsize size() const = 0;

LoopResult forAll(std::function<LoopResult (Item &)> func);

LoopResult forAll(std::function<LoopResult (Item const &)> func) const;

private:
Expand Down
30 changes: 17 additions & 13 deletions doomsday/sdk/libappfw/src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/ui/Data"
Expand All @@ -27,16 +27,6 @@ namespace ui {

dsize const Data::InvalidPos = dsize(-1);

static bool itemLessThan(Item const &a, Item const &b)
{
return a.sortKey().compareWithoutCase(b.sortKey()) < 0;
}

static bool itemGreaterThan(Item const &a, Item const &b)
{
return a.sortKey().compareWithoutCase(b.sortKey()) > 0;
}

DENG2_PIMPL_NOREF(Data)
{
DENG2_PIMPL_AUDIENCE(Addition)
Expand All @@ -56,15 +46,29 @@ void Data::sort(SortMethod method)
switch(method)
{
case Ascending:
sort(itemLessThan);
sort([] (Item const &a, Item const &b) {
return a.sortKey().compareWithoutCase(b.sortKey()) < 0;
});
break;

case Descending:
sort(itemGreaterThan);
sort([] (Item const &a, Item const &b) {
return a.sortKey().compareWithoutCase(b.sortKey()) > 0;
});
break;
}
}

LoopResult Data::forAll(std::function<LoopResult (Item &)> func)
{
for(DataPos pos = 0; pos < size(); ++pos)
{
if(auto result = func(at(pos)))
return result;
}
return LoopContinue;
}

LoopResult Data::forAll(std::function<LoopResult (Item const &)> func) const
{
for(DataPos pos = 0; pos < size(); ++pos)
Expand Down
12 changes: 8 additions & 4 deletions doomsday/sdk/libappfw/src/listdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/ui/ListData"
Expand Down Expand Up @@ -76,7 +76,7 @@ Data &ListData::clear()
}

Data &ListData::insert(Pos pos, Item *item)
{
{
_items.insert(pos, item);
item->setDataContext(*this);

Expand Down Expand Up @@ -120,7 +120,9 @@ struct ListItemSorter {

void ListData::sort(LessThanFunc lessThan)
{
qSort(_items.begin(), _items.end(), ListItemSorter(lessThan));
qSort(_items.begin(), _items.end(), [&lessThan] (Item const *a, Item const *b) {
return lessThan(*a, *b);
});

// Notify.
DENG2_FOR_AUDIENCE2(OrderChange, i)
Expand All @@ -131,7 +133,9 @@ void ListData::sort(LessThanFunc lessThan)

void ListData::stableSort(LessThanFunc lessThan)
{
qStableSort(_items.begin(), _items.end(), ListItemSorter(lessThan));
qStableSort(_items.begin(), _items.end(), [&lessThan] (Item const *a, Item const *b) {
return lessThan(*a, *b);
});

// Notify.
DENG2_FOR_AUDIENCE2(OrderChange, i)
Expand Down

0 comments on commit 7c92586

Please sign in to comment.