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

graphia-app / graphia / 42724698251

09 Nov 2026 10:47PM UTC coverage: 71.88% (+0.5%) from 71.427%
42724698251

push

github

timangus
Add Leiden clustering

11927 of 18208 branches covered (65.5%)

Branch coverage included in aggregate %.

486 of 497 new or added lines in 3 files covered. (97.79%)

699 existing lines in 55 files now uncovered.

30522 of 40847 relevant lines covered (74.72%)

740302.21 hits per line

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

92.15
/source/app/graph/mutablegraph.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 "mutablegraph.h"
21

22
#include "componentmanager.h"
23
#include "graphcomponent.h"
24

25
#include "shared/utils/container.h"
26

27
#include <QtGlobal>
28

29
#include <algorithm>
30
#include <iterator>
31

32
MutableGraph::MutableGraph(const MutableGraph& other)
33
{
402 ✔
34
    clone(other);
402 ✔
35
}
402 ✔
36

37
MutableGraph::~MutableGraph()
38
{
3,806 ✔
39
    // Ensure no transactions are in progress
40
    const std::unique_lock<std::mutex> lock(_mutex);
3,806 ✔
41
}
3,806 ✔
42

43
void MutableGraph::clear()
44
{
252 ✔
45
    beginTransaction();
252 ✔
46

47
    const bool changed = numNodes() > 0;
252 ✔
48

49
    for(auto nodeId : nodeIds())
252 !
50
        removeNode(nodeId);
×
51

52
    _updateRequired = true;
252 ✔
53
    endTransaction(changed);
252 ✔
54

55
    // Removing all the nodes should remove all the edges
56
    Q_ASSERT(numEdges() == 0);
252 !
57

58
    _n.clear();
252 ✔
59
    _n.resize(0);
252 ✔
60
    _e.clear();
252 ✔
61
    _e.resize(0);
252 ✔
62

63
    Graph::clear();
252 ✔
64
}
252 ✔
65

66
const std::vector<NodeId>& MutableGraph::nodeIds() const
67
{
34,335 ✔
68
    return _nodeIds;
34,335 ✔
69
}
34,335 ✔
70

71
size_t MutableGraph::numNodes() const
72
{
2,598 ✔
73
    return _nodeIds.size();
2,598 ✔
74
}
2,598 ✔
75

76
const Node& MutableGraph::nodeById(NodeId nodeId) const
77
{
26,496 ✔
78
    Q_ASSERT(_n._nodeIdsInUse[static_cast<size_t>(nodeId)]);
26,496 ✔
79
    return _n._nodes[static_cast<size_t>(nodeId)];
26,496 ✔
80
}
26,496 ✔
81

82
bool MutableGraph::containsNodeId(NodeId nodeId) const
83
{
143,638 ✔
84
    return static_cast<size_t>(nodeId) < static_cast<size_t>(_n._nodeIdsInUse.size()) &&
143,638 ✔
85
        _n._nodeIdsInUse[static_cast<size_t>(nodeId)];
143,638 ✔
86
}
143,638 ✔
87

88
MultiElementType MutableGraph::typeOf(NodeId nodeId) const
89
{
966,374 ✔
90
    return _n._mergedNodeIds.typeOf(nodeId);
966,374 ✔
91
}
966,374 ✔
92

93
ConstNodeIdDistinctSet MutableGraph::mergedNodeIdsForNodeId(NodeId nodeId) const
94
{
430,398 ✔
95
    return {nodeId, &_n._mergedNodeIds};
430,398 ✔
96
}
430,398 ✔
97

98
int MutableGraph::multiplicityOf(NodeId nodeId) const
99
{
3,308 ✔
100
    return _n._multiplicities[static_cast<size_t>(nodeId)];
3,308 ✔
101
}
3,308 ✔
102

