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
/*
Copyright (C) 2021 Daniel Schultz
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "longlong.h"
#include "mpoly.h"
#include "gr_mpoly.h"
/*
sort terms in [left, right) by exponent
assuming that bits in position >= pos are already sorted
and assuming exponent vectors fit into one word
and assuming that all bit positions that need to be sorted are in totalmask
*/
static void _gr_mpoly_radix_sort1(
gr_ptr Acoeffs,
ulong * Aexps,
slong left, slong right,
flint_bitcnt_t pos,
ulong cmpmask,
ulong totalmask,
gr_ctx_t cctx)
{
ulong mask, cmp;
slong mid, cur;
slong sz = cctx->sizeof_elem;
gr_method_swap_op swap = GR_SWAP_OP(cctx, SWAP);
while (pos > 0)
{
pos--;
FLINT_ASSERT(left <= right);
FLINT_ASSERT(pos < FLINT_BITS);
mask = UWORD(1) << pos;
cmp = cmpmask & mask;
/* insertion base case */
if (right - left < 10)
{
slong i, j;
for (i = left + 1; i < right; i++)
{
for (j = i; j > left && mpoly_monomial_gt1(Aexps[j],
Aexps[j - 1], cmpmask); j--)
{
swap(GR_ENTRY(Acoeffs, j, sz), GR_ENTRY(Acoeffs, j - 1, sz), cctx);
FLINT_SWAP(ulong, Aexps[j], Aexps[j - 1]);
}
}
return;
}
/* return if there is no information to sort on this bit */
if ((totalmask & mask) == 0)
continue;
/* find first 'zero' */
mid = left;
while (mid < right && (Aexps[mid] & mask) != cmp)
mid++;
/* make sure [left,mid) doesn't match cmpmask in position pos 'one'
[mid,right) does match cmpmask in position pos 'zero' */
cur = mid;
while (++cur < right)
{
if ((Aexps[cur] & mask) != cmp)
{
swap(GR_ENTRY(Acoeffs, cur, sz), GR_ENTRY(Acoeffs, mid, sz), cctx);
FLINT_SWAP(ulong, Aexps[cur], Aexps[mid]);
mid++;
}
}
if (mid - left < right - mid)
{
_gr_mpoly_radix_sort1(Acoeffs, Aexps, left, mid,
pos, cmpmask, totalmask, cctx);
left = mid;
}
else
{
_gr_mpoly_radix_sort1(Acoeffs, Aexps, mid, right,
pos, cmpmask, totalmask, cctx);
right = mid;
}
}
}
/*
sort terms in [left, right) by exponent
assuming that bits in position >= pos are already sorted
*/
static void _gr_mpoly_radix_sort(
gr_ptr Acoeffs,
ulong * Aexps,
slong left, slong right,
flint_bitcnt_t pos,
slong N,
ulong * cmpmask,
gr_ctx_t cctx)
{
ulong off, bit, mask, cmp;
slong mid, check;
slong sz = cctx->sizeof_elem;
gr_method_swap_op swap = GR_SWAP_OP(cctx, SWAP);
while (pos > 0)
{
pos--;
FLINT_ASSERT(left <= right);
FLINT_ASSERT(pos < N*FLINT_BITS);
off = pos/FLINT_BITS;
bit = pos%FLINT_BITS;
mask = UWORD(1) << bit;
cmp = cmpmask[off] & mask;
/* insertion base case */
if (right - left < 20)
{
slong i, j;
for (i = left + 1; i < right; i++)
{
for (j = i; j > left && mpoly_monomial_gt(Aexps + N*j,
Aexps + N*(j - 1), N, cmpmask); j--)
{
swap(GR_ENTRY(Acoeffs, j, sz), GR_ENTRY(Acoeffs, j - 1, sz), cctx);
mpoly_monomial_swap(Aexps + N*j, Aexps + N*(j - 1), N);
}
}
return;
}
/* find first 'zero' */
mid = left;
while (mid < right && ((Aexps+N*mid)[off] & mask) != cmp)
mid++;
/* make sure [left,mid) doesn't match cmpmask in position pos 'one'
[mid,right) does match cmpmask in position pos 'zero' */
check = mid;
while (++check < right)
{
if (((Aexps + N*check)[off] & mask) != cmp)
{
swap(GR_ENTRY(Acoeffs, check, sz), GR_ENTRY(Acoeffs, mid, sz), cctx);
mpoly_monomial_swap(Aexps + N*check, Aexps + N*mid, N);
mid++;
}
}
FLINT_ASSERT(left <= mid && mid <= right);
if (mid - left < right - mid)
{
_gr_mpoly_radix_sort(Acoeffs, Aexps, left, mid,
pos, N, cmpmask, cctx);
left = mid;
}
else
{
_gr_mpoly_radix_sort(Acoeffs, Aexps, mid, right,
pos, N, cmpmask, cctx);
right = mid;
}
}
}
/*
sort the terms in A by exponent
assuming that the exponents are valid (other than being in order)
*/
void gr_mpoly_sort_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx)
{
mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx);
gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx);
slong i, N;
flint_bitcnt_t pos;
gr_ptr Acoeffs = A->coeffs;
ulong * Aexps = A->exps;
ulong himask, * ptempexp;
TMP_INIT;
TMP_START;
N = mpoly_words_per_exp(A->bits, mctx);
ptempexp = (ulong *) TMP_ALLOC(N*sizeof(ulong));
mpoly_get_cmpmask(ptempexp, N, A->bits, mctx);
himask = 0;
for (i = 0; i < A->length; i++)
himask |= (Aexps + N*i)[N - 1];
pos = FLINT_BIT_COUNT(himask);
if (N == 1)
_gr_mpoly_radix_sort1(Acoeffs, Aexps, 0, A->length,
pos, ptempexp[0], himask, cctx);
else
_gr_mpoly_radix_sort(Acoeffs, Aexps, 0, A->length,
(N - 1)*FLINT_BITS + pos, N, ptempexp, cctx);
TMP_END;
}