tron@2186: /* $Id$ */ tron@2186: rubidium@10596: /** @file sortlist_type.h Base types for having sorted lists in GUIs. */ rubidium@10595: rubidium@10596: #ifndef SORTLIST_TYPE_H rubidium@10596: #define SORTLIST_TYPE_H belugas@10589: rubidium@10791: #include "core/enum_type.hpp" rubidium@10791: #include "core/bitmath_func.hpp" skidd13@10962: #include "core/mem_func.hpp" skidd13@10962: #include "core/sort_func.hpp" peter1138@10716: #include "misc/smallvec.h" skidd13@10717: #include "date_type.h" peter1138@10716: rubidium@6574: enum SortListFlags { smatz@10726: VL_NONE = 0, ///< no sort smatz@10726: VL_DESC = 1 << 0, ///< sort descending or ascending smatz@10726: VL_RESORT = 1 << 1, ///< instruct the code to resort the list in the next loop smatz@10726: VL_REBUILD = 1 << 2, ///< rebuild the sort list smatz@10726: VL_FIRST_SORT = 1 << 3, ///< sort with qsort first smatz@10726: VL_END = 1 << 4, rubidium@6574: }; rubidium@5838: DECLARE_ENUM_AS_BIT_SET(SortListFlags); rubidium@5838: rubidium@6574: struct Listing { belugas@6443: bool order; ///< Ascending/descending belugas@6443: byte criteria; ///< Sorting criteria rubidium@6574: }; tron@588: rubidium@10502: template skidd13@10717: class GUIList : public SmallVector { skidd13@10717: public: smatz@10730: typedef int CDECL SortFunction(const T*, const T*); skidd13@10717: skidd13@10981: protected: skidd13@10717: SortFunction* const *func_list; ///< The sort criteria functions skidd13@10717: SortListFlags flags; ///< used to control sorting/resorting/etc. skidd13@10717: uint8 sort_type; ///< what criteria to sort on skidd13@10717: uint16 resort_timer; ///< resort list after a given amount of ticks if set skidd13@10717: skidd13@10717: /** skidd13@10717: * Check if the list is sortable skidd13@10717: * skidd13@10717: * @return true if we can sort the list skidd13@10717: */ skidd13@10717: bool IsSortable() const skidd13@10717: { smatz@10725: return (this->data != NULL && this->items >= 2); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Reset the resort timer skidd13@10717: */ skidd13@10717: void ResetResortTimer() skidd13@10717: { skidd13@10717: /* Resort every 10 days */ skidd13@10717: this->resort_timer = DAY_TICKS * 10; skidd13@10717: } skidd13@10717: skidd13@10717: public: skidd13@10717: GUIList() : skidd13@10717: func_list(NULL), smatz@10726: flags(VL_FIRST_SORT), skidd13@10717: sort_type(0), skidd13@10717: resort_timer(1) skidd13@10717: {}; skidd13@10717: skidd13@10717: /** skidd13@10717: * Get the sorttype of the list skidd13@10717: * skidd13@10717: * @return The current sorttype skidd13@10717: */ skidd13@10717: uint8 SortType() const skidd13@10717: { skidd13@10717: return this->sort_type; skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Set the sorttype of the list skidd13@10717: * skidd13@10717: * @param n_type the new sort type skidd13@10717: */ skidd13@10717: void SetSortType(uint8 n_type) skidd13@10717: { skidd13@10717: if (this->sort_type != n_type) { smatz@10726: SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT); skidd13@10717: this->sort_type = n_type; skidd13@10717: } skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Export current sort conditions skidd13@10717: * skidd13@10717: * @return the current sort conditions skidd13@10717: */ skidd13@10717: Listing GetListing() const skidd13@10717: { skidd13@10717: Listing l; skidd13@10717: l.order = HASBITS(this->flags, VL_DESC); skidd13@10717: l.criteria = this->sort_type; skidd13@10717: skidd13@10717: return l; skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Import sort conditions skidd13@10717: * skidd13@10717: * @param l The sport conditions we want to use skidd13@10717: */ skidd13@10717: void SetListing(Listing l) skidd13@10717: { skidd13@10717: if (l.order) { skidd13@10717: SETBITS(this->flags, VL_DESC); skidd13@10717: } else { skidd13@10717: CLRBITS(this->flags, VL_DESC); skidd13@10717: } skidd13@10717: this->sort_type = l.criteria; smatz@10726: smatz@10726: SETBITS(this->flags, VL_FIRST_SORT); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Check if a resort is needed next loop skidd13@10717: * If used the resort timer will decrease every call skidd13@10717: * till 0. If 0 reached the resort bit will be set and skidd13@10717: * the timer will be reset. skidd13@10717: * skidd13@10717: * @return true if resort bit is set for next loop skidd13@10717: */ skidd13@10717: bool NeedResort() skidd13@10717: { skidd13@10717: if (--this->resort_timer == 0) { skidd13@10717: SETBITS(this->flags, VL_RESORT); skidd13@10717: this->ResetResortTimer(); skidd13@10717: return true; skidd13@10717: } skidd13@10717: return false; skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Force a resort next Sort call skidd13@10717: * Reset the resort timer if used too. skidd13@10717: */ skidd13@10717: void ForceResort() skidd13@10717: { skidd13@10717: SETBITS(this->flags, VL_RESORT); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Check if the sort order is descending skidd13@10717: * skidd13@10717: * @return true if the sort order is descending skidd13@10717: */ skidd13@10717: bool IsDescSortOrder() const skidd13@10717: { skidd13@10717: return HASBITS(this->flags, VL_DESC); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Toogle the sort order skidd13@10717: * Since that is the worst condition for the sort function skidd13@10717: * reverse the list here. skidd13@10717: */ skidd13@10962: void ToggleSortOrder() skidd13@10717: { skidd13@10717: this->flags ^= VL_DESC; skidd13@10717: skidd13@10962: if (this->IsSortable()) MemReverseT(this->data, this->items); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10962: * Sort the list. skidd13@10962: * For the first sorting we use qsort since it is skidd13@10962: * faster for irregular sorted data. After that we skidd13@10962: * use gsort. skidd13@10717: * skidd13@10717: * @param compare The function to compare two list items skidd13@10736: * @return true if the list sequence has been altered skidd13@10717: * */ skidd13@10962: bool Sort(SortFunction *compare) skidd13@10717: { smatz@10725: /* Do not sort if the resort bit is not set */ skidd13@10736: if (!HASBITS(this->flags, VL_RESORT)) return false; smatz@10725: smatz@10725: CLRBITS(this->flags, VL_RESORT); smatz@10725: smatz@10725: this->ResetResortTimer(); smatz@10725: skidd13@10717: /* Do not sort when the list is not sortable */ skidd13@10736: if (!this->IsSortable()) return false; skidd13@10717: smatz@10726: const bool desc = HASBITS(this->flags, VL_DESC); smatz@10726: smatz@10726: if (HASBITS(this->flags, VL_FIRST_SORT)) { smatz@10746: CLRBITS(this->flags, VL_FIRST_SORT); smatz@10726: skidd13@10962: QSortT(this->data, this->items, compare, desc); skidd13@10736: return true; smatz@10726: } smatz@10726: skidd13@10962: GSortT(this->data, this->items, compare, desc); skidd13@10736: return true; skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Hand the array of sort function pointers to the sort list skidd13@10717: * skidd13@10717: * @param n_funcs The pointer to the first sort func skidd13@10717: */ skidd13@10717: void SetSortFuncs(SortFunction* const* n_funcs) skidd13@10717: { skidd13@10717: this->func_list = n_funcs; skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Overload of Sort() skidd13@10717: * Overloaded to reduce external code skidd13@10736: * skidd13@10736: * @return true if the list sequence has been altered skidd13@10717: */ skidd13@10736: bool Sort() skidd13@10717: { skidd13@10717: assert(this->func_list != NULL); skidd13@10736: return this->Sort(this->func_list[this->sort_type]); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Check if a rebuild is needed skidd13@10717: * @return true if a rebuild is needed skidd13@10717: */ skidd13@10717: bool NeedRebuild() const skidd13@10717: { skidd13@10717: return HASBITS(this->flags, VL_REBUILD); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Force that a rebuild is needed skidd13@10717: */ skidd13@10717: void ForceRebuild() skidd13@10717: { skidd13@10717: SETBITS(this->flags, VL_REBUILD); skidd13@10717: } skidd13@10717: skidd13@10717: /** skidd13@10717: * Notify the sortlist that the rebuild is done skidd13@10717: * skidd13@10717: * @note This forces a resort skidd13@10717: */ skidd13@10717: void RebuildDone() skidd13@10717: { skidd13@10717: CLRBITS(this->flags, VL_REBUILD); skidd13@10981: SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT); skidd13@10717: } rubidium@6574: }; rubidium@7139: rubidium@10596: #endif /* SORTLIST_TYPE_H */