103
std::vector<EdgeId> MutableGraph::edgeIdsBetween(NodeId nodeIdA, NodeId nodeIdB) const
104
{
479 ✔
105
    std::vector<EdgeId> edgeIds;
479 ✔
106

107
    auto undirectedEdge = UndirectedEdge(nodeIdA, nodeIdB);
479 ✔
108
    if(u::contains(_e._connections, undirectedEdge))
479 ✔
109
    {
473 ✔
110
        const auto& edgeIdDistinctSet = _e._connections.at(undirectedEdge);
473 ✔
111

112
        std::ranges::copy(edgeIdDistinctSet, std::back_inserter(edgeIds));
473 ✔
113
    }
473 ✔
114

115
    return edgeIds;
479 ✔
116
}
479 ✔
117

118
EdgeId MutableGraph::firstEdgeIdBetween(NodeId nodeIdA, NodeId nodeIdB) const
119
{
24 ✔
120
    auto connection = _e._connections.find(UndirectedEdge(nodeIdA, nodeIdB));
24 ✔
121

122
    if(connection == _e._connections.end())
24 ✔
123
        return {};
12 ✔
124

125
    return *connection->second.begin();
12 ✔
126
}
24 ✔
127

128
bool MutableGraph::edgeExistsBetween(NodeId nodeIdA, NodeId nodeIdB) const
129
{
6 ✔
130
    return !firstEdgeIdBetween(nodeIdA, nodeIdB).isNull();
6 ✔
131
}
6 ✔
132

133
NodeId MutableGraph::addNode()
134
{
26,178 ✔
135
    if(!_unusedNodeIds.empty())
26,178 !
UNCOV
136
    {
×
UNCOV
137
        auto unusedNodeId = _unusedNodeIds.back();
×
UNCOV
138
        _unusedNodeIds.pop_back();
×
139

UNCOV
140
        return addNode(unusedNodeId);
×
UNCOV
141
    }
×
142

143
    return addNode(nextNodeId());
26,178 ✔
144
}
26,178 ✔
145

146
Node& MutableGraph::nodeBy(NodeId nodeId)
147
{
83,090 ✔
148
    return _n._nodes[static_cast<size_t>(nodeId)];
83,090 ✔
149
}
83,090 ✔
150

151
const Node& MutableGraph::nodeBy(NodeId nodeId) const
152
{
45,746 ✔
153
    return _n._nodes[static_cast<size_t>(nodeId)];
45,746 ✔
154
}
45,746 ✔
155

156
void MutableGraph::claimNodeId(NodeId nodeId)
157
{
27,666 ✔
158
    _n._nodeIdsInUse[static_cast<size_t>(nodeId)] = true;
27,666 ✔
159
}
27,666 ✔
160

161
void MutableGraph::releaseNodeId(NodeId nodeId)
162
{
528 ✔
163
    _n._nodeIdsInUse[static_cast<size_t>(nodeId)] = false;
528 ✔
164
}
528 ✔
165

166
Edge& MutableGraph::edgeBy(EdgeId edgeId)
167
{
27,712 ✔
168
    return _e._edges[static_cast<size_t>(edgeId)];
27,712 ✔
169
}
27,712 ✔
170

171
const Edge& MutableGraph::edgeBy(EdgeId edgeId) const
UNCOV
172
{
×
UNCOV
173
    return _e._edges[static_cast<size_t>(edgeId)];
×
UNCOV
174
}
×
175

176
void MutableGraph::claimEdgeId(EdgeId edgeId)
177
{
26,888 ✔
178
    _e._edgeIdsInUse[static_cast<size_t>(edgeId)] = true;
26,888 ✔
179
}
26,888 ✔
180

181
void MutableGraph::releaseEdgeId(EdgeId edgeId)
182
{
824 ✔
183
    _e._edgeIdsInUse[static_cast<size_t>(edgeId)] = false;
824 ✔
184
}
824 ✔
185

186
void MutableGraph::reserveNodeId(NodeId nodeId)
187
{
83,660 ✔
188
    if(nodeId < nextNodeId())
83,660 ✔
189
        return;
56,030 ✔
190

191
    auto unusedNodeId = nextNodeId();
27,630 ✔
192

193
    Graph::reserveNodeId(nodeId);
27,630 ✔
194
    _n.resize(static_cast<size_t>(nextNodeId()));
27,630 ✔
195

196
    while(unusedNodeId < nodeId)
27,632 ✔
197
        _unusedNodeIds.push_back(unusedNodeId++);
2 ✔
198
}
27,630 ✔
199

