diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2012-02-26 14:59:24 +0100 |
---|---|---|
committer | Erik Schilling <ablu.erikschilling@googlemail.com> | 2012-02-26 17:11:53 +0100 |
commit | 80512086d4e153e883e1cc972bc3240f3cea0891 (patch) | |
tree | 8d8e30ab7958d84dd94a000591c626517ce495b4 /src | |
parent | 3629aebb96959afc56cf04d1f2fc4a9f03e94183 (diff) | |
download | manaserv-80512086d4e153e883e1cc972bc3240f3cea0891.tar.gz manaserv-80512086d4e153e883e1cc972bc3240f3cea0891.tar.bz2 manaserv-80512086d4e153e883e1cc972bc3240f3cea0891.tar.xz manaserv-80512086d4e153e883e1cc972bc3240f3cea0891.zip |
Renamed some iterators + fixed one TODO (stored guilds in a map)
Reviewed-by: bjorn.
Diffstat (limited to 'src')
-rw-r--r-- | src/chat-server/guildmanager.cpp | 63 | ||||
-rw-r--r-- | src/chat-server/guildmanager.h | 6 |
2 files changed, 23 insertions, 46 deletions
diff --git a/src/chat-server/guildmanager.cpp b/src/chat-server/guildmanager.cpp index 1aa24251..e70134e9 100644 --- a/src/chat-server/guildmanager.cpp +++ b/src/chat-server/guildmanager.cpp @@ -36,12 +36,11 @@ GuildManager::GuildManager() GuildManager::~GuildManager() { - for (std::list<Guild*>::iterator itr = mGuilds.begin(); - itr != mGuilds.end(); ++itr) + for (std::map<int, Guild*>::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<int>::iterator itr = mOwners.begin(); - std::list<int>::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<Guild*>::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<int, Guild*>::const_iterator it = mGuilds.find(id); + return it == mGuilds.end() ? 0 : *it; } Guild *GuildManager::findByName(const std::string &name) const { - for (std::list<Guild*>::const_iterator itr = mGuilds.begin(), - itr_end = mGuilds.end(); - itr != itr_end; ++itr) + for (std::map<int, Guild*>::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<Guild*> GuildManager::getGuildsForPlayer(int playerId) const { std::vector<Guild*> guildList; - for (std::list<Guild*>::const_iterator itr = mGuilds.begin(); - itr != mGuilds.end(); ++itr) + for (std::list<Guild*>::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<Guild*> guildList = getGuildsForPlayer(player->characterId); - for (std::vector<Guild*>::const_iterator itr = guildList.begin(); - itr != guildList.end(); ++itr) + for (std::vector<Guild*>::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<int>::const_iterator itr = mOwners.begin(); - std::list<int>::const_iterator itr_end = mOwners.end(); + std::list<int>::const_iterator it = mOwners.begin(); + std::list<int>::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 b236b75c..565504ef 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>b_lindeijer:</b> Since this method is used so often, its - * efficiency should be improved, probably by storing the guilds - * in a map<int,Guild*> instead of list<Guild*>. - * * @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<Guild*> mGuilds; + std::map<int, Guild*> mGuilds; std::list<int> mOwners; }; |