aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6_generator/ApiExtractor/graph.h
blob: db5ba802e4781fc6ebbd87800bc684bf1ea5bfd0 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef GRAPH_H
#define GRAPH_H

#include "dotview.h"

#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qhash.h>
#include <QtCore/qlist.h>
#include <QtCore/qstring.h>
#include <QtCore/qtextstream.h>

#include <algorithm>

/// Result of topologically sorting of a graph (list of nodes in order
/// or list of nodes that have cyclic dependencies).
template <class Node>
struct GraphSortResult
{
    using NodeList = QList<Node>;

    bool isValid() const { return !result.isEmpty() && cyclic.isEmpty(); }
    void format(QDebug &debug) const;

    NodeList result;
    NodeList cyclic;
};

/// A graph that can have its nodes topologically sorted. The nodes need to
/// have operator==().
template <class Node>
class Graph
{
    using IndexList = QList<qsizetype>;

public:
    using NodeList = QList<Node>;

    Graph() = default;

    // Construct from a QList of nodes (unchecked, does not require operator==())
    explicit Graph(const NodeList &list) : m_nodes(list)
    {
        m_nodeEntries.resize(list.size());
    }

    // Construct from a sequence of nodes (checks for duplicated nodes using operator==())
    template<class It>
    explicit Graph(It i1, It i2)
    {
        const auto size = std::distance(i1, i2);
        m_nodes.reserve(size);
        m_nodeEntries.reserve(size);
        setNodes(i1, i2);
    }

    template<class It>
    void setNodes(It i1, It i2)
    {
        for (; i1 != i2; ++i1)
            addNode(*i1);
    }

    bool addNode(Node n);

    /// Returns whether node was registered
    bool hasNode(Node node) { return m_nodes.contains(node); }

    /// Returns the numbed of nodes in this graph.
    qsizetype nodeCount() const { return m_nodeEntries.size(); }

    /// Returns true if the graph contains the edge from -> to
    bool containsEdge(Node from, Node to) const;
    bool containsEdgeByIndexes(qsizetype fromIndex, qsizetype toIndex) const;
    /// Returns true if the graph has any edges
    bool hasEdges() const;

    /// Adds an edge to this graph.
    bool addEdge(Node from, Node to);
    bool addEdgeByIndexes(qsizetype fromIndex, qsizetype toIndex);
    /// Removes an edge from this graph.
    bool removeEdge(Node from, Node to);
    bool removeEdgeByIndexes(qsizetype fromIndex, qsizetype toIndex);
    /// Clears the graph
    void clear()
    {
        m_nodes.clear();
        m_nodeEntries.clear();
    }

    /// Dumps a dot graph to a file named \p filename.
    /// \param fileName file name where the output should be written.
    /// \param f function returning the name of a node
    template <class NameFunction>
    bool dumpDot(const QString& fileName, NameFunction f) const;
    template <class NameFunction>
    void formatDot(QTextStream &str, NameFunction f) const;
    template <class NameFunction>
    bool showGraph(const QString &name, NameFunction f) const;

    void format(QDebug &debug) const;

    /**
    *   Topologically sort this graph.
    *   \return A collection with all nodes topologically sorted or an empty collection if a cyclic
    *   dependency was found.
    */
    GraphSortResult<Node> topologicalSort() const;

private:
    enum Color : quint8 { WHITE, GRAY, BLACK };

    struct NodeEntry
    {
        IndexList targets;
        mutable Color color = WHITE;
    };

    Color colorAt(qsizetype i) const { return m_nodeEntries.at(i).color; }
    void depthFirstVisit(qsizetype i, IndexList *result) const;

    NodeList m_nodes;
    QList<NodeEntry> m_nodeEntries;
};

template <class Node>
bool Graph<Node>::addNode(Node n)
{
    if (hasNode(n))
        return false;
    m_nodes.append(n);
    m_nodeEntries.append(NodeEntry{});
    return true;
}

template <class Node>
void Graph<Node>::depthFirstVisit(qsizetype i, IndexList *result) const
{
    m_nodeEntries[i].color = GRAY;

    for (qsizetype toIndex : m_nodeEntries.at(i).targets) {
        switch (colorAt(toIndex)) {
        case WHITE:
            depthFirstVisit(toIndex, result);
            break;
        case GRAY:
            return; // This is not a DAG!
        case BLACK:
            break;
        }
    }

    m_nodeEntries[i].color = BLACK;

    result->append(i);
}

template <class Node>
GraphSortResult<Node> Graph<Node>::topologicalSort() const
{
    const qsizetype size = m_nodeEntries.size();

    GraphSortResult<Node> result;

    if (size > 1 && hasEdges()) {
        result.result.reserve(size);
        IndexList indexList;
        indexList.reserve(size);
        for (qsizetype i = 0; i < size; ++i)
            m_nodeEntries[i].color = WHITE;
        for (qsizetype i = 0; i < size; ++i)  {
            if (colorAt(i) == WHITE) // recursive calls may have set it to black
                depthFirstVisit(i, &indexList);
        }
        if (indexList.size() == size) { // Succeeded, all traversed
            for (qsizetype i = size - 1; i >= 0; --i)
                result.result.append(m_nodes.at(indexList.at(i)));
        } else { // Cyclic, Not a DAG!
            for (qsizetype i = 0; i < size; ++i) {
                if (!indexList.contains(i))
                    result.cyclic.append(m_nodes.at(i));
            }
        }
    } else { // no edges, shortcut. Legacy behavior: Also reverse in this case.
        result.result = m_nodes;
        std::reverse(result.result.begin(), result.result.end());
    }
    return result;
}

template <class Node>
bool Graph<Node>::containsEdge(Node from, Node to) const
{
    return containsEdgeByIndexes(m_nodes.indexOf(from), m_nodes.indexOf(to));
}

template <class Node>
bool Graph<Node>::containsEdgeByIndexes(qsizetype fromIndex, qsizetype toIndex) const
{
    return fromIndex >= 0 && fromIndex < m_nodeEntries.size()
        && m_nodeEntries.at(fromIndex).targets.contains(toIndex);
}

template <class Node>
bool Graph<Node>::hasEdges() const
{
    auto hashEdgesPred = [](const NodeEntry &nodeEntry) { return !nodeEntry.targets.isEmpty(); };
    return std::any_of(m_nodeEntries.cbegin(), m_nodeEntries.cend(), hashEdgesPred);
}

template <class Node>
bool Graph<Node>::addEdge(Node from, Node to)
{
    return addEdgeByIndexes(m_nodes.indexOf(from), m_nodes.indexOf(to));
}

template <class Node>
bool Graph<Node>::addEdgeByIndexes(qsizetype fromIndex, qsizetype toIndex)
{
    if (fromIndex < 0 || fromIndex >= m_nodeEntries.size()
        || toIndex < 0 || toIndex >= m_nodeEntries.size()
        || m_nodeEntries.at(fromIndex).targets.contains(toIndex)) {
        return false;
    }
    m_nodeEntries[fromIndex].targets.append(toIndex);
    return true;
}

template <class Node>
bool Graph<Node>::removeEdge(Node from, Node to)
{
    return removeEdgeByIndexes(m_nodes.indexOf(from), m_nodes.indexOf(to));
}

template <class Node>
bool Graph<Node>::removeEdgeByIndexes(qsizetype fromIndex, qsizetype toIndex)
{
    if (fromIndex < 0 || fromIndex >= m_nodeEntries.size()
        || toIndex < 0 || toIndex >= m_nodeEntries.size()) {
        return false;
    }
    auto &targets = m_nodeEntries[fromIndex].targets;
    const qsizetype toPos = targets.indexOf(toIndex);
    if (toPos == -1)
        return false;
    targets.removeAt(toPos);
    return true;
}

template <class Node>
template <class NameFunction>
bool Graph<Node>::dumpDot(const QString& fileName,
                          NameFunction nameFunction) const
{
    QFile output(fileName);
    if (!output.open(QIODevice::WriteOnly))
        return false;
    QTextStream s(&output);
    formatDot(s, nameFunction);
    return true;
}

template <class Node>
template <class NameFunction>
void Graph<Node>::formatDot(QTextStream &s,
                            NameFunction nameFunction) const
{
    s << "digraph D {\n";
    for (qsizetype i = 0, size = m_nodes.size(); i < size; ++i) {
        const auto &nodeEntry = m_nodeEntries.at(i);
        if (!nodeEntry.targets.isEmpty()) {
            const QString fromName = nameFunction(m_nodes.at(i));
            for (qsizetype i : nodeEntry.targets)
                s << '"' << fromName << "\" -> \"" << nameFunction(m_nodes.at(i)) << "\"\n";
        }
    }
    s << "}\n";
}

template <class Node>
template <class NameFunction>
bool Graph<Node>::showGraph(const QString &name, NameFunction f) const
{
    QString graph;
    QTextStream s(&graph);
    formatDot(s, f);
    return showDotGraph(name, graph);
}

template <class Node>
void Graph<Node>::format(QDebug &debug) const
{
    const qsizetype size = m_nodeEntries.size();
    debug << "nodes[" << size << "] = (";
    for (qsizetype i = 0; i < size; ++i) {
        const auto &nodeEntry = m_nodeEntries.at(i);
        if (i)
            debug << ", ";
        debug << m_nodes.at(i);
        if (!nodeEntry.targets.isEmpty()) {
            debug << " -> [";
            for (qsizetype t = 0, tsize = nodeEntry.targets.size(); t < tsize; ++t) {
                if (t)
                    debug << ", ";
                debug << m_nodes.at(nodeEntry.targets.at(t));
            }
            debug << ']';
        }
    }
    debug << ')';
}

template <class Node>
QDebug operator<<(QDebug debug, const Graph<Node> &g)
{
    QDebugStateSaver saver(debug);
    debug.noquote();
    debug.nospace();
    debug << "Graph(";
    g.format(debug);
    debug << ')';
    return debug;
}

template <class Node>
void GraphSortResult<Node>::format(QDebug &debug) const
{
    if (isValid())
        debug << "Valid, " << result;
    else
        debug << "Invalid, cyclic dependencies: " << cyclic;
}

template <class Node>
QDebug operator<<(QDebug debug, const GraphSortResult<Node> &r)
{
    QDebugStateSaver saver(debug);
    debug.noquote();
    debug.nospace();
    debug << "Graph::SortResult(";
    r.format(debug);
    debug << ')';
    return debug;
}

#endif // GRAPH_H