-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheliumLoop.h
115 lines (91 loc) · 3.02 KB
/
heliumLoop.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
void FN_NAME(HeliumData* self, VAR* array, size_t a, size_t b) {
size_t r = RUN_SIZE,
l = b - a, bl;
#if EXT_BUF
bl = self->extBufLen;
while (r <= bl) {
size_t twoR = r << 1, i;
for (i = a; i + twoR < b; i += twoR)
mergeOOP(self, array, i, i + r, i + twoR);
if (i + r < b) mergeOOP(self, array, i, i + r, b);
r = twoR;
}
#endif
#if INT_BUF
bl = self->bufLen;
while (r <= bl) {
size_t twoR = r << 1, i;
for (i = a; i + twoR < b; i += twoR)
mergeWithBuffer(self, array, i, i + r, i + twoR);
if (i + r < b) mergeWithBuffer(self, array, i, i + r, b);
r = twoR;
}
#endif
#if STRAT4
size_t kLen = self->keyLen,
kPos = self->keyPos;
#endif
while (r < l) {
size_t twoR = r << 1, i;
#if STRAT4
#if EXT_BUF && !INT_BUF
size_t bLen = self->blockLen;
if (twoR / bLen + 1 > kLen) {
do bLen <<= 1; while (twoR / bLen + 1 > kLen);
self->blockLen = bLen;
}
#else
size_t sqrtTwoR = self->blockLen;
for (; sqrtTwoR * sqrtTwoR < twoR; sqrtTwoR <<= 1);
size_t kCnt = twoR / sqrtTwoR + 1;
if (kCnt < kLen) {
self->bufLen = kLen - kCnt;
self->bufPos = kPos + kCnt;
self->hasIntBuf = 1;
} else {
if (kCnt > kLen) {
do sqrtTwoR <<= 1; while (twoR / sqrtTwoR + 1 > kLen);
}
self->bufLen = 0;
self->hasIntBuf = 0;
}
self->blockLen = sqrtTwoR;
#endif
#endif
for (i = a; i + twoR < b; i += twoR) {
#if EXT_KEYS
heliumCombineOOP(self, array, i, i + r, i + twoR);
#else
heliumCombineInPlace(self, array, i, i + r, i + twoR);
#endif
}
if (i + r < b) {
#if STRAT4
if (b - i - r <= kLen) {
self->bufPos = kPos;
self->bufLen = kLen;
}
#endif
#if EXT_KEYS
heliumCombineOOP(self, array, i, i + r, b);
#else
heliumCombineInPlace(self, array, i, i + r, b);
#endif
}
r = twoR;
}
#if !(EXT_KEYS && EXT_BUF)
size_t s = self->keyPos, e;
#if STRAT4
e = s + kLen;
#else
e = s + self->keyLen + self->bufLen;
#endif
self->bufLen = 0;
self->hasIntBuf = 0;
insertSort(array, s, e);
REDUCE_BOUNDS(array, a, s, e);
if (!optiSmartMergeLeft(self, array, a, s, e))
mergeInPlaceLeft(self, array, a, s, e);
#endif
}