summaryrefslogtreecommitdiff
path: root/src/tools/clangpchmanagerbackend/source/projectpartsmanager.cpp
blob: 8333ed96740f32b1013f5400ceba28768c3d59e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "projectpartsmanager.h"

#include <projectpartcontainer.h>

#include <algorithm>

namespace ClangBackEnd {

inline namespace Pch {

ProjectPartContainers ProjectPartsManager::update(ProjectPartContainers &&projectsParts)
{
    auto updatedProjectPartContainers = newProjectParts(std::move(projectsParts));

    mergeProjectParts(updatedProjectPartContainers);

    return updatedProjectPartContainers;
}

void ProjectPartsManager::remove(const Utils::SmallStringVector &ids)
{
    auto shouldRemove = [&] (const ProjectPartContainer &projectPart) {
        return std::find(ids.begin(), ids.end(), projectPart.projectPartId) != ids.end();
    };

    auto newEnd = std::remove_if(m_projectParts.begin(), m_projectParts.end(), shouldRemove);
    m_projectParts.erase(newEnd, m_projectParts.end());
}

ProjectPartContainers ProjectPartsManager::projects(const Utils::SmallStringVector &projectPartIds) const
{
    ProjectPartContainers projectPartsWithIds;

    std::copy_if(m_projectParts.begin(),
                 m_projectParts.end(),
                 std::back_inserter(projectPartsWithIds),
                 [&] (const ProjectPartContainer &projectPart) {
        return std::binary_search(projectPartIds.begin(), projectPartIds.end(), projectPart.projectPartId);
    });

    return projectPartsWithIds;
}

void ProjectPartsManager::updateDeferred(const ProjectPartContainers &deferredProjectsParts)
{
    using ProjectPartContainerReferences = std::vector<std::reference_wrapper<ProjectPartContainer>>;

    ProjectPartContainerReferences deferredProjectPartPointers;
    deferredProjectPartPointers.reserve(deferredProjectsParts.size());

    std::set_intersection(m_projectParts.begin(),
                          m_projectParts.end(),
                          deferredProjectsParts.begin(),
                          deferredProjectsParts.end(),
                          std::back_inserter(deferredProjectPartPointers),
                          [](const ProjectPartContainer &first, const ProjectPartContainer &second) {
                              return first.projectPartId < second.projectPartId;
                          });

    for (ProjectPartContainer &projectPart : deferredProjectPartPointers)
        projectPart.updateIsDeferred = true;
}

ProjectPartContainers ProjectPartsManager::deferredUpdates()
{
    ProjectPartContainers deferredProjectParts;
    deferredProjectParts.reserve(m_projectParts.size());

    std::copy_if(m_projectParts.cbegin(),
                 m_projectParts.cend(),
                 std::back_inserter(deferredProjectParts),
                 [](const ProjectPartContainer &projectPart) { return projectPart.updateIsDeferred; });

    for (ProjectPartContainer &projectPart : m_projectParts)
        projectPart.updateIsDeferred = false;

    return deferredProjectParts;
}

ProjectPartContainers ProjectPartsManager::newProjectParts(ProjectPartContainers &&projectsParts) const
{
    ProjectPartContainers updatedProjectPartContainers;
    updatedProjectPartContainers.reserve(projectsParts.size());

    std::set_difference(std::make_move_iterator(projectsParts.begin()),
                        std::make_move_iterator(projectsParts.end()),
                        m_projectParts.begin(),
                        m_projectParts.end(),
                        std::back_inserter(updatedProjectPartContainers));

    return updatedProjectPartContainers;
}

void ProjectPartsManager::mergeProjectParts(const ProjectPartContainers &projectsParts)
{
    ProjectPartContainers newProjectParts;
    newProjectParts.reserve(m_projectParts.size() + projectsParts.size());

    auto compare = [] (const ProjectPartContainer &first, const ProjectPartContainer &second) {
        return first.projectPartId < second.projectPartId;
    };

    std::set_union(projectsParts.begin(),
                   projectsParts.end(),
                   m_projectParts.begin(),
                   m_projectParts.end(),
                   std::back_inserter(newProjectParts),
                   compare);

    m_projectParts = newProjectParts;
}

const ProjectPartContainers &ProjectPartsManager::projectParts() const
{
    return m_projectParts;
}

} // namespace Pch

} // namespace ClangBackEnd