200
NodeId MutableGraph::addNode(NodeId nodeId)
201
{
27,666 ✔
202
    Q_ASSERT(!nodeId.isNull());
27,666 !
203

204
    beginTransaction();
27,666 ✔
205

206
    // The requested ID is not available or is out of range, so resize and append
207
    if(nodeId >= nextNodeId() || containsNodeId(nodeId))
27,666 !
208
    {
26,178 ✔
209
        nodeId = nextNodeId();
26,178 ✔
210
        reserveNodeId(nodeId);
26,178 ✔
211
    }
26,178 ✔
212

213
    claimNodeId(nodeId);
27,666 ✔
214
    auto& node = nodeBy(nodeId);
27,666 ✔
215
    node._id = nodeId;
27,666 ✔
216
    node._inEdgeIds.setCollection(&_e._inEdgeIdsCollection);
27,666 ✔
217
    node._outEdgeIds.setCollection(&_e._outEdgeIdsCollection);
27,666 ✔
218

219
    emit nodeAdded(this, nodeId);
27,666 ✔
220
    _updateRequired = true;
27,666 ✔
221
    endTransaction();
27,666 ✔
222

223
    return nodeId;
27,666 ✔
224
}
27,666 ✔
225

226
NodeId MutableGraph::addNode(const INode& node)
UNCOV
227
{
×
UNCOV
228
    return addNode(node.id());
×
UNCOV
229
}
×
230

231
void MutableGraph::removeNode(NodeId nodeId)
232
{
528 ✔
233
    Q_ASSERT(containsNodeId(nodeId));
528 !
234

235
    beginTransaction();
528 ✔
236

237
    // Remove all edges that touch this node
238
    for(auto edgeId : inEdgeIdsForNodeId(nodeId).copy())
528 ✔
239
        removeEdge(edgeId);
84 ✔
240

241
    // ...do in and out separately in case an edge is in both
242
    for(auto edgeId : outEdgeIdsForNodeId(nodeId).copy())
528 ✔
243
        removeEdge(edgeId);
174 ✔
244

245
    _n._mergedNodeIds.remove({}, nodeId);
528 ✔
246

247
    releaseNodeId(nodeId);
528 ✔
248
    _unusedNodeIds.push_back(nodeId);
528 ✔
249

250
    emit nodeRemoved(this, nodeId);
528 ✔
251
    _updateRequired = true;
528 ✔
252
    endTransaction();
528 ✔
253
}
528 ✔
254

255
const std::vector<EdgeId>& MutableGraph::edgeIds() const
256
{
11,682 ✔
257
    return _edgeIds;
11,682 ✔
258
}
11,682 ✔
259

260
size_t MutableGraph::numEdges() const
261
{
2,352 ✔
262
    return _edgeIds.size();
2,352 ✔
263
}
2,352 ✔
264

265
const Edge& MutableGraph::edgeById(EdgeId edgeId) const
266
{
3,543,864 ✔
267
    Q_ASSERT(containsEdgeId(edgeId));
3,543,864 ✔
268
    return _e._edges[static_cast<size_t>(edgeId)];
3,543,864 ✔
269
}
3,543,864 ✔
270

271
bool MutableGraph::containsEdgeId(EdgeId edgeId) const
272
{
3,635,724 ✔
273
    return static_cast<int>(edgeId) < static_cast<int>(_e._edgeIdsInUse.size()) &&
3,635,724 ✔
274
        _e._edgeIdsInUse[static_cast<size_t>(edgeId)];
3,635,724 ✔
275
}
3,635,724 ✔
276

277
MultiElementType MutableGraph::typeOf(EdgeId edgeId) const
278
{
574,242 ✔
279
    return _e._mergedEdgeIds.typeOf(edgeId);
574,242 ✔
280
}
574,242 ✔
281

282
ConstEdgeIdDistinctSet MutableGraph::mergedEdgeIdsForEdgeId(EdgeId edgeId) const
283
{
58,274 ✔
284
    return {edgeId, &_e._mergedEdgeIds};
58,274 ✔
285
}
58,274 ✔
286

