Fix
[ryzomcore.git] / nel / 3rdparty / seven_zip / Sort.c
blobe1097e3806829d4ca27a9e0ccf457af08f5d976e
1 /* Sort.c -- Sort functions
2 2014-04-05 : Igor Pavlov : Public domain */
4 #include "Precomp.h"
6 #include "Sort.h"
8 #define HeapSortDown(p, k, size, temp) \
9 { for (;;) { \
10 size_t s = (k << 1); \
11 if (s > size) break; \
12 if (s < size && p[s + 1] > p[s]) s++; \
13 if (temp >= p[s]) break; \
14 p[k] = p[s]; k = s; \
15 } p[k] = temp; }
17 void HeapSort(UInt32 *p, size_t size)
19 if (size <= 1)
20 return;
21 p--;
23 size_t i = size / 2;
26 UInt32 temp = p[i];
27 size_t k = i;
28 HeapSortDown(p, k, size, temp)
30 while (--i != 0);
35 size_t k = 1;
36 UInt32 temp = p[size];
37 p[size--] = p[1];
38 HeapSortDown(p, k, size, temp)
40 while (size > 1);
42 while (size > 3)
44 UInt32 temp = p[size];
45 size_t k = (p[3] > p[2]) ? 3 : 2;
46 p[size--] = p[1];
47 p[1] = p[k];
48 HeapSortDown(p, k, size, temp)
51 UInt32 temp = p[size];
52 p[size] = p[1];
53 if (size > 2 && p[2] < temp)
55 p[1] = p[2];
56 p[2] = temp;
58 else
59 p[1] = temp;
63 void HeapSort64(UInt64 *p, size_t size)
65 if (size <= 1)
66 return;
67 p--;
69 size_t i = size / 2;
72 UInt64 temp = p[i];
73 size_t k = i;
74 HeapSortDown(p, k, size, temp)
76 while (--i != 0);
81 size_t k = 1;
82 UInt64 temp = p[size];
83 p[size--] = p[1];
84 HeapSortDown(p, k, size, temp)
86 while (size > 1);
88 while (size > 3)
90 UInt64 temp = p[size];
91 size_t k = (p[3] > p[2]) ? 3 : 2;
92 p[size--] = p[1];
93 p[1] = p[k];
94 HeapSortDown(p, k, size, temp)
97 UInt64 temp = p[size];
98 p[size] = p[1];
99 if (size > 2 && p[2] < temp)
101 p[1] = p[2];
102 p[2] = temp;
104 else
105 p[1] = temp;
110 #define HeapSortRefDown(p, vals, n, size, temp) \
111 { size_t k = n; UInt32 val = vals[temp]; for (;;) { \
112 size_t s = (k << 1); \
113 if (s > size) break; \
114 if (s < size && vals[p[s + 1]] > vals[p[s]]) s++; \
115 if (val >= vals[p[s]]) break; \
116 p[k] = p[s]; k = s; \
117 } p[k] = temp; }
119 void HeapSortRef(UInt32 *p, UInt32 *vals, size_t size)
121 if (size <= 1)
122 return;
123 p--;
125 size_t i = size / 2;
128 UInt32 temp = p[i];
129 HeapSortRefDown(p, vals, i, size, temp);
131 while (--i != 0);
135 UInt32 temp = p[size];
136 p[size--] = p[1];
137 HeapSortRefDown(p, vals, 1, size, temp);
139 while (size > 1);