Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Sorting.c
Go to the documentation of this file.
1 /*
2 Base class for compare ooepartion for sorting.
3 
4 Example:
5 
6 class SCR_TestSortObject_CompareCost : SCR_SortCompare<SCR_TestSortObject>
7 {
8  override int Compare(SCR_TestSortObject left, SCR_TestSortObject right)
9  {
10  return left.cost < right.cost;
11  }
12 };
13 */
14 
15 class SCR_SortCompare<Class T>
16 {
20  static int Compare(T left, T right);
21 };
22 
23 
24 /*
25 Collection of functions related to sorting.
26 */
27 class SCR_Sorting<Class T, SCR_SortCompare TCompare>
28 {
29  //---------------------------------------------------------------------------------------------------
32  static void HeapSort(array<T> a, bool inverse = false)
33  {
34  // Heapify the array
35  Heap_Init(a, inverse);
36 
37  int endId = a.Count() - 1;
38 
39  while (endId > 0)
40  {
41  // Root is max now, swap it with last element
42  T temp = a[endId];
43  a[endId] = a[0];
44  a[0] = temp;
45 
46  endId--;
47 
48  Heap_SiftDown(a, 0, endId, inverse);
49  }
50  }
51 
52 
53 
54 
55 
56  //---------------------------------------------------------------------------------------------------
57  // Help functions for heap sorting
58  //---------------------------------------------------------------------------------------------------
59 
60  //---------------------------------------------------------------------------------------------------
61  static int Heap_IdParent(int nodeId)
62  {
63  // floor ( (nodeId - 1) / 2 )
64  return (nodeId - 1) >> 1;
65  }
66 
67  //---------------------------------------------------------------------------------------------------
68  static int Heap_IdLeftChild(int nodeId)
69  {
70  return 2*nodeId + 1;
71  }
72 
73 
74  //---------------------------------------------------------------------------------------------------
75  static void Heap_SiftDown(array<T> a, int startId, int endId, bool inverse)
76  {
77  int rootId = startId;
78 
79  // 1 when inverse, 0 otherwise. We do XOR operation with it to invert compare result.
80  int invertMask = inverse;
81 
82  while (Heap_IdLeftChild(rootId) <= endId)
83  {
84  int leftChildId = Heap_IdLeftChild(rootId);
85  int rightChildId = leftChildId + 1;
86  int maxId = rootId;
87 
88  // Find which of the three (root, left child, right child) is max
89  if (TCompare.Compare(a[maxId], a[leftChildId]) ^ invertMask)
90  maxId = leftChildId;
91  if (rightChildId <= endId)
92  {
93  if (TCompare.Compare(a[maxId], a[rightChildId]) ^ invertMask)
94  maxId = rightChildId;
95  }
96 
97  if (maxId == rootId)
98  return; // Root is max, and same for children relative to their children, ...
99  else
100  {
101  // Swap root with the max element,
102  // Evaluate the swapped child's tree
103  T temp = a[maxId];
104  a[maxId] = a[rootId];
105  a[rootId] = temp;
106  rootId = maxId;
107  }
108  }
109  }
110 
111  //---------------------------------------------------------------------------------------------------
113  static void Heap_Init(array<T> a, bool inverse)
114  {
115  int count = a.Count();
116 
117  int startId = Heap_IdParent(count - 1);
118 
119  while (startId >= 0)
120  {
121  Heap_SiftDown(a, startId, count - 1, inverse);
122  startId--;
123  }
124  }
125 };
Compare
SCR_VONRadialDisplay Compare
Definition: SCR_ContentBrowser_AddonsSubMenu.c:1612