Skip to content

Commit

Permalink
Merge pull request #15 from calcit-lang/docs-ops
Browse files Browse the repository at this point in the history
optimizations on loop_get, drop_left, push_left, drop_right
  • Loading branch information
NoEgAm authored May 31, 2022
2 parents 6d58702 + d29b68b commit 1ba22f2
Show file tree
Hide file tree
Showing 11 changed files with 1,222 additions and 377 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[package]
name = "im_ternary_tree"
version = "0.0.6"
version = "0.0.9"
edition = "2021"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
license = "MIT"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Structrual sharing tree for Calcit
## Persistetnt structrual sharing tree for Calcit

> borrowed ideas from 2-3 tree and finger-tree.
> a variant of 2-3 tree, with enhancements on ternary branching, optimized with tricks like finger-tree.
`t.pop_left()` and `t.push_right(..)` is optimized to be amortized `O(1)` at best cases and `O(log n)` when restructuring involed.

Tree layout from 0 to 159 watch [video](https://www.bilibili.com/video/BV1F34y147V7) or try [live demo](https://github.com/calcit-lang/explain-ternary-tree).

Expand Down
78 changes: 78 additions & 0 deletions benches/creating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ fn criterion_benchmark(c: &mut Criterion) {
})
});

c.bench_function("append to list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.append(idx)
}
})
});

c.bench_function("push_right list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;
Expand All @@ -33,6 +43,26 @@ fn criterion_benchmark(c: &mut Criterion) {
})
});

c.bench_function("unshift to list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.unshift(idx)
}
})
});

c.bench_function("push_left list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push_left(idx)
}
})
});

c.bench_function("inserting in middle", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;
Expand Down Expand Up @@ -93,6 +123,54 @@ fn criterion_benchmark(c: &mut Criterion) {
})
});

c.bench_function("drop-right", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_right()
}
})
});

c.bench_function("drop-left-shallow", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_left_shallow()
}
})
});

c.bench_function("drop-right-shallow", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_right_shallow()
}
})
});

c.bench_function("drop-left from push_right", |b| {
let mut data = TernaryTreeList::Empty;

Expand Down
18 changes: 9 additions & 9 deletions examples/drop_left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ pub fn main() -> Result<(), String> {
println!("{}", data4.format_inline());
}

let mut data = TernaryTreeList::Empty;
// let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx)
}
// for idx in 0..1000 {
// data = data.push(idx)
// }

let mut d = data;
// let mut d = data;

while d.len() > 1 {
d = d.drop_left();
println!("{}", d.format_inline());
}
// while d.len() > 1 {
// d = d.drop_left();
// println!("{}", d.format_inline());
// }

Ok(())
}
41 changes: 41 additions & 0 deletions examples/drop_right.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extern crate im_ternary_tree;
use im_ternary_tree::TernaryTreeList;

pub fn main() -> Result<(), String> {
let mut tree: TernaryTreeList<usize> = TernaryTreeList::Empty;

for idx in 0..60 {
tree = tree.push_right(idx);
}

for _ in 0..59 {
tree = tree.drop_right();
println!("{}", tree.format_inline());
}

let mut origin4: Vec<usize> = vec![];
for idx in 0..60 {
origin4.push(idx);
}
let mut data4 = TernaryTreeList::from(&origin4);

for _ in 0..59 {
data4 = data4.drop_right();
println!("{}", data4.format_inline());
}

// let mut data = TernaryTreeList::Empty;

// for idx in 0..1000 {
// data = data.push(idx)
// }

// let mut d = data;

// while d.len() > 1 {
// d = d.drop_right();
// println!("{}", d.format_inline());
// }

Ok(())
}
2 changes: 1 addition & 1 deletion examples/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn main() -> Result<(), String> {

for idx in 0..80 {
// println!();
tree = tree.push_right(idx);
tree = tree.push_left(idx);
println!("{}", tree.format_inline());
}

Expand Down
Loading

0 comments on commit 1ba22f2

Please sign in to comment.