Commit fba7dcdb authored by Daniel Teske's avatar Daniel Teske

ProjectExplorer: Introduce compareSortedLists

Which compares two sorted lists and returns a diff between them.

Change-Id: I278bd43f1bd999bae6575cbf38cddbdf3ff82418
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent f7caf3ef
......@@ -420,38 +420,12 @@ void CMakeProject::buildTree(CMakeProjectNode *rootNode, QList<ProjectExplorer::
qSort(oldList.begin(), oldList.end(), sortNodesByPath);
qSort(newList.begin(), newList.end(), sortNodesByPath);
// generate added and deleted list
QList<ProjectExplorer::FileNode *>::const_iterator oldIt = oldList.constBegin();
QList<ProjectExplorer::FileNode *>::const_iterator oldEnd = oldList.constEnd();
QList<ProjectExplorer::FileNode *>::const_iterator newIt = newList.constBegin();
QList<ProjectExplorer::FileNode *>::const_iterator newEnd = newList.constEnd();
QList<ProjectExplorer::FileNode *> added;
QList<ProjectExplorer::FileNode *> deleted;
while (oldIt != oldEnd && newIt != newEnd) {
if ( (*oldIt)->path() == (*newIt)->path()) {
delete *newIt;
++oldIt;
++newIt;
} else if ((*oldIt)->path() < (*newIt)->path()) {
deleted.append(*oldIt);
++oldIt;
} else {
added.append(*newIt);
++newIt;
}
}
ProjectExplorer::compareSortedLists(oldList, newList, deleted, added, sortNodesByPath);
while (oldIt != oldEnd) {
deleted.append(*oldIt);
++oldIt;
}
while (newIt != newEnd) {
added.append(*newIt);
++newIt;
}
qDeleteAll(ProjectExplorer::subtractSortedList(newList, added, sortNodesByPath));
// add added nodes
foreach (ProjectExplorer::FileNode *fn, added) {
......
......@@ -36,6 +36,7 @@
#include <QObject>
#include <QStringList>
#include <QDebug>
QT_BEGIN_NAMESPACE
class QFileInfo;
......@@ -343,6 +344,102 @@ private:
friend class Node;
};
template<class T1, class T3>
bool isSorted(const T1 &list, T3 sorter)
{
typename T1::const_iterator it, iit, end;
end = list.constEnd();
it = list.constBegin();
if (it == end)
return true;
iit = list.constBegin();
++iit;
while (iit != end) {
if (!sorter(*it, *iit))
return false;
it = iit++;
}
return true;
}
template <class T1, class T2, class T3>
void compareSortedLists(T1 oldList, T2 newList, T1 &removedList, T2 &addedList, T3 sorter)
{
Q_ASSERT(isSorted(oldList, sorter));
Q_ASSERT(isSorted(newList, sorter));
typename T1::const_iterator oldIt, oldEnd;
typename T2::const_iterator newIt, newEnd;
oldIt = oldList.constBegin();
oldEnd = oldList.constEnd();
newIt = newList.constBegin();
newEnd = newList.constEnd();
while (oldIt != oldEnd && newIt != newEnd) {
if (sorter(*oldIt, *newIt)) {
removedList.append(*oldIt);
++oldIt;
} else if (sorter(*newIt, *oldIt)) {
addedList.append(*newIt);
++newIt;
} else {
++oldIt;
++newIt;
}
}
while (oldIt != oldEnd) {
removedList.append(*oldIt);
++oldIt;
}
while (newIt != newEnd) {
addedList.append(*newIt);
++newIt;
}
}
template <class T1, class T3>
T1 subtractSortedList(T1 list1, T1 list2, T3 sorter)
{
Q_ASSERT(ProjectExplorer::isSorted(list1, sorter));
Q_ASSERT(ProjectExplorer::isSorted(list2, sorter));
typename T1::const_iterator list1It, list1End;
typename T1::const_iterator list2It, list2End;
list1It = list1.constBegin();
list1End = list1.constEnd();
list2It = list2.constBegin();
list2End = list2.constEnd();
T1 result;
while (list1It != list1End && list2It != list2End) {
if (sorter(*list1It, *list2It)) {
result.append(*list1It);
++list1It;
} else if (sorter(*list2It, *list1It)) {
qWarning() << "subtractSortedList: subtracting value that isn't in set";
} else {
++list1It;
++list2It;
}
}
while (list1It != list1End) {
result.append(*list1It);
++list1It;
}
return result;
}
} // namespace ProjectExplorer
#endif // PROJECTNODES_H
......@@ -100,10 +100,18 @@ static const FileTypeDataStorage fileTypeDataStorage[] = {
":/qmakeprojectmanager/images/unknown.png" }
};
bool sortNodesByPath(ProjectExplorer::Node *a, ProjectExplorer::Node *b)
class SortByPath
{
return a->path() < b->path();
}
public:
bool operator()(ProjectExplorer::Node *a, ProjectExplorer::Node *b)
{ return operator()(a->path(), b->path()); }
bool operator()(ProjectExplorer::Node *a, const QString &b)
{ return operator()(a->path(), b); }
bool operator()(const QString &a, ProjectExplorer::Node *b)
{ return operator()(a, b->path()); }
bool operator()(const QString &a, const QString &b)
{ return a < b; }
};
class QmakeNodeStaticData {
public:
......@@ -484,39 +492,20 @@ struct InternalNode
}
QList<FileNode*> filesToRemove;
QList<FileNode*> filesToAdd;
qSort(files);
qSort(existingFileNodes.begin(), existingFileNodes.end(), sortNodesByPath);
QList<FileNode*>::const_iterator existingNodeIter = existingFileNodes.constBegin();
QList<QString>::const_iterator newPathIter = files.constBegin();
while (existingNodeIter != existingFileNodes.constEnd()
&& newPathIter != files.constEnd()) {
if ((*existingNodeIter)->path() < *newPathIter) {
filesToRemove << *existingNodeIter;
++existingNodeIter;
} else if ((*existingNodeIter)->path() > *newPathIter) {
filesToAdd << new ProjectExplorer::FileNode(*newPathIter, type, false);
++newPathIter;
} else { // *existingNodeIter->path() == *newPathIter
++existingNodeIter;
++newPathIter;
}
}
while (existingNodeIter != existingFileNodes.constEnd()) {
filesToRemove << *existingNodeIter;
++existingNodeIter;
}
while (newPathIter != files.constEnd()) {
filesToAdd << new ProjectExplorer::FileNode(*newPathIter, type, false);
++newPathIter;
}
QStringList filesToAdd;
SortByPath sortByPath;
qSort(files.begin(), files.end(), sortByPath);
qSort(existingFileNodes.begin(), existingFileNodes.end(), sortByPath);
ProjectExplorer::compareSortedLists(existingFileNodes, files, filesToRemove, filesToAdd, sortByPath);
QList<FileNode *> nodesToAdd;
foreach (const QString &file, filesToAdd)
nodesToAdd << new ProjectExplorer::FileNode(file, type, false);
if (!filesToRemove.isEmpty())
folder->removeFileNodes(filesToRemove);
if (!filesToAdd.isEmpty())
folder->addFileNodes(filesToAdd);
folder->removeFileNodes(filesToRemove);
folder->addFileNodes(nodesToAdd);
}
};
}
......@@ -1818,10 +1807,11 @@ void QmakeProFileNode::applyEvaluate(EvalResult evalResult, bool async)
}
}
SortByPath sortByPath;
qSort(existingProjectNodes.begin(), existingProjectNodes.end(),
sortNodesByPath);
qSort(newProjectFilesExact);
qSort(newProjectFilesCumlative);
sortByPath);
qSort(newProjectFilesExact.begin(), newProjectFilesExact.end(), sortByPath);
qSort(newProjectFilesCumlative.begin(), newProjectFilesCumlative.end(), sortByPath);
QList<ProjectNode*> toAdd;
QList<ProjectNode*> toRemove;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment