sortedtimelinemodel.h 7.96 KB
Newer Older
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
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
** Contact: http://www.qt-project.org/legal
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#ifndef SORTEDTIMELINEMODEL_H
#define SORTEDTIMELINEMODEL_H

33
#include "abstracttimelinemodel_p.h"
34
35
36
37
38
#include <QVector>
#include <QLinkedList>

namespace QmlProfiler {

39
40
41
42
43
// The template has to be inserted into the hierarchy of public/private classes when Data is known.
// Otherwise we'd have to add implementation details to the public headers. This is why the class to
// be derived from is given as template parameter.
template<class Data, class Base = AbstractTimelineModel::AbstractTimelineModelPrivate>
class SortedTimelineModel : public Base {
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
public:
    struct Range : public Data {
        Range() : Data(), start(-1), duration(-1), parent(-1) {}
        Range(qint64 start, qint64 duration, const Data &item) :
            Data(item), start(start), duration(duration), parent(-1) {}
        qint64 start;
        qint64 duration;
        int parent;
        inline qint64 timestamp() const {return start;}
    };

    struct RangeEnd {
        RangeEnd() : startIndex(-1), end(-1) {}
        RangeEnd(int startIndex, qint64 end) :
            startIndex(startIndex), end(end) {}
        int startIndex;
        qint64 end;
        inline qint64 timestamp() const {return end;}
    };

    void clear()
    {
        ranges.clear();
        endTimes.clear();
    }

    inline int count() const { return ranges.count(); }

72
73
74
    qint64 duration(int index) const { return range(index).duration; }
    qint64 startTime(int index) const { return range(index).start; }

75
76
77
    inline qint64 lastEndTime() const { return endTimes.last().end; }
    inline qint64 firstStartTime() const { return ranges.first().start; }

78
    inline const Range &range(int index) const { return ranges[index]; }
79
80
81
82
83
84
85
    inline Data &data(int index) { return ranges[index]; }

    inline int insert(qint64 startTime, qint64 duration, const Data &item)
    {
        /* Doing insert-sort here is preferable as most of the time the times will actually be
         * presorted in the right way. So usually this will just result in appending. */
        int index = insertSorted(ranges, Range(startTime, duration, item));
86
87
        if (index < ranges.size() - 1)
            incrementStartIndices(index);
88
89
90
91
92
93
        insertSorted(endTimes, RangeEnd(index, startTime + duration));
        return index;
    }

    inline int insertStart(qint64 startTime, const Data &item)
    {
94
95
96
97
        int index = insertSorted(ranges, Range(startTime, 0, item));
        if (index < ranges.size() - 1)
            incrementStartIndices(index);
        return index;
98
99
100
101
102
103
104
105
    }

    inline void insertEnd(int index, qint64 duration)
    {
        ranges[index].duration = duration;
        insertSorted(endTimes, RangeEnd(index, ranges[index].start + duration));
    }

106
    inline int firstIndex(qint64 startTime) const
107
    {
108
        int index = firstIndexNoParents(startTime);
109
110
111
112
113
114
        if (index == -1)
            return -1;
        int parent = ranges[index].parent;
        return parent == -1 ? index : parent;
    }

115
    inline int firstIndexNoParents(qint64 startTime) const
116
117
118
119
120
121
122
123
124
125
126
127
    {
        // in the "endtime" list, find the first event that ends after startTime
        if (endTimes.isEmpty())
            return -1;
        if (endTimes.count() == 1 || endTimes.first().end >= startTime)
            return endTimes.first().startIndex;
        if (endTimes.last().end <= startTime)
            return -1;

        return endTimes[lowerBound(endTimes, startTime) + 1].startIndex;
    }

128
    inline int lastIndex(qint64 endTime) const
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    {
        // in the "starttime" list, find the last event that starts before endtime
        if (ranges.isEmpty() || ranges.first().start >= endTime)
            return -1;
        if (ranges.count() == 1)
            return 0;
        if (ranges.last().start <= endTime)
            return ranges.count() - 1;

        return lowerBound(ranges, endTime);
    }

    inline void computeNesting()
    {
        QLinkedList<int> parents;
        for (int range = 0; range != count(); ++range) {
            Range &current = ranges[range];
146
147
148
            for (QLinkedList<int>::iterator parentIt = parents.begin();;) {
                Range &parent = ranges[*parentIt];
                qint64 parentEnd = parent.start + parent.duration;
149
                if (parentEnd < current.start) {
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
                    if (parent.start == current.start) {
                        if (parent.parent == -1) {
                            parent.parent = range;
                        } else {
                            Range &ancestor = ranges[parent.parent];
                            if (ancestor.start == current.start &&
                                    ancestor.duration < current.duration)
                                parent.parent = range;
                        }
                        // Just switch the old parent range for the new, larger one
                        *parentIt = range;
                        break;
                    } else {
                        parentIt = parents.erase(parentIt);
                    }
165
                } else if (parentEnd >= current.start + current.duration) {
166
167
                    // no need to insert
                    current.parent = *parentIt;
168
169
                    break;
                } else {
170
171
172
173
174
175
                    ++parentIt;
                }

                if (parentIt == parents.end()) {
                    parents.append(range);
                    break;
176
177
178
179
180
181
                }
            }
        }
    }

protected:
182
183
184
185
186
187
188
189
    void incrementStartIndices(int index)
    {
        for (int i = 0; i < endTimes.size(); ++i) {
            if (endTimes[i].startIndex >= index)
                endTimes[i].startIndex++;
        }
    }

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    template<typename RangeDelimiter>
    static inline int insertSorted(QVector<RangeDelimiter> &container, const RangeDelimiter &item)
    {
        for (int i = container.count();;) {
            if (i == 0) {
                container.prepend(item);
                return 0;
            }
            if (container[--i].timestamp() <= item.timestamp()) {
                container.insert(++i, item);
                return i;
            }
        }
    }

    template<typename RangeDelimiter>
206
    static inline int lowerBound(const QVector<RangeDelimiter> &container, qint64 time)
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    {
        int fromIndex = 0;
        int toIndex = container.count() - 1;
        while (toIndex - fromIndex > 1) {
            int midIndex = (fromIndex + toIndex)/2;
            if (container[midIndex].timestamp() < time)
                fromIndex = midIndex;
            else
                toIndex = midIndex;
        }

        return fromIndex;
    }

    QVector<Range> ranges;
    QVector<RangeEnd> endTimes;
};

}

#endif