UPS: apcupsd support with gui
[tomato.git] / release / src / lzma / C / Sort.c
blobea669c17be2422bddd8a538c940d5c8f3501eb85
1 /* Sort.c */
3 #include "Sort.h"
5 #define HeapSortDown(p, k, size, temp) \
6 { for (;;) { \
7 UInt32 s = (k << 1); \
8 if (s > size) break; \
9 if (s < size && p[s + 1] > p[s]) s++; \
10 if (temp >= p[s]) break; \
11 p[k] = p[s]; k = s; \
12 } p[k] = temp; }
14 void HeapSort(UInt32 *p, UInt32 size)
16 if (size <= 1)
17 return;
18 p--;
20 UInt32 i = size / 2;
23 UInt32 temp = p[i];
24 UInt32 k = i;
25 HeapSortDown(p, k, size, temp)
27 while(--i != 0);
32 UInt32 k = 1;
33 UInt32 temp = p[size];
34 p[size--] = p[1];
35 HeapSortDown(p, k, size, temp)
37 while (size > 1);
39 while (size > 3)
41 UInt32 temp = p[size];
42 UInt32 k = (p[3] > p[2]) ? 3 : 2;
43 p[size--] = p[1];
44 p[1] = p[k];
45 HeapSortDown(p, k, size, temp)
48 UInt32 temp = p[size];
49 p[size] = p[1];
50 if (size > 2 && p[2] < temp)
52 p[1] = p[2];
53 p[2] = temp;
55 else
56 p[1] = temp;
61 #define HeapSortRefDown(p, vals, n, size, temp) \
62 { UInt32 k = n; UInt32 val = vals[temp]; for (;;) { \
63 UInt32 s = (k << 1); \
64 if (s > size) break; \
65 if (s < size && vals[p[s + 1]] > vals[p[s]]) s++; \
66 if (val >= vals[p[s]]) break; \
67 p[k] = p[s]; k = s; \
68 } p[k] = temp; }
70 void HeapSortRef(UInt32 *p, UInt32 *vals, UInt32 size)
72 if (size <= 1)
73 return;
74 p--;
76 UInt32 i = size / 2;
79 UInt32 temp = p[i];
80 HeapSortRefDown(p, vals, i, size, temp);
82 while(--i != 0);
86 UInt32 temp = p[size];
87 p[size--] = p[1];
88 HeapSortRefDown(p, vals, 1, size, temp);
90 while (size > 1);