]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemrange.h
GIT_SILENT Update Appstream for new release
[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 KItemRange(int index = 0, int count = 0);
17 int index;
18 int count;
19
20 bool operator==(const KItemRange &other) const;
21 };
22
23 inline KItemRange::KItemRange(int index, int count)
24 : index(index)
25 , count(count)
26 {
27 }
28
29 inline bool KItemRange::operator==(const KItemRange &other) const
30 {
31 return index == other.index && count == other.count;
32 }
33
34 class KItemRangeList : public QList<KItemRange>
35 {
36 public:
37 KItemRangeList()
38 : QList<KItemRange>()
39 {
40 }
41 explicit KItemRangeList(const QList<KItemRange> &list)
42 : QList<KItemRange>(list)
43 {
44 }
45
46 template<class Container>
47 static KItemRangeList fromSortedContainer(const Container &container);
48
49 KItemRangeList &operator<<(const KItemRange &range)
50 {
51 append(range);
52 return *this;
53 }
54 };
55
56 template<class Container>
57 KItemRangeList KItemRangeList::fromSortedContainer(const Container &container)
58 {
59 typename Container::const_iterator it = container.constBegin();
60 const typename Container::const_iterator end = container.constEnd();
61
62 if (it == end) {
63 return KItemRangeList();
64 }
65
66 KItemRangeList result;
67
68 int index = *it;
69 int count = 1;
70
71 // Remove duplicates, see https://bugs.kde.org/show_bug.cgi?id=335672
72 while (it != end && *it == index) {
73 ++it;
74 }
75
76 while (it != end) {
77 if (*it == index + count) {
78 ++count;
79 } else {
80 result << KItemRange(index, count);
81 index = *it;
82 count = 1;
83 }
84 ++it;
85
86 // Remove duplicates, see https://bugs.kde.org/show_bug.cgi?id=335672
87 while (it != end && *it == *(it - 1)) {
88 ++it;
89 }
90 }
91
92 result << KItemRange(index, count);
93 return result;
94 }
95
96 #endif