287
int MutableGraph::multiplicityOf(EdgeId edgeId) const
288
{
728 ✔
289
    return _e._multiplicities[static_cast<size_t>(edgeId)];
728 ✔
290
}
728 ✔
291

292
EdgeIdDistinctSets MutableGraph::edgeIdsForNodeId(NodeId nodeId) const
293
{
44,258 ✔
294
    EdgeIdDistinctSets set;
44,258 ✔
295
    const auto& node = nodeBy(nodeId);
44,258 ✔
296

297
    set.add(node._inEdgeIds);
44,258 ✔
298
    set.add(node._outEdgeIds);
44,258 ✔
299

300
    return set;
44,258 ✔
301
}
44,258 ✔
302

303
EdgeIdDistinctSet MutableGraph::inEdgeIdsForNodeId(NodeId nodeId) const
304
{
530 ✔
305
    return nodeBy(nodeId)._inEdgeIds;
530 ✔
306
}
530 ✔
307

308
EdgeIdDistinctSet MutableGraph::outEdgeIdsForNodeId(NodeId nodeId) const
309
{
530 ✔
310
    return nodeBy(nodeId)._outEdgeIds;
530 ✔
311
}
530 ✔
312

313
EdgeId MutableGraph::addEdge(NodeId sourceId, NodeId targetId)
314
{
25,018 ✔
315
    if(!_unusedEdgeIds.empty())
25,018 !
UNCOV
316
    {
×
UNCOV
317
        auto unusedEdgeId = _unusedEdgeIds.back();
×
UNCOV
318
        _unusedEdgeIds.pop_back();
×
319

UNCOV
320
        return addEdge(unusedEdgeId, sourceId, targetId);
×
UNCOV
321
    }
×
322

323
    return addEdge(nextEdgeId(), sourceId, targetId);
25,018 ✔
324
}
25,018 ✔
325

326
void MutableGraph::reserveEdgeId(EdgeId edgeId)
327
{
80,742 ✔
328
    if(edgeId < nextEdgeId())
80,742 ✔
329
        return;
54,024 ✔
330

331
    auto unusedEdgeId = nextEdgeId();
26,718 ✔
332

333
    Graph::reserveEdgeId(edgeId);
26,718 ✔
334
    _e.resize(static_cast<size_t>(nextEdgeId()));
26,718 ✔
335

336
    while(unusedEdgeId < edgeId)
26,722 ✔
337
        _unusedEdgeIds.push_back(unusedEdgeId++);
4 ✔
338
}
26,718 ✔
339

340
NodeId MutableGraph::mergeNodes(NodeId nodeIdA, NodeId nodeIdB)
341
{
2 ✔
342
    return _n._mergedNodeIds.add(nodeIdA, nodeIdB);
2 ✔
343
}
2 ✔
344

345
NodeId MutableGraph::mergeNodes(const std::vector<NodeId>& nodeIds)
346
{
102 ✔
347
    auto setId = *std::ranges::min_element(nodeIds);
102 ✔
348

349
    for(auto nodeId : nodeIds)
102 ✔
350
        _n._mergedNodeIds.add(setId, nodeId);
214 ✔
351

352
    return setId;
102 ✔
353
}
102 ✔
354

