12 #ifndef SORTLIST_TYPE_H 13 #define SORTLIST_TYPE_H 48 template <
typename T,
typename F = const
char*>
69 return std::vector<T>::size() >= 2;
83 sort_func_list(
nullptr),
84 filter_func_list(
nullptr),
98 return this->sort_type;
108 if (this->sort_type != n_type) {
110 this->sort_type = n_type;
152 return this->filter_type;
162 if (this->filter_type != n_type) {
163 this->filter_type = n_type;
206 if (--this->resort_timer == 0) {
208 this->ResetResortTimer();
230 return (this->flags &
VL_DESC) != 0;
242 if (this->IsSortable())
MemReverseT(std::vector<T>::data(), std::vector<T>::size());
255 bool Sort(SortFunction *compare)
258 if (!(this->flags &
VL_RESORT))
return false;
260 CLRBITS(this->flags, VL_RESORT);
262 this->ResetResortTimer();
265 if (!this->IsSortable())
return false;
267 const bool desc = (this->flags &
VL_DESC) != 0;
270 CLRBITS(this->flags, VL_FIRST_SORT);
272 std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](
const T &a,
const T &b) {
return desc ? compare(b, a) : compare(a, b); });
276 std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](
const T &a,
const T &b) {
return desc ? compare(b, a) : compare(a, b); });
287 this->sort_func_list = n_funcs;
298 assert(this->sort_func_list !=
nullptr);
299 return this->Sort(this->sort_func_list[this->sort_type]);
333 bool Filter(FilterFunction *decide, F filter_data)
336 if (!(this->flags &
VL_FILTER))
return false;
338 bool changed =
false;
339 for (
auto it = std::vector<T>::begin(); it != std::vector<T>::end(); ) {
340 if (!decide(&*it, filter_data)) {
341 it = std::vector<T>::erase(it);
358 this->filter_func_list = n_funcs;
369 if (this->filter_func_list ==
nullptr)
return false;
370 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.