diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Common/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Common/UserLists/UserListDiff.cpp | 46 | ||||
-rw-r--r-- | src/Common/UserLists/UserListDiff.h | 27 | ||||
-rw-r--r-- | src/Common/UserLists/Util.cpp | 21 | ||||
-rw-r--r-- | src/Common/XmlData.h | 8 |
5 files changed, 38 insertions, 66 deletions
diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index d82c0d9..18b8901 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -19,7 +19,7 @@ mad_library(Common Requests/StatusRequest.h UserLists/UserList.cpp UserLists/UserList.h - UserLists/UserListDiff.cpp UserLists/UserListDiff.h + UserLists/UserListDiff.h UserLists/UserListEntry.h UserLists/Util.cpp UserLists/Util.h diff --git a/src/Common/UserLists/UserListDiff.cpp b/src/Common/UserLists/UserListDiff.cpp deleted file mode 100644 index e763afe..0000000 --- a/src/Common/UserLists/UserListDiff.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * UserListDiff.cpp - * - * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty 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/>. - */ - -#include "UserListDiff.h" -#include "UserList.h" - -namespace Mad { -namespace Common { -namespace UserLists { - -UserListDiff::UserListDiff(const UserList *oldList, const UserList *newList) { - for(UserList::const_iterator user = oldList->begin(); user != oldList->end(); ++user) - deletedUsers.insert(*user); - - for(UserList::const_iterator user = newList->begin(); user != newList->end(); ++user) { - std::set<UserListEntry>::const_iterator it = deletedUsers.find(*user); - - if(it != deletedUsers.end()) { - deletedUsers.erase(it); - unchangedUsers.insert(*user); - } - else { - addedUsers.insert(*user); - } - } -} - -} -} -} diff --git a/src/Common/UserLists/UserListDiff.h b/src/Common/UserLists/UserListDiff.h index 41b8e81..5000adf 100644 --- a/src/Common/UserLists/UserListDiff.h +++ b/src/Common/UserLists/UserListDiff.h @@ -25,6 +25,7 @@ #include "UserListEntry.h" #include <Core/Exception.h> +#include <boost/bimap.hpp> #include <set> namespace Mad { @@ -34,14 +35,16 @@ namespace UserLists { class UserList; class MAD_COMMON_EXPORT UserListDiff { + public: + typedef boost::bimap<boost::bimaps::set_of<UserListEntry>, boost::bimaps::set_of<UserListEntry> > EntryListMap; + private: std::set<UserListEntry> addedUsers; std::set<UserListEntry> deletedUsers; - std::set<UserListEntry> unchangedUsers; + EntryListMap updatedUsers; public: UserListDiff() {} - UserListDiff(const UserList *oldList, const UserList *newList); void invert() { addedUsers.swap(deletedUsers); @@ -55,29 +58,35 @@ class MAD_COMMON_EXPORT UserListDiff { return deletedUsers; } - const std::set<UserListEntry>& getUnchangedUsers() const { - return unchangedUsers; + const EntryListMap& getUpdatedUsers() const { + return updatedUsers; } bool insertAddedUser(UserListEntry user) { - if(deletedUsers.count(user) || unchangedUsers.count(user)) + if(updatedUsers.right.count(user)) return false; return addedUsers.insert(user).second; } bool insertDeletedUser(UserListEntry user) { - if(addedUsers.count(user) || unchangedUsers.count(user)) + if(updatedUsers.left.count(user)) return false; return deletedUsers.insert(user).second; } - bool insertUnchangedUser(UserListEntry user) { - if(addedUsers.count(user) || deletedUsers.count(user)) + bool insertUpdatedUser(UserListEntry oldUser, UserListEntry newUser) { + if(deletedUsers.count(oldUser) || addedUsers.count(newUser)) + return false; + + // this won't allow exchanging two users, but that's ok for now... + if(updatedUsers.left.count(oldUser) || updatedUsers.right.count(newUser)) + return false; + if(updatedUsers.right.count(oldUser) || updatedUsers.left.count(newUser)) return false; - return unchangedUsers.insert(user).second; + return updatedUsers.left.insert(std::make_pair(oldUser, newUser)).second; } }; diff --git a/src/Common/UserLists/Util.cpp b/src/Common/UserLists/Util.cpp index cf04218..483383e 100644 --- a/src/Common/UserLists/Util.cpp +++ b/src/Common/UserLists/Util.cpp @@ -77,9 +77,13 @@ void Util::serializeUserListDiff(const UserListDiff *diff, XmlData *data) { for(std::set<UserListEntry>::const_iterator user = diff->getDeletedUsers().begin(); user != diff->getDeletedUsers().end(); ++user) serializeUserListEntry(&*user, userList->addEntry()); - userList = data->createList("unchangedUsers"); - for(std::set<UserListEntry>::const_iterator user = diff->getUnchangedUsers().begin(); user != diff->getUnchangedUsers().end(); ++user) - serializeUserListEntry(&*user, userList->addEntry()); + userList = data->createList("updatedUsers"); + const UserListDiff::EntryListMap::left_map &updatedUsers = diff->getUpdatedUsers().left; + for(UserListDiff::EntryListMap::left_const_iterator user = updatedUsers.begin(); user != updatedUsers.end(); ++user) { + XmlData::List *entryList = userList->addEntry()->createList("entry"); + serializeUserListEntry(&user->first, entryList->addEntry()); + serializeUserListEntry(&user->second, entryList->addEntry()); + } } boost::shared_ptr<UserListDiff> Util::deserializeUserListDiff(const XmlData *data) { @@ -97,10 +101,15 @@ boost::shared_ptr<UserListDiff> Util::deserializeUserListDiff(const XmlData *dat diff->insertDeletedUser(deserializeUserListEntry(user)); } - userList = data->getList("unchangedUsers"); + userList = data->getList("updatedUsers"); if(userList) { - for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) - diff->insertUnchangedUser(deserializeUserListEntry(user)); + for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) { + const XmlData::List *entryList = user->getList("entry"); + if(entryList->getSize() != 2) + continue; + + diff->insertUpdatedUser(deserializeUserListEntry(entryList->begin()), deserializeUserListEntry(entryList->begin()+1)); + } } return diff; diff --git a/src/Common/XmlData.h b/src/Common/XmlData.h index 4f566fa..4028508 100644 --- a/src/Common/XmlData.h +++ b/src/Common/XmlData.h @@ -342,11 +342,11 @@ class MAD_COMMON_EXPORT XmlData { } iterator_base operator+(const difference_type &n) const { - return iterator(it+n); + return iterator_base(it+n); } iterator_base operator++(int) { - return iterator(it++); + return iterator_base(it++); } iterator_base& operator++() { @@ -360,11 +360,11 @@ class MAD_COMMON_EXPORT XmlData { } iterator_base operator-(const difference_type &n) const { - return iterator(it-n); + return iterator_base(it-n); } iterator_base operator--(int) { - return iterator(it--); + return iterator_base(it--); } iterator_base& operator--() { |