• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphia-app / graphia / 42536896007

08 Nov 2026 11:26PM UTC coverage: 71.592% (-0.02%) from 71.608%
42536896007

push

github

timangus
CLAUDE: Run cppcheck at the exhaustive check level

11777 of 18026 branches covered (65.33%)

Branch coverage included in aggregate %.

29982 of 40303 relevant lines covered (74.39%)

408140.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.44
/source/app/graph/componentmanager.cpp
1
/* Copyright © 2013-2025 Tim Angus
2
 * Copyright © 2013-2025 Tom Freeman
3
 *
4
 * This file is part of Graphia.
5
 *
6
 * Graphia is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Graphia is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with Graphia.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
#include "componentmanager.h"
21

22
#include "shared/utils/thread.h"
23
#include "shared/graph/elementid_debug.h"
24

25
#include "graph.h"
26
#include "graphcomponent.h"
27

28
#include <QDebug>
29

30
#include <algorithm>
31
#include <chrono>
32
#include <iterator>
33
#include <vector>
34

35
ComponentManager::ComponentManager(Graph& graph,
36
                                   const NodeConditionFn& nodeFilter,
37
                                   const EdgeConditionFn& edgeFilter) :
38
    _nextComponentId(0),
2,092 ✔
39
    _nodesComponentId(graph),
2,092 ✔
40
    _edgesComponentId(graph),
2,092 ✔
41
    _debug(qEnvironmentVariableIntValue("COMPONENTS_DEBUG"))
2,092 ✔
42
{
2,092 ✔
43
    // Ignore all multi-elements
44
    addNodeFilter([&graph](NodeId nodeId) { return graph.typeOf(nodeId) == MultiElementType::Tail; });
87,786 ✔
45
    addEdgeFilter([&graph](EdgeId edgeId) { return graph.typeOf(edgeId) == MultiElementType::Tail; });
83,916 ✔
46

47
    if(nodeFilter)
2,092 !
48
        addNodeFilter(nodeFilter);
×
49

50
    if(edgeFilter)
2,092 ✔
51
        addEdgeFilter(edgeFilter);
72 ✔
52

53
    connect(&graph, &Graph::graphChanged, this, &ComponentManager::onGraphChanged, Qt::DirectConnection);
2,092 ✔
54

55
    graph.update();
2,092 ✔
56
    update(&graph);
2,092 ✔
57
}
2,092 ✔
58

59
ComponentManager::~ComponentManager()
60
{
2,092 ✔
61
    // Let the ComponentArrays know that we're going away
62
    for(auto* componentArray : _componentArrays)
2,092 !
63
        componentArray->invalidate();
×
64
}
2,092 ✔
65

66
ComponentIdSet ComponentManager::assignConnectedElementsComponentId(const Graph* graph,
67
        NodeId rootId, ComponentId componentId,
68
        NodeArray<ComponentId>& nodesComponentId,
69
        EdgeArray<ComponentId>& edgesComponentId)
70
{
5,328 ✔
71
    std::vector<NodeId> nodeIds{rootId};
5,328 ✔
72
    ComponentIdSet oldComponentIdsAffected;
5,328 ✔
73

74
    for(size_t head = 0; head < nodeIds.size(); head++)
34,390 ✔
75
    {
29,062 ✔
76
        auto nodeId = nodeIds[head];
29,062 ✔
77
        oldComponentIdsAffected.insert(_nodesComponentId.at(nodeId));
29,062 ✔
78
        for(auto mergedNodeId : graph->mergedNodeIdsForNodeId(nodeId))
29,062 ✔
79
            nodesComponentId[mergedNodeId] = componentId;
29,262 ✔
80

81
        for(auto edgeId : graph->edgeIdsForNodeId(nodeId))
29,062 ✔
82
        {
55,944 ✔
83
            if(edgeIdFiltered(edgeId))
55,944 ✔
84
                continue;
828 ✔
85

86
            for(auto mergedEdgeId : graph->mergedEdgeIdsForEdgeId(edgeId))
55,116 ✔
87
                edgesComponentId[mergedEdgeId] = componentId;
55,604 ✔
88

89
            auto oppositeNodeId = graph->edgeById(edgeId).oppositeId(nodeId);
55,116 ✔
90

91
            if(nodesComponentId[oppositeNodeId] != componentId)
55,116 ✔
92
            {
23,734 ✔
93
                nodeIds.push_back(oppositeNodeId);
23,734 ✔
94
                for(auto mergedNodeId : graph->mergedNodeIdsForNodeId(oppositeNodeId))
23,734 ✔
95
                    nodesComponentId[mergedNodeId] = componentId;
23,758 ✔
96
            }
23,734 ✔
97
        }
55,116 ✔
98
    }
29,062 ✔
99

100
    // We don't count nodes that haven't yet been assigned a component
101
    oldComponentIdsAffected.erase(ComponentId());
5,328 ✔
102

103
    return oldComponentIdsAffected;
5,328 ✔
104
}
5,328 ✔
105

106
void ComponentManager::insertComponentArray(IGraphArray* componentArray)
107
{
574 ✔
108
    const std::unique_lock<std::mutex> lock(_componentArraysMutex);
574 ✔
109
    _componentArrays.insert(componentArray);
574 ✔
110
}
574 ✔
111

112
void ComponentManager::eraseComponentArray(IGraphArray* componentArray)
113
{
574 ✔
114
    const std::unique_lock<std::mutex> lock(_componentArraysMutex);
574 ✔
115
    _componentArrays.erase(componentArray);
574 ✔
116
}
574 ✔
117

118
void ComponentManager::update(const Graph* graph)
119
{
3,744 ✔
120
    if(_debug > 0) qDebug() << "ComponentManager::update begins" << this;
3,744 !
121

122
    std::unique_lock<std::recursive_mutex> lock(_updateMutex);
3,744 ✔
123

124
    ComponentIdMap<ComponentIdSet> splitComponents;
3,744 ✔
125
    ComponentIdSet splitComponentIds;
3,744 ✔
126
    ComponentIdMap<ComponentIdSet> mergedComponents;
3,744 ✔
127
    ComponentIdSet mergedComponentIds;
3,744 ✔
128
    ComponentIdSet componentIds;
3,744 ✔
129

130
    NodeArray<ComponentId> newNodesComponentId(*graph);
3,744 ✔
131
    EdgeArray<ComponentId> newEdgesComponentId(*graph);
3,744 ✔
132

133
    // Search for mergers and splitters
134
    for(auto nodeId : graph->nodeIds())
3,744 ✔
135
    {
29,262 ✔
136
        if(nodeIdFiltered(nodeId))
29,262 ✔
137
            continue;
200 ✔
138

139
        auto oldComponentId = _nodesComponentId[nodeId];
29,062 ✔
140

141
        if(newNodesComponentId[nodeId].isNull() && !oldComponentId.isNull())
29,062 ✔
142
        {
562 ✔
143
            if(componentIds.contains(oldComponentId))
562 ✔
144
            {
22 ✔
145
                // We have already used this ID so this is a component that has split
146
                auto newComponentId = generateComponentId();
22 ✔
147
                componentIds.insert(newComponentId);
22 ✔
148
                assignConnectedElementsComponentId(graph, nodeId, newComponentId,
22 ✔
149
                                                   newNodesComponentId, newEdgesComponentId);
22 ✔
150

151
                queueGraphComponentUpdate(graph, oldComponentId);
22 ✔
152
                queueGraphComponentUpdate(graph, newComponentId);
22 ✔
153

154
                splitComponents[oldComponentId].insert(oldComponentId);
22 ✔
155
                splitComponents[oldComponentId].insert(newComponentId);
22 ✔
156
                splitComponentIds.insert(newComponentId);
22 ✔
157
            }
22 ✔
158
            else
540 ✔
159
            {
540 ✔
160
                componentIds.insert(oldComponentId);
540 ✔
161
                auto componentIdsAffected = assignConnectedElementsComponentId(graph, nodeId, oldComponentId,
540 ✔
162
                                                                               newNodesComponentId, newEdgesComponentId);
540 ✔
163
                queueGraphComponentUpdate(graph, oldComponentId);
540 ✔
164

165
                if(componentIdsAffected.size() > 1)
540 ✔
166
                {
18 ✔
167
                    // More than one old component IDs were observed so components have merged
168
                    mergedComponents[oldComponentId].insert(componentIdsAffected.begin(), componentIdsAffected.end());
18 ✔
169
                    componentIdsAffected.erase(oldComponentId);
18 ✔
170
                    mergedComponentIds.insert(componentIdsAffected.begin(), componentIdsAffected.end());
18 ✔
171
                }
18 ✔
172
            }
540 ✔
173
        }
562 ✔
174
    }
29,062 ✔
175

176
    // Search for entirely new components
177
    for(auto nodeId : graph->nodeIds())
3,744 ✔
178
    {
29,262 ✔
179
        if(nodeIdFiltered(nodeId))
29,262 ✔
180
            continue;
200 ✔
181

182
        if(newNodesComponentId[nodeId].isNull() && _nodesComponentId[nodeId].isNull())
29,062 !
183
        {
4,766 ✔
184
            auto newComponentId = generateComponentId();
4,766 ✔
185
            componentIds.insert(newComponentId);
4,766 ✔
186
            assignConnectedElementsComponentId(graph, nodeId, newComponentId, newNodesComponentId, newEdgesComponentId);
4,766 ✔
187
            queueGraphComponentUpdate(graph, newComponentId);
4,766 ✔
188
        }
4,766 ✔
189
    }
29,062 ✔
190

191
    // Resize the component arrays
192
    for(auto* componentArray : _componentArrays)
3,744 ✔
193
        componentArray->resize(componentArrayCapacity());
60 ✔
194

195
    // Search for added or removed components
196
    ComponentIdSet componentIdsToBeAdded;
3,744 ✔
197
    ComponentIdSet componentIdsToBeRemoved;
3,744 ✔
198

199
    std::ranges::set_difference(componentIds, _componentIdsSet,
3,744 ✔
200
        std::inserter(componentIdsToBeAdded, componentIdsToBeAdded.begin()));
3,744 ✔
201
    std::ranges::set_difference(_componentIdsSet, componentIds,
3,744 ✔
202
        std::inserter(componentIdsToBeRemoved, componentIdsToBeRemoved.begin()));
3,744 ✔
203

204
    // Find nodes and edges that have been added or removed
205
    ComponentIdMap<NodeIdSet> nodeIdAdds;
3,744 ✔
206
    ComponentIdMap<EdgeIdSet> edgeIdAdds;
3,744 ✔
207
    ComponentIdMap<NodeIdSet> nodeIdRemoves;
3,744 ✔
208
    ComponentIdMap<EdgeIdSet> edgeIdRemoves;
3,744 ✔
209
    NodeIdMap<std::pair<ComponentId, ComponentId>> nodeIdMoves;
3,744 ✔
210
    EdgeIdMap<std::pair<ComponentId, ComponentId>> edgeIdMoves;
3,744 ✔
211

212
    auto maxNumNodes = static_cast<int>(std::max(_nodesComponentId.size(), newNodesComponentId.size()));
3,744 ✔
213
    for(NodeId nodeId(0); nodeId < maxNumNodes; ++nodeId)
33,354 ✔
214
    {
29,610 ✔
215
        auto oldComponentId = _nodesComponentId[nodeId];
29,610 ✔
216
        auto newComponentId = newNodesComponentId[nodeId];
29,610 ✔
217

218
        if(oldComponentId == newComponentId)
29,610 ✔
219
            continue;
1,112 ✔
220

221
        if(oldComponentId.isNull() && !newComponentId.isNull())
28,498 !
222
            nodeIdAdds[newComponentId].insert(nodeId);
28,262 ✔
223
        else if(!oldComponentId.isNull() && newComponentId.isNull())
236 !
224
            nodeIdRemoves[oldComponentId].insert(nodeId);
184 ✔
225
        else if(!oldComponentId.isNull() && !newComponentId.isNull())
52 !
226
        {
52 ✔
227
            if(!componentIdsToBeRemoved.contains(oldComponentId) && !componentIdsToBeAdded.contains(newComponentId))
52 ✔
228
                nodeIdMoves[nodeId] = {oldComponentId, newComponentId};
4 ✔
229
            else if(componentIdsToBeRemoved.contains(oldComponentId) && componentIdsToBeAdded.contains(newComponentId))
48 !
230
                nodeIdAdds[newComponentId].insert(nodeId);
×
231
        }
52 ✔
232
    }
28,498 ✔
233

234
    auto maxNumEdges = static_cast<int>(std::max(_edgesComponentId.size(), newEdgesComponentId.size()));
3,744 ✔
235
    for(EdgeId edgeId(0); edgeId < maxNumEdges; ++edgeId)
32,570 ✔
236
    {
28,826 ✔
237
        auto oldComponentId = _edgesComponentId[edgeId];
28,826 ✔
238
        auto newComponentId = newEdgesComponentId[edgeId];
28,826 ✔
239

240
        if(oldComponentId == newComponentId)
28,826 ✔
241
            continue;
1,004 ✔
242

243
        if(oldComponentId.isNull() && !newComponentId.isNull())
27,822 !
244
            edgeIdAdds[newComponentId].insert(edgeId);
27,464 ✔
245
        else if(!oldComponentId.isNull() && newComponentId.isNull())
358 !
246
            edgeIdRemoves[oldComponentId].insert(edgeId);
346 ✔
247
        else if(!oldComponentId.isNull() && !newComponentId.isNull())
12 !
248
        {
12 ✔
249
            if(!componentIdsToBeRemoved.contains(oldComponentId) && !componentIdsToBeAdded.contains(newComponentId))
12 ✔
250
                edgeIdMoves[edgeId] = {oldComponentId, newComponentId};
2 ✔
251
            else if(componentIdsToBeRemoved.contains(oldComponentId) && componentIdsToBeAdded.contains(newComponentId))
10 !
252
                edgeIdAdds[newComponentId].insert(edgeId);
×
253
        }
12 ✔
254
    }
27,822 ✔
255

256
    // In the case where nodes move from one component to another, a merge
257
    // is potentially falsely detected, so check that the merges have
258
    // corresponding removes, erasing them if they don't
259
    for(auto& [merger, mergees] : mergedComponents)
3,744 ✔
260
    {
18 ✔
261
        std::erase_if(mergees, [&](const auto& componentId)
18 ✔
262
        {
36 ✔
263
            // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
264
            return !componentIdsToBeRemoved.contains(componentId) && componentId != merger;
36 ✔
265
        });
36 ✔
266
    }
18 ✔
267

268
    std::erase_if(mergedComponents, [](const auto& m) { return m.second.size() <= 1; });
3,744 ✔
269
    std::erase_if(mergedComponentIds, [&](const auto& componentId)
3,744 ✔
270
    {
3,744 ✔
271
        return !componentIdsToBeRemoved.contains(componentId);
18 ✔
272
    });
18 ✔
273

274
    // Notify all the merges
275
    for(auto& mergee : mergedComponents)
3,744 ✔
276
    {
16 ✔
277
        if(_debug > 0) qDebug() << "componentsWillMerge" << mergee.second << "->" << mergee.first;
16 !
278
        emit componentsWillMerge(graph, ComponentMergeSet(std::move(mergee.second), mergee.first));
16 ✔
279
    }
16 ✔
280

281
    // Removed components
282
    for(auto componentId : componentIdsToBeRemoved)
3,744 ✔
283
    {
74 ✔
284
        Q_ASSERT(!componentId.isNull());
74 !
285
        if(_debug > 0) qDebug() << "componentWillBeRemoved" << componentId;
74 !
286
        const bool hasMerged = mergedComponentIds.contains(componentId);
74 ✔
287
        emit componentWillBeRemoved(graph, componentId, hasMerged);
74 ✔
288

289
        if(!hasMerged)
74 ✔
290
        {
58 ✔
291
            nodeIdRemoves.erase(componentId);
58 ✔
292
            edgeIdRemoves.erase(componentId);
58 ✔
293
        }
58 ✔
294

295
        _componentIdsSet.erase(componentId);
74 ✔
296
        removeGraphComponent(componentId);
74 ✔
297
    }
74 ✔
298

299
    _componentIds.clear();
3,744 ✔
300
    std::ranges::copy(_componentIdsSet, std::back_inserter(_componentIds));
3,744 ✔
301

302
    shrinkComponentsArrayToFit();
3,744 ✔
303

304
    _nodesComponentId = std::move(newNodesComponentId);
3,744 ✔
305
    _edgesComponentId = std::move(newEdgesComponentId);
3,744 ✔
306

307
    updateGraphComponents(graph);
3,744 ✔
308

309
    _updatesRequired.clear();
3,744 ✔
310

311
    std::ranges::copy(componentIdsToBeAdded, std::back_inserter(_componentIds));
3,744 ✔
312
    std::ranges::copy(componentIdsToBeAdded, std::inserter(_componentIdsSet, _componentIdsSet.begin()));
3,744 ✔
313

314
    std::ranges::stable_sort(_componentIds,
3,744 ✔
315
    [this](auto a, auto b)
3,744 ✔
316
    {
9,954 ✔
317
        auto componentA = this->componentById(a);
9,954 ✔
318
        auto componentB = this->componentById(b);
9,954 ✔
319

320
        if(componentA->numNodes() == componentB->numNodes())
9,954 ✔
321
            return a < b;
4,354 ✔
322

323
        return componentA->numNodes() > componentB->numNodes();
5,600 ✔
324
    });
9,954 ✔
325

326
    lock.unlock();
3,744 ✔
327

328
    // Notify all the new components
329
    for(auto componentId : componentIdsToBeAdded)
3,744 ✔
330
    {
4,788 ✔
331
        Q_ASSERT(!componentId.isNull());
4,788 !
332
        if(_debug > 0) qDebug() << "componentAdded" << componentId;
4,788 !
333
        const bool hasSplit = splitComponentIds.contains(componentId);
4,788 ✔
334
        emit componentAdded(graph, componentId, hasSplit);
4,788 ✔
335

336
        if(!hasSplit)
4,788 ✔
337
        {
4,766 ✔
338
            nodeIdAdds.erase(componentId);
4,766 ✔
339
            edgeIdAdds.erase(componentId);
4,766 ✔
340
        }
4,766 ✔
341
    }
4,788 ✔
342

343
    // Notify all the splits
344
    for(auto& splitee : splitComponents)
3,744 ✔
345
    {
22 ✔
346
        if(_debug > 0) qDebug() << "componentSplit" << splitee.first << "->" << splitee.second;
22 !
347
        emit componentSplit(graph, ComponentSplitSet(splitee.first, std::move(splitee.second)));
22 ✔
348
    }
22 ✔
349

350
    // Notify node adds and removes
351
    for(const auto& [componentId, nodeIdSet] : nodeIdAdds)
3,744 ✔
352
    {
50 ✔
353
        for(auto nodeId : nodeIdSet)
50 ✔
354
            emit nodeAddedToComponent(graph, nodeId, componentId);
58 ✔
355
    }
50 ✔
356

357
    for(const auto& [componentId, edgeIdSet] : edgeIdAdds)
3,744 ✔
358
    {
74 ✔
359
        for(auto edgeId : edgeIdSet)
74 ✔
360
            emit edgeAddedToComponent(graph, edgeId, componentId);
138 ✔
361
    }
74 ✔
362

363
    for(const auto& [componentId, nodeIdSet] : nodeIdRemoves)
3,744 ✔
364
    {
86 ✔
365
        for(auto nodeId : nodeIdSet)
86 ✔
366
            emit nodeRemovedFromComponent(graph, nodeId, componentId);
106 ✔
367
    }
86 ✔
368

369
    for(const auto& [componentId, edgeIdSet] : edgeIdRemoves)
3,744 ✔
370
    {
200 ✔
371
        for(auto edgeId : edgeIdSet)
200 ✔
372
            emit edgeRemovedFromComponent(graph, edgeId, componentId);
316 ✔
373
    }
200 ✔
374

375
    for(const auto& [nodeId, moveComponentIds] : nodeIdMoves)
3,744 ✔
376
        emit nodeMovedBetweenComponents(graph, nodeId, moveComponentIds.first, moveComponentIds.second);
4 ✔
377

378
    for(const auto& [edgeId, moveComponentIds] : edgeIdMoves)
3,744 ✔
379
        emit edgeMovedBetweenComponents(graph, edgeId, moveComponentIds.first, moveComponentIds.second);
2 ✔
380

381
    if(_debug > 1)
3,744 !
382
    {
×
383
        qDebug() << "ComponentIds:" << _componentIdsSet;
×
384

385
        for(auto componentId : _componentIds)
×
386
            qDebug() << componentId << componentFor(componentId)->nodeIds();
×
387
    }
×
388

389
    if(_debug > 0) qDebug() << "ComponentManager::update ends" << this << _componentIdsSet;
3,744 !
390
}
3,744 ✔
391

392
ComponentId ComponentManager::generateComponentId()
393
{
4,788 ✔
394
    ComponentId newComponentId;
4,788 ✔
395

396
    if(!_vacatedComponentIdQueue.empty())
4,788 ✔
397
    {
38 ✔
398
        newComponentId = _vacatedComponentIdQueue.front();
38 ✔
399
        _vacatedComponentIdQueue.pop();
38 ✔
400
    }
38 ✔
401
    else
4,750 ✔
402
        newComponentId = _nextComponentId++;
4,750 ✔
403

404
    return newComponentId;
4,788 ✔
405
}
4,788 ✔
406

407
void ComponentManager::queueGraphComponentUpdate(const Graph* graph, ComponentId componentId)
408
{
5,350 ✔
409
    _updatesRequired.insert(componentId);
5,350 ✔
410

411
    if(componentFor(componentId) == nullptr)
5,350 ✔
412
    {
4,788 ✔
413
        auto graphComponent = std::make_unique<GraphComponent>(graph);
4,788 ✔
414
        setComponentFor(componentId, std::move(graphComponent));
4,788 ✔
415
    }
4,788 ✔
416
}
5,350 ✔
417

418
void ComponentManager::updateGraphComponents(const Graph* graph)
419
{
3,744 ✔
420
    for(auto componentId : _componentIds)
3,744 ✔
421
    {
540 ✔
422
        if(_updatesRequired.contains(componentId))
540 !
423
        {
540 ✔
424
            auto* graphComponent = componentFor(componentId);
540 ✔
425

426
            graphComponent->_nodeIds.clear();
540 ✔
427
            graphComponent->_edgeIds.clear();
540 ✔
428
        }
540 ✔
429
    }
540 ✔
430

431
    for(auto nodeId : graph->nodeIds())
3,744 ✔
432
    {
29,262 ✔
433
        if(nodeIdFiltered(nodeId))
29,262 ✔
434
            continue;
200 ✔
435

436
        auto componentId = _nodesComponentId[nodeId];
29,062 ✔
437

438
        if(_updatesRequired.contains(componentId))
29,062 !
439
            componentFor(componentId)->_nodeIds.push_back(nodeId);
29,062 ✔
440
    }
29,062 ✔
441

442
    for(auto edgeId : graph->edgeIds())
3,744 ✔
443
    {
27,972 ✔
444
        if(edgeIdFiltered(edgeId))
27,972 ✔
445
            continue;
414 ✔
446

447
        auto componentId = _edgesComponentId[edgeId];
27,558 ✔
448

449
        if(_updatesRequired.contains(componentId))
27,558 !
450
            componentFor(componentId)->_edgeIds.push_back(edgeId);
27,558 ✔
451
    }
27,558 ✔
452
}
3,744 ✔
453

454
void ComponentManager::removeGraphComponent(ComponentId componentId)
455
{
74 ✔
456
    if(componentFor(componentId) != nullptr)
74 !
457
    {
74 ✔
458
        setComponentFor(componentId, nullptr);
74 ✔
459
        _vacatedComponentIdQueue.push(componentId);
74 ✔
460
        _updatesRequired.erase(componentId);
74 ✔
461
    }
74 ✔
462
}
74 ✔
463

464
GraphComponent* ComponentManager::componentFor(ComponentId componentId)
465
{
62,584 ✔
466
    Q_ASSERT(!componentId.isNull());
62,584 !
467

468
    auto index = static_cast<size_t>(componentId);
62,584 ✔
469

470
    if(index >= _components.size())
62,584 ✔
471
        return nullptr;
3,432 ✔
472

473
    return _components.at(index).get();
59,152 ✔
474
}
62,584 ✔
475

476
const GraphComponent* ComponentManager::componentFor(ComponentId componentId) const
477
{
266,598 ✔
478
    Q_ASSERT(!componentId.isNull());
266,598 !
479

480
    auto index = static_cast<size_t>(componentId);
266,598 ✔
481

482
    if(index >= _components.size())
266,598 !
483
        return nullptr;
×
484

485
    return _components.at(index).get();
266,598 ✔
486
}
266,598 ✔
487

488
void ComponentManager::setComponentFor(ComponentId componentId, std::unique_ptr<GraphComponent> graphComponent)
489
{
4,862 ✔
490
    Q_ASSERT(!componentId.isNull());
4,862 !
491

492
    auto index = static_cast<size_t>(componentId);
4,862 ✔
493
    if(graphComponent != nullptr && index >= _components.size())
4,862 ✔
494
    {
3,432 ✔
495
        const size_t newSize = index > 0 ? index * 2 : 1;
3,432 ✔
496
        _components.resize(newSize);
3,432 ✔
497
    }
3,432 ✔
498

499
    _components[index] = std::move(graphComponent);
4,862 ✔
500
}
4,862 ✔
501

502
void ComponentManager::shrinkComponentsArrayToFit()
503
{
3,744 ✔
504
    if(_components.empty())
3,744 ✔
505
        return;
1,624 ✔
506

507
    size_t newSize = _components.size();
2,120 ✔
508

509
    while(newSize > 0 && _components.at(newSize - 1) == nullptr)
3,140 ✔
510
        newSize--;
1,020 ✔
511

512
    _components.resize(newSize);
2,120 ✔
513
}
2,120 ✔
514

515
void ComponentManager::onGraphChanged(const Graph* graph, bool changeOccurred)
516
{
1,756 ✔
517
    if(_enabled && changeOccurred)
1,756 !
518
        update(graph);
1,652 ✔
519
}
1,756 ✔
520

521
template<typename T> class unique_lock_with_warning
522
{
523
public:
524
    unique_lock_with_warning() = default;
525
    unique_lock_with_warning(unique_lock_with_warning&&) noexcept = default;
526
    unique_lock_with_warning(const unique_lock_with_warning&) = delete;
527
    unique_lock_with_warning& operator=(unique_lock_with_warning&&) noexcept = default;
528
    unique_lock_with_warning& operator=(const unique_lock_with_warning&) = delete;
529

530
    explicit unique_lock_with_warning(T& mutex) :
531
        _lock(mutex, std::defer_lock)
272,560 ✔
532
    {
272,560 ✔
533
        const int MIN_WARNING_MILLISECONDS = 100;
272,560 ✔
534
        const std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
272,560 ✔
535

536
        if(!_lock.try_lock())
272,560 !
537
        {
×
538
            _lock.lock();
×
539

540
            const std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
×
541
            auto timeToAcquireLock = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
×
542

543
            if(timeToAcquireLock > MIN_WARNING_MILLISECONDS)
×
544
            {
×
545
                qWarning() << "WARNING: thread" << u::currentThreadName() <<
×
546
                              "was blocked for" << timeToAcquireLock << "ms";
×
547
            }
×
548
        }
×
549
    }
272,560 ✔
550

551
    ~unique_lock_with_warning()
552
    {
272,560 ✔
553
        if(_lock.owns_lock())
272,560 !
554
            _lock.unlock();
272,560 ✔
555
    }
272,560 ✔
556

557
private:
558
    std::unique_lock<T> _lock;
559
};
560

561
const std::vector<ComponentId>& ComponentManager::componentIds() const
562
{
1,756 ✔
563
    const unique_lock_with_warning<std::recursive_mutex> lock(_updateMutex);
1,756 ✔
564

565
    return _componentIds;
1,756 ✔
566
}
1,756 ✔
567

568
bool ComponentManager::containsComponentId(ComponentId componentId) const
569
{
×
570
    const unique_lock_with_warning<std::recursive_mutex> lock(_updateMutex);
×
571

572
    return _componentIdsSet.contains(componentId);
×
573
}
×
574

575
const GraphComponent* ComponentManager::componentById(ComponentId componentId) const
576
{
266,598 ✔
577
    const unique_lock_with_warning<std::recursive_mutex> lock(_updateMutex);
266,598 ✔
578

579
    const auto* component = componentFor(componentId);
266,598 ✔
580
    Q_ASSERT(component != nullptr);
266,598 !
581

582
    return component;
266,598 ✔
583
}
266,598 ✔
584

585
ComponentId ComponentManager::componentIdOfNode(NodeId nodeId) const
586
{
3,520 ✔
587
    if(nodeId.isNull())
3,520 !
588
        return {};
×
589

590
    const unique_lock_with_warning<std::recursive_mutex> lock(_updateMutex);
3,520 ✔
591

592
    auto componentId = _nodesComponentId.at(nodeId);
3,520 ✔
593
    if(_componentIdsSet.contains(componentId))
3,520 !
594
        return componentId;
3,520 ✔
595

596
    if(_debug > 0) qDebug() << "Can't find componentId of nodeId" << nodeId;
×
597
    return {};
×
598
}
3,520 ✔
599

600
ComponentId ComponentManager::componentIdOfEdge(EdgeId edgeId) const
601
{
686 ✔
602
    if(edgeId.isNull())
686 !
603
        return {};
×
604

605
    const unique_lock_with_warning<std::recursive_mutex> lock(_updateMutex);
686 ✔
606

607
    auto componentId = _edgesComponentId.at(edgeId);
686 ✔
608
    if(_componentIdsSet.contains(componentId))
686 ✔
609
        return componentId;
684 ✔
610

611
    if(_debug > 0) qDebug() << "Can't find componentId of edgeId" << edgeId;
2 !
612
    return {};
2 ✔
613
}
686 ✔
614

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc