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,
impl<T> SbtNode<T>where
T: Unsigned,
pub fn to_path(&self) -> SbtPath<T>
Sourcepub fn lca<I, J>(path1: I, path2: J) -> Selfwhere
I: IntoIterator<Item = T>,
J: IntoIterator<Item = T>,
pub fn lca<I, J>(path1: I, path2: J) -> Selfwhere
I: IntoIterator<Item = T>,
J: IntoIterator<Item = T>,
Examples found in repository?
crates/library_checker/src/number_theory/stern_brocot_tree.rs (line 58)
18pub fn stern_brocot_tree(reader: impl Read, writer: impl Write) {
19 prepare_io!(reader, writer);
20 sc!(t);
21 for _ in 0..t {
22 sc!(query: Query);
23 match query {
24 Query::EncodePath { a, b } => {
25 let path = SbtPath::from(URational::new(a, b));
26 let len = if path.path.first() == Some(&0) {
27 path.path.len() - 1
28 } else {
29 path.path.len()
30 };
31 pp!(len, !);
32 for (i, count) in path.into_iter().enumerate() {
33 if count == 0 {
34 continue;
35 }
36 if i % 2 == 0 {
37 pp!(@ns " R ", count, !);
38 } else {
39 pp!(@ns " L ", count, !);
40 }
41 }
42 pp!();
43 }
44 Query::DecodePath { path, .. } => {
45 let node: SbtNode<u32> = if path.first().is_some_and(|t| t.0 == 'L') {
46 [0].into_iter()
47 .chain(path.into_iter().map(|(_, c)| c))
48 .collect()
49 } else {
50 path.into_iter().map(|(_, c)| c).collect()
51 };
52 let val = node.eval();
53 pp!(val.num, val.den);
54 }
55 Query::Lca { a, b, c, d } => {
56 let path1 = SbtPath::from(URational::new(a, b));
57 let path2 = SbtPath::from(URational::new(c, d));
58 let val = SbtNode::lca(path1, path2).eval();
59 pp!(val.num, val.den);
60 }
61 Query::Ancestor { k, a, b } => {
62 let mut path = SbtPath::from(URational::new(a, b));
63 let depth = path.depth();
64 if k <= depth {
65 path.up(depth - k);
66 let val = path.eval();
67 pp!(val.num, val.den);
68 } else {
69 pp!("-1");
70 }
71 }
72 Query::Range { a, b } => {
73 let node = SbtPath::from(URational::new(a, b)).to_node();
74 pp!(node.l.num, node.l.den, node.r.num, node.r.den);
75 }
76 }
77 }
78}Trait Implementations§
impl<T> Copy for SbtNode<T>
impl<T> Eq for SbtNode<T>
Source§impl<T> FromIterator<T> for SbtNode<T>where
T: Unsigned,
impl<T> FromIterator<T> for SbtNode<T>where
T: Unsigned,
Source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
Creates a value from an iterator. Read more
Source§impl<T> SternBrocotTree for SbtNode<T>where
T: Unsigned,
impl<T> SternBrocotTree for SbtNode<T>where
T: Unsigned,
impl<T> StructuralPartialEq for SbtNode<T>
Auto Trait Implementations§
impl<T> Freeze for SbtNode<T>
impl<T> RefUnwindSafe for SbtNode<T>where
URational<T>: RefUnwindSafe,
impl<T> Send for SbtNode<T>
impl<T> Sync for SbtNode<T>
impl<T> Unpin for SbtNode<T>
impl<T> UnsafeUnpin for SbtNode<T>where
URational<T>: UnsafeUnpin,
impl<T> UnwindSafe for SbtNode<T>where
URational<T>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more