-
Notifications
You must be signed in to change notification settings - Fork 9
/
HayateShiki.h
583 lines (437 loc) · 20.1 KB
/
HayateShiki.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#pragma once
// Copyright(c) 2018 Emura Daisuke
// MIT License
#include <cstddef>
#include <utility>
#include <malloc.h>
#if _MSC_VER//[
// msvc
#define alloca _alloca
#elif __clang__//][
// clang
#else//][
// gcc
#endif//]
#define Auto decltype(auto)
#define local_alloc(T, s) static_cast<T*>(alloca(s))
#define local_array(T, n) local_alloc(T, sizeof(T) * n)
namespace HayateShiki {
//
template <class RandomAccessIterator>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last);
template <class RandomAccessIterator, class Compare>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last, Compare comp);
//
template <class RandomAccessIterator, class Compare> class Private
{
private:
static constexpr std::size_t Bit(int v) noexcept
{
return (1ULL << v);
}
static constexpr std::size_t cbIns = 5;
static constexpr std::size_t cnIns = Bit(cbIns);
private:
template <class ForwardIterator, class ValueType> class Array
{
public:
using val_t = ValueType;
using ref_t = val_t&;
using ptr_t = val_t*;
using itr_t = ptr_t;
using dif_t = ptrdiff_t;
private:
bool mbTemporary;
dif_t mSize;
ptr_t mData;
public:
~Array() noexcept
{
if (mbTemporary) ::operator delete(mData, std::nothrow);
}
Array(ForwardIterator first, ForwardIterator last)
:mbTemporary(false)
,mSize(std::distance(first, last))
,mData(&*first)
{}
Array(dif_t Size)
:mbTemporary(true)
,mSize(Size)
,mData(static_cast<ptr_t>(::operator new(sizeof(val_t) * Size)))
{}
itr_t begin() const noexcept
{
return mData;
}
itr_t end() const noexcept
{
return mData + mSize;
}
private:
Array(const Array&) = delete;
Array& operator =(const Array&) = delete;
};
public:
class Technique {
private:
using rai_t = RandomAccessIterator;
using rai_v = typename std::iterator_traits<rai_t>::value_type;
using itr_t = typename Array<rai_t, rai_v>::itr_t;
using val_t = typename Array<rai_t, rai_v>::val_t;
using ref_t = typename Array<rai_t, rai_v>::ref_t;
using dif_t = typename Array<rai_t, rai_v>::dif_t;
using cmp_t = Compare;
private:
const dif_t mnOriginal;
Array<rai_t, rai_v> maOriginal;
Array<rai_t, rai_v> maExternal;
const cmp_t mComp;
private:
struct Part
{
enum oRow {
oAsc,
oDsc,
oNum,
};
itr_t a[oNum];
dif_t n[oNum];
oRow o;
};
struct Unit
{
itr_t a;
dif_t n;
};
struct Dive
{
Unit mUnit;
itr_t miJoin;
};
private:
int Msb(std::size_t v) noexcept
{
if (v){
union IEEE_754 {
float v;
struct {
unsigned int fraction:23;
unsigned int exponent:8;
unsigned int sign:1;
} map;
};
IEEE_754 lzc;
lzc.v = v;
return (lzc.map.exponent - 127);
}
return -1;
}
int MsbAlignment(std::size_t v) noexcept
{
return Msb(v + v - 1);
}
int LowerLimit(int v, int limit) noexcept
{
return (v > limit)? v: limit;
}
itr_t Copy(itr_t iDst, itr_t iSrc, dif_t nSrc)
{
while (nSrc--) *iDst++ = std::move(*iSrc++);
return iDst;
}
itr_t Copy(itr_t iDst, itr_t iSrc, dif_t nSrc, ref_t v)
{
*iDst = std::move(v);
return Copy(++iDst, ++iSrc, --nSrc);
}
itr_t Join(itr_t iJoin, Unit& rUnit, const Part& rPart)
{
auto nDsc = rPart.n[Part::oDsc];
auto nAsc = rPart.n[Part::oAsc];
rUnit.a = iJoin;
rUnit.n = nDsc + nAsc;
iJoin = Copy(iJoin, rPart.a[Part::oDsc], nDsc);
iJoin = Copy(iJoin, rPart.a[Part::oAsc], nAsc);
return iJoin;
}
itr_t Join(itr_t aJoin, Unit& rUnit, const Part& rPart0, const Part& rPart1)
{
auto iJoin = aJoin;
auto o0 = rPart0.o;
auto o1 = rPart1.o;
auto i0 = rPart0.a[o0];
auto i1 = rPart1.a[o1];
auto n0 = rPart0.n[o0];
auto n1 = rPart1.n[o1];
auto v0 = std::move(*i0);
auto v1 = std::move(*i1);
auto Comp = mComp;
while (true){
if (Comp(v1, v0)){
*iJoin++ = std::move(v1);
if (--n1){
v1 = std::move(*++i1);
} else {
if (o1){
o1 = Part::oAsc;
i1 = rPart1.a[o1];
n1 = rPart1.n[o1];
v1 = std::move(*i1);
} else {
iJoin = Copy(iJoin, i0, n0, v0);
if (o0) iJoin = Copy(iJoin, rPart0.a[Part::oAsc], rPart0.n[Part::oAsc]);
break;
}
}
} else {
*iJoin++ = std::move(v0);
if (--n0){
v0 = std::move(*++i0);
} else {
if (o0){
o0 = Part::oAsc;
i0 = rPart0.a[o0];
n0 = rPart0.n[o0];
v0 = std::move(*i0);
} else {
iJoin = Copy(iJoin, i1, n1, v1);
if (o1) iJoin = Copy(iJoin, rPart1.a[Part::oAsc], rPart1.n[Part::oAsc]);
break;
}
}
}
}
rUnit.a = aJoin;
rUnit.n = std::distance(aJoin, iJoin);
return iJoin;
}
itr_t Join(itr_t aJoin, Unit& rUnit, const Unit& rUnit0, const Unit& rUnit1)
{
auto iJoin = aJoin;
auto i0 = rUnit0.a;
auto i1 = rUnit1.a;
auto n0 = rUnit0.n;
auto n1 = rUnit1.n;
auto v0 = std::move(*i0);
auto v1 = std::move(*i1);
auto Comp = mComp;
while (true){
if (Comp(v1, v0)){
*iJoin++ = std::move(v1);
if (--n1){
v1 = std::move(*++i1);
} else {
iJoin = Copy(iJoin, i0, n0, v0);
break;
}
} else {
*iJoin++ = std::move(v0);
if (--n0){
v0 = std::move(*++i0);
} else {
iJoin = Copy(iJoin, i1, n1, v1);
break;
}
}
}
rUnit.a = aJoin;
rUnit.n = std::distance(aJoin, iJoin);
return iJoin;
}
itr_t Ascending(itr_t iSrc, itr_t eSrc, dif_t nSrc)
{
auto nIns = (nSrc < cnIns)? nSrc: cnIns;
auto aIns = iSrc - 1;
auto eIns = iSrc + nIns;
auto Comp = mComp;
while (++iSrc != eIns){
if (Comp(iSrc[0], iSrc[-1])){
auto i = iSrc;
auto v = std::move(i[0]);
do {
i[0] = std::move(i[-1]);
} while (--i != aIns && Comp(v, i[-1]));
i[0] = std::move(v);
}
}
for (; (iSrc != eSrc) && !Comp(iSrc[0], iSrc[-1]); ++iSrc);
return iSrc;
}
itr_t Descending(itr_t iSrc, itr_t eSrc, dif_t nSrc)
{
auto nIns = (nSrc < cnIns)? nSrc: cnIns;
auto aIns = iSrc - 1;
auto eIns = iSrc + nIns;
auto Comp = mComp;
while (++iSrc != eIns){
if (!Comp(iSrc[0], iSrc[-1])){
auto i = iSrc;
auto v = std::move(i[0]);
do {
i[0] = std::move(i[-1]);
} while (--i != aIns && !Comp(v, i[-1]));
i[0] = std::move(v);
}
}
for (; (iSrc != eSrc) && Comp(iSrc[0], iSrc[-1]); ++iSrc);
std::reverse(aIns, iSrc);
return iSrc;
}
dif_t MakePart(Part& rPart, itr_t& riSrc, itr_t eSrc, itr_t& raDsc)
{
auto iSrc = riSrc;
auto aAsc = iSrc;
auto eAsc = eSrc;
auto Comp = mComp;
{ //
Auto nSrc = std::distance(iSrc, eSrc);
if (nSrc > 1){
if (Comp(iSrc[1], iSrc[0])){
eAsc = Descending(iSrc+1, eSrc, nSrc-1);
} else {
eAsc = Ascending(iSrc+1, eSrc, nSrc-1);
}
}
}
{ //
auto aDsc = raDsc;
auto eDsc = aDsc;
auto iOdd = eAsc;
auto nOdd = std::distance(iOdd, eSrc);
if (nOdd){
if (Comp(*iOdd, aAsc[0]/*Min*/)){
*--aDsc = std::move(*iOdd++);
while (--nOdd){
if (Comp(*iOdd, eAsc[-1]/*Max*/)){
if (Comp(*iOdd, aDsc[0]/*Min*/)){
*--aDsc = std::move(*iOdd++);
} else {
break;
}
} else {
*eAsc++ = std::move(*iOdd++);
}
}
}
}
{ //
Auto nDsc = std::distance(aDsc, eDsc);
rPart.a[Part::oAsc] = aAsc;
rPart.n[Part::oAsc] = std::distance(aAsc, eAsc);
rPart.a[Part::oDsc] = aDsc;
rPart.n[Part::oDsc] = nDsc;
rPart.o = (nDsc)? Part::oDsc: Part::oAsc;
}
riSrc = iOdd;
raDsc = aDsc;
return nOdd;
}
}
void Turn(itr_t iDst, itr_t iSrc, dif_t nSrc)
{
iDst += nSrc;
iSrc += nSrc;
while (nSrc--) *--iDst = std::move(*--iSrc);
}
void Turn(const Part& rPart)
{
auto nDsc = rPart.n[Part::oDsc];
auto nAsc = rPart.n[Part::oAsc];
auto aDsc = maOriginal.begin();
auto aAsc = aDsc + nDsc;
if (nDsc){
Turn(aAsc, rPart.a[Part::oAsc], nAsc);
Turn(aDsc, rPart.a[Part::oDsc], nDsc);
}
}
public:
~Technique() noexcept
{}
Technique(rai_t const first, rai_t const last, cmp_t comp)
:mnOriginal(std::distance(first, last))
,maOriginal(first, last)
,maExternal(mnOriginal)
,mComp(comp)
{}
void Sort()
{
if (mnOriginal > 1){
Auto nDive = LowerLimit((MsbAlignment(mnOriginal) - cbIns), 1);
Auto aDive = local_array(Dive, (nDive+1));
for (int oDive = 0; oDive < nDive; ++oDive) aDive[oDive].miJoin = (oDive & Bit(0))? maExternal.begin(): maOriginal.begin();
{ //
std::size_t nJoin = 0;
{ //
auto iJoin = maExternal.begin();
Auto iSrc = maOriginal.begin();
Auto eSrc = maOriginal.end();
while (iSrc != eSrc){
Unit vUnit;
{ //
Part vPart0, vPart1;
auto aDsc = maExternal.end();
if (MakePart(vPart0, iSrc, eSrc, aDsc)){
MakePart(vPart1, iSrc, eSrc, aDsc);
iJoin = Join(iJoin, vUnit, vPart0, vPart1);
} else {
if (nJoin){
iJoin = Join(iJoin, vUnit, vPart0);
} else {
Turn(vPart0);
break;
}
}
}
{ //
auto pDive = &aDive[0];
auto Carry = nJoin++;
Carry = (nJoin ^ Carry) & Carry;
for (; Carry; Carry >>= 1, ++pDive){
pDive->miJoin = Join(pDive->miJoin, vUnit, pDive->mUnit, vUnit);
}
pDive->mUnit = vUnit;
}
}
}
if (nJoin){
auto bJoin = nJoin & -nJoin;
auto oDive = Msb(bJoin);
auto pResult = &aDive[oDive++];
{ //
auto pDive = &aDive[oDive];
auto Carry = nJoin ^ bJoin;
Carry >>= oDive;
for (; Carry; Carry >>= 1, ++pDive){
if (Carry & Bit(0)){
Join(pDive->miJoin, pResult->mUnit, pDive->mUnit, pResult->mUnit);
}
}
}
if (pResult->mUnit.a == maExternal.begin()){
Copy(maOriginal.begin(), pResult->mUnit.a, pResult->mUnit.n);
}
}
}
}
}
};
public:
static void Sort(RandomAccessIterator const first, RandomAccessIterator const last, Compare comp)
{
Technique(first, last, comp).Sort();
}
};
template <class RandomAccessIterator>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last)
{
HayateShiki::sort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
template <class RandomAccessIterator, class Compare>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last, Compare comp)
{
HayateShiki::Private<RandomAccessIterator, Compare>::Sort(first, last, comp);
}
}
#undef Auto
#undef local_alloc
#undef local_array