Struct URational

Source
pub struct URational<T>
where T: Unsigned,
{ pub num: T, pub den: T, }

Fields§

§num: T§den: T

Implementations§

Source§

impl<T> URational<T>
where T: Unsigned,

Source

pub fn new(num: T, den: T) -> Self

Examples found in repository?
crates/competitive/src/algorithm/stern_brocot_tree.rs (line 145)
143    fn root() -> Self {
144        Self {
145            l: URational::new(T::zero(), T::one()),
146            r: URational::new(T::one(), T::zero()),
147        }
148    }
More examples
Hide additional examples
crates/competitive/src/num/urational.rs (line 78)
74    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
75    where
76        U: Unsigned,
77    {
78        URational::new(f(self.num), f(self.den))
79    }
80    pub fn map_unchecked<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
81    where
82        U: Unsigned,
83    {
84        URational::new_unchecked(f(self.num), f(self.den))
85    }
86    pub fn map_eval<U>(self, mut f: impl FnMut(T) -> U) -> <U as Div>::Output
87    where
88        U: Div,
89    {
90        f(self.num) / f(self.den)
91    }
92}
93
94impl<T> Bounded for URational<T>
95where
96    T: Unsigned,
97{
98    fn maximum() -> Self {
99        Self::new_unchecked(T::one(), T::zero())
100    }
101    fn minimum() -> Self {
102        Self::zero()
103    }
104}
105
106impl<T> Zero for URational<T>
107where
108    T: Unsigned,
109{
110    fn zero() -> Self {
111        Self::new_unchecked(T::zero(), T::one())
112    }
113}
114impl<T> One for URational<T>
115where
116    T: Unsigned,
117{
118    fn one() -> Self {
119        Self::new_unchecked(T::one(), T::one())
120    }
121}
122
123impl<T> Add for URational<T>
124where
125    T: Unsigned,
126{
127    type Output = Self;
128    fn add(self, rhs: Self) -> Self::Output {
129        Self::new(self.num * rhs.den + self.den * rhs.num, self.den * rhs.den)
130    }
131}
132impl<T> Sub for URational<T>
133where
134    T: Unsigned,
135{
136    type Output = Self;
137    fn sub(self, rhs: Self) -> Self::Output {
138        Self::new(self.num * rhs.den - self.den * rhs.num, self.den * rhs.den)
139    }
140}
141impl<T> Mul for URational<T>
142where
143    T: Unsigned,
144{
145    type Output = Self;
146    fn mul(self, rhs: Self) -> Self::Output {
147        Self::new(self.num * rhs.num, self.den * rhs.den)
148    }
149}
150impl<T> Div for URational<T>
151where
152    T: Unsigned,
153{
154    type Output = Self;
155    fn div(self, rhs: Self) -> Self::Output {
156        Self::new(self.num * rhs.den, self.den * rhs.num)
157    }
crates/library_checker/src/math/stern_brocot_tree.rs (line 18)
9pub fn stern_brocot_tree(reader: impl Read, mut writer: impl Write) {
10    let s = read_all_unchecked(reader);
11    let mut scanner = Scanner::new(&s);
12    scan!(scanner, t);
13    for _ in 0..t {
14        scan!(scanner, type_: String);
15        match type_.as_str() {
16            "ENCODE_PATH" => {
17                scan!(scanner, a: u32, b: u32);
18                let path = SbtPath::from(URational::new(a, b));
19                let len = if path.path.first() == Some(&0) {
20                    path.path.len() - 1
21                } else {
22                    path.path.len()
23                };
24                write!(writer, "{}", len).ok();
25                for (i, count) in path.into_iter().enumerate() {
26                    if count == 0 {
27                        continue;
28                    }
29                    if i % 2 == 0 {
30                        write!(writer, " R {}", count).ok();
31                    } else {
32                        write!(writer, " L {}", count).ok();
33                    }
34                }
35                writeln!(writer).ok();
36            }
37            "DECODE_PATH" => {
38                scan!(scanner, k, path: [(char, u32); k]);
39                let node: SbtNode<u32> = if path.first().is_some_and(|t| t.0 == 'L') {
40                    [0].into_iter()
41                        .chain(path.into_iter().map(|(_, c)| c))
42                        .collect()
43                } else {
44                    path.into_iter().map(|(_, c)| c).collect()
45                };
46                let val = node.eval();
47                writeln!(writer, "{} {}", val.num, val.den).ok();
48            }
49            "LCA" => {
50                scan!(scanner, [a, b, c, d]: [u32; const 4]);
51                let path1 = SbtPath::from(URational::new(a, b));
52                let path2 = SbtPath::from(URational::new(c, d));
53                let val = SbtNode::lca(path1, path2).eval();
54                writeln!(writer, "{} {}", val.num, val.den).ok();
55            }
56            "ANCESTOR" => {
57                scan!(scanner, [k, a, b]: [u32; const 3]);
58                let mut path = SbtPath::from(URational::new(a, b));
59                let depth = path.depth();
60                if k <= depth {
61                    path.up(depth - k);
62                    let val = path.eval();
63                    writeln!(writer, "{} {}", val.num, val.den).ok();
64                } else {
65                    writeln!(writer, "-1").ok();
66                }
67            }
68            "RANGE" => {
69                scan!(scanner, [a, b]: [u32; const 2]);
70                let node = SbtPath::from(URational::new(a, b)).to_node();
71                writeln!(
72                    writer,
73                    "{} {} {} {}",
74                    node.l.num, node.l.den, node.r.num, node.r.den
75                )
76                .ok();
77            }
78            _ => unreachable!(),
79        }
80    }
81}
Source

pub fn new_unchecked(num: T, den: T) -> Self

Examples found in repository?
crates/competitive/src/num/urational.rs (line 66)
64    pub fn new(num: T, den: T) -> Self {
65        let g = num.gcd(den);
66        Self::new_unchecked(num / g, den / g)
67    }
68    pub fn new_unchecked(num: T, den: T) -> Self {
69        Self { num, den }
70    }
71    pub fn eval(self) -> T {
72        self.num / self.den
73    }
74    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
75    where
76        U: Unsigned,
77    {
78        URational::new(f(self.num), f(self.den))
79    }
80    pub fn map_unchecked<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
81    where
82        U: Unsigned,
83    {
84        URational::new_unchecked(f(self.num), f(self.den))
85    }
86    pub fn map_eval<U>(self, mut f: impl FnMut(T) -> U) -> <U as Div>::Output
87    where
88        U: Div,
89    {
90        f(self.num) / f(self.den)
91    }
92}
93
94impl<T> Bounded for URational<T>
95where
96    T: Unsigned,
97{
98    fn maximum() -> Self {
99        Self::new_unchecked(T::one(), T::zero())
100    }
101    fn minimum() -> Self {
102        Self::zero()
103    }
104}
105
106impl<T> Zero for URational<T>
107where
108    T: Unsigned,
109{
110    fn zero() -> Self {
111        Self::new_unchecked(T::zero(), T::one())
112    }
113}
114impl<T> One for URational<T>
115where
116    T: Unsigned,
117{
118    fn one() -> Self {
119        Self::new_unchecked(T::one(), T::one())
120    }
More examples
Hide additional examples
crates/competitive/src/algorithm/stern_brocot_tree.rs (line 155)
154    fn eval(&self) -> URational<Self::T> {
155        URational::new_unchecked(self.l.num + self.r.num, self.l.den + self.r.den)
156    }
Source

pub fn eval(self) -> T

Source

pub fn map<U>(self, f: impl FnMut(T) -> U) -> URational<U>
where U: Unsigned,

Source

pub fn map_unchecked<U>(self, f: impl FnMut(T) -> U) -> URational<U>
where U: Unsigned,

Source

pub fn map_eval<U>(self, f: impl FnMut(T) -> U) -> <U as Div>::Output
where U: Div,

Trait Implementations§

Source§

impl<T> Add for URational<T>
where T: Unsigned,

Source§

type Output = URational<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> AddAssign for URational<T>
where T: Unsigned,

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<T> Bounded for URational<T>
where T: Unsigned,

Source§

fn maximum() -> Self

Source§

fn minimum() -> Self

Source§

fn is_maximum(&self) -> bool

Source§

fn is_minimum(&self) -> bool

Source§

fn set_maximum(&mut self)

Source§

fn set_minimum(&mut self)

Source§

impl<T> Clone for URational<T>
where T: Unsigned + Clone,

Source§

fn clone(&self) -> URational<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for URational<T>
where T: Unsigned + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Div for URational<T>
where T: Unsigned,

Source§

type Output = URational<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> DivAssign for URational<T>
where T: Unsigned,

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl<T> From<URational<T>> for SbtNode<T>
where T: Unsigned,

Source§

fn from(r: URational<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<URational<T>> for SbtPath<T>
where T: Unsigned,

Source§

fn from(r: URational<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> Mul for URational<T>
where T: Unsigned,

Source§

type Output = URational<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> MulAssign for URational<T>
where T: Unsigned,

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<T> One for URational<T>
where T: Unsigned,

Source§

fn one() -> Self

Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Source§

fn set_one(&mut self)

Source§

impl<T> Ord for URational<T>
where T: Unsigned,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq for URational<T>
where T: Unsigned,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> PartialOrd for URational<T>
where T: Unsigned,

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Sub for URational<T>
where T: Unsigned,

Source§

type Output = URational<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> SubAssign for URational<T>
where T: Unsigned,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<T> Zero for URational<T>
where T: Unsigned,

Source§

fn zero() -> Self

Source§

fn is_zero(&self) -> bool
where Self: PartialEq,

Source§

fn set_zero(&mut self)

Source§

impl<T> Copy for URational<T>
where T: Unsigned + Copy,

Source§

impl<T> Eq for URational<T>
where T: Unsigned,

Auto Trait Implementations§

§

impl<T> Freeze for URational<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for URational<T>
where T: RefUnwindSafe,

§

impl<T> Send for URational<T>
where T: Send,

§

impl<T> Sync for URational<T>
where T: Sync,

§

impl<T> Unpin for URational<T>
where T: Unpin,

§

impl<T> UnwindSafe for URational<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsTotalOrd for T
where T: PartialOrd,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PartialOrdExt for T
where T: PartialOrd,

Source§

fn chmin(&mut self, other: T)

Source§

fn chmax(&mut self, other: T)

Source§

fn minmax(self, other: T) -> (T, T)

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.