forked from arminbiere/satch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.h
173 lines (159 loc) · 4.42 KB
/
qsort.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
#ifndef QSORT_H_
#define QSORT_H_
/*------------------------------------------------------------------------*/
// Generic header only qsort implementation ported originally from CaDiCaL in
// C++ to C in Kissat
/*------------------------------------------------------------------------*/
#define GREATER_SWAP(TYPE,P,Q,LESS) \
do { \
if (LESS (Q, P)) \
SWAP (TYPE, P, Q); \
} while(0)
#define SWAP(TYPE,A,B) \
do { \
TYPE TMP_SWAP = (A); \
(A) = (B); \
(B) = (TMP_SWAP); \
} while (0)
#define SORTER (solver->sorter)
#define PARTITION(TYPE,L,R,A,LESS) \
do { \
const size_t L_PARTITION = (L); \
I_QUICK_SORT = L_PARTITION - 1; \
\
size_t J_PARTITION = (R); \
\
TYPE PIVOT_PARTITION = A[J_PARTITION]; \
\
for (;;) \
{ \
while (LESS (A[++I_QUICK_SORT], PIVOT_PARTITION)) \
; \
while (LESS (PIVOT_PARTITION, A[--J_PARTITION])) \
if (J_PARTITION == L_PARTITION) \
break; \
\
if (I_QUICK_SORT >= J_PARTITION) \
break; \
\
SWAP (TYPE, A[I_QUICK_SORT], A[J_PARTITION]); \
} \
\
SWAP (TYPE, A[I_QUICK_SORT], A[R]); \
} while(0)
#define QUICK_SORT_LIMIT 10
#define QUICK_SORT(TYPE,N,A,LESS) \
do { \
assert (N); \
assert (EMPTY_STACK (SORTER)); \
\
size_t L_QUICK_SORT = 0; \
size_t R_QUICK_SORT = N - 1; \
\
if (R_QUICK_SORT - L_QUICK_SORT <= QUICK_SORT_LIMIT) \
break; \
\
for (;;) \
{ \
const size_t M = L_QUICK_SORT + (R_QUICK_SORT - L_QUICK_SORT) / 2; \
\
SWAP (TYPE, A[M], A[R_QUICK_SORT - 1]); \
\
GREATER_SWAP (TYPE, A[L_QUICK_SORT], A[R_QUICK_SORT - 1], LESS); \
GREATER_SWAP (TYPE, A[L_QUICK_SORT], A[R_QUICK_SORT], LESS); \
GREATER_SWAP (TYPE, A[R_QUICK_SORT - 1], A[R_QUICK_SORT], LESS); \
\
size_t I_QUICK_SORT; \
\
PARTITION (TYPE, L_QUICK_SORT + 1, R_QUICK_SORT - 1, A, LESS); \
assert (L_QUICK_SORT < I_QUICK_SORT); \
assert (I_QUICK_SORT <= R_QUICK_SORT); \
\
size_t LL_QUICK_SORT; \
size_t RR_QUICK_SORT; \
\
if (I_QUICK_SORT - L_QUICK_SORT < R_QUICK_SORT - I_QUICK_SORT) \
{ \
LL_QUICK_SORT = I_QUICK_SORT + 1; \
RR_QUICK_SORT = R_QUICK_SORT; \
R_QUICK_SORT = I_QUICK_SORT - 1; \
} \
else \
{ \
LL_QUICK_SORT = L_QUICK_SORT; \
RR_QUICK_SORT = I_QUICK_SORT - 1; \
L_QUICK_SORT = I_QUICK_SORT + 1; \
} \
if (R_QUICK_SORT - L_QUICK_SORT > QUICK_SORT_LIMIT) \
{ \
assert (RR_QUICK_SORT - LL_QUICK_SORT > QUICK_SORT_LIMIT); \
PUSH (SORTER, LL_QUICK_SORT); \
PUSH (SORTER, RR_QUICK_SORT); \
} \
else if (RR_QUICK_SORT - LL_QUICK_SORT > QUICK_SORT_LIMIT) \
{ \
L_QUICK_SORT = LL_QUICK_SORT; \
R_QUICK_SORT = RR_QUICK_SORT; \
} \
else if (!EMPTY_STACK (SORTER)) \
{ \
R_QUICK_SORT = POP (SORTER); \
L_QUICK_SORT = POP (SORTER); \
} else break; \
} \
} while (0)
#define INSERTION_SORT(TYPE,N,A,LESS) \
do { \
size_t L_INSERTION_SORT = 0; \
size_t R_INSERTION_SORT = N - 1; \
\
for (size_t I_INSERTION_SORT = R_INSERTION_SORT; \
I_INSERTION_SORT > L_INSERTION_SORT; \
I_INSERTION_SORT--) \
GREATER_SWAP (TYPE, A[I_INSERTION_SORT - 1], A[I_INSERTION_SORT], LESS); \
\
for (size_t I_INSERTION_SORT = L_INSERTION_SORT + 2; \
I_INSERTION_SORT <= R_INSERTION_SORT; \
I_INSERTION_SORT++) \
{ \
TYPE PIVOT_INSERTION_SORT = A[I_INSERTION_SORT]; \
\
size_t J_INSERTION_SORT = I_INSERTION_SORT; \
\
while (LESS (PIVOT_INSERTION_SORT, A[J_INSERTION_SORT - 1])) \
{ \
A[J_INSERTION_SORT] = A[J_INSERTION_SORT - 1]; \
J_INSERTION_SORT--; \
} \
\
A[J_INSERTION_SORT] = PIVOT_INSERTION_SORT; \
} \
} while (0)
#ifdef NDEBUG
#define CHECK_SORTED(...) do { } while(0)
#else
#define CHECK_SORTED(N,A,LESS) \
do { \
for (size_t I_CHECK_SORTED = 0; I_CHECK_SORTED < N - 1; I_CHECK_SORTED++) \
assert (!LESS (A[I_CHECK_SORTED+1], A[I_CHECK_SORTED])); \
} while(0)
#endif
#define SORT(TYPE,N,A,LESS) \
do { \
const size_t N_SORT = (N); \
if (!N_SORT) \
break; \
TYPE * A_SORT = (A); \
QUICK_SORT (TYPE, N_SORT, A_SORT, LESS); \
INSERTION_SORT (TYPE, N_SORT, A_SORT, LESS); \
CHECK_SORTED (N_SORT, A_SORT, LESS); \
} while (0)
#define SORT_STACK(TYPE,S,LESS) \
do { \
const size_t N_SORT_STACK = SIZE_STACK (S); \
if (N_SORT_STACK <= 1) \
break; \
TYPE * A_SORT_STACK = (S).begin; \
SORT (TYPE, N_SORT_STACK, A_SORT_STACK, LESS); \
} while (0)
#endif // QSORT_H_