use std::fmt;
use std::error::Error;
use std::mem::MaybeUninit;
use crate::prim::{*, typ::*, mpz::*, mpf::*, gmp::*};
#[repr(C)]
pub struct __mpq_struct {
pub _mp_num: __mpz_struct,
pub _mp_den: __mpz_struct
}
impl SNew for __mpq_struct {
#[inline]
fn new() -> Self {
unsafe {
let num = MaybeUninit::<mpz_s>::uninit();
let den = MaybeUninit::<mpz_s>::uninit();
__mpq_struct {
_mp_num: num.assume_init(),
_mp_den: den.assume_init()
}
}
}
}
impl Drop for __mpq_struct {
fn drop(&mut self) {
}
}
impl AsPtr for __mpq_struct {}
impl AsPtrMut for __mpq_struct {}
impl __mpq_struct {
#[inline]
pub fn clear(&mut self) -> () {
mpq_clear(self)
}
#[inline]
pub fn init() -> Self {
let mut t = mpq_s::new();
mpq_init(&mut t);
t
}
#[inline]
pub fn set(&mut self, q: mpq_r) -> &mut Self {
mpq_set(self, q);
self
}
#[inline]
pub fn set_ui(&mut self, (u, f): (ui_t, ui_t)) -> &mut Self {
mpq_set_ui(self, u, f);
self
}
#[inline]
pub fn set_si(&mut self, (s, f): (si_t, ui_t)) -> &mut Self {
mpq_set_si(self, s, f);
self
}
#[inline]
pub fn set_d(&mut self, d: double_t) -> &mut Self {
mpq_set_d(self, d);
self
}
#[inline]
pub fn set_z(&mut self, a: mpz_r) -> &mut Self {
mpq_set_z(self, a);
self
}
#[inline]
pub fn set_f(&mut self, f: mpf_r) -> &mut Self {
mpq_set_f(self, f);
self
}
#[inline]
pub fn set_num(&mut self, num: mpz_r) -> &mut Self {
mpq_set_num(self, num);
self
}
#[inline]
pub fn set_den(&mut self, den: mpz_r) -> &mut Self {
mpq_set_den(self, den);
self
}
#[inline]
pub fn set_str(&mut self, s: &str, b: int_t) -> &mut Self {
mpq_set_str(self, s, b);
self
}
#[inline]
pub fn fmtstr(&self, b: int_t) -> String {
mpq_get_str(None, b, self).expect("mpq fmtstr")
}
#[inline]
pub fn get_d(&self) -> double_t {
mpq_get_d(self)
}
#[inline]
pub fn get_num(&self) -> mpz_s {
mpz_s::init_set(self.numref())
}
#[inline]
pub fn get_den(&self) -> mpz_s {
mpz_s::init_set(self.denref())
}
#[inline]
pub fn numref(&self) -> mpz_r {
mpq_numref(self)
}
#[inline]
pub fn denref(&self) -> mpz_r {
mpq_denref(self)
}
#[inline]
pub fn swap(&mut self, r: mpq_t) -> &mut Self {
mpq_swap(self, r);
self
}
#[inline]
pub fn cmp(&self, r: mpq_r) -> int_t {
mpq_cmp(self, r)
}
#[inline]
pub fn cmp_z(&self, a: mpz_r) -> int_t {
mpq_cmp_z(self, a)
}
#[inline]
pub fn cmp_ui(&self, (u, f): (ui_t, ui_t)) -> int_t {
mpq_cmp_ui(self, u, f)
}
#[inline]
pub fn cmp_si(&self, (s, f): (si_t, ui_t)) -> int_t {
mpq_cmp_si(self, s, f)
}
#[inline]
pub fn equal(&self, r: mpq_r) -> bool {
mpq_equal(self, r)
}
#[inline]
pub fn sgn(&self) -> int_t {
mpq_sgn(self)
}
#[inline]
pub fn inv(&self) -> Self {
let mut t = mpq_s::init();
mpq_inv(&mut t, self);
t
}
#[inline]
pub fn abs(&self) -> Self {
let mut t = mpq_s::init();
mpq_abs(&mut t, self);
t
}
#[inline]
pub fn neg(&self) -> Self {
let mut t = mpq_s::init();
mpq_neg(&mut t, self);
t
}
#[inline]
pub fn sub(&mut self, r: mpq_r) -> &mut Self {
mpq_sub(self, &mpq_s::from(&*self), r); self
}
#[inline]
pub fn add(&mut self, r: mpq_r) -> &mut Self {
mpq_add(self, &mpq_s::from(&*self), r); self
}
#[inline]
pub fn mul(&mut self, r: mpq_r) -> &mut Self {
mpq_mul(self, &mpq_s::from(&*self), r); self
}
#[inline]
pub fn mul_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
mpq_mul_2exp(self, &mpq_s::from(&*self), n); self
}
#[inline]
pub fn div(&mut self, r: mpq_r) -> &mut Self {
mpq_div(self, &mpq_s::from(&*self), r); self
}
#[inline]
pub fn div_2exp(&mut self, n: mp_bitcnt_t) -> &mut Self {
mpq_div_2exp(self, &mpq_s::from(&*self), n); self
}
#[inline]
pub fn frac(a: mpz_r, b: mpz_r) -> Self {
let mut t = mpq_s::init();
t.set_num(a).set_den(b);
t
}
#[inline]
pub fn reduce(&mut self) -> &mut Self {
let gcd = &self.numref().gcd(self.denref());
let mut num = mpz_s::init();
let mut den = mpz_s::init();
mpz_divexact(&mut num, self.numref(), gcd);
mpz_divexact(&mut den, self.denref(), gcd);
self.set_num(&num);
self.set_den(&den);
self
}
#[inline]
pub fn cmp_u(&self, u: ui_t) -> int_t {
self.cmp_ui((u, 1))
}
#[inline]
pub fn cmp_s(&self, s: si_t) -> int_t {
self.cmp_si((s, 1))
}
}
impl fmt::Debug for __mpq_struct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?}) / ({:?})", self._mp_num, self._mp_den)
}
}
impl fmt::Display for __mpq_struct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.fmtstr(10))
}
}
#[allow(non_camel_case_types)]
pub type mpq_s = __mpq_struct; #[allow(non_camel_case_types)]
pub type mpq_t<'a> = &'a mut mpq_s; #[allow(non_camel_case_types)]
pub type mpq_r<'a> = &'a mpq_s;
pub fn mpq_clears(vq: &mut Vec<mpq_t>) -> () {
vq.iter_mut().for_each(|q| {
unsafe { __gmpq_clear(*q) } })
}
#[inline]
pub fn mpq_clear(q: mpq_t) -> () {
unsafe { __gmpq_clear(q) }
}
pub fn mpq_inits(vq: &mut Vec<mpq_t>) -> () {
vq.iter_mut().for_each(|q| {
unsafe { __gmpq_init(*q) } })
}
#[inline]
pub fn mpq_init(q: mpq_t) -> () {
unsafe { __gmpq_init(q) }
}
#[inline]
pub fn mpq_set(q: mpq_t, r: mpq_r) -> () {
unsafe { __gmpq_set(q, r) }
}
#[inline]
pub fn mpq_set_ui(q: mpq_t, u: ui_t, f: ui_t) -> () {
unsafe { __gmpq_set_ui(q, u, f) }
}
#[inline]
pub fn mpq_set_si(q: mpq_t, s: si_t, f: ui_t) -> () {
unsafe { __gmpq_set_si(q, s, f) }
}
#[inline]
pub fn mpq_set_d(q: mpq_t, d: double_t) -> () {
unsafe { __gmpq_set_d(q, d) }
}
#[inline]
pub fn mpq_set_z(q: mpq_t, a: mpz_r) -> () {
unsafe { __gmpq_set_z(q, a) }
}
#[inline]
pub fn mpq_set_f(q: mpq_t, f: mpf_r) -> () {
unsafe { __gmpq_set_f(q, f) }
}
#[inline]
pub fn mpq_set_num(q: mpq_t, num: mpz_r) -> () {
unsafe { __gmpq_set_num(q, num) }
}
#[inline]
pub fn mpq_set_den(q: mpq_t, den: mpz_r) -> () {
unsafe { __gmpq_set_den(q, den) }
}
#[inline]
pub fn mpq_set_str(q: mpq_t, s: &str, b: int_t) -> () {
mpq_set_str_u8z(q, to_u8z!(s), b)
}
#[inline]
pub fn mpq_set_str_u8z(q: mpq_t, s: &[u8], b: int_t) -> () {
unsafe { __gmpq_set_str(q, s as *const [u8] as *const u8, b) }
}
pub fn mpq_get_u8z<'a>(s: Option<&mut [u8]>, b: int_t, q: &'a mpq_s) ->
Option<Vec<u8>> {
let ff = s == None;
unsafe {
let p = __gmpq_get_str(
match s { None => 0 as *mut u8, Some(s) => s as *mut [u8] as *mut u8 },
b, q);
u8zvec(p, ff)
}
}
pub fn mpq_get_str<'a>(s: Option<&mut String>, b: int_t, q: &'a mpq_s) ->
Result<String, Box<dyn Error>> {
let r = mpq_get_u8z(
match s { None => None, Some(s) => Some(unsafe { s.as_bytes_mut() }) },
b, q);
match r {
None => Err("err mpq get str".into()),
Some(r) => Ok(String::from_utf8(r)?)
}
}
#[inline]
pub fn mpq_get_d(q: mpq_r) -> double_t {
unsafe { __gmpq_get_d(q) }
}
#[inline]
pub fn mpq_get_num(num: mpz_t, q: mpq_r) -> () {
unsafe { __gmpq_get_num(num, q) }
}
#[inline]
pub fn mpq_get_den(den: mpz_t, q: mpq_r) -> () {
unsafe { __gmpq_get_den(den, q) }
}
#[inline]
pub fn mpq_numref(q: mpq_r) -> mpz_r {
&q._mp_num
}
#[inline]
pub fn mpq_denref(q: mpq_r) -> mpz_r {
&q._mp_den
}
#[inline]
pub fn mpq_swap(q: mpq_t, r: mpq_t) -> () {
unsafe { __gmpq_swap(q, r) }
}
#[inline]
pub fn mpq_cmp(q: mpq_r, r: mpq_r) -> int_t {
unsafe { __gmpq_cmp(q, r) }
}
#[inline]
pub fn mpq_cmp_z(q: mpq_r, a: mpz_r) -> int_t {
unsafe { __gmpq_cmp_z(q, a) }
}
#[inline]
pub fn mpq_cmp_ui(q: mpq_r, u: ui_t, f: ui_t) -> int_t {
unsafe { __gmpq_cmp_ui(q, u, f) }
}
#[inline]
pub fn mpq_cmp_si(q: mpq_r, s: si_t, f: ui_t) -> int_t {
unsafe { __gmpq_cmp_si(q, s, f) }
}
#[inline]
pub fn mpq_equal(q: mpq_r, r: mpq_r) -> bool {
unsafe { __gmpq_equal(q, r) != 0 }
}
#[inline]
pub fn mpq_sgn(q: mpq_r) -> int_t {
let t = q._mp_num._mp_size;
if t < 0 { -1 } else { if t > 0 { 1 } else { 0 } }
}
#[inline]
pub fn mpq_inv(p: mpq_t, q: mpq_r) -> () {
unsafe { __gmpq_inv(p, q) }
}
#[inline]
pub fn mpq_abs(p: mpq_t, q: mpq_r) -> () {
unsafe { __gmpq_abs(p, q) }
}
#[inline]
pub fn mpq_neg(p: mpq_t, q: mpq_r) -> () {
unsafe { __gmpq_neg(p, q) }
}
#[inline]
pub fn mpq_sub(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
unsafe { __gmpq_sub(p, q, r) }
}
#[inline]
pub fn mpq_add(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
unsafe { __gmpq_add(p, q, r) }
}
#[inline]
pub fn mpq_mul(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
unsafe { __gmpq_mul(p, q, r) }
}
#[inline]
pub fn mpq_mul_2exp(p: mpq_t, q: mpq_r, n: mp_bitcnt_t) -> () {
unsafe { __gmpq_mul_2exp(p, q, n) }
}
#[inline]
pub fn mpq_div(p: mpq_t, q: mpq_r, r: mpq_r) -> () {
unsafe { __gmpq_div(p, q, r) }
}
#[inline]
pub fn mpq_div_2exp(p: mpq_t, q: mpq_r, n: mp_bitcnt_t) -> () {
unsafe { __gmpq_div_2exp(p, q, n) }
}