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
use std::ops::Deref;
use std::{borrow::Borrow, collections::HashSet};
use lazy_static::lazy_static;
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::Zero;
use crate::{
lexical, Datatype, DecimalDatatype, IntDatatype, IntegerDatatype, LongDatatype,
NonNegativeIntegerDatatype, NonPositiveIntegerDatatype, ShortDatatype, UnsignedIntDatatype,
UnsignedLongDatatype, UnsignedShortDatatype, XsdDatatype,
};
mod integer;
pub use integer::*;
lazy_static! {
static ref I64_MIN: BigInt = i64::MIN.into();
static ref I64_MIN_RATIO: BigRational = I64_MIN.clone().into();
static ref I32_MIN: BigInt = i32::MIN.into();
static ref I32_MIN_RATIO: BigRational = I32_MIN.clone().into();
static ref I16_MIN: BigInt = i16::MIN.into();
static ref I16_MIN_RATIO: BigRational = I16_MIN.clone().into();
static ref I8_MIN: BigInt = i8::MIN.into();
static ref I8_MIN_RATIO: BigRational = I8_MIN.clone().into();
static ref U64_MAX: BigInt = u64::MAX.into();
static ref U64_MAX_RATIO: BigRational = U64_MAX.clone().into();
static ref U32_MAX: BigInt = u32::MAX.into();
static ref U32_MAX_RATIO: BigRational = U32_MAX.clone().into();
static ref U16_MAX: BigInt = u16::MAX.into();
static ref U16_MAX_RATIO: BigRational = U16_MAX.clone().into();
static ref U8_MAX: BigInt = u8::MAX.into();
static ref U8_MAX_RATIO: BigRational = U8_MAX.clone().into();
static ref TEN: BigInt = 10u32.into();
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Decimal(BigRational);
#[derive(Default)]
pub struct DecimalCheck {
set: HashSet<BigInt>,
}
impl DecimalCheck {
pub fn is_decimal(&mut self, r: &BigRational) -> bool {
self.set.clear();
let mut rem = if *r < BigRational::zero() {
-r.numer()
} else {
r.numer().clone()
};
rem %= r.denom();
while !rem.is_zero() && !self.set.contains(&rem) {
self.set.insert(rem.clone());
rem = (rem * TEN.clone()) % r.denom();
}
rem.is_zero()
}
}
#[inline(always)]
pub fn is_decimal(r: &BigRational) -> bool {
let mut c = DecimalCheck::default();
c.is_decimal(r)
}
impl Decimal {
pub fn decimal_type(&self) -> Option<DecimalDatatype> {
if self.0.is_integer() {
if self.0 >= BigRational::zero() {
if self.0 > BigRational::zero() {
if self.0 <= *U8_MAX_RATIO {
Some(UnsignedShortDatatype::UnsignedByte.into())
} else if self.0 <= *U16_MAX_RATIO {
Some(UnsignedIntDatatype::UnsignedShort(None).into())
} else if self.0 <= *U32_MAX_RATIO {
Some(UnsignedLongDatatype::UnsignedInt(None).into())
} else if self.0 <= *U64_MAX_RATIO {
Some(NonNegativeIntegerDatatype::UnsignedLong(None).into())
} else {
Some(NonNegativeIntegerDatatype::PositiveInteger.into())
}
} else {
Some(UnsignedShortDatatype::UnsignedByte.into())
}
} else if self.0 >= *I8_MIN_RATIO {
Some(ShortDatatype::Byte.into())
} else if self.0 >= *I16_MIN_RATIO {
Some(IntDatatype::Short(None).into())
} else if self.0 >= *I32_MIN_RATIO {
Some(LongDatatype::Int(None).into())
} else if self.0 >= *I64_MIN_RATIO {
Some(IntegerDatatype::Long(None).into())
} else {
Some(NonPositiveIntegerDatatype::NegativeInteger.into())
}
} else {
None
}
}
}
impl<'a> From<&'a lexical::Decimal> for Decimal {
fn from(value: &'a lexical::Decimal) -> Self {
let numer: BigInt = value.integer_part().as_str().parse().unwrap();
match value.fractional_part() {
Some(fract) => {
let f = BigRational::new(1.into(), fract.as_str().len().into());
let fract: BigRational = fract.as_str().parse().unwrap();
Self(BigRational::from(numer) + fract * f)
}
None => Self(numer.into()),
}
}
}
impl From<lexical::DecimalBuf> for Decimal {
#[inline(always)]
fn from(value: lexical::DecimalBuf) -> Self {
value.as_decimal().into()
}
}
impl From<Integer> for Decimal {
fn from(value: Integer) -> Self {
let n: BigInt = value.into();
Self(n.into())
}
}
impl AsRef<BigRational> for Decimal {
#[inline(always)]
fn as_ref(&self) -> &BigRational {
&self.0
}
}
impl Borrow<BigRational> for Decimal {
#[inline(always)]
fn borrow(&self) -> &BigRational {
&self.0
}
}
impl Deref for Decimal {
type Target = BigRational;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, thiserror::Error)]
#[error("no decimal representation for rational number {0}")]
pub struct NoDecimalRepresentation(pub BigRational);
impl TryFrom<BigRational> for Decimal {
type Error = NoDecimalRepresentation;
#[inline(always)]
fn try_from(value: BigRational) -> Result<Self, Self::Error> {
if is_decimal(&value) {
Ok(Self(value))
} else {
Err(NoDecimalRepresentation(value))
}
}
}
impl From<Decimal> for BigRational {
#[inline(always)]
fn from(value: Decimal) -> Self {
value.0
}
}
impl XsdDatatype for Decimal {
#[inline(always)]
fn type_(&self) -> Datatype {
self.decimal_type().into()
}
}