Debian Codegraph @

Dekko/utils/PriorityQueue.h

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 Corentin Chary <[email protected]>
4 **
5 ** This file could be part of the QtCore module of the Qt Toolkit.
6 **
7 ** $QT_BEGIN_LICENSE:LGPL$
8 ** No Commercial Usage
9 ** This file contains pre-release code and may not be distributed.
10 ** You may use this file in accordance with the terms and conditions
11 ** contained in the Technology Preview License Agreement accompanying
12 ** this package.
13 **
14 ** GNU Lesser General Public License Usage
15 ** Alternatively, this file may be used under the terms of the GNU Lesser
16 ** General Public License version 2.1 as published by the Free Software
17 ** Foundation and appearing in the file LICENSE.LGPL included in the
18 ** packaging of this file. Please review the following information to
19 ** ensure the GNU Lesser General Public License version 2.1 requirements
20 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
21 **
22 ** In addition, as a special exception, Nokia gives you certain additional
23 ** rights. These rights are described in the Nokia Qt LGPL Exception
24 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 **
26 ** If you have questions regarding the use of this file, please contact
27 ** Nokia at [email protected].
28 **
29 **
30 **
31 **
32 **
33 **
34 **
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #ifndef QPRIORITY_QUEUE_H
41 #define QPRIORITY_QUEUE_H
42
43 #include <algorithm>
44 #include <QList>
45
46 template <class T> class QList;
47
48 /* Fallback class used when stl is not available */
49 template <class T, typename LessThan = std::less < T* > >
50 class QPriorityQueuePrivate {
51 public:
52 inline QPriorityQueuePrivate(LessThan l) : lessThan(l), d() {}
53 inline ~QPriorityQueuePrivate() {}
54
55 inline void clear() { qDeleteAll(d); return d.clear(); }
56 inline int size() const { return d.size(); }
57 inline bool empty() const { return d.empty(); }
58
59 inline T *top() { return d.front(); }
60
61 void pop();
62 void push(T *value);
63
64 QList<T *> toList() {return d;}
65 private:
66 inline int parent(int i) {
67 return (i - 1) / 2;
68 }
69 inline int leftChild(int i) {
70 return 2 * i + 1;
71 }
72 inline int rightChild(int i) {
73 return 2 * i + 2;
74 }
75
76 LessThan lessThan;
77 QList < T *> d;
78 };
79
80 template <class T, typename LessThan = std::less < T* > >
81 class Q_CORE_EXPORT QPriorityQueue
82 {
83 public:
84 inline QPriorityQueue(LessThan l = std::less < T* >())
85 : lessThan(l), d(lessThan) { }
86 inline QPriorityQueue(const QPriorityQueue<T> &q)
87 : lessThan(q.lessThan), d(q.d) { }
88 inline ~QPriorityQueue() { }
89
90 QPriorityQueue<T> &operator=(const QPriorityQueue<T> &q) {
91 d = q.d; return *this;
92 }
93
94 inline int size() const { return d.size(); }
95 inline bool isEmpty() const { return empty(); }
96
97 // like qlist
98 void clear();
99 void append(T *t) { return enqueue(t); }
100 void append(const QList<T*> t);
101 T* takeFirst() { return dequeue(); }
102 inline int length() const { return size(); } // Same as count()
103 inline T* first() { return head(); }
104 inline const T* first() const { return head(); }
105 inline void removeFirst() { pop(); }
106 inline bool startsWith(const T &t) const
107 { return !isEmpty() && first() == t; }
108
109 // like qqueue
110 inline void enqueue(T* t) { push(t); }
111 inline T* dequeue() { T* t = d.top(); d.pop(); return t; }
112 inline T* head() { return top(); }
113 inline const T* head() const { return top(); }
114
115 // stl compatibility
116 typedef int size_type;
117 typedef T* value_type;
118
119 inline bool empty() const { return d.empty(); }
120 inline value_type top() { Q_ASSERT(!isEmpty()); return d.top(); }
121 inline void push(value_type x) { return d.push(x); }
122 inline void pop() { Q_ASSERT(!isEmpty()); d.pop(); }
123 inline QList<T*> toList() { return d.toList(); }
124
125 // comfort
126 inline QPriorityQueue<T> &operator+=(T* t) {
127 enqueue(t); return *this;
128 }
129 inline QPriorityQueue<T> &operator<< (T* t) {
130 enqueue(t); return *this;
131 }
132 inline QPriorityQueue<T> &operator>> (T* t) {
133 t = d.top(); d.pop(); return *this;
134 }
135
136
137 private:
138 LessThan lessThan;
139
140 QPriorityQueuePrivate <T> d;
141 };
142
143
144
145 template <typename T, typename LessThan>
146 Q_INLINE_TEMPLATE void QPriorityQueue<T, LessThan>::clear()
147 {
148 d.clear();
149 }
150
151
152 template <typename T, typename LessThan>
153 Q_OUTOFLINE_TEMPLATE void QPriorityQueue<T, LessThan>::append(const QList<T *> t)
154 {
155 foreach (T * e, t)
156 push(e);
157 }
158
159 /*!
160 * Pop an element from the queue and reorder it using an
161 * inlined binary heap.
162 *
163 * \internal
164 */
165 template <typename T, typename LessThan>
166 Q_OUTOFLINE_TEMPLATE void QPriorityQueuePrivate<T, LessThan>::pop()
167 {
168 int i = 0;
169 ssize_t size = d.size();;
170
171 if(!size)
172 return;
173 if(size == 1)
174 return d.clear();
175
176 d[0] = d.takeLast();
177
178 while(i < size - 1) {
179 int left = leftChild(i);
180 int right = rightChild(i);
181 bool validLeft = left < size;
182 bool validRight = right < size;
183
184 if(validLeft && lessThan(d.at(i), d.at(left)))
185 if(validRight && !lessThan(d.at(right), d.at(left))) {
186 d.swap(i, right);
187 i = right;
188 } else {
189 d.swap(i, left);
190 i = left;
191 }
192 else if(validRight && lessThan(d.at(i), d.at(right))) {
193 d.swap(i, right);
194 i = right;
195 }
196 else
197 break;
198 }
199 }
200
201 /*!
202 * Push an element with a given priority to the right place.
203 *
204 * \internal
205 */
206 template <typename T, typename LessThan>
207 Q_OUTOFLINE_TEMPLATE void QPriorityQueuePrivate<T, LessThan>::push(T *value)
208 {
209 int i = d.size();
210 d.append(value);
211 while(i != 0 && !lessThan(d.at(i), d.at(parent(i)))) {
212 d.swap(i, parent(i));
213 i = parent(i);
214 }
215 }
216
217
218 #endif // QPRIORITY_QUEUE_H