355
EdgeId MutableGraph::addEdge(EdgeId edgeId, NodeId sourceId, NodeId targetId)
356
{
26,888 ✔
357
    Q_ASSERT(!edgeId.isNull());
26,888 !
358
    Q_ASSERT(_n._nodeIdsInUse[static_cast<size_t>(sourceId)]);
26,888 !
359
    Q_ASSERT(_n._nodeIdsInUse[static_cast<size_t>(targetId)]);
26,888 !
360

361
    beginTransaction();
26,888 ✔
362

363
    // The requested ID is not available or is out of range, so resize and append
364
    if(edgeId >= nextEdgeId() || containsEdgeId(edgeId))
26,888 !
365
    {
25,018 ✔
366
        edgeId = nextEdgeId();
25,018 ✔
367
        reserveEdgeId(edgeId);
25,018 ✔
368
    }
25,018 ✔
369

370
    claimEdgeId(edgeId);
26,888 ✔
371
    auto& edge = edgeBy(edgeId);
26,888 ✔
372
    edge._id = edgeId;
26,888 ✔
373
    edge._sourceId = sourceId;
26,888 ✔
374
    edge._targetId = targetId;
26,888 ✔
375

376
    nodeBy(sourceId)._outEdgeIds.add(edgeId);
26,888 ✔
377
    nodeBy(targetId)._inEdgeIds.add(edgeId);
26,888 ✔
378

379
    auto& connection = _e._connections.try_emplace(
26,888 ✔
380
        UndirectedEdge(sourceId, targetId), &_e._mergedEdgeIds).first->second;
26,888 ✔
381
    connection.add(edgeId);
26,888 ✔
382

383
    emit edgeAdded(this, edgeId);
26,888 ✔
384
    _updateRequired = true;
26,888 ✔
385
    endTransaction();
26,888 ✔
386

387
    return edgeId;
26,888 ✔
388
}
26,888 ✔
389

390
EdgeId MutableGraph::addEdge(const IEdge& edge)
391
{
62 ✔
392
    return addEdge(edge.id(), edge.sourceId(), edge.targetId());
62 ✔
393
}
62 ✔
394

395
void MutableGraph::removeEdge(EdgeId edgeId)
396
{
824 ✔
397
    Q_ASSERT(containsEdgeId(edgeId));
824 !
398

399
    beginTransaction();
824 ✔
400

401
    // Remove all node references to this edge
402
    const auto& edge = edgeBy(edgeId);
824 ✔
403

404
    nodeBy(edge.sourceId())._outEdgeIds.remove(edgeId);
824 ✔
405
    nodeBy(edge.targetId())._inEdgeIds.remove(edgeId);
824 ✔
406

407
    auto undirectedEdge = UndirectedEdge(edge.sourceId(), edge.targetId());
824 ✔
408
    auto& connection = _e._connections[undirectedEdge];
824 ✔
409
    Q_ASSERT(!connection.empty());
824 !
410
    connection.remove(edgeId);
824 ✔
411

412
    if(connection.empty())
824 ✔
413
        _e._connections.erase(undirectedEdge);
822 ✔
414

415
    releaseEdgeId(edgeId);
824 ✔
416
    _unusedEdgeIds.push_back(edgeId);
824 ✔
417

418
    emit edgeRemoved(this, edgeId);
824 ✔
419
    _updateRequired = true;
824 ✔
420
    endTransaction();
824 ✔
421
}
824 ✔
422

423
// Move the edges to connect to nodeId
424
template<typename C> static void moveEdgesTo(MutableGraph& graph, NodeId nodeId,
425
                                             const C& inEdgeIds,
426
                                             const C& outEdgeIds)
427
{
104 ✔
428
    // Don't bother emitting signals for edges that are moving
429
    const bool wasBlocked = graph.blockSignals(true);
104 ✔
430

431
    for(auto edgeIdToMove : inEdgeIds)
104 ✔
432
    {
44 ✔
433
        auto sourceId = graph.edgeById(edgeIdToMove).sourceId();
44 ✔
434
        graph.removeEdge(edgeIdToMove);
44 ✔
435
        graph.addEdge(edgeIdToMove, sourceId, nodeId);
44 ✔
436
    }
44 ✔
437

438
    for(auto edgeIdToMove : outEdgeIds)
104 ✔
439
    {
64 ✔
440
        auto targetId = graph.edgeById(edgeIdToMove).targetId();
64 ✔
441
        graph.removeEdge(edgeIdToMove);
64 ✔
442
        graph.addEdge(edgeIdToMove, nodeId, targetId);
64 ✔
443
    }
64 ✔
444

445
    graph.blockSignals(wasBlocked);
104 ✔
446
}
104 ✔
447

