src/sortlist_type.h
author rubidium
Wed, 17 Dec 2008 23:08:11 +0000
changeset 10434 3659467c844c
parent 9555 68e7c84b2d19
permissions -rw-r--r--
(svn r14687) -Change: log all configure errors to config.log
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
9274
42b67e65f1c2 (svn r13140) -Codechange: move the gui-list-sorting out of window_gui.h so window_gui.h only needs to be included in *_gui.cpp.
rubidium
parents: 9273
diff changeset
     3
/** @file sortlist_type.h Base types for having sorted lists in GUIs. */
9273
35e0224ea8f1 (svn r13139) -Codechange: move DrawWindowWidgets and DrawWindowViewport to the Window class and remove Window from their naming.
rubidium
parents: 9272
diff changeset
     4
9274
42b67e65f1c2 (svn r13140) -Codechange: move the gui-list-sorting out of window_gui.h so window_gui.h only needs to be included in *_gui.cpp.
rubidium
parents: 9273
diff changeset
     5
#ifndef SORTLIST_TYPE_H
42b67e65f1c2 (svn r13140) -Codechange: move the gui-list-sorting out of window_gui.h so window_gui.h only needs to be included in *_gui.cpp.
rubidium
parents: 9273
diff changeset
     6
#define SORTLIST_TYPE_H
9267
22095cc802dd (svn r13133) -Codechange: Add a base class (descending from Window) for all new windows that are going to require a ResetObjectToPlace to be performed on closing.
belugas
parents: 9264
diff changeset
     7
9427
af652de004a0 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 9386
diff changeset
     8
#include "core/enum_type.hpp"
af652de004a0 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 9386
diff changeset
     9
#include "core/bitmath_func.hpp"
9520
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
    10
#include "core/mem_func.hpp"
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
    11
