src/sortlist_type.h
author smatz
Mon, 26 May 2008 21:08:03 +0000
changeset 10725 73a2e153ff13
parent 10717 996d586844d9
child 10726 55b2fac285ca
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
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
10596
0ee9eba64c9c (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: 10595
diff changeset
     3
/** @file sortlist_type.h Base types for having sorted lists in GUIs. */
10595
7957c71b0dfe (svn r13139) -Codechange: move DrawWindowWidgets and DrawWindowViewport to the Window class and remove Window from their naming.
rubidium
parents: 10594
diff changeset
     4
10596
0ee9eba64c9c (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: 10595
diff changeset
     5
#ifndef SORTLIST_TYPE_H
0ee9eba64c9c (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: 10595
diff changeset
     6
#define SORTLIST_TYPE_H
10589
2998e6072c03 (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: 10586
diff changeset
     7
10716
2406d1520245 (svn r13266) -Codechange: Use SmallVector in GUIList
peter1138
parents: 10596
diff changeset
     8
#include "misc/smallvec.h"
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
     9
#include "date_type.h"
10716
2406d1520245 (svn r13266) -Codechange: Use SmallVector in GUIList
peter1138
parents: 10596
diff changeset
    10
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    11
enum SortListFlags {
8504
a258f7640c56 (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 8492
diff changeset
    12
	VL_NONE    = 0,      ///< no sort
a258f7640c56 (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 8492
diff changeset
    13
	VL_DESC    = 1 << 0, ///< sort descending or ascending
a258f7640c56 (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 8492
diff changeset
    14
	VL_RESORT  = 1 << 1, ///< instruct the code to resort the list in the next loop
a258f7640c56 (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 8492
diff changeset
    15
	VL_REBUILD = 1 << 2, ///< create sort-listing to use for qsort and friends
a258f7640c56 (svn r11568) -Codechange: Use bit shifts instead of values for flags. It helps readability a little bit.
belugas
parents: 8492
diff changeset
    16
	VL_END     = 1 << 3,
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    17
};
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    18
DECLARE_ENUM_AS_BIT_SET(SortListFlags);
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    19
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    20
struct Listing {
6443
b8f06d8eb7be (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: 6350
diff changeset
    21
	bool order;    ///< Ascending/descending
b8f06d8eb7be (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: 6350
diff changeset
    22
	byte criteria; ///< Sorting criteria
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    23
};
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 543
diff changeset
    24
10502
996d9308d650 (svn r13045) -Codechange: make list_d (now GUIList) more generic and uniform.
rubidium
parents: 10498
diff changeset
    25
template <typename T>
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    26
class GUIList : public SmallVector<T, 32> {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    27
public:
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    28
	typedef int SortFunction(const T*, const T*);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    29
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    30
public: // Temporary: public for conversion only
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    31
	SortFunction* const *func_list; ///< The sort criteria functions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    32
	SortListFlags flags;            ///< used to control sorting/resorting/etc.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    33
	uint8 sort_type;                ///< what criteria to sort on
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    34
	uint16 resort_timer;            ///< resort list after a given amount of ticks if set
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    35
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    36
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    37
	 * Check if the list is sortable
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    38
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    39
	 * @return true if we can sort the list
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    40
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    41
	bool IsSortable() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    42
	{
10725
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
    43
		return (this->data != NULL && this->items >= 2);
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    44
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    45
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    46
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    47
	 * Reset the resort timer
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    48
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    49
	void ResetResortTimer()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    50
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    51
		/* Resort every 10 days */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    52
		this->resort_timer = DAY_TICKS * 10;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    53
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    54
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    55
public:
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    56
	GUIList() :
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    57
		func_list(NULL),
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    58
		flags(VL_NONE),
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    59
		sort_type(0),
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    60
		resort_timer(1)
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    61
	{};
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    62
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    63
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    64
	 * Get the sorttype of the list
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    65
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    66
	 * @return The current sorttype
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    67
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    68
	uint8 SortType() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    69
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    70
		return this->sort_type;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    71
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    72
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    73
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    74
	 * Set the sorttype of the list
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    75
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    76
	 * @param n_type the new sort type
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    77
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    78
	void SetSortType(uint8 n_type)
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    79
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    80
		if (this->sort_type != n_type) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    81
			SETBITS(this->flags, VL_RESORT);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    82
			this->sort_type = n_type;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    83
		}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    84
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    85
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    86
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    87
	 * Export current sort conditions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    88
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    89
	 * @return the current sort conditions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    90
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    91
	Listing GetListing() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    92
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    93
		Listing l;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    94
		l.order = HASBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    95
		l.criteria = this->sort_type;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    96
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    97
		return l;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    98
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    99
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   100
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   101
	 * Import sort conditions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   102
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   103
	 * @param l The sport conditions we want to use
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   104
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   105
	void SetListing(Listing l)
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   106
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   107
		if (l.order) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   108
			SETBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   109
		} else {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   110
			CLRBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   111
		}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   112
		this->sort_type = l.criteria;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   113
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   114
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   115
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   116
	 * Check if a resort is needed next loop
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   117
	 *  If used the resort timer will decrease every call
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   118
	 *  till 0. If 0 reached the resort bit will be set and
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   119
	 *  the timer will be reset.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   120
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   121
	 * @return true if resort bit is set for next loop
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   122
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   123
	bool NeedResort()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   124
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   125
		if (--this->resort_timer == 0) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   126
			SETBITS(this->flags, VL_RESORT);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   127
			this->ResetResortTimer();
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   128
			return true;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   129
		}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   130
		return false;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   131
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   132
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   133
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   134
	 * Force a resort next Sort call
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   135
	 *  Reset the resort timer if used too.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   136
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   137
	void ForceResort()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   138
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   139
		SETBITS(this->flags, VL_RESORT);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   140
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   141
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   142
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   143
	 * Check if the sort order is descending
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   144
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   145
	 * @return true if the sort order is descending
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   146
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   147
	bool IsDescSortOrder() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   148
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   149
		return HASBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   150
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   151
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   152
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   153
	 * Toogle the sort order
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   154
	 *  Since that is the worst condition for the sort function
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   155
	 *  reverse the list here.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   156
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   157
	FORCEINLINE void ToggleSortOrder()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   158
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   159
		this->flags ^= VL_DESC;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   160
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   161
		if (this->IsSortable()) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   162
			T *a = this->data;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   163
			T *b = a + (this->items - 1);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   164
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   165
			do {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   166
				Swap(*a, *b);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   167
			} while (((a + 1) != b) && (++a != --b));
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   168
		}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   169
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   170
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   171
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   172
	 * GnomeSort algorithm
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   173
	 *  This sorting uses a slightly modifyied Gnome search.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   174
	 *  The basic Gnome search trys to sort already sorted
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   175
	 *  list parts. The modification skips these.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   176
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   177
	 * @param compare The function to compare two list items
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   178
	 * */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   179
	FORCEINLINE void Sort(SortFunction compare)
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   180
	{
10725
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   181
		/* Do not sort if the resort bit is not set */
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   182
		if (!HASBITS(this->flags, VL_RESORT)) return;
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   183
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   184
		CLRBITS(this->flags, VL_RESORT);
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   185
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   186
		this->ResetResortTimer();
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   187
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   188
		/* Do not sort when the list is not sortable */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   189
		if (!this->IsSortable()) return;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   190
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   191
		T *a = this->data;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   192
		T *b = a + 1;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   193
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   194
		uint length = this->items;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   195
		uint offset = 0; // Jump variable
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   196
		const bool desc = HASBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   197
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   198
		while (length > 1) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   199
			const int diff = compare(a, b);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   200
			if ((!desc && diff <= 0) || (desc && diff >= 0)) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   201
				if (offset != 0) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   202
					/* Jump back to the last direction switch point */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   203
					a += offset;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   204
					b += offset;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   205
					offset = 0;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   206
					continue;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   207
				}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   208
				a++;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   209
				b++;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   210
				length--;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   211
			} else {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   212
				Swap(*a, *b);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   213
				if (a != this->data) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   214
					offset++;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   215
					a--;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   216
					b--;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   217
				}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   218
			}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   219
		}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   220
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   221
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   222
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   223
	 * Hand the array of sort function pointers to the sort list
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   224
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   225
	 * @param n_funcs The pointer to the first sort func
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   226
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   227
	void SetSortFuncs(SortFunction* const* n_funcs)
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   228
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   229
		this->func_list = n_funcs;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   230
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   231
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   232
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   233
	 * Overload of Sort()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   234
	 * Overloaded to reduce external code
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   235
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   236
	void Sort()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   237
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   238
		assert(this->func_list != NULL);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   239
		this->Sort(this->func_list[this->sort_type]);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   240
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   241
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   242
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   243
	 * Check if a rebuild is needed
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   244
	 * @return true if a rebuild is needed
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   245
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   246
	bool NeedRebuild() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   247
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   248
		return HASBITS(this->flags, VL_REBUILD);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   249
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   250
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   251
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   252
	 * Force that a rebuild is needed
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   253
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   254
	void ForceRebuild()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   255
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   256
		SETBITS(this->flags, VL_REBUILD);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   257
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   258
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   259
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   260
	 * Notify the sortlist that the rebuild is done
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   261
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   262
	 * @note This forces a resort
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   263
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   264
	void RebuildDone()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   265
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   266
		CLRBITS(this->flags, VL_REBUILD);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   267
		SETBITS(this->flags, VL_RESORT);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   268
	}
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   269
};
7139
4ae3ab180d05 (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: 7134
diff changeset
   270
10596
0ee9eba64c9c (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: 10595
diff changeset
   271
#endif /* SORTLIST_TYPE_H */