]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemrange.h
Merge branch 'release/20.12'
[dolphin.git] / src / kitemviews / kitemrange.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
4 *
5 * Based on the Itemviews NG project from Trolltech Labs
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #ifndef KITEMRANGE_H
11 #define KITEMRANGE_H
12
13 #include <QList>
14
15 struct KItemRange
16 {
17 KItemRange(int index = 0, int count = 0);
18 int index;
19 int count;
20
21 bool operator == (const KItemRange& other) const;
22 };
23
24 inline KItemRange::KItemRange(int index, int count) :
25 index(index),
26 count(count)
27 {
28 }
29
30 inline bool KItemRange::operator == (const KItemRange& other) const
31 {
32 return index == other.index && count == other.count;
33 }
34
35
36 class KItemRangeList : public QList<KItemRange>
37 {
38 public:
39 KItemRangeList() : QList<KItemRange>() {}
40 explicit KItemRangeList(const QList<KItemRange>& list) : QList<KItemRange>(list) {}
41
42 template<class Container>
43 static KItemRangeList fromSortedContainer(const Container& container);
44
45 KItemRangeList& operator<<(const KItemRange& range)
46 {
47 append(range);
48 return *this;
49 }
50 };
51
52 template<class Container>
53 KItemRangeList KItemRangeList::fromSortedContainer(const Container& container)
54 {
55 typename Container::const_iterator it = container.constBegin();
56 const typename Container::const_iterator end = container.constEnd();
57
58 if (it == end) {
59 return KItemRangeList();
60 }
61
62 KItemRangeList result;
63
64 int index = *it;
65 int count = 1;
66
67 // Remove duplicates, see https://bugs.kde.org/show_bug.cgi?id=335672
68 while (it != end && *it == index) {
69 ++it;
70 }
71
72 while (it != end) {
73 if (*it == index + count) {
74 ++count;
75 } else {
76 result << KItemRange(index, count);
77 index = *it;
78 count = 1;
79 }
80 ++it;
81
82 // Remove duplicates, see https://bugs.kde.org/show_bug.cgi?id=335672
83 while (it != end && *it == *(it - 1)) {
84 ++it;
85 }
86 }
87
88 result << KItemRange(index, count);
89 return result;
90 }
91
92 #endif