Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
sort.qh File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define heapify(_count)
#define siftdown(_start, _end)

Typedefs

using comparefunc_t = int (int i1, int i2, entity pass)
 <0 for <, ==0 for ==, >0 for > (like strcmp)
using swapfunc_t = void (int i1, int i2, entity pass)
 is only ever called for i1 < i2

Functions

ERASEABLE void heapsort (int n, swapfunc_t swap, comparefunc_t cmp, entity pass)
ERASEABLE void shuffle (float n, swapfunc_t swap, entity pass)

Macro Definition Documentation

◆ heapify

#define heapify ( _count)
Value:
MACRO_BEGIN \
for (int start = floor(((_count) - 2) / 2); start >= 0; --start) \
siftdown(start, (_count) - 1); \
#define MACRO_END
Definition macro.qh:7
float floor(float f)
#define siftdown(_start, _end)

Referenced by heapsort().

◆ siftdown

#define siftdown ( _start,
_end )
Value:
MACRO_BEGIN \
for (int root = (_start); root * 2 + 1 <= (_end); ) \
{ \
int child = root * 2 + 1; \
if (child < (_end) && cmp(child, child + 1, pass) < 0) \
++child; \
if (cmp(root, child, pass) >= 0) \
break; \
swap(root, child, pass); \
root = child; \
} \
#define pass(name, colormin, colormax)

Referenced by heapsort().

Typedef Documentation

◆ comparefunc_t

using comparefunc_t = int (int i1, int i2, entity pass)

<0 for <, ==0 for ==, >0 for > (like strcmp)

Definition at line 6 of file sort.qh.

◆ swapfunc_t

using swapfunc_t = void (int i1, int i2, entity pass)

is only ever called for i1 < i2

Definition at line 4 of file sort.qh.

Function Documentation

◆ heapsort()

ERASEABLE void heapsort ( int n,
swapfunc_t swap,
comparefunc_t cmp,
entity pass )

Definition at line 9 of file sort.qh.

10{
11 #define heapify(_count) MACRO_BEGIN \
12 for (int start = floor(((_count) - 2) / 2); start >= 0; --start) \
13 siftdown(start, (_count) - 1); \
14 MACRO_END
15
16 #define siftdown(_start, _end) MACRO_BEGIN \
17 for (int root = (_start); root * 2 + 1 <= (_end); ) \
18 { \
19 int child = root * 2 + 1; \
20 if (child < (_end) && cmp(child, child + 1, pass) < 0) \
21 ++child; \
22 if (cmp(root, child, pass) >= 0) \
23 break; \
24 swap(root, child, pass); \
25 root = child; \
26 } \
27 MACRO_END
28
29 heapify(n);
30 int end = n - 1;
31 while (end > 0)
32 {
33 swap(0, end, pass);
34 --end;
35 siftdown(0, end);
36 }
37}
#define heapify(_count)

References entity(), heapify, pass, and siftdown.

Referenced by _MapInfo_FilterGametype(), Dump_Turret_Settings(), Dump_Weapon_Settings(), HUD_Weapons(), MapInfo_FilterString(), MapVote_CheckRules_count(), and W_FixWeaponOrder_BuildImpulseList().

◆ shuffle()

ERASEABLE void shuffle ( float n,
swapfunc_t swap,
entity pass )

Definition at line 40 of file sort.qh.

41{
42 for (int i = 1; i < n; ++i)
43 {
44 // swap i-th item at a random position from 0 to i
45 // proof for even distribution:
46 // n = 1: obvious
47 // n -> n+1:
48 // item n+1 gets at any position with chance 1/(n+1)
49 // all others will get their 1/n chance reduced by factor n/(n+1)
50 // to be on place n+1, their chance will be 1/(n+1)
51 // 1/n * n/(n+1) = 1/(n+1)
52 // q.e.d.
53 int j = floor(random() * (i + 1));
54 if (j != i)
55 swap(j, i, pass);
56 }
57}
float random(void)

References entity(), floor(), pass, and random().

Referenced by shufflewords().