diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-09-30 01:51:01 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-09-30 01:51:01 +0200 |
commit | a158d1f367759bf0c110b2fdb21d7c00f5a9cea6 (patch) | |
tree | 9367dcc01f31271bb79d39ee692ee51880198573 /src/Common/UserLists | |
parent | 1b3a914d5fdf5a578e24754845c6350c59c0b523 (diff) | |
download | mad-a158d1f367759bf0c110b2fdb21d7c00f5a9cea6.tar mad-a158d1f367759bf0c110b2fdb21d7c00f5a9cea6.zip |
UserListDiff: Extended diffs to handle user renames; fixed a XmlData::List bug
Diffstat (limited to 'src/Common/UserLists')
-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 |
3 files changed, 33 insertions, 61 deletions
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; |