Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/vf/vq.c

raw
1 #include "otfcc/vf/vq.h"
2 #include "support/util.h"
3
4 // Variation vector
5 caryll_standardValType(pos_t, vq_iPosT);
6 caryll_VectorImplFunctions(VV, pos_t, vq_iPosT);
7
8 static VV createNeutralVV(tableid_t dimensions) {
9 VV vv;
10 iVV.initN(&vv, dimensions);
11 for (tableid_t j = 0; j < dimensions; j++) {
12 vv.items[j] = 0;
13 }
14 return vv;
15 };
16 caryll_VectorInterfaceTypeName(VV) iVV = {
17 caryll_VectorImplAssignments(VV, pos_t, vq_iPosT),
18 .neutral = createNeutralVV,
19 };
20
21 // VQS
22 static INLINE void initVQSegment(vq_Segment *vqs) {
23 vqs->type = VQ_STILL;
24 vqs->val.still = 0;
25 }
26 static INLINE void copyVQSegment(vq_Segment *dst, const vq_Segment *src) {
27 dst->type = src->type;
28 switch (dst->type) {
29 case VQ_STILL:
30 dst->val.still = src->val.still;
31 break;
32 case VQ_DELTA:
33 dst->val.delta.quantity = src->val.delta.quantity;
34 dst->val.delta.region = src->val.delta.region;
35 }
36 }
37 static INLINE void disposeVQSegment(vq_Segment *vqs) {
38 switch (vqs->type) {
39 case VQ_DELTA:
40 break;
41 default:;
42 }
43 initVQSegment(vqs);
44 }
45
46 caryll_standardValTypeFn(vq_Segment, initVQSegment, copyVQSegment, disposeVQSegment);
47 static vq_Segment vqsCreateStill(pos_t x) {
48 vq_Segment vqs;
49 vq_iSegment.init(&vqs);
50 vqs.val.still = x;
51 return vqs;
52 }
53 static vq_Segment vqsCreateDelta(pos_t delta, vq_Region *region) {
54 vq_Segment vqs;
55 vq_iSegment.init(&vqs);
56 vqs.type = VQ_DELTA;
57 vqs.val.delta.quantity = delta;
58 vqs.val.delta.region = region;
59 return vqs;
60 }
61
62 static int vqsCompare(const vq_Segment a, const vq_Segment b) {
63 if (a.type < b.type) return -1;
64 if (a.type > b.type) return 1;
65 switch (a.type) {
66 case VQ_STILL: {
67 if (a.val.still < b.val.still) return -1;
68 if (a.val.still > b.val.still) return 1;
69 return 0;
70 }
71 case VQ_DELTA: {
72 int vqrc = vq_compareRegion(a.val.delta.region, b.val.delta.region);
73 if (vqrc) return vqrc;
74 if (a.val.delta.quantity < b.val.delta.quantity) return -1;
75 if (a.val.delta.quantity > b.val.delta.quantity) return 1;
76 return 0;
77 }
78 default:
79 fprintf(stderr,"! warning: vqsCompare unknown a.type %u. Return -1.\n",a.type);
80 return -1;
81 }
82 }
83 caryll_OrdEqFns(vq_Segment, vqsCompare);
84 static void showVQS(const vq_Segment x) {
85 switch (x.type) {
86 case VQ_STILL:
87 fprintf(stderr, "%g", x.val.still);
88 return;
89 case VQ_DELTA:
90 fprintf(stderr, "{%g%s", x.val.delta.quantity, x.val.delta.touched ? " " : "* ");
91 vq_showRegion(x.val.delta.region);
92 fprintf(stderr, "}\n");
93 return;
94
95 default:;
96 }
97 }
98 caryll_ShowFns(vq_Segment, showVQS);
99 caryll_ElementInterfaceOf(vq_Segment) vq_iSegment = {
100 caryll_standardValTypeMethods(vq_Segment),
101 caryll_OrdEqAssigns(vq_Segment), // Ord and Eq
102 caryll_ShowAssigns(vq_Segment), // Show
103 // creating instances
104 .createStill = vqsCreateStill,
105 .createDelta = vqsCreateDelta,
106 };
107
108 caryll_standardVectorImpl(vq_SegList, vq_Segment, vq_iSegment, vq_iSegList);
109
110 // Monoid
111
112 static INLINE void vqInit(VQ *a) {
113 a->kernel = 0;
114 vq_iSegList.init(&a->shift);
115 }
116 static INLINE void vqCopy(VQ *a, const VQ *b) {
117 a->kernel = b->kernel;
118 vq_iSegList.copy(&a->shift, &b->shift);
119 }
120 static INLINE void vqDispose(VQ *a) {
121 a->kernel = 0;
122 vq_iSegList.dispose(&a->shift);
123 }
124
125 caryll_standardValTypeFn(VQ, vqInit, vqCopy, vqDispose);
126 static VQ vqNeutral() {
127 return iVQ.createStill(0);
128 }
129 static bool vqsCompatible(const vq_Segment a, const vq_Segment b) {
130 if (a.type != b.type) return false;
131 switch (a.type) {
132 case VQ_STILL:
133 return true;
134 case VQ_DELTA:
135 return 0 == vq_compareRegion(a.val.delta.region, b.val.delta.region);
136 default:
137 fprintf(stderr,"! warning: vqsCompatible unknown a.type %u. Return false.\n",a.type);
138 return false;
139 }
140 }
141 static void simplifyVq(MODIFY VQ *x) {
142 if (!x->shift.length) return;
143 vq_iSegList.sort(&x->shift, vq_iSegment.compareRef);
144 size_t k = 0;
145 for (size_t j = 1; j < x->shift.length; j++) {
146 if (vqsCompatible(x->shift.items[k], x->shift.items[j])) {
147 switch (x->shift.items[k].type) {
148 case VQ_STILL:
149 x->shift.items[k].val.still += x->shift.items[j].val.still;
150 break;
151 case VQ_DELTA:
152 x->shift.items[k].val.delta.quantity += x->shift.items[j].val.delta.quantity;
153 break;
154 }
155 vq_iSegment.dispose(&x->shift.items[j]);
156 } else {
157 x->shift.items[k] = x->shift.items[j];
158 k++;
159 }
160 }
161 x->shift.length = k + 1;
162 }
163 static void vqInplacePlus(MODIFY VQ *a, const VQ b) {
164 a->kernel += b.kernel;
165 for (size_t p = 0; p < b.shift.length; p++) {
166 vq_Segment *k = &b.shift.items[p];
167 if (k->type == VQ_STILL) {
168 a->kernel += k->val.still;
169 } else {
170 vq_Segment s;
171 vq_iSegment.copy(&s, k);
172 vq_iSegList.push(&a->shift, s);
173 }
174 }
175 simplifyVq(a);
176 }
177
178 caryll_MonoidFns(VQ, vqNeutral, vqInplacePlus);
179
180 // Module
181 static void vqInplaceScale(MODIFY VQ *a, pos_t b) {
182 a->kernel *= b;
183 for (size_t j = 0; j < a->shift.length; j++) {
184 vq_Segment *s = &a->shift.items[j];
185 switch (s->type) {
186 case VQ_STILL:
187 s->val.still *= b;
188 break;
189 case VQ_DELTA:
190 s->val.delta.quantity *= b;
191 break;
192 }
193 }
194 }
195
196 // Group
197 static void vqInplaceNegate(MODIFY VQ *a) {
198 vqInplaceScale(a, -1);
199 }
200
201 caryll_GroupFns(VQ, vqInplaceNegate);
202 caryll_ModuleFns(VQ, pos_t, vqInplaceScale);
203
204 // Ord
205 static int vqCompare(const VQ a, const VQ b) {
206 if (a.shift.length < b.shift.length) return -1;
207 if (a.shift.length > b.shift.length) return 1;
208 for (size_t j = 0; j < a.shift.length; j++) {
209 int cr = vqsCompare(a.shift.items[j], b.shift.items[j]);
210 if (cr) return cr;
211 }
212 return a.kernel - b.kernel;
213 }
214 caryll_OrdEqFns(VQ, vqCompare);
215
216 // Show
217 static void showVQ(const VQ x) {
218 fprintf(stderr, "%g + {", x.kernel);
219 for (size_t j = 0; j < x.shift.length; j++) {
220 if (j) fprintf(stderr, " ");
221 vq_iSegment.show(x.shift.items[j]);
222 }
223 fprintf(stderr, "}\n");
224 }
225 caryll_ShowFns(VQ, showVQ);
226
227 // Still instances
228 static pos_t vqGetStill(const VQ v) {
229 pos_t result = v.kernel;
230 for (size_t j = 0; j < v.shift.length; j++) {
231 switch (v.shift.items[j].type) {
232 case VQ_STILL:
233 result += v.shift.items[j].val.still;
234 default:;
235 }
236 }
237 return result;
238 }
239 static VQ vqCreateStill(pos_t x) {
240 VQ vq;
241 iVQ.init(&vq);
242 vq.kernel = x;
243 return vq;
244 }
245 static bool vqIsStill(const VQ v) {
246 for (size_t j = 0; j < v.shift.length; j++) {
247 switch (v.shift.items[j].type) {
248 case VQ_STILL:
249 break;
250 default:
251 return false;
252 }
253 }
254 return true;
255 }
256 static bool vqIsZero(const VQ v, const pos_t err) {
257 return vqIsStill(v) && fabs(vqGetStill(v)) < err;
258 }
259 static void vqAddDelta(MODIFY VQ *v, const bool touched, const vq_Region *const r,
260 const pos_t quantity) {
261 if (!quantity) return;
262 vq_Segment nudge;
263 nudge.type = VQ_DELTA;
264 nudge.val.delta.region = r;
265 nudge.val.delta.touched = touched;
266 nudge.val.delta.quantity = quantity;
267 vq_iSegList.push(&v->shift, nudge);
268 }
269
270 // pointLinearTfm
271 static VQ vqPointLinearTfm(const VQ ax, pos_t a, const VQ x, pos_t b, const VQ y) {
272 VQ targetX = iVQ.dup(ax);
273 iVQ.inplacePlusScale(&targetX, a, x);
274 iVQ.inplacePlusScale(&targetX, b, y);
275 return targetX;
276 }
277
278 caryll_VectorInterfaceTypeName(VQ) iVQ = {
279 caryll_standardValTypeMethods(VQ),
280 .getStill = vqGetStill,
281 .createStill = vqCreateStill,
282 .isStill = vqIsStill,
283 .isZero = vqIsZero,
284 caryll_MonoidAssigns(VQ), // Monoid
285 caryll_GroupAssigns(VQ), // Group
286 caryll_ModuleAssigns(VQ), // Module
287 caryll_OrdEqAssigns(VQ), // Eq-Ord
288 caryll_ShowAssigns(VQ), // Show
289 .pointLinearTfm = vqPointLinearTfm, // pointLinearTfm
290 .addDelta = vqAddDelta // addDelta
291 };