src/sortlist_type.h
author skidd13
Mon, 16 Jun 2008 17:10:55 +0000
changeset 10981 20a58e431c29
parent 10962 2649d7a316af
child 11019 9c818b06c54d
permissions -rw-r--r--
(svn r13535) -Codechange: Protect GUIList internals
-Fix: VL_FIST_SORT should be set after list rebuild too
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
10791
0f3e94733e65 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 10746
diff changeset
     8
#include "core/enum_type.hpp"
0f3e94733e65 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 10746
diff changeset
     9
#include "core/bitmath_func.hpp"
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
    10
#include "core/mem_func.hpp"
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
    11
#include "core/sort_func.hpp"
10716
2406d1520245 (svn r13266) -Codechange: Use SmallVector in GUIList
peter1138
parents: 10596
diff changeset
    12
#include "misc/smallvec.h"
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    13
#include "date_type.h"
10716
2406d1520245 (svn r13266) -Codechange: Use SmallVector in GUIList
peter1138
parents: 10596
diff changeset
    14
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    15
enum SortListFlags {
10726
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    16
	VL_NONE       = 0,      ///< no sort
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    17
	VL_DESC       = 1 << 0, ///< sort descending or ascending
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    18
	VL_RESORT     = 1 << 1, ///< instruct the code to resort the list in the next loop
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    19
	VL_REBUILD    = 1 << 2, ///< rebuild the sort list
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    20
	VL_FIRST_SORT = 1 << 3, ///< sort with qsort first
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    21
	VL_END        = 1 << 4,
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    22
};
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    23
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
    24
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    25
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
    26
	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
    27
	byte criteria; ///< Sorting criteria
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    28
};
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 543
diff changeset
    29
10502
996d9308d650 (svn r13045) -Codechange: make list_d (now GUIList) more generic and uniform.
rubidium
parents: 10498
diff changeset
    30
