aboutsummaryrefslogtreecommitdiff
path: root/src/numbers/dynnum.rs
blob: eab11c6fddc841a17b272464bb3a5c3a646b9459 (plain)
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
1/*  OpenTally: Open-source election vote counting
2 *  Copyright © 2021 Lee Yingtong Li (RunasSudo)
3 *
4 *  This program is free software: you can redistribute it and/or modify
5 *  it under the terms of the GNU Affero General Public License as published by
6 *  the Free Software Foundation, either version 3 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *  GNU Affero General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Affero General Public License
15 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 */
17
18use super::{Assign, Fixed, GuardedFixed, NativeFloat64, Number, Rational};
19
20use num_traits::{Num, One, Zero};
21//use wasm_bindgen::prelude::wasm_bindgen;
22
23use std::cmp::{Ord, Ordering};
24use std::fmt;
25use std::mem::ManuallyDrop;
26use std::ops::{self, Deref, DerefMut};
27
28/// Represents the underlying implementation for [DynNum]s
29//#[wasm_bindgen]
30pub enum NumKind {
31	/// See [crate::numbers::fixed]
32	Fixed,
33	/// See [crate::numbers::gfixed]
34	GuardedFixed,
35	/// See [crate::numbers::native]
36	NativeFloat64,
37	/// See [crate::numbers::rational_rug] or [crate::numbers::rational_num]
38	Rational,
39}
40
41/// Determines which underlying implementation to use
42static mut KIND: NumKind = NumKind::Fixed;
43
44/// Returns which underlying implementation is in use
45#[inline]
46fn get_kind() -> &'static NumKind {
47	unsafe {
48		return &KIND;
49	}
50}
51
52/// A wrapper for different numeric types using dynamic dispatch
53pub union DynNum {
54	fixed: ManuallyDrop<Fixed>,
55	gfixed: ManuallyDrop<GuardedFixed>,
56	float64: NativeFloat64,
57	rational: ManuallyDrop<Rational>,
58}
59
60impl DynNum {
61	/// Set which underlying implementation to use
62	pub fn set_kind(kind: NumKind) {
63		unsafe {
64			KIND = kind;
65		}
66	}
67}
68
69macro_rules! impl_1arg_nowrap {
70	($self:expr, $arg:expr, $func:ident) => {
71		// Safety: Access only correct union field
72		unsafe {
73			match get_kind() {
74				NumKind::Fixed => {
75					$self.fixed.$func($arg)
76				}
77				NumKind::GuardedFixed => {
78					$self.gfixed.$func($arg)
79				}
80				NumKind::NativeFloat64 => {
81					$self.float64.$func($arg)
82				}
83				NumKind::Rational => {
84					$self.rational.$func($arg)
85				}
86			}
87		}
88	}
89}
90
91macro_rules! impl_assoc_nowrap {
92	($func:ident) => {
93		match get_kind() {
94			NumKind::Fixed => {
95				Fixed::$func()
96			}
97			NumKind::GuardedFixed => {
98				GuardedFixed::$func()
99			}
100			NumKind::NativeFloat64 => {
101				NativeFloat64::$func()
102			}
103			NumKind::Rational => {
104				Rational::$func()
105			}
106		}
107	}
108}
109
110impl Number for DynNum {
111	fn new() -> Self {
112		match get_kind() {
113			NumKind::Fixed => {
114				DynNum { fixed: ManuallyDrop::new(Fixed::new()) }
115			}
116			NumKind::GuardedFixed => {
117				DynNum { gfixed: ManuallyDrop::new(GuardedFixed::new()) }
118			}
119			NumKind::NativeFloat64 => {
120				DynNum { float64: NativeFloat64::new() }
121			}
122			NumKind::Rational => {
123				DynNum { rational: ManuallyDrop::new(Rational::new()) }
124			}
125		}
126	}
127	
128	fn describe() -> String { impl_assoc_nowrap!(describe) }
129	fn pow_assign(&mut self, exponent: i32) { impl_1arg_nowrap!(self, exponent, pow_assign) }
130	fn floor_mut(&mut self, dps: usize) { impl_1arg_nowrap!(self, dps, floor_mut) }
131	fn ceil_mut(&mut self, dps: usize) { impl_1arg_nowrap!(self, dps, ceil_mut) }
132	fn round_mut(&mut self, dps: usize) { impl_1arg_nowrap!(self, dps, round_mut) }
133}
134
135impl Drop for DynNum {
136	fn drop(&mut self) {
137		// Safety: Access only correct union field
138		unsafe {
139			match get_kind() {
140				NumKind::Fixed => {
141					ManuallyDrop::drop(&mut self.fixed);
142				}
143				NumKind::GuardedFixed => {
144					ManuallyDrop::drop(&mut self.gfixed);
145				}
146				NumKind::NativeFloat64 => {}
147				NumKind::Rational => {
148					ManuallyDrop::drop(&mut self.rational);
149				}
150			}
151		}
152	}
153}
154
155macro_rules! impl_0arg_wrap {
156	($self:expr, $func:ident) => {
157		// Safety: Access only correct union field
158		unsafe {
159			match get_kind() {
160				NumKind::Fixed => {
161					DynNum { fixed: ManuallyDrop::new($self.fixed.deref().$func()) }
162				}
163				NumKind::GuardedFixed => {
164					DynNum { gfixed: ManuallyDrop::new($self.gfixed.deref().$func()) }
165				}
166				NumKind::NativeFloat64 => {
167					DynNum { float64: $self.float64.$func() }
168				}
169				NumKind::Rational => {
170					DynNum { rational: ManuallyDrop::new($self.rational.deref().$func()) }
171				}
172			}
173		}
174	}
175}
176
177impl Clone for DynNum {
178	fn clone(&self) -> Self { impl_0arg_wrap!(self, clone) }
179}
180
181impl Num for DynNum {
182	type FromStrRadixErr = Self; // TODO
183	
184	fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
185		match get_kind() {
186			NumKind::Fixed => {
187				Ok(DynNum { fixed: ManuallyDrop::new(Fixed::from_str_radix(str, radix).unwrap()) })
188			}
189			NumKind::GuardedFixed => {
190				Ok(DynNum { gfixed: ManuallyDrop::new(GuardedFixed::from_str_radix(str, radix).unwrap()) })
191			}
192			NumKind::NativeFloat64 => {
193				Ok(DynNum { float64: NativeFloat64::from_str_radix(str, radix).unwrap() })
194			}
195			NumKind::Rational => {
196				Ok(DynNum { rational: ManuallyDrop::new(Rational::from_str_radix(str, radix).unwrap()) })
197			}
198		}
199	}
200}
201
202macro_rules! impl_1other_nowrap {
203	($self:expr, $rhs:expr, $func:ident) => {
204		// Safety: Access only correct union field
205		unsafe {
206			match get_kind() {
207				NumKind::Fixed => {
208					$self.fixed.deref().$func($rhs.fixed.deref())
209				}
210				NumKind::GuardedFixed => {
211					$self.gfixed.deref().$func($rhs.gfixed.deref())
212				}
213				NumKind::NativeFloat64 => {
214					$self.float64.$func(&$rhs.float64)
215				}
216				NumKind::Rational => {
217					$self.rational.deref().$func($rhs.rational.deref())
218				}
219			}
220		}
221	}
222}
223
224macro_rules! impl_1other_nowrap_mut {
225	($self:expr, $rhs:expr, $func:ident) => {
226		// Safety: Access only correct union field
227		unsafe {
228			match get_kind() {
229				NumKind::Fixed => {
230					$self.fixed.deref_mut().$func($rhs.fixed.deref())
231				}
232				NumKind::GuardedFixed => {
233					$self.gfixed.deref_mut().$func($rhs.gfixed.deref())
234				}
235				NumKind::NativeFloat64 => {
236					$self.float64.$func(&$rhs.float64)
237				}
238				NumKind::Rational => {
239					$self.rational.deref_mut().$func($rhs.rational.deref())
240				}
241			}
242		}
243	}
244}
245
246impl Assign for DynNum {
247	fn assign(&mut self, src: Self) { impl_1other_nowrap_mut!(self, src, assign) }
248}
249
250impl Assign<&Self> for DynNum {
251	fn assign(&mut self, src: &Self) { impl_1other_nowrap_mut!(self, src, assign) }
252}
253
254impl From<usize> for DynNum {
255	fn from(n: usize) -> Self {
256		match get_kind() {
257			NumKind::Fixed => {
258				DynNum { fixed: ManuallyDrop::new(Fixed::from(n)) }
259			}
260			NumKind::GuardedFixed => {
261				DynNum { gfixed: ManuallyDrop::new(GuardedFixed::from(n)) }
262			}
263			NumKind::NativeFloat64 => {
264				DynNum { float64: NativeFloat64::from(n) }
265			}
266			NumKind::Rational => {
267				DynNum { rational: ManuallyDrop::new(Rational::from(n)) }
268			}
269		}
270	}
271}
272
273impl fmt::Display for DynNum {
274	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { impl_1arg_nowrap!(self, f, fmt) }
275}
276
277impl fmt::Debug for DynNum {
278	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { impl_1arg_nowrap!(self, f, fmt) }
279}
280
281impl PartialEq for DynNum {
282	fn eq(&self, other: &Self) -> bool { impl_1other_nowrap!(self, other, eq) }
283}
284
285impl Eq for DynNum {}
286
287impl PartialOrd for DynNum {
288	fn partial_cmp(&self, other: &Self) -> Option<Ordering> { impl_1other_nowrap!(self, other, partial_cmp) }
289}
290
291impl Ord for DynNum {
292	fn cmp(&self, other: &Self) -> Ordering { impl_1other_nowrap!(self, other, cmp) }
293}
294
295macro_rules! impl_assoc_wrap {
296	($func:ident) => {
297		match get_kind() {
298			NumKind::Fixed => {
299				DynNum { fixed: ManuallyDrop::new(Fixed::$func()) }
300			}
301			NumKind::GuardedFixed => {
302				DynNum { gfixed: ManuallyDrop::new(GuardedFixed::$func()) }
303				}
304			NumKind::NativeFloat64 => {
305				DynNum { float64: NativeFloat64::$func() }
306			}
307			NumKind::Rational => {
308				DynNum { rational: ManuallyDrop::new(Rational::$func()) }
309			}
310		}
311	}
312}
313
314impl One for DynNum {
315	fn one() -> Self { impl_assoc_wrap!(one) }
316}
317
318macro_rules! impl_0arg_nowrap {
319	($self:expr, $func:ident) => {
320		// Safety: Access only correct union field
321		unsafe {
322			match get_kind() {
323				NumKind::Fixed => {
324					$self.fixed.deref().$func()
325				}
326				NumKind::GuardedFixed => {
327					$self.gfixed.deref().$func()
328				}
329				NumKind::NativeFloat64 => {
330					$self.float64.$func()
331				}
332				NumKind::Rational => {
333					$self.rational.deref().$func()
334				}
335			}
336		}
337	}
338}
339
340impl Zero for DynNum {
341	fn zero() -> Self { impl_assoc_wrap!(zero) }
342	
343	fn is_zero(&self) -> bool { impl_0arg_nowrap!(self, is_zero) }
344}
345
346impl ops::Neg for DynNum {
347	type Output = Self;
348	fn neg(self) -> Self::Output { impl_0arg_wrap!(self, neg) }
349}
350
351macro_rules! impl_1other_wrap {
352	($self:expr, $rhs:expr, $func:ident) => {
353		// Safety: Access only correct union field
354		unsafe {
355			match get_kind() {
356				NumKind::Fixed => {
357					DynNum { fixed: ManuallyDrop::new($self.fixed.deref().$func($rhs.fixed.deref())) }
358				}
359				NumKind::GuardedFixed => {
360					DynNum { gfixed: ManuallyDrop::new($self.gfixed.deref().$func($rhs.gfixed.deref())) }
361				}
362				NumKind::NativeFloat64 => {
363					DynNum { float64: $self.float64.$func($rhs.float64) }
364				}
365				NumKind::Rational => {
366					DynNum { rational: ManuallyDrop::new($self.rational.deref().$func($rhs.rational.deref())) }
367				}
368			}
369		}
370	}
371}
372
373impl ops::Add for DynNum {
374	type Output = Self;
375	fn add(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, add) }
376}
377
378impl ops::Sub for DynNum {
379	type Output = Self;
380	fn sub(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, sub) }
381}
382
383impl ops::Mul for DynNum {
384	type Output = Self;
385	fn mul(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, mul) }
386}
387
388impl ops::Div for DynNum {
389	type Output = Self;
390	fn div(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, div) }
391}
392
393impl ops::Rem for DynNum {
394	type Output = Self;
395	fn rem(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, rem) }
396}
397
398impl ops::Add<&Self> for DynNum {
399	type Output = Self;
400	fn add(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, add) }
401}
402
403impl ops::Sub<&Self> for DynNum {
404	type Output = Self;
405	fn sub(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, sub) }
406}
407
408impl ops::Mul<&Self> for DynNum {
409	type Output = Self;
410	fn mul(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, mul) }
411}
412
413impl ops::Div<&Self> for DynNum {
414	type Output = Self;
415	fn div(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, div) }
416}
417
418impl ops::Rem<&Self> for DynNum {
419	type Output = Self;
420	fn rem(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, rem) }
421}
422
423impl ops::AddAssign for DynNum {
424	fn add_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, add_assign) }
425}
426
427impl ops::SubAssign for DynNum {
428	fn sub_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, sub_assign) }
429}
430
431impl ops::MulAssign for DynNum {
432	fn mul_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, mul_assign) }
433}
434
435impl ops::DivAssign for DynNum {
436	fn div_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, div_assign) }
437}
438
439impl ops::RemAssign for DynNum {
440	fn rem_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, rem_assign) }
441}
442
443impl ops::AddAssign<&Self> for DynNum {
444	fn add_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, add_assign) }
445}
446
447impl ops::SubAssign<&Self> for DynNum {
448	fn sub_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, sub_assign) }
449}
450
451impl ops::MulAssign<&Self> for DynNum {
452	fn mul_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, mul_assign) }
453}
454
455impl ops::DivAssign<&Self> for DynNum {
456	fn div_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, div_assign) }
457}
458
459impl ops::RemAssign<&Self> for DynNum {
460	fn rem_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, rem_assign) }
461}
462
463impl ops::Neg for &DynNum {
464	type Output = DynNum;
465	fn neg(self) -> Self::Output { impl_0arg_wrap!(self, neg) }
466}
467
468impl ops::Add<Self> for &DynNum {
469	type Output = DynNum;
470	fn add(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, add) }
471}
472
473impl ops::Sub<Self> for &DynNum {
474	type Output = DynNum;
475	fn sub(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, sub) }
476}
477
478impl ops::Mul<Self> for &DynNum {
479	type Output = DynNum;
480	fn mul(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, mul) }
481}
482
483impl ops::Div<Self> for &DynNum {
484	type Output = DynNum;
485	fn div(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, div) }
486}
487
488impl ops::Rem<Self> for &DynNum {
489	type Output = DynNum;
490	fn rem(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, rem) }
491}
492
493/*
494impl ops::Add<&&Rational> for &Rational {
495	
496}
497
498impl ops::Sub<&&Rational> for &Rational {
499	
500}
501
502impl ops::Mul<&&Rational> for &Rational {
503	
504}
505
506impl ops::Div<&&Rational> for &Rational {
507	
508}
509
510impl ops::Rem<&&Rational> for &Rational {
511	
512}
513*/
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.