bool Sort(Comp compare)
{
/* Do not sort if the resort bit is not set */
if (!(this->flags & VL_RESORT)) return false;
CLRBITS(this->flags, VL_RESORT);
this->ResetResortTimer();
/* Do not sort when the list is not sortable */
if (!this->IsSortable()) return false;
const bool desc = (this->flags & VL_DESC) != 0;
if constexpr (std::is_same_v) {
std::sort(std::vector::begin(), std::vector::end(), [&](const T &a, const T &b) { return desc ? compare(b, a) : compare(a, b); });
} else {
std::sort(std::vector::begin(), std::vector::end(), [&](const T &a, const T &b) { return desc ? compare(b, a, params) : compare(a, b, params); });
}
return true;
}
/**
* Hand the sort function pointers to the GUIList.
*
* @param n_funcs Span covering the sort function pointers.
*/
void SetSortFuncs(std::span n_funcs)
{
this->sort_func_list = n_funcs;
}
/**
* Overload of #Sort(SortFunction *compare)
* Overloaded to reduce external code
*
* @return true if the list sequence has been altered
*/
bool Sort()
{
if (this->sort_func_list.empty()) return false;
assert(this->sort_type < this->sort_func_list.size());
return this->Sort(this->sort_func_list[this->sort_type]);
}
/**
* Check if the filter is enabled
*
* @return true if the filter is enabled
*/
bool IsFilterEnabled() const
{
return (this->flags & VL_FILTER) != 0;
}
/**
* Enable or disable the filter
*
* @param state If filtering should be enabled or disabled
*/
void SetFilterState(bool state)
{
if (state) {
SETBITS(this->flags, VL_FILTER);
} else {
CLRBITS(this->flags, VL_FILTER);
}
}
/**
* Filter the list.
*
* @param decide The function to decide about an item
* @param filter_data Additional data passed to the filter function
* @return true if the list has been altered by filtering
*/
bool Filter(FilterFunction *decide, F filter_data)
{
/* Do not filter if the filter bit is not set */
if (!(this->flags & VL_FILTER)) return false;
bool changed = false;
for (auto it = std::vector::begin(); it != std::vector::end(); /* Nothing */) {
if (!decide(&*it, filter_data)) {
it = std::vector::erase(it);
changed = true;
} else {
it++;
}
}
return changed;
}
/**
* Hand the filter function pointers to the GUIList.
*
* @param n_funcs Span covering the filter function pointers.
*/
void SetFilterFuncs(std::span n_funcs)
{
this->filter_func_list = n_funcs;
}
/**
* Filter the data with the currently selected filter.
*
* @param filter_data Additional data passed to the filter function.
* @return true if the list has been altered by filtering
*/
bool Filter(F filter_data)
{
if (this->filter_func_list.empty()) return false;
assert(this->filter_type < this->filter_func_list.size());
return this->Filter(this->filter_func_list[this->filter_type], filter_data);
}
/**
* Check if a rebuild is needed
* @return true if a rebuild is needed
*/
bool NeedRebuild() const
{
return (this->flags & VL_REBUILD) != 0;
}
/**
* Force that a rebuild is needed
*/
void ForceRebuild()
{
SETBITS(this->flags, VL_REBUILD);
}
/**
* Notify the sortlist that the rebuild is done
*
* @note This forces a resort
*/
void RebuildDone()
{
CLRBITS(this->flags, VL_REBUILD);
SETBITS(this->flags, VL_RESORT);
}
};
#endif /* SORTLIST_TYPE_H */