template <typename T>
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    31
class GUIList : public SmallVector<T, 32> {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    32
public:
10730
40906a6f031e (svn r13280) -Fix (r13276): MSVC compilation was broken
smatz
parents: 10729
diff changeset
    33
	typedef int CDECL SortFunction(const T*, const T*);
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    34
10981
20a58e431c29 (svn r13535) -Codechange: Protect GUIList internals
skidd13
parents: 10962
diff changeset
    35
protected:
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    36
	SortFunction* const *func_list; ///< The sort criteria functions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    37
	SortListFlags flags;            ///< used to control sorting/resorting/etc.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    38
	uint8 sort_type;                ///< what criteria to sort on
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    39
	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
    40
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    41
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    42
	 * Check if the list is sortable
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    43
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    44
	 * @return true if we can sort the list
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
	bool IsSortable() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    47
	{
10725
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
    48
		return (this->data != NULL && this->items >= 2);
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    49
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    52
	 * Reset the resort timer
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
	void ResetResortTimer()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    55
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    56
		/* Resort every 10 days */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    57
		this->resort_timer = DAY_TICKS * 10;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    58
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    59
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    60
public:
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    61
	GUIList() :
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    62
		func_list(NULL),
10726
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    63
		flags(VL_FIRST_SORT),
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    64
		sort_type(0),
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    65
		resort_timer(1)
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    66
	{};
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    69
	 * Get the sorttype of the list
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    70
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    71
	 * @return The current sorttype
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
	uint8 SortType() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    74
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    75
		return this->sort_type;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    76
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    79
	 * Set the sorttype of the list
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    80
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    81
	 * @param n_type the new sort type
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    82
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    83
	void SetSortType(uint8 n_type)
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
		if (this->sort_type != n_type) {
10726
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
    86
			SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT);
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    87
			this->sort_type = n_type;
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
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    92
	 * Export current sort conditions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    93
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    94
	 * @return the current sort conditions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    95
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    96
	Listing GetListing() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    97
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    98
		Listing l;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
    99
		l.order = HASBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   100
		l.criteria = this->sort_type;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   101
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   102
		return l;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   103
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   106
	 * Import sort conditions
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   107
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   108
	 * @param l The sport conditions we want to use
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   109
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   110
	void SetListing(Listing l)
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
		if (l.order) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   113
			SETBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   114
		} else {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   115
			CLRBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   116
		}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   117
		this->sort_type = l.criteria;
10726
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   118
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   119
		SETBITS(this->flags, VL_FIRST_SORT);
10717
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
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
	 * Check if a resort is needed next loop
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   124
	 *  If used the resort timer will decrease every call
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   125
	 *  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
   126
	 *  the timer will be reset.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   127
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   128
	 * @return true if resort bit is set for next loop
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
	bool NeedResort()
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
		if (--this->resort_timer == 0) {
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   133
			SETBITS(this->flags, VL_RESORT);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   134
			this->ResetResortTimer();
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   135
			return true;
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
		return false;
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
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
	 * Force a resort next Sort call
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   142
	 *  Reset the resort timer if used too.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   143
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   144
	void ForceResort()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   145
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   146
		SETBITS(this->flags, VL_RESORT);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   147
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   150
	 * Check if the sort order is descending
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
	 * @return true if the sort order is descending
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   153
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   154
	bool IsDescSortOrder() const
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   155
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   156
		return HASBITS(this->flags, VL_DESC);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   157
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   160
	 * Toogle the sort order
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   161
	 *  Since that is the worst condition for the sort function
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   162
	 *  reverse the list here.
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   163
	 */
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   164
	void ToggleSortOrder()
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   165
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   166
		this->flags ^= VL_DESC;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   167
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   168
		if (this->IsSortable()) MemReverseT(this->data, this->items);
10717
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
	/**
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   172
	 * Sort the list.
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   173
	 *  For the first sorting we use qsort since it is
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   174
	 *  faster for irregular sorted data. After that we
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   175
	 *  use gsort.
10717
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
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   178
	 * @return true if the list sequence has been altered
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   179
	 * */
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   180
	bool Sort(SortFunction *compare)
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   181
	{
10725
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   182
		/* Do not sort if the resort bit is not set */
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   183
		if (!HASBITS(this->flags, VL_RESORT)) return false;
10725
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   184
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   185
		CLRBITS(this->flags, VL_RESORT);
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   186
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   187
		this->ResetResortTimer();
73a2e153ff13 (svn r13275) -Fix: sort lists with 2 items, too
smatz
parents: 10717
diff changeset
   188
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   189
		/* Do not sort when the list is not sortable */
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   190
		if (!this->IsSortable()) return false;
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   191
10726
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   192
		const bool desc = HASBITS(this->flags, VL_DESC);
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   193
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   194
		if (HASBITS(this->flags, VL_FIRST_SORT)) {
10746
dd943cc2c373 (svn r13296) -Fix (r13276): VL_FIRST_SORT was never reset
smatz
parents: 10736
diff changeset
   195
			CLRBITS(this->flags, VL_FIRST_SORT);
10726
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   196
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   197
			QSortT(this->data, this->items, compare, desc);
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   198
			return true;
10726
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   199
		}
55b2fac285ca (svn r13276) -Codechange: use qsort() for initial sorting of a list for better performance (credits go to skidd13 and peter1138)
smatz
parents: 10725
diff changeset
   200
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents: 10791
diff changeset
   201
		GSortT(this->data, this->items, compare, desc);
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   202
		return true;
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   203
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   204
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   205
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   206
	 * 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
   207
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   208
	 * @param n_funcs The pointer to the first sort func
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   209
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   210
	void SetSortFuncs(SortFunction* const* n_funcs)
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   211
	{
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   212
		this->func_list = n_funcs;
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   213
	}
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   214
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   215
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   216
	 * Overload of Sort()
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   217
	 * Overloaded to reduce external code
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   218
	 *
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   219
	 * @return true if the list sequence has been altered
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   220
	 */
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   221
	bool Sort()
10717
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
		assert(this->func_list != NULL);
10736
e3111a03f9c2 (svn r13286) -Codechange: GUIList Sort returns now if the list sequence has been altered
skidd13
parents: 10730
diff changeset
   224
		return this->Sort(this->func_list[this->sort_type]);
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   225
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   228
	 * Check if a rebuild is needed
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   229
	 * @return true if a rebuild is needed
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
	bool NeedRebuild() const
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
		return HASBITS(this->flags, VL_REBUILD);
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   234
	}
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
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   237
	 * Force that a rebuild is needed
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   238
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   239
	void ForceRebuild()
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
		SETBITS(this->flags, VL_REBUILD);
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
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   244
	/**
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   245
	 * Notify the sortlist that the rebuild is done
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   246
	 *
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   247
	 * @note This forces a resort
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   248
	 */
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   249
	void RebuildDone()
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
		CLRBITS(this->flags, VL_REBUILD);
10981
20a58e431c29 (svn r13535) -Codechange: Protect GUIList internals
skidd13
parents: 10962
diff changeset
   252
		SETBITS(this->flags, VL_RESORT | VL_FIRST_SORT);
10717
996d586844d9 (svn r13267) -Codechange: extend GUIList with a GnomeSort
skidd13
parents: 10716
diff changeset
   253
	}
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   254
};
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
   255
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
   256
#endif /* SORTLIST_TYPE_H */