OpenTTD
sortlist_type.h
Go to the documentation of this file.
1 /* $Id: sortlist_type.h 24900 2013-01-08 22:46:42Z planetmaker $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifndef SORTLIST_TYPE_H
13 #define SORTLIST_TYPE_H
14 
15 #include "core/enum_type.hpp"
16 #include "core/bitmath_func.hpp"
17 #include "core/sort_func.hpp"
18 #include "core/smallvec_type.hpp"
19 #include "date_type.h"
20 
23  VL_NONE = 0,
24  VL_DESC = 1 << 0,
25  VL_RESORT = 1 << 1,
26  VL_REBUILD = 1 << 2,
27  VL_FIRST_SORT = 1 << 3,
28  VL_FILTER = 1 << 4,
29  VL_END = 1 << 5,
30 };
32 
33 
34 struct Listing {
35  bool order;
36  byte criteria;
37 };
39 struct Filtering {
40  bool state;
41  byte criteria;
42 };
43 
49 template <typename T, typename F = const char*>
50 class GUIList : public SmallVector<T, 32> {
51 public:
52  typedef int CDECL SortFunction(const T*, const T*);
53  typedef bool CDECL FilterFunction(const T*, F);
54 
55 protected:
56  SortFunction * const *sort_func_list;
57  FilterFunction * const *filter_func_list;
59  uint8 sort_type;
60  uint8 filter_type;
61  uint16 resort_timer;
62 
68  bool IsSortable() const
69  {
70  return (this->data != NULL && this->items >= 2);
71  }
72 
77  {
78  /* Resort every 10 days */
79  this->resort_timer = DAY_TICKS * 10;
80  }
81 
82 public:
83  GUIList() :
84  sort_func_list(NULL),
85  filter_func_list(NULL),
86  flags(VL_FIRST_SORT),
87  sort_type(0),
88  filter_type(0),
89  resort_timer(1)
90  {};
91 
97  uint8 SortType() const
98  {
99  return this->sort_type;
100  }
101 
107  void SetSortType(uint8 n_type)
108  {
109  if (this->sort_type != n_type) {
110  SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT);
111  this->sort_type = n_type;
112  }
113  }
114 
121  {
122  Listing l;
123  l.order = (this->flags & VL_DESC) != 0;
124  l.criteria = this->sort_type;
125 
126  return l;
127  }
128 
135  {
136  if (l.order) {
137  SETBITS(this->flags, VL_DESC);
138  } else {
139  CLRBITS(this->flags, VL_DESC);
140  }
141  this->sort_type = l.criteria;
142 
143  SETBITS(this->flags, VL_FIRST_SORT);
144  }
145 
151  uint8 FilterType() const
152  {
153  return this->filter_type;
154  }
155 
161  void SetFilterType(uint8 n_type)
162  {
163  if (this->filter_type != n_type) {
164  this->filter_type = n_type;
165  }
166  }
167 
174  {
175  Filtering f;
176  f.state = (this->flags & VL_FILTER) != 0;
177  f.criteria = this->filter_type;
178 
179  return f;
180  }
181 
188  {
189  if (f.state) {
190  SETBITS(this->flags, VL_FILTER);
191  } else {
192  CLRBITS(this->flags, VL_FILTER);
193  }
194  this->filter_type = f.criteria;
195  }
196 
205  bool NeedResort()
206  {
207  if (--this->resort_timer == 0) {
208  SETBITS(this->flags, VL_RESORT);
209  this->ResetResortTimer();
210  return true;
211  }
212  return false;
213  }
214 
219  void ForceResort()
220  {
221  SETBITS(this->flags, VL_RESORT);
222  }
223 
229  bool IsDescSortOrder() const
230  {
231  return (this->flags & VL_DESC) != 0;
232  }
233 
240  {
241  this->flags ^= VL_DESC;
242 
243  if (this->IsSortable()) MemReverseT(this->data, this->items);
244  }
245 
256  bool Sort(SortFunction *compare)
257  {
258  /* Do not sort if the resort bit is not set */
259  if (!(this->flags & VL_RESORT)) return false;
260 
261  CLRBITS(this->flags, VL_RESORT);
262 
263  this->ResetResortTimer();
264 
265  /* Do not sort when the list is not sortable */
266  if (!this->IsSortable()) return false;
267 
268  const bool desc = (this->flags & VL_DESC) != 0;
269 
270  if (this->flags & VL_FIRST_SORT) {
271  CLRBITS(this->flags, VL_FIRST_SORT);
272 
273  QSortT(this->data, this->items, compare, desc);
274  return true;
275  }
276 
277  GSortT(this->data, this->items, compare, desc);
278  return true;
279  }
280 
286  void SetSortFuncs(SortFunction * const *n_funcs)
287  {
288  this->sort_func_list = n_funcs;
289  }
290 
297  bool Sort()
298  {
299  assert(this->sort_func_list != NULL);
300  return this->Sort(this->sort_func_list[this->sort_type]);
301  }
302 
308  bool IsFilterEnabled() const
309  {
310  return (this->flags & VL_FILTER) != 0;
311  }
312 
319  {
320  if (state) {
321  SETBITS(this->flags, VL_FILTER);
322  } else {
323  CLRBITS(this->flags, VL_FILTER);
324  }
325  }
326 
334  bool Filter(FilterFunction *decide, F filter_data)
335  {
336  /* Do not filter if the filter bit is not set */
337  if (!(this->flags & VL_FILTER)) return false;
338 
339  bool changed = false;
340  for (uint iter = 0; iter < this->items;) {
341  T *item = &this->data[iter];
342  if (!decide(item, filter_data)) {
343  this->Erase(item);
344  changed = true;
345  } else {
346  iter++;
347  }
348  }
349 
350  return changed;
351  }
352 
358  void SetFilterFuncs(FilterFunction * const *n_funcs)
359  {
360  this->filter_func_list = n_funcs;
361  }
362 
369  bool Filter(F filter_data)
370  {
371  if (this->filter_func_list == NULL) return false;
372  return this->Filter(this->filter_func_list[this->filter_type], filter_data);
373  }
374 
379  bool NeedRebuild() const
380  {
381  return (this->flags & VL_REBUILD) != 0;
382  }
383 
388  {
389  SETBITS(this->flags, VL_REBUILD);
390  }
391 
397  void RebuildDone()
398  {
399  CLRBITS(this->flags, VL_REBUILD);
400  SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT);
401  }
402 };
403 
404 #endif /* SORTLIST_TYPE_H */
List template of &#39;things&#39; T to sort in a GUI.
Definition: sortlist_type.h:50
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
void RebuildDone()
Notify the sortlist that the rebuild is done.
static void GSortT(T *base, uint num, int(CDECL *comparator)(const T *, const T *), bool desc=false)
Type safe Gnome Sort.
Definition: sort_func.hpp:52
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
bool Filter(FilterFunction *decide, F filter_data)
Filter the list.
uint8 filter_type
what criteria to filter on
Definition: sortlist_type.h:60
void SetSortFuncs(SortFunction *const *n_funcs)
Hand the array of sort function pointers to the sort list.
uint16 resort_timer
resort list after a given amount of ticks if set
Definition: sortlist_type.h:61
#define SETBITS(x, y)
Sets several bits in a variable.
void SetFilterType(uint8 n_type)
Set the filtertype of the list.
static const int DAY_TICKS
1 day is 74 ticks; _date_fract used to be uint16 and incremented by 885.
Definition: date_type.h:30
bool Filter(F filter_data)
Filter the data with the currently selected filter.
Functions related to bit mathematics.
bool NeedResort()
Check if a resort is needed next loop If used the resort timer will decrease every call till 0...
SortListFlags flags
used to control sorting/resorting/etc.
Definition: sortlist_type.h:58
Simple vector template class.
void SetFilterFuncs(FilterFunction *const *n_funcs)
Hand the array of filter function pointers to the sort list.
#define CLRBITS(x, y)
Clears several bits in a variable.
static void MemReverseT(T *ptr1, T *ptr2)
Type safe memory reverse operation.
Definition: mem_func.hpp:79
rebuild the sort list
Definition: sortlist_type.h:26
void SetListing(Listing l)
Import sort conditions.
Type (helpers) for enums.
Data structure describing what to show in the list (filter criteria).
Definition: sortlist_type.h:39
bool NeedRebuild() const
Check if a rebuild is needed.
sort descending or ascending
Definition: sortlist_type.h:24
void ForceRebuild()
Force that a rebuild is needed.
bool IsSortable() const
Check if the list is sortable.
Definition: sortlist_type.h:68
byte criteria
Filtering criteria.
Definition: sortlist_type.h:41
SortFunction *const * sort_func_list
the sort criteria functions
Definition: sortlist_type.h:56
void ResetResortTimer()
Reset the resort timer.
Definition: sortlist_type.h:76
Listing GetListing() const
Export current sort conditions.
bool order
Ascending/descending.
Definition: sortlist_type.h:35
sort with quick sort first
Definition: sortlist_type.h:27
bool state
Filter on/off.
Definition: sortlist_type.h:40
instruct the code to resort the list in the next loop
Definition: sortlist_type.h:25
SortListFlags
Flags of the sort list.
Definition: sortlist_type.h:22
no sort
Definition: sortlist_type.h:23
void SetSortType(uint8 n_type)
Set the sorttype of the list.
bool Sort(SortFunction *compare)
Sort the list.
void ForceResort()
Force a resort next Sort call Reset the resort timer if used too.
uint8 sort_type
what criteria to sort on
Definition: sortlist_type.h:59
void SetFilterState(bool state)
Enable or disable the filter.
filter disabled/enabled
Definition: sortlist_type.h:28
void ToggleSortOrder()
Toggle the sort order Since that is the worst condition for the sort function reverse the list here...
bool CDECL FilterFunction(const T *, F)
Signature of filter function.
Definition: sortlist_type.h:53
int CDECL SortFunction(const T *, const T *)
Signature of sort function.
Definition: sortlist_type.h:52
Functions related to sorting operations.
Data structure describing how to show the list (what sort direction and criteria).
Definition: sortlist_type.h:34
Types related to the dates in OpenTTD.
bool IsFilterEnabled() const
Check if the filter is enabled.
void SetFiltering(Filtering f)
Import filter conditions.
bool IsDescSortOrder() const
Check if the sort order is descending.
byte criteria
Sorting criteria.
Definition: sortlist_type.h:36
Filtering GetFiltering() const
Export current filter conditions.
uint8 FilterType() const
Get the filtertype of the list.
static void QSortT(T *base, uint num, int(CDECL *comparator)(const T *, const T *), bool desc=false)
Type safe qsort()
Definition: sort_func.hpp:28
bool Sort()
Overload of Sort(SortFunction *compare) Overloaded to reduce external code.
FilterFunction *const * filter_func_list
the filter criteria functions
Definition: sortlist_type.h:57
uint8 SortType() const
Get the sorttype of the list.
Definition: sortlist_type.h:97