author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Fri, 16 Apr 2010 11:39:52 +0300 | |
branch | RCL_3 |
changeset 8 | 740e5562c97f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt3Support module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef Q3TL_H |
|
43 |
#define Q3TL_H |
|
44 |
||
45 |
#include <QtCore/qalgorithms.h> |
|
46 |
||
47 |
QT_BEGIN_HEADER |
|
48 |
||
49 |
QT_BEGIN_NAMESPACE |
|
50 |
||
51 |
QT_MODULE(Qt3SupportLight) |
|
52 |
||
53 |
template <typename T, typename LessThan> |
|
54 |
Q_OUTOFLINE_TEMPLATE void qHeapSortPushDown(T *heap, int first, int last, LessThan lessThan) |
|
55 |
{ |
|
56 |
int r = first; |
|
57 |
while (r <= last / 2) { |
|
58 |
if (last == 2 * r) { |
|
59 |
// node r has only one child |
|
60 |
if (lessThan(heap[2 * r], heap[r])) |
|
61 |
qSwap(heap[r], heap[2 * r]); |
|
62 |
r = last; |
|
63 |
} else { |
|
64 |
// node r has two children |
|
65 |
if (lessThan(heap[2 * r], heap[r]) && !lessThan(heap[2 * r + 1], heap[2 * r])) { |
|
66 |
// swap with left child |
|
67 |
qSwap(heap[r], heap[2 * r]); |
|
68 |
r *= 2; |
|
69 |
} else if (lessThan(heap[2 * r + 1], heap[r]) |
|
70 |
&& lessThan(heap[2 * r + 1], heap[2 * r])) { |
|
71 |
// swap with right child |
|
72 |
qSwap(heap[r], heap[2 * r + 1]); |
|
73 |
r = 2 * r + 1; |
|
74 |
} else { |
|
75 |
r = last; |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
template <typename BiIterator, typename T, typename LessThan> |
|
82 |
Q_OUTOFLINE_TEMPLATE void qHeapSortHelper(BiIterator begin, BiIterator end, const T & /* dummy */, LessThan lessThan) |
|
83 |
{ |
|
84 |
BiIterator it = begin; |
|
85 |
uint n = 0; |
|
86 |
while (it != end) { |
|
87 |
++n; |
|
88 |
++it; |
|
89 |
} |
|
90 |
if (n == 0) |
|
91 |
return; |
|
92 |
||
93 |
// Create the heap |
|
94 |
BiIterator insert = begin; |
|
95 |
T *realheap = new T[n]; |
|
96 |
T *heap = realheap - 1; |
|
97 |
int size = 0; |
|
98 |
for(; insert != end; ++insert) { |
|
99 |
heap[++size] = *insert; |
|
100 |
int i = size; |
|
101 |
while (i > 1 && lessThan(heap[i], heap[i / 2])) { |
|
102 |
qSwap(heap[i], heap[i / 2]); |
|
103 |
i /= 2; |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
// Now do the sorting |
|
108 |
for (int i = n; i > 0; i--) { |
|
109 |
*begin++ = heap[1]; |
|
110 |
if (i > 1) { |
|
111 |
heap[1] = heap[i]; |
|
112 |
qHeapSortPushDown(heap, 1, i - 1, lessThan); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
delete[] realheap; |
|
117 |
} |
|
118 |
||
119 |
template <typename BiIterator, typename T> |
|
120 |
inline void qHeapSortHelper(BiIterator begin, BiIterator end, const T &dummy) |
|
121 |
{ |
|
122 |
qHeapSortHelper(begin, end, dummy, qLess<T>()); |
|
123 |
} |
|
124 |
||
125 |
template <typename BiIterator, typename LessThan> |
|
126 |
inline void qHeapSort(BiIterator begin, BiIterator end, LessThan lessThan) |
|
127 |
{ |
|
128 |
if (begin != end) |
|
129 |
qHeapSortHelper(begin, end, *begin, lessThan); |
|
130 |
} |
|
131 |
||
132 |
template <typename BiIterator> |
|
133 |
inline void qHeapSort(BiIterator begin, BiIterator end) |
|
134 |
{ |
|
135 |
if (begin != end) |
|
136 |
qHeapSortHelper(begin, end, *begin); |
|
137 |
} |
|
138 |
||
139 |
template <typename Container> |
|
140 |
inline void qHeapSort(Container &c) |
|
141 |
{ |
|
142 |
#ifdef Q_CC_BOR |
|
143 |
// Work around Borland 5.5 optimizer bug |
|
144 |
c.detach(); |
|
145 |
#endif |
|
146 |
if (!c.empty()) |
|
147 |
qHeapSortHelper(c.begin(), c.end(), *c.begin()); |
|
148 |
} |
|
149 |
||
150 |
||
151 |
template <typename BiIterator, typename LessThan> |
|
152 |
void qBubbleSort(BiIterator begin, BiIterator end, LessThan lessThan) |
|
153 |
{ |
|
154 |
// Goto last element; |
|
155 |
BiIterator last = end; |
|
156 |
||
157 |
// empty list |
|
158 |
if (begin == end) |
|
159 |
return; |
|
160 |
||
161 |
--last; |
|
162 |
// only one element ? |
|
163 |
if (last == begin) |
|
164 |
return; |
|
165 |
||
166 |
// So we have at least two elements in here |
|
167 |
while (begin != last) { |
|
168 |
bool swapped = false; |
|
169 |
BiIterator swapPos = begin; |
|
170 |
BiIterator x = end; |
|
171 |
BiIterator y = x; |
|
172 |
y--; |
|
173 |
do { |
|
174 |
--x; |
|
175 |
--y; |
|
176 |
if (lessThan(*x, *y)) { |
|
177 |
swapped = true; |
|
178 |
qSwap(*x, *y); |
|
179 |
swapPos = y; |
|
180 |
} |
|
181 |
} while (y != begin); |
|
182 |
if (!swapped) |
|
183 |
return; |
|
184 |
begin = swapPos; |
|
185 |
++begin; |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
template <typename BiIterator, typename T> |
|
190 |
void qBubbleSortHelper(BiIterator begin, BiIterator end, T) |
|
191 |
{ |
|
192 |
qBubbleSort(begin, end, qLess<T>()); |
|
193 |
} |
|
194 |
||
195 |
template <typename BiIterator> |
|
196 |
void qBubbleSort(BiIterator begin, BiIterator end) |
|
197 |
{ |
|
198 |
if (begin != end) |
|
199 |
qBubbleSortHelper(begin, end, *begin); |
|
200 |
} |
|
201 |
||
202 |
template <typename Container> |
|
203 |
inline void qBubbleSort(Container &c) |
|
204 |
{ |
|
205 |
qBubbleSort(c.begin(), c.end()); |
|
206 |
} |
|
207 |
||
208 |
QT_END_NAMESPACE |
|
209 |
||
210 |
QT_END_HEADER |
|
211 |
||
212 |
#endif // Q3TL_H |