Struct SbtNode

Source
pub struct SbtNode<T>
where T: Unsigned,
{ pub l: URational<T>, pub r: URational<T>, }

Fields§

§l: URational<T>§r: URational<T>

Implementations§

Source§

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

Source

pub fn to_path(&self) -> SbtPath<T>

Source

pub fn lca<I, J>(path1: I, path2: J) -> Self
where I: IntoIterator<Item = T>, J: IntoIterator<Item = T>,

Examples found in repository?
crates/library_checker/src/math/stern_brocot_tree.rs (line 53)
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}

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> SbtNode<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 SbtNode<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> 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> FromIterator<T> for SbtNode<T>
where T: Unsigned,

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<T> PartialEq for SbtNode<T>
where T: Unsigned + PartialEq,

Source§

fn eq(&self, other: &SbtNode<T>) -> 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> SternBrocotTree for SbtNode<T>
where T: Unsigned,

Source§

type T = T

Source§

fn root() -> Self

Source§

fn is_root(&self) -> bool

Source§

fn eval(&self) -> URational<Self::T>

Source§

fn down_left(&mut self, count: Self::T)

Source§

fn down_right(&mut self, count: Self::T)

Source§

fn up(&mut self, count: Self::T) -> Self::T

Returns the remaining count after moving up.
Source§

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

Source§

impl<T> Eq for SbtNode<T>
where T: Unsigned + Eq,

Source§

impl<T> StructuralPartialEq for SbtNode<T>
where T: Unsigned,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for SbtNode<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> 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> 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.