10645
|
1 |
/* $Id$ */
|
|
2 |
|
|
3 |
/** @file sortlist_type.h Base types for having sorted lists in GUIs. */
|
|
4 |
|
|
5 |
#ifndef SORTLIST_TYPE_H
|
|
6 |
#define SORTLIST_TYPE_H
|
|
7 |
|
10829
|
8 |
#include "core/enum_type.hpp"
|
|
9 |
#include "core/bitmath_func.hpp"
|
10776
|
10 |
#include "misc/smallvec.h"
|
|
11 |
#include "date_type.h"
|
|
12 |
|
10645
|
13 |
enum SortListFlags {
|
10776
|
14 |
VL_NONE = 0, ///< no sort
|
|
15 |
VL_DESC = 1 << 0, ///< sort descending or ascending
|
|
16 |
VL_RESORT = 1 << 1, ///< instruct the code to resort the list in the next loop
|
|
17 |
VL_REBUILD = 1 << 2, ///< rebuild the sort list
|
|
18 |
VL_FIRST_SORT = 1 << 3, ///< sort with qsort first
|
|
19 |
VL_END = 1 << 4,
|
10645
|
20 |
};
|
|
21 |
DECLARE_ENUM_AS_BIT_SET(SortListFlags);
|
|
22 |
|
|
23 |
struct Listing {
|
|
24 |
bool order; ///< Ascending/descending
|
|
25 |
byte criteria; ///< Sorting criteria
|
|
26 |
};
|
|
27 |
|
|
28 |
template <typename T>
|
10776
|
29 |
class GUIList : public SmallVector<T, 32> {
|
|
30 |
public:
|
|
31 |
typedef int CDECL SortFunction(const T*, const T*);
|
|
32 |
|
|
33 |
public: // Temporary: public for conversion only
|
|
34 |
SortFunction* const *func_list; ///< The sort criteria functions
|
|
35 |
SortListFlags flags; ///< used to control sorting/resorting/etc.
|
|
36 |
uint8 sort_type; ///< what criteria to sort on
|
|
37 |
uint16 resort_timer; ///< resort list after a given amount of ticks if set
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Check if the list is sortable
|
|
41 |
*
|
|
42 |
* @return true if we can sort the list
|
|
43 |
*/
|
|
44 |
bool IsSortable() const
|
|
45 |
{
|
|
46 |
return (this->data != NULL && this->items >= 2);
|
|
47 |
}
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Reset the resort timer
|
|
51 |
*/
|
|
52 |
void ResetResortTimer()
|
|
53 |
{
|
|
54 |
/* Resort every 10 days */
|
|
55 |
this->resort_timer = DAY_TICKS * 10;
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Reverse the list
|
|
60 |
*/
|
|
61 |
void Reverse()
|
|
62 |
{
|
|
63 |
assert(this->IsSortable());
|
|
64 |
|
|
65 |
T *a = this->data;
|
|
66 |
T *b = a + (this->items - 1);
|
|
67 |
|
|
68 |
do {
|
|
69 |
Swap(*a, *b);
|
|
70 |
} while (++a < --b);
|
|
71 |
}
|
|
72 |
|
|
73 |
public:
|
|
74 |
GUIList() :
|
|
75 |
func_list(NULL),
|
|
76 |
flags(VL_FIRST_SORT),
|
|
77 |
sort_type(0),
|
|
78 |
resort_timer(1)
|
|
79 |
{};
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Get the sorttype of the list
|
|
83 |
*
|
|
84 |
* @return The current sorttype
|
|
85 |
*/
|
|
86 |
uint8 SortType() const
|
|
87 |
{
|
|
88 |
return this->sort_type;
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Set the sorttype of the list
|
|
93 |
*
|
|
94 |
* @param n_type the new sort type
|
|
95 |
*/
|
|
96 |
void SetSortType(uint8 n_type)
|
|
97 |
{
|
|
98 |
if (this->sort_type != n_type) {
|
|
99 |
SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT);
|
|
100 |
this->sort_type = n_type;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Export current sort conditions
|
|
106 |
*
|
|
107 |
* @return the current sort conditions
|
|
108 |
*/
|
|
109 |
Listing GetListing() const
|
|
110 |
{
|
|
111 |
Listing l;
|
|
112 |
l.order = HASBITS(this->flags, VL_DESC);
|
|
113 |
l.criteria = this->sort_type;
|
|
114 |
|
|
115 |
return l;
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Import sort conditions
|
|
120 |
*
|
|
121 |
* @param l The sport conditions we want to use
|
|
122 |
*/
|
|
123 |
void SetListing(Listing l)
|
|
124 |
{
|
|
125 |
if (l.order) {
|
|
126 |
SETBITS(this->flags, VL_DESC);
|
|
127 |
} else {
|
|
128 |
CLRBITS(this->flags, VL_DESC);
|
|
129 |
}
|
|
130 |
this->sort_type = l.criteria;
|
|
131 |
|
|
132 |
SETBITS(this->flags, VL_FIRST_SORT);
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Check if a resort is needed next loop
|
|
137 |
* If used the resort timer will decrease every call
|
|
138 |
* till 0. If 0 reached the resort bit will be set and
|
|
139 |
* the timer will be reset.
|
|
140 |
*
|
|
141 |
* @return true if resort bit is set for next loop
|
|
142 |
*/
|
|
143 |
bool NeedResort()
|
|
144 |
{
|
|
145 |
if (--this->resort_timer == 0) {
|
|
146 |
SETBITS(this->flags, VL_RESORT);
|
|
147 |
this->ResetResortTimer();
|
|
148 |
return true;
|
|
149 |
}
|
|
150 |
return false;
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Force a resort next Sort call
|
|
155 |
* Reset the resort timer if used too.
|
|
156 |
*/
|
|
157 |
void ForceResort()
|
|
158 |
{
|
|
159 |
SETBITS(this->flags, VL_RESORT);
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Check if the sort order is descending
|
|
164 |
*
|
|
165 |
* @return true if the sort order is descending
|
|
166 |
*/
|
|
167 |
bool IsDescSortOrder() const
|
|
168 |
{
|
|
169 |
return HASBITS(this->flags, VL_DESC);
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Toogle the sort order
|
|
174 |
* Since that is the worst condition for the sort function
|
|
175 |
* reverse the list here.
|
|
176 |
*/
|
|
177 |
FORCEINLINE void ToggleSortOrder()
|
|
178 |
{
|
|
179 |
this->flags ^= VL_DESC;
|
|
180 |
|
|
181 |
if (this->IsSortable()) this->Reverse();
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* GnomeSort algorithm
|
|
186 |
* This sorting uses a slightly modifyied Gnome search.
|
|
187 |
* The basic Gnome search trys to sort already sorted
|
|
188 |
* list parts. The modification skips these. For the first
|
|
189 |
* sorting we use qsort since it is faster for irregular
|
|
190 |
* sorted data.
|
|
191 |
*
|
|
192 |
* @param compare The function to compare two list items
|
|
193 |
* @return true if the list sequence has been altered
|
|
194 |
* */
|
|
195 |
FORCEINLINE bool Sort(SortFunction *compare)
|
|
196 |
{
|
|
197 |
/* Do not sort if the resort bit is not set */
|
|
198 |
if (!HASBITS(this->flags, VL_RESORT)) return false;
|
|
199 |
|
|
200 |
CLRBITS(this->flags, VL_RESORT);
|
|
201 |
|
|
202 |
this->ResetResortTimer();
|
|
203 |
|
|
204 |
/* Do not sort when the list is not sortable */
|
|
205 |
if (!this->IsSortable()) return false;
|
|
206 |
|
|
207 |
const bool desc = HASBITS(this->flags, VL_DESC);
|
|
208 |
|
|
209 |
if (HASBITS(this->flags, VL_FIRST_SORT)) {
|
|
210 |
CLRBITS(this->flags, VL_FIRST_SORT);
|
|
211 |
qsort(this->data, this->items, sizeof(T), (int (CDECL *)(const void *, const void *))compare);
|
|
212 |
|
|
213 |
if (desc) this->Reverse();
|
|
214 |
return true;
|
|
215 |
}
|
|
216 |
|
|
217 |
T *a = this->data;
|
|
218 |
T *b = a + 1;
|
|
219 |
|
|
220 |
uint length = this->items;
|
|
221 |
uint offset = 0; // Jump variable
|
|
222 |
|
|
223 |
while (length > 1) {
|
|
224 |
const int diff = compare(a, b);
|
|
225 |
if ((!desc && diff <= 0) || (desc && diff >= 0)) {
|
|
226 |
if (offset != 0) {
|
|
227 |
/* Jump back to the last direction switch point */
|
|
228 |
a += offset;
|
|
229 |
b += offset;
|
|
230 |
offset = 0;
|
|
231 |
continue;
|
|
232 |
}
|
|
233 |
a++;
|
|
234 |
b++;
|
|
235 |
length--;
|
|
236 |
} else {
|
|
237 |
Swap(*a, *b);
|
|
238 |
if (a != this->data) {
|
|
239 |
offset++;
|
|
240 |
a--;
|
|
241 |
b--;
|
|
242 |
}
|
|
243 |
}
|
|
244 |
}
|
|
245 |
return true;
|
|
246 |
}
|
|
247 |
|
|
248 |
/**
|
|
249 |
* Hand the array of sort function pointers to the sort list
|
|
250 |
*
|
|
251 |
* @param n_funcs The pointer to the first sort func
|
|
252 |
*/
|
|
253 |
void SetSortFuncs(SortFunction* const* n_funcs)
|
|
254 |
{
|
|
255 |
this->func_list = n_funcs;
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Overload of Sort()
|
|
260 |
* Overloaded to reduce external code
|
|
261 |
*
|
|
262 |
* @return true if the list sequence has been altered
|
|
263 |
*/
|
|
264 |
bool Sort()
|
|
265 |
{
|
|
266 |
assert(this->func_list != NULL);
|
|
267 |
return this->Sort(this->func_list[this->sort_type]);
|
|
268 |
}
|
|
269 |
|
|
270 |
/**
|
|
271 |
* Check if a rebuild is needed
|
|
272 |
* @return true if a rebuild is needed
|
|
273 |
*/
|
|
274 |
bool NeedRebuild() const
|
|
275 |
{
|
|
276 |
return HASBITS(this->flags, VL_REBUILD);
|
|
277 |
}
|
|
278 |
|
|
279 |
/**
|
|
280 |
* Force that a rebuild is needed
|
|
281 |
*/
|
|
282 |
void ForceRebuild()
|
|
283 |
{
|
|
284 |
SETBITS(this->flags, VL_REBUILD);
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Notify the sortlist that the rebuild is done
|
|
289 |
*
|
|
290 |
* @note This forces a resort
|
|
291 |
*/
|
|
292 |
void RebuildDone()
|
|
293 |
{
|
|
294 |
CLRBITS(this->flags, VL_REBUILD);
|
|
295 |
SETBITS(this->flags, VL_RESORT);
|
|
296 |
}
|
10645
|
297 |
};
|
|
298 |
|
|
299 |
#endif /* SORTLIST_TYPE_H */
|