Xonotic
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) More...
 
using swapfunc_t = void(int i1, int i2, entity pass)
 is only ever called for i1 < i2 More...
 

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:
for (int start = floor(((_count) - 2) / 2); start >= 0; --start) \
{ \
siftdown(start, (_count) - 1); \
} \
MACRO_END
for(int slot=0;slot< MAX_WEAPONSLOTS;++slot)
Definition: impulse.qc:97

Referenced by heapsort().

◆ siftdown

#define siftdown (   _start,
  _end 
)
Value:
for (int root = (_start); root * 2 + 1 <= (_end); ) \
{ \
int child = root * 2 + 1; \
if (child < (_end) && cmp(child, child + 1, pass) < 0) child += 1; \
if (cmp(root, child, pass) >= 0) break; \
swap(root, child, pass); \
root = child; \
} \
#define MACRO_END
Definition: macro.qh:7
for(int slot=0;slot< MAX_WEAPONSLOTS;++slot)
Definition: impulse.qc:97
if(IS_DEAD(this))
Definition: impulse.qc:92
#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.

References ERASEABLE, heapify, and siftdown.

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

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

◆ shuffle()

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

Definition at line 42 of file sort.qh.

References floor(), and random().

Referenced by shufflewords().

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