summaryrefslogtreecommitdiffstats
path: root/src/qsectionspans_p.h
blob: 7d67ef8a50fd02b0006e13088b61528a820565fe (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
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef QTSECTIONSPANS_P_H
#define QTSECTIONSPANS_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <qdebug.h>

QT_BEGIN_NAMESPACE

template <typename T>
class QtSectionSpans
{
public:
    QtSectionSpans() : cachedSpanStart(-1), cachedSectionStart(-1) {}

    struct Span {
        inline Span(T v, uint c = 1) : value(v), count(c) {}
        T value;
        uint count;
    };

    void setValue(int index, T value)
    {
        int spanFirstSection, i;
        useCache(index, &i, &spanFirstSection);
        invalidateCache();
        // create span
        for (; i < spans.count(); ++i) {
            int spanSectionCount = spans.at(i).count;
            int spanLastSection = spanFirstSection + spanSectionCount - 1;
            // can we replace the span ?
            if (spanFirstSection == index && spanSectionCount == 1) {
                int prev = i - 1;
                int next = i + 1;
                bool mergeWithPrev = (prev >= 0 && spans.at(prev).value == value);
                bool mergeWithNext = (next < spans.count() && spans.at(next).value == value);
                if (mergeWithPrev && mergeWithNext) {
                    spans[prev].count += spans.at(next).count + 1;
                    // remove the replaced span
                    spans.removeAt(i);
                    // remove the next span
                    spans.removeAt(i);
                    return;
                }
                if (mergeWithPrev) {
                    spans[prev].count += 1;
                    spans.removeAt(i);
                    return;
                }
                if (mergeWithNext) {
                    spans[next].count += 1;
                    spans.removeAt(i);
                    return;
                }
                // replace the span; count is the same
                spans[i].value = value;
                return;
            }
            // is it part of the beginning of the span ?
            if (spanFirstSection == index) {
                spans[i].count -= 1;
                int prev = i - 1;
                bool mergeWithPrev = (prev >= 0 && spans.at(prev).value == value);
                if (mergeWithPrev) {
                    spans[prev].count += 1;
                } else { // we have to create a new span
                    spans.insert(i, Span(value));
                }
                return;
            }
            // is it part of the end of the span ?
            if (spanLastSection == index) {
                spans[i].count -= 1;
                int next = i + 1;
                bool mergeWithNext = (next < spans.count() && spans.at(next).value == value);
                if (mergeWithNext) {
                    spans[next].count += 1;
                } else { // we have to create a new span
                    spans.insert(next, Span(value));
                }
                return;
            }
            // is it in the middle of the span ?
            if (spanFirstSection < index && spanLastSection > index) {
                T spanSectionValue = spans.at(i).value;
                // first span
                spans[i].count = index - spanFirstSection;
                // middle span
                spans.insert(i + 1, Span(value));
                // last span
                int lastSpanCount = spanLastSection - index;
                spans.insert(i + 2, Span(spanSectionValue, lastSpanCount));
                return;
            }
            // next span
            spanFirstSection += spanSectionCount;
        }
        qWarning() << "setValue: no section span found for index" << index;
    }

    T sectionValue(int index, T defaultValue) const
    {
        int sectionStart, i;
        useCache(index, &i, &sectionStart);
        // get value
        for (; i < spans.count(); ++i) {
            const Span &span = spans.at(i);
            int sectionEnd = sectionStart + span.count - 1;
            if (index >= sectionStart && index <= sectionEnd) {
                setCache(i, sectionStart);
                return span.value;
            }
            sectionStart = sectionEnd + 1;
        }
        return defaultValue;
    }

    void addSections(T value, uint count)
    {
        invalidateCache();
        // The value should be the default value of T
        int i = spans.count() - 1;
        // can we merge with the last span ?
        if (!spans.isEmpty() && spans.at(i).value == value) {
            spans[i].count += count;
        } else { // we need to add a new span
            spans.append(Span(value, count));
        }
    }

    void removeSections(int removeCount)
    {
        invalidateCache();
        for (int i = spans.count() - 1; i >= 0 && removeCount > 0; --i) {
            uint sectionSpanCount = spans.at(i).count;
            // can we remove the whole span ?
            if (sectionSpanCount <= uint(removeCount)) {
                spans.removeAt(i);
            } else { // remove part of the span
                spans[i].count -= removeCount;
            }
            removeCount -= sectionSpanCount;
        }
    }

    void insertSectionsAt(T value, int index, int count)
    {
        int spanFirstSection, i;
        useCache(index, &i, &spanFirstSection);
        invalidateCache();
        for (; i < spans.count(); ++i) {
            T spanValue = spans.at(i).value;
            uint spanSectionCount = spans.at(i).count;
            int spanLastSection = spanFirstSection + spanSectionCount - 1;
            // is it the correct span ?
            if (index >= spanFirstSection && index <= spanLastSection) {
                // can we add to the span ?
                if (spanValue == value) {
                    spans[i].count += count;
                } else if (index == spanFirstSection) {
                    // insert before
                    spans.insert(i, Span(value, count));
                } else {
                    // split the span
                    spans[i].count = index - spanFirstSection;
                    spans.insert(i + 1, Span(value, count));
                    spans.insert(i + 2, Span(spanValue, spanSectionCount - index + 1));
                }
                return; // done
            }
            spanFirstSection += spanSectionCount;
        }
        qWarning() << "insertSectionsAt: no section found for index" << index;
    }

    void removeSectionsAt(int index, int removeCount)
    {
        int spanFirstSection, i;
        useCache(index, &i, &spanFirstSection);
        invalidateCache();
        for (; i < spans.count(); ++i) {
            uint spanSectionCount = spans.at(i).count;
            int spanLastSection = spanFirstSection + spanSectionCount - 1;
            if (index >= spanFirstSection && index <= spanLastSection) {
                // can we remove the span ?
                if (index == spanFirstSection && spanSectionCount <= uint(removeCount)) {
                    int prev = qMax(i - 1, 0); // if 'i' is 0, it doesn't matter
                    int next = qMin(i + 1, spans.count() - 1);
                    bool merge = (spans.at(prev).value == spans.at(next).value);
                    if (merge) {
                        spans[prev].count += spans.at(next).count;
                        // remove the span
                        spans.removeAt(i);
                        // remove the next span
                        spans.removeAt(i--);
                    } else {
                        // remove the span
                        spans.removeAt(i--);
                    }
                    removeCount -= spanSectionCount;
                    if (removeCount <= 0)
                        return; // done
                } else { // remove part of the span
                    int spanLocalIndex = (index - spanFirstSection);
                    uint spanRemoveCount = qMin<uint>(spanSectionCount - spanLocalIndex + 1, removeCount);
                    spans[i].count -= spanRemoveCount;
                    spanFirstSection += spans.at(i).count;
                    removeCount -= spanRemoveCount;
                    if (removeCount <= 0)
                        return; // done
                }
            } else { // nothing removed yet
                spanFirstSection += spanSectionCount;
            }
        }
        qWarning() << "removeSectionsAt: no section found for index" << index;
    }

    void replaceAll(T value)
    {
        int count = sectionCount();
        clear();
        spans.append(Span(value, count));
    }

    int sectionCount() const
    {
        int count = 0;
        for (int i = 0; i < spans.count(); ++i)
            count += spans.at(i).count;
        return count;
    }

    // direct QList access

    inline const Span &at(int i) const
    {
        return spans.at(i);
    }

    inline Span &operator[](int i)
    {
        invalidateCache();
        return spans[i];
    }

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

    inline void clear()
    {
        invalidateCache();
        spans.clear();
    }

    // caching

    inline void invalidateCache()
    {
        cachedSpanStart = -1;
        cachedSectionStart = -1;
    }

    inline void useCache(int index, int *spanStart, int *sectionStart) const
    {
        const bool useCache = (cachedSectionStart <= index && cachedSectionStart != -1);
        *sectionStart = (useCache ? cachedSectionStart : 0);
        *spanStart = (useCache ? cachedSpanStart : 0);
    }

    inline void setCache(int spanStart, int sectionStart) const
    {
        cachedSpanStart = spanStart;
        cachedSectionStart = sectionStart;
    }

    QList<Span> spans;
    mutable int cachedSpanStart;
    mutable int cachedSectionStart;
};

QT_END_NAMESPACE

#endif//QTSECTIONSPANS_P_H