From c971602b2def753f46d41c9e3b6f2a2d38cdbf68 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Fri, 27 Apr 2018 18:19:43 +0300 Subject: [PATCH] Add remove_if for maps --- include/Utils.h | 13 +++++++++++++ src/RoomList.cc | 11 ++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/include/Utils.h b/include/Utils.h index 5586a479..c3b30084 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -55,6 +55,19 @@ scaleDown(uint64_t max_width, uint64_t max_height, const ImageType &source) final_width, final_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); } +//! Delete items in a container based on a predicate. +template +void +erase_if(ContainerT &items, const PredicateT &predicate) +{ + for (auto it = items.begin(); it != items.end();) { + if (predicate(*it)) + it = items.erase(it); + else + ++it; + } +}; + //! Calculate the Levenshtein distance between two strings with character skipping. int levenshtein_distance(const std::string &s1, const std::string &s2); diff --git a/src/RoomList.cc b/src/RoomList.cc index cfc8fecc..61b3e4bc 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc @@ -183,15 +183,12 @@ RoomList::initialize(const QMap &info) void RoomList::cleanupInvites(const std::map &invites) { - if (invites.empty()) + if (invites.size() == 0) return; - for (auto it = rooms_.begin(); it != rooms_.end();) { - if (it->second->isInvite() && (invites.find(it->first) == invites.end())) - it = rooms_.erase(it); - else - ++it; - } + utils::erase_if(rooms_, [invites](auto &room) { + return room.second->isInvite() && (invites.find(room.first) == invites.end()); + }); } void