10 #ifndef SORTLIST_TYPE_H 11 #define SORTLIST_TYPE_H 46 template <
typename T,
typename F = const
char*>
67 return std::vector<T>::size() >= 2;
81 sort_func_list(
nullptr),
82 filter_func_list(
nullptr),
96 return this->sort_type;
106 if (this->sort_type != n_type) {
108 this->sort_type = n_type;
150 return this->filter_type;
160 if (this->filter_type != n_type) {
161 this->filter_type = n_type;
204 if (--this->resort_timer == 0) {
206 this->ResetResortTimer();
228 return (this->flags &
VL_DESC) != 0;
240 if (this->IsSortable())
MemReverseT(std::vector<T>::data(), std::vector<T>::size());
253 bool Sort(SortFunction *compare)
256 if (!(this->flags &
VL_RESORT))
return false;
258 CLRBITS(this->flags, VL_RESORT);
260 this->ResetResortTimer();
263 if (!this->IsSortable())
return false;
265 const bool desc = (this->flags &
VL_DESC) != 0;
268 CLRBITS(this->flags, VL_FIRST_SORT);
270 std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](
const T &a,
const T &b) {
return desc ? compare(b, a) : compare(a, b); });
274 std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](
const T &a,
const T &b) {
return desc ? compare(b, a) : compare(a, b); });
285 this->sort_func_list = n_funcs;
296 assert(this->sort_func_list !=
nullptr);
297 return this->Sort(this->sort_func_list[this->sort_type]);
331 bool Filter(FilterFunction *decide, F filter_data)
334 if (!(this->flags &
VL_FILTER))
return false;
336 bool changed =
false;
337 for (
auto it = std::vector<T>::begin(); it != std::vector<T>::end(); ) {
338 if (!decide(&*it, filter_data)) {
339 it = std::vector<T>::erase(it);
356 this->filter_func_list = n_funcs;
367 if (this->filter_func_list ==
nullptr)
return false;
368 return this->Filter(this->filter_func_list[this->filter_type], filter_data);
List template of 'things' T to sort in a GUI.
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
void RebuildDone()
Notify the sortlist that the rebuild is done.
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
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
#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.
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.
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.
void SetListing(Listing l)
Import sort conditions.
Type (helpers) for enums.
Data structure describing what to show in the list (filter criteria).
bool NeedRebuild() const
Check if a rebuild is needed.
sort descending or ascending
void ForceRebuild()
Force that a rebuild is needed.
bool IsSortable() const
Check if the list is sortable.
byte criteria
Filtering criteria.
SortFunction *const * sort_func_list
the sort criteria functions
void ResetResortTimer()
Reset the resort timer.
Listing GetListing() const
Export current sort conditions.
bool order
Ascending/descending.
sort with quick sort first
instruct the code to resort the list in the next loop
SortListFlags
Flags of the sort list.
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
void SetFilterState(bool state)
Enable or disable the filter.
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.
Data structure describing how to show the list (what sort direction and criteria).
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.
Filtering GetFiltering() const
Export current filter conditions.
uint8 FilterType() const
Get the filtertype of the list.
bool SortFunction(const T &, const T &)
Signature of sort function.
bool Sort()
Overload of Sort(SortFunction *compare) Overloaded to reduce external code.
FilterFunction *const * filter_func_list
the filter criteria functions
uint8 SortType() const
Get the sorttype of the list.