#include "core/sort_func.hpp"
9555
68e7c84b2d19 (svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents: 9531
diff changeset
    12
#include "core/smallvec_type.hpp"
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    13
#include "date_type.h"
9365
95e9cbc0257e (svn r13266) -Codechange: Use SmallVector in GUIList
peter1138
parents: 9274
diff changeset
    14
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    15
enum SortListFlags {
9372
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    16
	VL_NONE       = 0,      ///< no sort
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    17
	VL_DESC       = 1 << 0, ///< sort descending or ascending
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    18
	VL_RESORT     = 1 << 1, ///< instruct the code to resort the list in the next loop
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    19
	VL_REBUILD    = 1 << 2, ///< rebuild the sort list
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    20
	VL_FIRST_SORT = 1 << 3, ///< sort with qsort first
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    21
	VL_END        = 1 << 4,
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    22
};
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    23
DECLARE_ENUM_AS_BIT_SET(SortListFlags);
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    24
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    25
struct Listing {
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6024
diff changeset
    26
	bool order;    ///< Ascending/descending
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6024
diff changeset
    27
	byte criteria; ///< Sorting criteria
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    28
};
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 543
diff changeset
    29
9182
412cb6c0caf1 (svn r13045) -Codechange: make list_d (now GUIList) more generic and uniform.
rubidium
parents: 9178
diff changeset
    30
template <typename T>
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    31
class GUIList : public SmallVector<T, 32> {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    32
public:
9376
afdc4beedc99 (svn r13280) -Fix (r13276): MSVC compilation was broken
smatz
parents: 9375
diff changeset
    33
	typedef int CDECL SortFunction(const T*, const T*);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    34
9531
737129e510cb (svn r13535) -Codechange: Protect GUIList internals
skidd13
parents: 9520
diff changeset
    35
protected:
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    36
	SortFunction* const *func_list; ///< The sort criteria functions
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    37
	SortListFlags flags;            ///< used to control sorting/resorting/etc.
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    38
	uint8 sort_type;                ///< what criteria to sort on
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    39
	uint16 resort_timer;            ///< resort list after a given amount of ticks if set
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    40
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    41
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    42
	 * Check if the list is sortable
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    43
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    44
	 * @return true if we can sort the list
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    45
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    46
	bool IsSortable() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    47
	{
9371
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
    48
		return (this->data != NULL && this->items >= 2);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    49
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    50
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    51
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    52
	 * Reset the resort timer
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    53
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    54
	void ResetResortTimer()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    55
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    56
		/* Resort every 10 days */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    57
		this->resort_timer = DAY_TICKS * 10;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    58
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    59
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    60
public:
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    61
	GUIList() :
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    62
		func_list(NULL),
9372
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    63
		flags(VL_FIRST_SORT),
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    64
		sort_type(0),
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    65
		resort_timer(1)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    66
	{};
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    67
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    68
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    69
	 * Get the sorttype of the list
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    70
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    71
	 * @return The current sorttype
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    72
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    73
	uint8 SortType() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    74
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    75
		return this->sort_type;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    76
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    77
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    78
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    79
	 * Set the sorttype of the list
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    80
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    81
	 * @param n_type the new sort type
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    82
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    83
	void SetSortType(uint8 n_type)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    84
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    85
		if (this->sort_type != n_type) {
9372
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
    86
			SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    87
			this->sort_type = n_type;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    88
		}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    89
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    90
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    91
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    92
	 * Export current sort conditions
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    93
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    94
	 * @return the current sort conditions
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    95
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    96
	Listing GetListing() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    97
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    98
		Listing l;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    99
		l.order = HASBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   100
		l.criteria = this->sort_type;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   101
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   102
		return l;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   103
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   104
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   105
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   106
	 * Import sort conditions
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   107
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   108
	 * @param l The sport conditions we want to use
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   109
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   110
	void SetListing(Listing l)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   111
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   112
		if (l.order) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   113
			SETBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   114
		} else {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   115
			CLRBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   116
		}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   117
		this->sort_type = l.criteria;
9372
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   118
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   119
		SETBITS(this->flags, VL_FIRST_SORT);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   120
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   121
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   122
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   123
	 * Check if a resort is needed next loop
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   124
	 *  If used the resort timer will decrease every call
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   125
	 *  till 0. If 0 reached the resort bit will be set and
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   126
	 *  the timer will be reset.
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   127
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   128
	 * @return true if resort bit is set for next loop
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   129
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   130
	bool NeedResort()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   131
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   132
		if (--this->resort_timer == 0) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   133
			SETBITS(this->flags, VL_RESORT);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   134
			this->ResetResortTimer();
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   135
			return true;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   136
		}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   137
		return false;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   138
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   139
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   140
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   141
	 * Force a resort next Sort call
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   142
	 *  Reset the resort timer if used too.
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   143
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   144
	void ForceResort()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   145
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   146
		SETBITS(this->flags, VL_RESORT);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   147
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   148
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   149
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   150
	 * Check if the sort order is descending
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   151
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   152
	 * @return true if the sort order is descending
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   153
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   154
	bool IsDescSortOrder() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   155
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   156
		return HASBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   157
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   158
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   159
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   160
	 * Toogle the sort order
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   161
	 *  Since that is the worst condition for the sort function
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   162
	 *  reverse the list here.
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   163
	 */
9520
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   164
	void ToggleSortOrder()
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   165
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   166
		this->flags ^= VL_DESC;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   167
9520
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   168
		if (this->IsSortable()) MemReverseT(this->data, this->items);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   169
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   170
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   171
	/**
9520
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   172
	 * Sort the list.
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   173
	 *  For the first sorting we use qsort since it is
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   174
	 *  faster for irregular sorted data. After that we
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   175
	 *  use gsort.
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   176
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   177
	 * @param compare The function to compare two list items
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   178
	 * @return true if the list sequence has been altered
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   179
	 * */
9520
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   180
	bool Sort(SortFunction *compare)
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   181
	{
9371
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   182
		/* Do not sort if the resort bit is not set */
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   183
		if (!HASBITS(this->flags, VL_RESORT)) return false;
9371
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   184
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   185
		CLRBITS(this->flags, VL_RESORT);
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   186
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   187
		this->ResetResortTimer();
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   188
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   189
		/* Do not sort when the list is not sortable */
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   190
		if (!this->IsSortable()) return false;
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   191
9372
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   192
		const bool desc = HASBITS(this->flags, VL_DESC);
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   193
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   194
		if (HASBITS(this->flags, VL_FIRST_SORT)) {
9386
f3fd0650f9d5 (svn r13296) -Fix (r13276): VL_FIRST_SORT was never reset
smatz
parents: 9379
diff changeset
   195
			CLRBITS(this->flags, VL_FIRST_SORT);
9372
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   196
9520
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   197
			QSortT(this->data, this->items, compare, desc);
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   198
			return true;
9372
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   199
		}
2ee65824ee6d (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 9371
diff changeset
   200
9520
f08d9fc662a6 (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 9427
diff changeset
   201
		GSortT(this->data, this->items, compare, desc);
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   202
		return true;
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   203
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   204
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   205
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   206
	 * Hand the array of sort function pointers to the sort list
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   207
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   208
	 * @param n_funcs The pointer to the first sort func
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   209
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   210
	void SetSortFuncs(SortFunction* const* n_funcs)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   211
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   212
		this->func_list = n_funcs;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   213
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   214
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   215
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   216
	 * Overload of Sort()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   217
	 * Overloaded to reduce external code
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   218
	 *
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   219
	 * @return true if the list sequence has been altered
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   220
	 */
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   221
	bool Sort()
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   222
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   223
		assert(this->func_list != NULL);
9379
2c6c6a0e2233 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 9376
diff changeset
   224
		return this->Sort(this->func_list[this->sort_type]);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   225
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   226
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   227
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   228
	 * Check if a rebuild is needed
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   229
	 * @return true if a rebuild is needed
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   230
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   231
	bool NeedRebuild() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   232
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   233
		return HASBITS(this->flags, VL_REBUILD);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   234
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   235
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   236
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   237
	 * Force that a rebuild is needed
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   238
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   239
	void ForceRebuild()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   240
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   241
		SETBITS(this->flags, VL_REBUILD);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   242
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   243
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   244
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   245
	 * Notify the sortlist that the rebuild is done
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   246
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   247
	 * @note This forces a resort
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   248
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   249
	void RebuildDone()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   250
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   251
		CLRBITS(this->flags, VL_REBUILD);
9531
737129e510cb (svn r13535) -Codechange: Protect GUIList internals
skidd13
parents: 9520
diff changeset
   252
		SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   253
	}
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   254
};
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6638
diff changeset
   255
9274
42b67e65f1c2 (svn r13140) -Codechange: move the gui-list-sorting out of window_gui.h so window_gui.h only needs to be included in *_gui.cpp.
rubidium
parents: 9273
diff changeset
   256
#endif /* SORTLIST_TYPE_H */