-
Notifications
You must be signed in to change notification settings - Fork 0
/
inOut.h
199 lines (166 loc) · 5.93 KB
/
inOut.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
void IN_OUT_FN(rotate)(HeliumData* self, VAR* array, size_t a, size_t m, size_t b) {
size_t rl = b - m,
ll = m - a;
#if EXT
size_t bl = self->extBufLen;
VAR* buf = self->extBuf;
#else
size_t bl = self->bufLen,
buf = self->bufPos;
#endif
size_t min = (rl != ll && (bl < ll ? (bl < rl ? bl : rl) : (ll < rl ? ll : rl)) > SMALL_MERGE) ? bl : 1;
while ((rl > min && ll > min) || (rl < SMALL_MERGE && rl > 1 && ll < SMALL_MERGE && ll > 1)) {
if (rl < ll) {
blockSwapFW(array, a, m, rl);
a += rl;
ll -= rl;
} else {
b -= ll;
rl -= ll;
blockSwapBW(array, a, b, ll);
}
}
if (rl == 1) insertToLeft( array, m, a);
else if (ll == 1) insertToRight(array, a, b - 1);
if (min == 1 || rl <= 1 || ll <= 1) return;
#if EXT
if (rl < ll) {
memcpy(buf , array + m, rl * sizeof(VAR));
memcpy(array + b - ll, array + a, ll * sizeof(VAR));
memcpy(array + a , buf , rl * sizeof(VAR));
} else {
memcpy(buf , array + a, ll * sizeof(VAR));
memcpy(array + a , array + m, rl * sizeof(VAR));
memcpy(array + b - ll, buf, ll * sizeof(VAR));
}
#else
if (rl < ll) {
blockSwapBW(array, m, buf, rl);
for (size_t i = m + rl; i-- > a + rl;)
swap(array, i, i - rl);
blockSwapBW(array, buf, a, rl);
} else {
blockSwapFW(array, a, buf, ll);
for (size_t i = a; i + ll < b; i++)
swap(array, i, i + ll);
blockSwapFW(array, buf, b - ll, ll);
}
#endif
}
inline void IN_OUT_FN(blockSelect)(HeliumData* self, VAR* array, size_t a, size_t leftBlocks, size_t rightBlocks, size_t blockLen) {
#if EXT
size_t stKey = 0,
i1 = 0,
k = 0;
size_t* keys = self->keys;
#else
size_t stKey = self->keyPos,
i1 = stKey,
k = stKey;
#endif
size_t tm = stKey + leftBlocks,
j1 = tm,
tb = tm + rightBlocks;
while (k < j1 && j1 < tb) {
if (CMP(
array + a + (i1 - stKey + 1) * blockLen - 1,
array + a + (j1 - stKey + 1) * blockLen - 1
) <= 0) {
if (i1 > k) blockSwapFW(array, a + (k - stKey) * blockLen, a + (i1 - stKey) * blockLen, blockLen);
#if EXT
indexSwap(keys, k++, i1);
#else
swap(array, k++, i1);
#endif
i1 = k;
for (size_t i = (k + 1 > tm ? k + 1 : tm); i < j1; i++) {
#if EXT
if (keys[i] < keys[i1]) i1 = i;
#else
if (CMP(array + i, array + i1) < 0) i1 = i;
#endif
}
} else {
blockSwapFW(array, a + (k - stKey) * blockLen, a + (j1 - stKey) * blockLen, blockLen);
#if EXT
indexSwap(keys, k, j1++);
#else
swap(array, k, j1++);
#endif
if (i1 == k++) i1 = j1 - 1;
}
}
while (k < j1 - 1) {
if (i1 > k) blockSwapFW(array, a + (k - stKey) * blockLen, a + (i1 - stKey) * blockLen, blockLen);
#if EXT
indexSwap(keys, k++, i1);
#else
swap(array, k++, i1);
#endif
i1 = k;
for (size_t i = k + 1; i < j1; i++) {
#if EXT
if (keys[i] < keys[i1]) i1 = i;
#else
if (CMP(array + i, array + i1) < 0) i1 = i;
#endif
}
}
}
#if EXT
#define COMPARE_KEYS(keys, key, midKey) (keys[key] < (midKey))
#define K_TYPE size_t
#else
#define COMPARE_KEYS(array, key, midKey) (CMP(array + (key), &(midKey)) < 0)
#define K_TYPE VAR
#endif
inline void IN_OUT_FN(mergeBlocks)(HeliumData* self, VAR* array, size_t a, K_TYPE midKey, size_t blockQty, size_t blockLen, size_t lastLen) {
#if EXT
size_t* keys = self->keys;
size_t stKey = 0;
#else
VAR* keys = array;
size_t stKey = self->keyPos;
#endif
size_t f = a;
char left = COMPARE_KEYS(keys, stKey, midKey);
for (size_t i = 1; i < blockQty; i++) {
if (left ^ COMPARE_KEYS(keys, stKey + i, midKey)) {
size_t next = a + i * blockLen, nextEnd;
if (left) {
nextEnd = binarySearchLeft( array, next, next + blockLen, array + next - 1);
smartMergeLeft(self, array, f, next, nextEnd);
} else {
nextEnd = binarySearchRight(array, next, next + blockLen, array + next - 1);
smartMergeRight(self, array, f, next, nextEnd);
}
f = nextEnd;
left = !left;
}
}
if (left && lastLen != 0) {
size_t lastFrag = a + blockQty * blockLen;
smartMergeLeft(self, array, f, lastFrag, lastFrag + lastLen);
}
}
#undef COMPARE_KEYS
#undef K_TYPE
void IN_OUT_FN(heliumCombine)(HeliumData* self, VAR* array, size_t a, size_t m, size_t b) {
size_t blockLen = self->blockLen;
COMBINE_REDUCE(self, array, a, m, b, blockLen);
size_t leftBlocks = (m - a) / blockLen,
rightBlocks = (b - m) / blockLen,
blockQty = leftBlocks + rightBlocks,
frag = (b - a) - blockQty * blockLen;
#if EXT
size_t* keys = self->keys;
for (size_t i = blockQty; i--;) keys[i] = i;
size_t midKey = leftBlocks;
#else
size_t keyPos = self->keyPos;
insertSort(array, keyPos, keyPos + blockQty + 1);
VAR midKey = array[keyPos + leftBlocks];
#endif
IN_OUT_FN(blockSelect)(self, array, a, leftBlocks, rightBlocks, blockLen);
IN_OUT_FN(mergeBlocks)(self, array, a, midKey, blockQty, blockLen, frag);
}