448
void MutableGraph::contractEdge(EdgeId edgeId)
449
{
2 ✔
450
    // Can't contract an edge that doesn't exist
451
    if(!containsEdgeId(edgeId))
2 !
UNCOV
452
        return;
×
453

454
    beginTransaction();
2 ✔
455

456
    const auto& edge = edgeById(edgeId);
2 ✔
457
    auto sourceId = edge.sourceId();
2 ✔
458
    auto targetId = edge.targetId();
2 ✔
459
    auto [nodeId, nodeIdToMerge] = std::minmax(sourceId, targetId);
2 ✔
460

461
    removeEdge(edgeId);
2 ✔
462
    moveEdgesTo(*this, nodeId,
2 ✔
463
        inEdgeIdsForNodeId(nodeIdToMerge).copy(),
2 ✔
464
        outEdgeIdsForNodeId(nodeIdToMerge).copy());
2 ✔
465
    mergeNodes(nodeId, nodeIdToMerge);
2 ✔
466

467
    _updateRequired = true;
2 ✔
468
    endTransaction();
2 ✔
469
}
2 ✔
470

471
void MutableGraph::contractEdges(const EdgeIdSet& edgeIds)
472
{
74 ✔
473
    if(edgeIds.empty())
74 ✔
474
        return;
2 ✔
475

476
    beginTransaction();
72 ✔
477

478
    // Divide into components, but ignore any edges that aren't being contracted,
479
    // so that each component represents a set of nodes that will be merged
480
    const ComponentManager componentManager(*this, nullptr,
72 ✔
481
    [&edgeIds](EdgeId edgeId)
72 ✔
482
    {
846 ✔
483
        return !u::contains(edgeIds, edgeId);
846 ✔
484
    });
846 ✔
485

486
    removeEdges(edgeIds);
72 ✔
487

488
    for(auto componentId : componentManager.componentIds())
72 ✔
489
    {
250 ✔
490
        const auto* component = componentManager.componentById(componentId);
250 ✔
491

492
        // Nothing to contract
493
        if(component->numEdges() == 0)
250 ✔
494
            continue;
148 ✔
495

496
        const auto& nodeIds = component->nodeIds();
102 ✔
497
        auto nodeId = *std::ranges::min_element(nodeIds);
102 ✔
498

499
        moveEdgesTo(*this, nodeId,
102 ✔
500
            inEdgeIdsForNodeIds(nodeIds).copy(),
102 ✔
501
            outEdgeIdsForNodeIds(nodeIds).copy());
102 ✔
502

503
        mergeNodes(nodeIds);
102 ✔
504
    }
102 ✔
505

506
    _updateRequired = true;
72 ✔
507
    endTransaction();
72 ✔
508
}
72 ✔
509

510
MutableGraph& MutableGraph::clone(const MutableGraph& other)
511
{
2,164 ✔
512
    beginTransaction();
2,164 ✔
513

514
    // Store the differences between the graphs
515
    auto diff = diffTo(other);
2,164 ✔
516

517
    _n             = other._n;
2,164 ✔
518
    _nodeIds       = other._nodeIds;
2,164 ✔
519
    _unusedNodeIds = other._unusedNodeIds;
2,164 ✔
520
    Graph::reserveNodeId(other.largestNodeId());
2,164 ✔
521
    _n.resize(static_cast<size_t>(nextNodeId()));
2,164 ✔
522

523
    _e             = other._e;
2,164 ✔
524
    _edgeIds       = other._edgeIds;
2,164 ✔
525
    _unusedEdgeIds = other._unusedEdgeIds;
2,164 ✔
526
    Graph::reserveEdgeId(other.largestEdgeId());
2,164 ✔
527
    _e.resize(static_cast<size_t>(nextEdgeId()));
2,164 ✔
528

529
    // Reset collection pointers to collections in this
530
    for(auto& node : _n._nodes)
2,164 ✔
531
    {
30,078 ✔
532
        node._inEdgeIds.setCollection(&_e._inEdgeIdsCollection);
30,078 ✔
533
        node._outEdgeIds.setCollection(&_e._outEdgeIdsCollection);
30,078 ✔
534
    }
30,078 ✔
535

536
    for(auto& connection : _e._connections)
2,164 ✔
537
        connection.second.setCollection(&_e._mergedEdgeIds);
27,860 ✔
538

539
    // Signal all the changes based on the diff before we cloned
540
    for(const NodeId nodeId : diff._nodesAdded)
2,164 ✔
541
        emit nodeAdded(this, nodeId);
28,364 ✔
542

543
    for(const EdgeId edgeId : diff._edgesAdded)
2,164 ✔
544
        emit edgeAdded(this, edgeId);
27,244 ✔
545

546
    for(const EdgeId edgeId : diff._edgesRemoved)
2,164 ✔
547
        emit edgeRemoved(this, edgeId);
86 ✔
548

549
    for(const NodeId nodeId : diff._nodesRemoved)
2,164 ✔
550
        emit nodeRemoved(this, nodeId);
58 ✔
551

552
    _updateRequired = true;
2,164 ✔
553
    endTransaction(!diff.empty());
2,164 ✔
554

555
    return *this;
2,164 ✔
556
}
2,164 ✔
557

