From 80512086d4e153e883e1cc972bc3240f3cea0891 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Sun, 26 Feb 2012 14:59:24 +0100 Subject: Renamed some iterators + fixed one TODO (stored guilds in a map) Reviewed-by: bjorn. --- src/chat-server/guildmanager.cpp | 63 ++++++++++++++-------------------------- src/chat-server/guildmanager.h | 6 +--- 2 files changed, 23 insertions(+), 46 deletions(-) (limited to 'src/chat-server') diff --git a/src/chat-server/guildmanager.cpp b/src/chat-server/guildmanager.cpp index 1aa2425..e70134e 100644 --- a/src/chat-server/guildmanager.cpp +++ b/src/chat-server/guildmanager.cpp @@ -36,12 +36,11 @@ GuildManager::GuildManager() GuildManager::~GuildManager() { - for (std::list::iterator itr = mGuilds.begin(); - itr != mGuilds.end(); ++itr) + for (std::map::iterator it = mGuilds.begin(); + it != mGuilds.end(); ++it) { - delete *itr; + delete *it; } - mGuilds.clear(); } Guild* GuildManager::createGuild(const std::string &name, int playerId) @@ -89,40 +88,22 @@ void GuildManager::removeGuildMember(Guild *guild, int playerId) if (guild->memberCount() == 0) removeGuild(guild); - // remove the user from owners list - std::list::iterator itr = mOwners.begin(); - std::list::iterator itr_end = mOwners.end(); - while (itr != itr_end) - { - if ((*itr) == playerId) - { - mOwners.remove(playerId); - break; - } - ++itr; - } + mOwners.remove(playerId); } Guild *GuildManager::findById(short id) const { - for (std::list::const_iterator itr = mGuilds.begin(), - itr_end = mGuilds.end(); - itr != itr_end; ++itr) - { - Guild *guild = (*itr); - if (guild->getId() == id) - return guild; - } - return 0; + std::map::const_iterator it = mGuilds.find(id); + return it == mGuilds.end() ? 0 : *it; } Guild *GuildManager::findByName(const std::string &name) const { - for (std::list::const_iterator itr = mGuilds.begin(), - itr_end = mGuilds.end(); - itr != itr_end; ++itr) + for (std::map::const_iterator it = mGuilds.begin(), + it_end = mGuilds.end(); + it != it_end; ++it) { - Guild *guild = (*itr); + Guild *guild = *it; if (guild->getName() == name) return guild; } @@ -138,12 +119,12 @@ std::vector GuildManager::getGuildsForPlayer(int playerId) const { std::vector guildList; - for (std::list::const_iterator itr = mGuilds.begin(); - itr != mGuilds.end(); ++itr) + for (std::list::const_iterator it = mGuilds.begin(); + it != mGuilds.end(); ++it) { - if ((*itr)->checkInGuild(playerId)) + if ((*it)->checkInGuild(playerId)) { - guildList.push_back((*itr)); + guildList.push_back(*it); } } return guildList; @@ -153,10 +134,10 @@ void GuildManager::disconnectPlayer(ChatClient *player) { std::vector guildList = getGuildsForPlayer(player->characterId); - for (std::vector::const_iterator itr = guildList.begin(); - itr != guildList.end(); ++itr) + for (std::vector::const_iterator it = guildList.begin(); + it != guildList.end(); ++it) { - chatHandler->sendGuildListUpdate((*itr)->getName(), + chatHandler->sendGuildListUpdate((*it)->getName(), player->characterName, GUILD_EVENT_OFFLINE_PLAYER); } @@ -182,14 +163,14 @@ int GuildManager::changeMemberLevel(ChatClient *player, Guild *guild, bool GuildManager::alreadyOwner(int playerId) const { - std::list::const_iterator itr = mOwners.begin(); - std::list::const_iterator itr_end = mOwners.end(); + std::list::const_iterator it = mOwners.begin(); + std::list::const_iterator it_end = mOwners.end(); - while (itr != itr_end) + while (it != it_end) { - if ((*itr) == playerId) + if (*it == playerId) return true; - ++itr; + ++it; } return false; diff --git a/src/chat-server/guildmanager.h b/src/chat-server/guildmanager.h index b236b75..565504e 100644 --- a/src/chat-server/guildmanager.h +++ b/src/chat-server/guildmanager.h @@ -60,10 +60,6 @@ class GuildManager /** * Returns the guild with the given id. O(n) * - * @todo b_lindeijer: Since this method is used so often, its - * efficiency should be improved, probably by storing the guilds - * in a map instead of list. - * * @return the guild with the given id, or NULL if it doesn't exist */ Guild *findById(short id) const; @@ -110,7 +106,7 @@ class GuildManager void setUserRights(Guild *guild, int playerId, int rights); private: - std::list mGuilds; + std::map mGuilds; std::list mOwners; }; -- cgit