2021-05-29 03:01:07 +10:00
|
|
|
/* OpenTally: Open-source election vote counting
|
|
|
|
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Fixed-point arithmetic (using `ibig`)
|
2021-06-04 22:05:48 +10:00
|
|
|
mod fixed;
|
2021-06-14 21:43:43 +10:00
|
|
|
/// Guarded fixed-point arithmetic (using `ibig`)
|
|
|
|
mod gfixed;
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Native 64-bit floating point arithmetic
|
2021-05-29 03:01:07 +10:00
|
|
|
mod native;
|
2021-05-30 18:28:39 +10:00
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Exact rational arithmetic (using `rug`/GMP)
|
2021-05-30 18:28:39 +10:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-05-30 23:00:28 +10:00
|
|
|
mod rational_rug;
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Exact rational arithmetic (using `num-bigint`)
|
2021-05-30 23:00:28 +10:00
|
|
|
//#[cfg(target_arch = "wasm32")]
|
|
|
|
mod rational_num;
|
2021-05-29 03:01:07 +10:00
|
|
|
|
|
|
|
use num_traits::{NumAssignRef, NumRef};
|
|
|
|
|
2021-09-05 00:04:09 +10:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
use rkyv::{Archive, Archived, Deserialize, Fallible, Resolver, Serialize, with::{ArchiveWith, DeserializeWith, SerializeWith}};
|
|
|
|
|
2021-05-30 02:28:52 +10:00
|
|
|
use std::cmp::Ord;
|
2021-05-29 03:01:07 +10:00
|
|
|
use std::fmt;
|
|
|
|
use std::ops;
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Assign value, avoiding additional allocations
|
2021-05-30 18:28:39 +10:00
|
|
|
pub trait Assign<Src=Self> {
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Set the value of `self` to the value of `src`, avoiding additional allocations
|
2021-05-30 18:28:39 +10:00
|
|
|
fn assign(&mut self, src: Src);
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Trait for OpenTally numeric representations
|
2021-05-29 03:01:07 +10:00
|
|
|
//pub trait Number: NumRef + NumAssignRef + PartialOrd + Assign + Clone + fmt::Display where for<'a> &'a Self: RefNum<&'a Self> {
|
2021-06-01 21:20:38 +10:00
|
|
|
pub trait Number:
|
2021-06-29 15:31:38 +10:00
|
|
|
NumRef + NumAssignRef + ops::Neg<Output=Self> + Ord + Assign + From<usize> + Clone + fmt::Debug + fmt::Display
|
2021-06-01 21:20:38 +10:00
|
|
|
where
|
2021-08-03 18:38:45 +10:00
|
|
|
for<'a> Self: Assign<&'a Self>,
|
2021-06-01 21:20:38 +10:00
|
|
|
{
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Return a new [Number]
|
2021-05-29 03:01:07 +10:00
|
|
|
fn new() -> Self;
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Convert to CLI argument representation
|
2021-06-03 21:35:25 +10:00
|
|
|
fn describe() -> String;
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Convert to CLI argument representation, returning an empty string if the default
|
2021-06-12 16:03:31 +10:00
|
|
|
fn describe_opt() -> String { Self::describe() }
|
2021-06-03 21:35:25 +10:00
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Exponentiate `self` to the `exponent` power
|
2021-06-01 21:20:38 +10:00
|
|
|
fn pow_assign(&mut self, exponent: i32);
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Round `self` down if necessary to `dps` decimal places
|
2021-05-29 17:51:45 +10:00
|
|
|
fn floor_mut(&mut self, dps: usize);
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Round `self` up if necessary to `dps` decimal places
|
2021-06-02 18:07:05 +10:00
|
|
|
fn ceil_mut(&mut self, dps: usize);
|
2021-08-03 18:38:45 +10:00
|
|
|
/// Round `self` half-up to the nearest `dps` decimal places
|
|
|
|
fn round_mut(&mut self, dps: usize);
|
2021-05-29 03:01:07 +10:00
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Parse the given string into a [Number]
|
2021-05-29 03:01:07 +10:00
|
|
|
fn parse(s: &str) -> Self {
|
|
|
|
if let Ok(value) = Self::from_str_radix(s, 10) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
panic!("Syntax Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 00:04:09 +10:00
|
|
|
/// rkyv-serialized representation of [Number]
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub struct SerializedNumber;
|
|
|
|
|
|
|
|
/// rkyv-serialized representation of [Option<Number>]
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub struct SerializedOptionNumber;
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
impl<N: Number> ArchiveWith<N> for SerializedNumber {
|
|
|
|
type Archived = Archived<String>;
|
|
|
|
type Resolver = Resolver<String>;
|
|
|
|
|
|
|
|
unsafe fn resolve_with(field: &N, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
|
|
|
|
field.to_string().resolve(pos, resolver, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
impl<N: Number> ArchiveWith<Option<N>> for SerializedOptionNumber {
|
|
|
|
type Archived = Archived<String>;
|
|
|
|
type Resolver = Resolver<String>;
|
|
|
|
|
|
|
|
unsafe fn resolve_with(field: &Option<N>, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
|
|
|
|
match field {
|
|
|
|
Some(n) => {
|
|
|
|
n.to_string().resolve(pos, resolver, out);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
String::new().resolve(pos, resolver, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
impl<N: Number, S: Fallible + ?Sized> SerializeWith<N, S> for SerializedNumber where String: Serialize<S> {
|
|
|
|
fn serialize_with(field: &N, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
|
|
|
|
return field.to_string().serialize(serializer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
impl<N: Number, S: Fallible + ?Sized> SerializeWith<Option<N>, S> for SerializedOptionNumber where String: Serialize<S> {
|
|
|
|
fn serialize_with(field: &Option<N>, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
|
|
|
|
match field {
|
|
|
|
Some(n) => {
|
|
|
|
return n.to_string().serialize(serializer);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return String::new().serialize(serializer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
impl<N: Number, D: Fallible + ?Sized> DeserializeWith<Archived<String>, N, D> for SerializedNumber where Archived<String>: Deserialize<String, D> {
|
|
|
|
fn deserialize_with(field: &Archived<String>, deserializer: &mut D) -> Result<N, D::Error> {
|
|
|
|
return Ok(N::parse(&field.deserialize(deserializer)?));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
impl<N: Number, D: Fallible + ?Sized> DeserializeWith<Archived<String>, Option<N>, D> for SerializedOptionNumber where Archived<String>: Deserialize<String, D> {
|
|
|
|
fn deserialize_with(field: &Archived<String>, deserializer: &mut D) -> Result<Option<N>, D::Error> {
|
|
|
|
let s = field.deserialize(deserializer)?;
|
|
|
|
if s.len() == 0 {
|
|
|
|
return Ok(None);
|
|
|
|
} else {
|
|
|
|
return Ok(Some(N::parse(&s)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:05:48 +10:00
|
|
|
pub use self::fixed::Fixed;
|
2021-06-14 21:43:43 +10:00
|
|
|
pub use self::gfixed::GuardedFixed;
|
2021-05-29 17:51:45 +10:00
|
|
|
pub use self::native::NativeFloat64;
|
2021-05-30 18:28:39 +10:00
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-05-30 23:00:28 +10:00
|
|
|
pub use self::rational_rug::Rational;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub use self::rational_num::Rational;
|