558
MutableGraph& MutableGraph::operator=(const MutableGraph& other)
559
{
1,762 ✔
560
    if(this == &other)
1,762 !
UNCOV
561
        return *this;
×
562

563
    clone(other);
1,762 ✔
564
    return *this;
1,762 ✔
565
}
1,762 ✔
566

567
MutableGraph::Diff MutableGraph::diffTo(const MutableGraph& other)
568
{
2,164 ✔
569
    MutableGraph::Diff diff;
2,164 ✔
570

571
    auto maxNodeId = std::max(nextNodeId(), other.nextNodeId());
2,164 ✔
572
    for(NodeId nodeId(0); nodeId < maxNodeId; ++nodeId)
32,242 ✔
573
    {
30,078 ✔
574
        if(nodeId < nextNodeId() && nodeId < other.nextNodeId())
30,078 !
575
        {
1,346 ✔
576
            if(containsNodeId(nodeId) && !other.containsNodeId(nodeId))
1,346 ✔
577
                diff._nodesRemoved.push_back(nodeId);
58 ✔
578
            else if(!containsNodeId(nodeId) && other.containsNodeId(nodeId))
1,288 ✔
579
                diff._nodesAdded.push_back(nodeId);
128 ✔
580
        }
1,346 ✔
581
        else if(nodeId < nextNodeId() && containsNodeId(nodeId))
28,732 !
UNCOV
582
            diff._nodesRemoved.push_back(nodeId);
×
583
        else if(nodeId < other.nextNodeId() && other.containsNodeId(nodeId))
28,732 !
584
            diff._nodesAdded.push_back(nodeId);
28,236 ✔
585
    }
30,078 ✔
586

587
    auto maxEdgeId = std::max(nextEdgeId(), other.nextEdgeId());
2,164 ✔
588
    for(EdgeId edgeId(0); edgeId < maxEdgeId; ++edgeId)
31,098 ✔
589
    {
28,934 ✔
590
        if(edgeId < nextEdgeId() && edgeId < other.nextEdgeId())
28,934 !
591
        {
1,146 ✔
592
            if(containsEdgeId(edgeId) && !other.containsEdgeId(edgeId))
1,146 ✔
593
                diff._edgesRemoved.push_back(edgeId);
86 ✔
594
            else if(!containsEdgeId(edgeId) && other.containsEdgeId(edgeId))
1,060 ✔
595
                diff._edgesAdded.push_back(edgeId);
182 ✔
596
        }
1,146 ✔
597
        else if(edgeId < nextEdgeId() && containsEdgeId(edgeId))
27,788 !
UNCOV
598
            diff._edgesRemoved.push_back(edgeId);
×
599
        else if(edgeId < other.nextEdgeId() && other.containsEdgeId(edgeId))
27,788 !
600
            diff._edgesAdded.push_back(edgeId);
27,062 ✔
601
    }
28,934 ✔
602

603
    return diff;
2,164 ✔
604
}
2,164 ✔
605

