src/sortlist_type.h
author smatz
Mon, 26 May 2008 21:08:03 +0000
changeset 9371 da6ac5609943
parent 9366 2d029154291f
child 9372 2ee65824ee6d
permissions -rw-r--r--
(svn r13275) -Fix: sort lists with 2 items, too
-Fix: reset VL_RESORT and resort timer even when no sort was needed
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
9365
95e9cbc0257e (svn r13266) -Codechange: Use SmallVector in GUIList
peter1138
parents: 9274
diff changeset
     8
#include "misc/smallvec.h"
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
     9
#include "date_type.h"
9365
95e9cbc0257e (svn r13266) -Codechange: Use SmallVector in GUIList
peter1138
parents: 9274
diff changeset
    10
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    11
enum SortListFlags {
8008
4cc180fe96ab (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 7996
diff changeset
    12
	VL_NONE    = 0,      ///< no sort
4cc180fe96ab (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 7996
diff changeset
    13
	VL_DESC    = 1 << 0, ///< sort descending or ascending
4cc180fe96ab (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 7996
diff changeset
    14
	VL_RESORT  = 1 << 1, ///< instruct the code to resort the list in the next loop
4cc180fe96ab (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 7996
diff changeset
    15
	VL_REBUILD = 1 << 2, ///< create sort-listing to use for qsort and friends
4cc180fe96ab (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 7996
diff changeset
    16
	VL_END     = 1 << 3,
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    17
};
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5475
diff changeset
    18
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
    19
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    20
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
    21
	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
    22
	byte criteria; ///< Sorting criteria
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    23
};
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 543
diff changeset
    24
9182
412cb6c0caf1 (svn r13045) -Codechange: make list_d (now GUIList) more generic and uniform.
rubidium
parents: 9178
diff changeset
    25
template <typename T>
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    26
class GUIList : public SmallVector<T, 32> {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    27
public:
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    28
	typedef int SortFunction(const T*, const T*);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    29
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    30
public: // Temporary: public for conversion only
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    31
	SortFunction* const *func_list; ///< The sort criteria functions
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    32
	SortListFlags flags;            ///< used to control sorting/resorting/etc.
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    33
	uint8 sort_type;                ///< what criteria to sort on
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    34
	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
    35
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    36
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    37
	 * Check if the list is sortable
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    38
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    39
	 * @return true if we can sort the list
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
	bool IsSortable() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    42
	{
9371
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
    43
		return (this->data != NULL && this->items >= 2);
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    44
	}
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
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    47
	 * Reset the resort timer
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    48
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    49
	void ResetResortTimer()
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
		/* Resort every 10 days */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    52
		this->resort_timer = DAY_TICKS * 10;
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
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    55
public:
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    56
	GUIList() :
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    57
		func_list(NULL),
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    58
		flags(VL_NONE),
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    59
		sort_type(0),
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    60
		resort_timer(1)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    61
	{};
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    62
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    63
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    64
	 * Get the sorttype of the list
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    65
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    66
	 * @return The current sorttype
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
	uint8 SortType() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    69
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    70
		return this->sort_type;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    71
	}
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
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    74
	 * Set the sorttype of the list
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    75
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    76
	 * @param n_type the new sort type
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
	void SetSortType(uint8 n_type)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    79
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    80
		if (this->sort_type != n_type) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    81
			SETBITS(this->flags, VL_RESORT);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    82
			this->sort_type = n_type;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    83
		}
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
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    86
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    87
	 * Export current sort conditions
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
	 * @return the current sort conditions
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
	Listing GetListing() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    92
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    93
		Listing l;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    94
		l.order = HASBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    95
		l.criteria = this->sort_type;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    96
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    97
		return l;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    98
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
    99
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   100
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   101
	 * Import sort conditions
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   102
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   103
	 * @param l The sport conditions we want to use
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
	void SetListing(Listing l)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   106
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   107
		if (l.order) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   108
			SETBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   109
		} else {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   110
			CLRBITS(this->flags, VL_DESC);
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
		this->sort_type = l.criteria;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   113
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   114
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   115
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   116
	 * Check if a resort is needed next loop
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   117
	 *  If used the resort timer will decrease every call
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   118
	 *  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
   119
	 *  the timer will be reset.
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
	 * @return true if resort bit is set for next loop
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
	bool NeedResort()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   124
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   125
		if (--this->resort_timer == 0) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   126
			SETBITS(this->flags, VL_RESORT);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   127
			this->ResetResortTimer();
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   128
			return true;
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
		return false;
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
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   133
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   134
	 * Force a resort next Sort call
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   135
	 *  Reset the resort timer if used too.
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
	void ForceResort()
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
		SETBITS(this->flags, VL_RESORT);
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
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   142
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   143
	 * Check if the sort order is descending
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   144
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   145
	 * @return true if the sort order is descending
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   146
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   147
	bool IsDescSortOrder() const
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
		return HASBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   150
	}
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
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   153
	 * Toogle the sort order
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   154
	 *  Since that is the worst condition for the sort function
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   155
	 *  reverse the list here.
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   156
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   157
	FORCEINLINE void ToggleSortOrder()
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
		this->flags ^= VL_DESC;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   160
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   161
		if (this->IsSortable()) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   162
			T *a = this->data;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   163
			T *b = a + (this->items - 1);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   164
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   165
			do {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   166
				Swap(*a, *b);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   167
			} while (((a + 1) != b) && (++a != --b));
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   168
		}
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
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   172
	 * GnomeSort algorithm
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   173
	 *  This sorting uses a slightly modifyied Gnome search.
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   174
	 *  The basic Gnome search trys to sort already sorted
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   175
	 *  list parts. The modification skips these.
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
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   178
	 * */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   179
	FORCEINLINE void Sort(SortFunction compare)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   180
	{
9371
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   181
		/* Do not sort if the resort bit is not set */
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   182
		if (!HASBITS(this->flags, VL_RESORT)) return;
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   183
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   184
		CLRBITS(this->flags, VL_RESORT);
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   185
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   186
		this->ResetResortTimer();
da6ac5609943 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 9366
diff changeset
   187
9366
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   188
		/* Do not sort when the list is not sortable */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   189
		if (!this->IsSortable()) return;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   190
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   191
		T *a = this->data;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   192
		T *b = a + 1;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   193
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   194
		uint length = this->items;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   195
		uint offset = 0; // Jump variable
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   196
		const bool desc = HASBITS(this->flags, VL_DESC);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   197
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   198
		while (length > 1) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   199
			const int diff = compare(a, b);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   200
			if ((!desc && diff <= 0) || (desc && diff >= 0)) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   201
				if (offset != 0) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   202
					/* Jump back to the last direction switch point */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   203
					a += offset;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   204
					b += offset;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   205
					offset = 0;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   206
					continue;
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
				a++;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   209
				b++;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   210
				length--;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   211
			} else {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   212
				Swap(*a, *b);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   213
				if (a != this->data) {
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   214
					offset++;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   215
					a--;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   216
					b--;
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   217
				}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   218
			}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   219
		}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   220
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   221
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
	 * 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
   224
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   225
	 * @param n_funcs The pointer to the first sort func
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
	void SetSortFuncs(SortFunction* const* n_funcs)
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   228
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   229
		this->func_list = n_funcs;
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
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
	 * Overload of Sort()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   234
	 * Overloaded to reduce external code
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
	void Sort()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   237
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   238
		assert(this->func_list != NULL);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   239
		this->Sort(this->func_list[this->sort_type]);
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
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
	 * Check if a rebuild is needed
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   244
	 * @return true if a rebuild is needed
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   245
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   246
	bool NeedRebuild() const
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   247
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   248
		return HASBITS(this->flags, VL_REBUILD);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   249
	}
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
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   252
	 * Force that a rebuild is needed
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   253
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   254
	void ForceRebuild()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   255
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   256
		SETBITS(this->flags, VL_REBUILD);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   257
	}
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   258
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   259
	/**
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   260
	 * Notify the sortlist that the rebuild is done
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   261
	 *
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   262
	 * @note This forces a resort
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   263
	 */
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   264
	void RebuildDone()
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   265
	{
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   266
		CLRBITS(this->flags, VL_REBUILD);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   267
		SETBITS(this->flags, VL_RESORT);
2d029154291f (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 9365
diff changeset
   268
	}
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   269
};
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
   270
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
   271
#endif /* SORTLIST_TYPE_H */