4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file sort_func.hpp Functions related to sorting operations. */
15 #include "mem_func.hpp"
20 * @note Use this sort for irregular sorted data.
22 * @param base Pointer to the first element of the array to be sorted.
23 * @param num Number of elements in the array pointed by base.
24 * @param comparator Function that compares two elements.
25 * @param desc Sort descending.
28 static inline void QSortT(T
*base
, uint num
, int (CDECL
*comparator
)(const T
*, const T
*), bool desc
= false)
32 qsort(base
, num
, sizeof(T
), (int (CDECL
*)(const void *, const void *))comparator
);
34 if (desc
) MemReverseT(base
, num
);
38 * Type safe Gnome Sort.
40 * This is a slightly modified Gnome search. The basic
41 * Gnome search tries to sort already sorted list parts.
42 * The modification skips these.
44 * @note Use this sort for presorted / regular sorted data.
46 * @param base Pointer to the first element of the array to be sorted.
47 * @param num Number of elements in the array pointed by base.
48 * @param comparator Function that compares two elements.
49 * @param desc Sort descending.
52 static inline void GSortT(T
*base
, uint num
, int (CDECL
*comparator
)(const T
*, const T
*), bool desc
= false)
57 assert(comparator
!= NULL
);
64 const int diff
= comparator(a
, b
);
65 if ((!desc
&& diff
<= 0) || (desc
&& diff
>= 0)) {
67 /* Jump back to the last direction switch point */
80 if (a
== base
) continue;
89 #endif /* SORT_FUNC_HPP */