606
void MutableGraph::beginTransaction()
607
{
62,008 ✔
608
    if(_graphChangeDepth++ <= 0)
62,008 ✔
609
    {
4,234 ✔
610
        emit transactionWillBegin(this);
4,234 ✔
611
        _mutex.lock();
4,234 ✔
612
        _graphChangeOccurred = false;
4,234 ✔
613
        emit graphWillChange(this);
4,234 ✔
614
    }
4,234 ✔
615
}
62,008 ✔
616

617
void MutableGraph::endTransaction(bool graphChangeOccurred)
618
{
62,008 ✔
619
    _graphChangeOccurred = _graphChangeOccurred || graphChangeOccurred;
62,008 ✔
620

621
    Q_ASSERT(_graphChangeDepth > 0);
62,008 !
622
    if(--_graphChangeDepth <= 0)
62,008 ✔
623
    {
4,234 ✔
624
        update();
4,234 ✔
625
        emit graphChanged(this, _graphChangeOccurred);
4,234 ✔
626
        _mutex.unlock();
4,234 ✔
627
    }
4,234 ✔
628
}
62,008 ✔
629

630
bool MutableGraph::update()
631
{
9,420 ✔
632
    if(!_updateRequired)
9,420 ✔
633
        return false;
4,862 ✔
634

635
    _updateRequired = false;
4,558 ✔
636

637
    _nodeIds.clear();
4,558 ✔
638
    _unusedNodeIds.clear();
4,558 ✔
639
    std::ranges::fill(_n._multiplicities, 0);
4,558 ✔
640
    for(NodeId nodeId(0); nodeId < nextNodeId(); ++nodeId)
65,030 ✔
641
    {
60,472 ✔
642
        if(containsNodeId(nodeId))
60,472 ✔
643
        {
59,150 ✔
644
            _nodeIds.emplace_back(nodeId);
59,150 ✔
645

646
            if(typeOf(nodeId) == MultiElementType::Head)
59,150 ✔
647
            {
224 ✔
648
                const auto& mergedNodeIds = mergedNodeIdsForNodeId(nodeId);
224 ✔
649
                auto multiplicity = mergedNodeIds.size();
224 ✔
650
                for(auto mergedNodeId : mergedNodeIds)
224 ✔
651
                    _n._multiplicities[static_cast<size_t>(mergedNodeId)] = multiplicity;
540 ✔
652
            }
224 ✔
653
            else if(typeOf(nodeId) == MultiElementType::Not)
58,926 ✔
654
                _n._multiplicities[static_cast<size_t>(nodeId)] = 1;
58,610 ✔
655
        }
59,150 ✔
656
        else
1,322 ✔
657
            _unusedNodeIds.emplace_back(nodeId);
1,322 ✔
658
    }
60,472 ✔
659

660
    _edgeIds.clear();
4,558 ✔
661
    _unusedEdgeIds.clear();
4,558 ✔
662
    std::ranges::fill(_e._multiplicities, 0);
4,558 ✔
663
    for(EdgeId edgeId(0); edgeId < nextEdgeId(); ++edgeId)
62,084 ✔
664
    {
57,526 ✔
665
        if(containsEdgeId(edgeId))
57,526 ✔
666
        {
55,546 ✔
667
            _edgeIds.emplace_back(edgeId);
55,546 ✔
668

669
            if(typeOf(edgeId) == MultiElementType::Head)
55,546 ✔
670
            {
332 ✔
671
                const auto& mergedEdgeIds = mergedEdgeIdsForEdgeId(edgeId);
332 ✔
672
                auto multiplicity = mergedEdgeIds.size();
332 ✔
673
                for(auto mergedEdgeId : mergedEdgeIds)
332 ✔
674
                    _e._multiplicities[static_cast<size_t>(mergedEdgeId)] = multiplicity;
840 ✔
675
            }
332 ✔
676
            else if(typeOf(edgeId) == MultiElementType::Not)
55,214 ✔
677
                _e._multiplicities[static_cast<size_t>(edgeId)] = 1;
54,706 ✔
678
        }
55,546 ✔
679
        else
1,980 ✔
680
            _unusedEdgeIds.emplace_back(edgeId);
1,980 ✔
681
    }
57,526 ✔
682

683
    return true;
4,558 ✔
684
}
9,420 ✔
685

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