]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemset.cpp
1200bc7a97c145450064d5a143f4bba7425dc1b1
2 * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
10 KItemSet::iterator
KItemSet::insert(int i
)
12 if (m_itemRanges
.empty()) {
13 m_itemRanges
.push_back(KItemRange(i
, 1));
14 return iterator(m_itemRanges
.begin(), 0);
17 KItemRangeList::iterator rangeBegin
= m_itemRanges
.begin();
18 if (i
< rangeBegin
->index
) {
19 // The inserted index is smaller than all existing items.
20 if (i
== rangeBegin
->index
- 1) {
21 // Move the beginning of the first range one item to the front.
25 // Insert a new range at the beginning.
26 rangeBegin
= m_itemRanges
.insert(rangeBegin
, KItemRange(i
, 1));
29 return iterator(rangeBegin
, 0);
32 KItemRangeList::iterator rangeEnd
= m_itemRanges
.end();
33 KItemRangeList::iterator lastRange
= rangeEnd
- 1;
34 if (i
>= lastRange
->index
) {
35 // i either belongs to the last range, or it is larger than all existing items.
36 const int lastItemPlus1
= lastRange
->index
+ lastRange
->count
;
37 if (i
== lastItemPlus1
) {
38 // Move the end of the last range one item to the back.
40 } else if (i
> lastItemPlus1
) {
41 // Append a new range.
42 lastRange
= m_itemRanges
.insert(rangeEnd
, KItemRange(i
, 1));
45 return iterator(lastRange
, i
- lastRange
->index
);
48 // We know that i is between the smallest existing item and the first item
49 // of the last range. Find the lowest range whose 'index' is smaller than i.
50 KItemRangeList::iterator low
= rangeBegin
;
51 KItemRangeList::iterator high
= lastRange
;
53 while (low
+ 1 != high
) {
54 const int span
= high
- low
;
57 KItemRangeList::iterator mid
= low
+ span
/ 2;
65 Q_ASSERT(low
->index
<= i
&& high
->index
> i
);
67 if (i
== low
->index
+ low
->count
) {
68 // i is just one item behind the range low.
69 if (i
== high
->index
- 1) {
70 // i closes the gap between low and high. Merge the two ranges.
71 const int newRangeCount
= low
->count
+ 1 + high
->count
;
72 KItemRangeList::iterator behindNewRange
= m_itemRanges
.erase(high
);
73 KItemRangeList::iterator newRange
= behindNewRange
- 1;
74 newRange
->count
= newRangeCount
;
75 return iterator(newRange
, i
- newRange
->index
);
77 // Extend low by one item.
79 return iterator(low
, low
->count
- 1);
81 } else if (i
> low
->index
+ low
->count
) {
82 if (i
== high
->index
- 1) {
83 // Extend high by one item to the front.
86 return iterator(high
, 0);
88 // Insert a new range between low and high.
89 KItemRangeList::iterator newRange
= m_itemRanges
.insert(high
, KItemRange(i
, 1));
90 return iterator(newRange
, 0);
93 // The range low already contains i.
94 return iterator(low
, i
- low
->index
);
98 KItemSet::iterator
KItemSet::erase(iterator it
)
100 KItemRangeList::iterator rangeIt
= it
.m_rangeIt
;
102 if (it
.m_offset
== 0) {
103 // Removed index is at the beginning of a range.
104 if (rangeIt
->count
> 1) {
108 // The range only contains the removed index.
109 rangeIt
= m_itemRanges
.erase(rangeIt
);
111 return iterator(rangeIt
, 0);
112 } else if (it
.m_offset
== rangeIt
->count
- 1) {
113 // Removed index is at the end of a range.
116 return iterator(rangeIt
, 0);
118 // The removed index is in the middle of a range.
119 const int newRangeIndex
= *it
+ 1;
120 const int newRangeCount
= rangeIt
->count
- it
.m_offset
- 1;
121 const KItemRange
newRange(newRangeIndex
, newRangeCount
);
123 rangeIt
->count
= it
.m_offset
;
125 rangeIt
= m_itemRanges
.insert(rangeIt
, newRange
);
127 return iterator(rangeIt
, 0);
131 KItemSet
KItemSet::operator+(const KItemSet
& other
) const
135 KItemRangeList::const_iterator it1
= m_itemRanges
.constBegin();
136 KItemRangeList::const_iterator it2
= other
.m_itemRanges
.constBegin();
138 const KItemRangeList::const_iterator end1
= m_itemRanges
.constEnd();
139 const KItemRangeList::const_iterator end2
= other
.m_itemRanges
.constEnd();
141 while (it1
!= end1
|| it2
!= end2
) {
143 // We are past the end of 'this' already. Append all remaining
144 // item ranges from 'other'.
145 while (it2
!= end2
) {
146 sum
.m_itemRanges
.append(*it2
);
149 } else if (it2
== end2
) {
150 // We are past the end of 'other' already. Append all remaining
151 // item ranges from 'this'.
152 while (it1
!= end1
) {
153 sum
.m_itemRanges
.append(*it1
);
157 // Find the beginning of the next range.
158 int index
= qMin(it1
->index
, it2
->index
);
162 if (it1
!= end1
&& it1
->index
<= index
+ count
) {
163 // The next range from 'this' overlaps with the current range in the sum.
164 count
= qMax(count
, it1
->index
+ it1
->count
- index
);
168 if (it2
!= end2
&& it2
->index
<= index
+ count
) {
169 // The next range from 'other' overlaps with the current range in the sum.
170 count
= qMax(count
, it2
->index
+ it2
->count
- index
);
173 } while ((it1
!= end1
&& it1
->index
<= index
+ count
)
174 || (it2
!= end2
&& it2
->index
<= index
+ count
));
176 sum
.m_itemRanges
.append(KItemRange(index
, count
));
183 KItemSet
KItemSet::operator^(const KItemSet
& other
) const
185 // We are looking for all ints which are either in *this or in other,
189 // When we go through all integers from INT_MIN to INT_MAX and start
190 // in the state "do not add to result", every beginning/end of a range
191 // of *this and other toggles the "add/do not add to result" state.
192 // Therefore, we just have to put ints where any range starts/ends to
193 // a sorted array, and then we can calculate the result quite easily.
194 QVector
<int> rangeBoundaries
;
195 rangeBoundaries
.resize(2 * (m_itemRanges
.count() + other
.m_itemRanges
.count()));
196 const QVector
<int>::iterator begin
= rangeBoundaries
.begin();
197 const QVector
<int>::iterator end
= rangeBoundaries
.end();
198 QVector
<int>::iterator it
= begin
;
200 for (const KItemRange
& range
: qAsConst(m_itemRanges
)) {
202 *it
++ = range
.index
+ range
.count
;
205 const QVector
<int>::iterator middle
= it
;
207 for (const KItemRange
& range
: qAsConst(other
.m_itemRanges
)) {
209 *it
++ = range
.index
+ range
.count
;
213 std::inplace_merge(begin
, middle
, end
);
217 const int rangeBegin
= *it
;
220 if (*it
== rangeBegin
) {
221 // It seems that ranges from both *this and other start at
222 // rangeBegin. Do not start a new range, but read the next int.
224 // Example: Consider the symmetric difference of the sets
225 // {1, 2, 3, 4} and {1, 2}. The sorted list of range boundaries is
226 // 1 1 3 5. Discarding the duplicate 1 yields the result
227 // rangeBegin = 3, rangeEnd = 5, which corresponds to the set {3, 4}.
230 // The end of the current range is the next *single* int that we
231 // find. If an int appears twice in rangeBoundaries, the range does
234 // Example: Consider the symmetric difference of the sets
235 // {1, 2, 3, 4, 8, 9, 10} and {5, 6, 7}. The sorted list of range
236 // boundaries is 1 5 5 8 8 11, and discarding all duplicates yields
237 // the result rangeBegin = 1, rangeEnd = 11, which corresponds to
238 // the set {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
239 bool foundEndOfRange
= false;
245 if (it
== end
|| *it
!= rangeEnd
) {
246 foundEndOfRange
= true;
250 } while (!foundEndOfRange
);
252 result
.m_itemRanges
.append(KItemRange(rangeBegin
, rangeEnd
- rangeBegin
));
259 bool KItemSet::isValid() const
261 const KItemRangeList::const_iterator begin
= m_itemRanges
.constBegin();
262 const KItemRangeList::const_iterator end
= m_itemRanges
.constEnd();
264 for (KItemRangeList::const_iterator it
= begin
; it
!= end
; ++it
) {
265 if (it
->count
<= 0) {
270 const KItemRangeList::const_iterator previous
= it
- 1;
271 if (previous
->index
+ previous
->count
>= it
->index
) {
280 KItemRangeList::iterator
KItemSet::rangeForItem(int i
)
282 const KItemRangeList::iterator end
= m_itemRanges
.end();
283 KItemRangeList::iterator low
= m_itemRanges
.begin();
284 KItemRangeList::iterator high
= end
;
286 if (low
== end
|| low
->index
> i
) {
290 while (low
!= high
&& low
+ 1 != high
) {
291 KItemRangeList::iterator mid
= low
+ (high
- low
) / 2;
292 if (mid
->index
> i
) {
299 Q_ASSERT(low
->index
<= i
);
300 if (low
->index
+ low
->count
> i
) {
307 KItemRangeList::const_iterator
KItemSet::constRangeForItem(int i
) const
309 const KItemRangeList::const_iterator end
= m_itemRanges
.constEnd();
310 KItemRangeList::const_iterator low
= m_itemRanges
.constBegin();
311 KItemRangeList::const_iterator high
= end
;
313 if (low
== end
|| low
->index
> i
) {
317 while (low
!= high
&& low
+ 1 != high
) {
318 KItemRangeList::const_iterator mid
= low
+ (high
- low
) / 2;
319 if (mid
->index
> i
) {
326 Q_ASSERT(low
->index
<= i
);
327 if (low
->index
+ low
->count
> i
) {