Skip to content

Commit

Permalink
Merge pull request #21 from frengor/no-invalid
Browse files Browse the repository at this point in the history
Remove invalid `Cc`s and `Cc::new_cyclic`
  • Loading branch information
frengor authored Nov 7, 2023
2 parents 32105c6 + f848f15 commit d3409b7
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 392 deletions.
47 changes: 36 additions & 11 deletions benches/benches/binary_trees_with_parent_pointers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Benchmark adapted from the shredder crate, released under MIT license. Src: https://github.com/Others/shredder/blob/266de5a3775567463ee82febc42eed1c9a8b6197/benches/shredder_benchmark.rs

use std::cell::RefCell;
use std::hint::black_box;

use rust_cc::*;
Expand Down Expand Up @@ -32,7 +33,7 @@ enum TreeNodeWithParent {
right: Cc<TreeNodeWithParent>,
},
Nested {
parent: Cc<TreeNodeWithParent>,
parent: RefCell<Option<Cc<TreeNodeWithParent>>>,
left: Cc<TreeNodeWithParent>,
right: Cc<TreeNodeWithParent>,
},
Expand Down Expand Up @@ -64,22 +65,46 @@ impl TreeNodeWithParent {
return Cc::new(Self::End);
}

Cc::<Self>::new_cyclic(|cc| Self::Root {
left: Self::new_nested(depth - 1, cc.clone()),
right: Self::new_nested(depth - 1, cc.clone()),
})
let root = Cc::new(Self::Root {
left: Self::new_nested(depth - 1),
right: Self::new_nested(depth - 1),
});

if let Self::Root{ left, right } = &*root {
if let Self::Nested { parent, ..} = &**left {
*parent.borrow_mut() = Some(root.clone());
}

if let Self::Nested { parent, ..} = &**right {
*parent.borrow_mut() = Some(root.clone());
}
}

root
}

fn new_nested(depth: usize, parent: Cc<Self>) -> Cc<Self> {
fn new_nested(depth: usize) -> Cc<Self> {
if depth == 0 {
return Cc::new(Self::End);
}

Cc::<Self>::new_cyclic(|cc| Self::Nested {
left: Self::new_nested(depth - 1, cc.clone()),
right: Self::new_nested(depth - 1, cc.clone()),
parent,
})
let cc = Cc::new(Self::Nested {
left: Self::new_nested(depth - 1),
right: Self::new_nested(depth - 1),
parent: RefCell::new(None),
});

if let Self::Nested{ left, right, .. } = &*cc {
if let Self::Nested { parent, ..} = &**left {
*parent.borrow_mut() = Some(cc.clone());
}

if let Self::Nested { parent, ..} = &**right {
*parent.borrow_mut() = Some(cc.clone());
}
}

cc
}

fn check(&self) -> usize {
Expand Down
Loading

0 comments on commit d3409b7

Please sign in to comment.