Changeset View
Changeset View
Standalone View
Standalone View
src/matchessolver.cpp
Show All 38 Lines | |||||
39 | } | 39 | } | ||
40 | 40 | | |||
41 | void MatchesSolver::startMatching() | 41 | void MatchesSolver::startMatching() | ||
42 | { | 42 | { | ||
43 | //Will contain all the sets to be merged | 43 | //Will contain all the sets to be merged | ||
44 | QHash<QString, QSet<QString> > jobsData; | 44 | QHash<QString, QSet<QString> > jobsData; | ||
45 | // has a relation of each person, to know where it is | 45 | // has a relation of each person, to know where it is | ||
46 | QHash<QString, QString> destinationResolver; | 46 | QHash<QString, QString> destinationResolver; | ||
47 | Q_FOREACH (const Match &m, m_matches) { | 47 | for (const Match &m : qAsConst(m_matches)) { | ||
48 | QString urlA = m.indexA.data(PersonsModel::PersonUriRole).toString(); | 48 | QString urlA = m.indexA.data(PersonsModel::PersonUriRole).toString(); | ||
49 | QString urlB = m.indexB.data(PersonsModel::PersonUriRole).toString(); | 49 | QString urlB = m.indexB.data(PersonsModel::PersonUriRole).toString(); | ||
50 | Q_ASSERT(urlA != urlB); | 50 | Q_ASSERT(urlA != urlB); | ||
51 | 51 | | |||
52 | const bool inA = destinationResolver.contains(urlA); | 52 | const bool inA = destinationResolver.contains(urlA); | ||
53 | const bool inB = destinationResolver.contains(urlB); | 53 | const bool inB = destinationResolver.contains(urlB); | ||
54 | if (inA && inB) { | 54 | if (inA && inB) { | ||
55 | //Both are in already, so we have to merge both sets | 55 | //Both are in already, so we have to merge both sets | ||
56 | destinationResolver[urlB] = urlA; | 56 | destinationResolver[urlB] = urlA; | ||
57 | jobsData[urlA] = jobsData.take(urlB); | 57 | jobsData[urlA] = jobsData.take(urlB); | ||
58 | 58 | | |||
59 | //we've put all items pointed to by urlA, to the B set | 59 | //we've put all items pointed to by urlA, to the B set | ||
60 | //now we re-assign anything pointing to B as pointing to A | 60 | //now we re-assign anything pointing to B as pointing to A | ||
61 | //because they are the same | 61 | //because they are the same | ||
62 | QList<QString> keys = destinationResolver.keys(urlB); | 62 | const auto keys = destinationResolver.keys(urlB); | ||
63 | foreach (const QString &key, keys) { | 63 | auto it = keys.constBegin(); | ||
apol: You are calling destinationResolver.keys(urlB) twice here. | |||||
64 | destinationResolver[key] = urlA; | 64 | const auto end = keys.constEnd(); | ||
65 | for (; it != end; ++it) { | ||||
66 | destinationResolver[*it] = urlA; | ||||
pino: iterators, please | |||||
dfaure: `lst` used to be called `keys`, there's no particular reason to rename it. | |||||
65 | } | 67 | } | ||
66 | } else { | 68 | } else { | ||
67 | //if A is in but not B, we want B wherever A is | 69 | //if A is in but not B, we want B wherever A is | ||
68 | if (inA) { | 70 | if (inA) { | ||
69 | qSwap(urlB, urlA); | 71 | qSwap(urlB, urlA); | ||
70 | } | 72 | } | ||
71 | //in case B is anywhere, add A to that set, otherwise just insert B | 73 | //in case B is anywhere, add A to that set, otherwise just insert B | ||
72 | const QString mergeUrl = destinationResolver.value(urlB, urlB); | 74 | const QString mergeUrl = destinationResolver.value(urlB, urlB); | ||
73 | 75 | | |||
74 | QSet<QString> &jobs = jobsData[mergeUrl]; | 76 | QSet<QString> &jobs = jobsData[mergeUrl]; | ||
75 | jobs.insert(urlB); | 77 | jobs.insert(urlB); | ||
76 | jobs.insert(urlA); | 78 | jobs.insert(urlA); | ||
77 | 79 | | |||
78 | //remember where urlA and urlB are | 80 | //remember where urlA and urlB are | ||
79 | Q_ASSERT(urlA != mergeUrl); | 81 | Q_ASSERT(urlA != mergeUrl); | ||
80 | destinationResolver.insert(urlA, mergeUrl); | 82 | destinationResolver.insert(urlA, mergeUrl); | ||
81 | if (urlB != mergeUrl) { | 83 | if (urlB != mergeUrl) { | ||
82 | destinationResolver.insert(urlB, mergeUrl); | 84 | destinationResolver.insert(urlB, mergeUrl); | ||
83 | } | 85 | } | ||
84 | } | 86 | } | ||
85 | } | 87 | } | ||
86 | 88 | | |||
87 | Q_FOREACH (const QSet<QString> &uris, jobsData) { | 89 | for (const QSet<QString> &uris : qAsConst(jobsData)) { | ||
88 | if (PersonManager::instance()->mergeContacts(uris.toList()).isEmpty()) { | 90 | if (PersonManager::instance()->mergeContacts(uris.toList()).isEmpty()) { | ||
89 | qCWarning(KPEOPLE_LOG) << "error: failing to merge contacts: " << uris; | 91 | qCWarning(KPEOPLE_LOG) << "error: failing to merge contacts: " << uris; | ||
90 | } | 92 | } | ||
91 | } | 93 | } | ||
92 | emitResult(); | 94 | emitResult(); | ||
93 | } | 95 | } |
You are calling destinationResolver.keys(urlB) twice here.