Skip to content

Commit

Permalink
get back Arc for leaves; tag 0.0.2-a2
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Dec 4, 2021
1 parent c47bcdb commit d635ffd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "im_ternary_tree"
version = "0.0.2-a1"
version = "0.0.2-a2"
edition = "2021"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ where
match self {
Empty => {
if idx == 0 {
Ok(TernaryTreeList::Tree(TernaryTree::Leaf(item)))
Ok(TernaryTreeList::Tree(TernaryTree::Leaf(Arc::new(item))))
} else {
Err(String::from("inserting into empty, but index is not 0"))
}
Expand Down Expand Up @@ -199,7 +199,7 @@ where
}
pub fn prepend(&self, item: T, disable_balancing: bool) -> Self {
match self {
Empty => TernaryTreeList::Tree(TernaryTree::Leaf(item)),
Empty => TernaryTreeList::Tree(TernaryTree::Leaf(Arc::new(item))),
Tree(t) => TernaryTreeList::Tree(t.prepend(item, disable_balancing)),
}
}
Expand All @@ -208,13 +208,13 @@ where
}
pub fn append(&self, item: T, disable_balancing: bool) -> Self {
match self {
Empty => TernaryTreeList::Tree(TernaryTree::Leaf(item)),
Empty => TernaryTreeList::Tree(TernaryTree::Leaf(Arc::new(item))),
Tree(t) => TernaryTreeList::Tree(t.append(item, disable_balancing)),
}
}
pub fn push_right(&self, item: T) -> Self {
match self {
Empty => TernaryTreeList::Tree(TernaryTree::Leaf(item)),
Empty => TernaryTreeList::Tree(TernaryTree::Leaf(Arc::new(item))),
Tree(t) => TernaryTreeList::Tree(t.push_right(item)),
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/slice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::sync::Arc;

use crate::tree::*;
use crate::TernaryTreeList;
Expand All @@ -16,7 +17,7 @@ where
} else {
let mut ys: Vec<TernaryTree<T>> = Vec::with_capacity(xs.len());
for x in &xs {
ys.push(Leaf(x.to_owned()))
ys.push(Leaf(Arc::new(x.to_owned())))
}

TernaryTreeList::Tree(TernaryTree::rebuild_list(xs.len(), 0, &ys))
Expand All @@ -34,7 +35,7 @@ where
} else {
let mut ys: Vec<TernaryTree<T>> = Vec::with_capacity(xs.len());
for x in xs {
ys.push(Leaf(x.to_owned()))
ys.push(Leaf(Arc::new(x.to_owned())))
}

TernaryTreeList::Tree(TernaryTree::rebuild_list(xs.len(), 0, &ys))
Expand All @@ -53,7 +54,7 @@ where
} else {
let mut ys: Vec<TernaryTree<T>> = Vec::with_capacity(xs.len());
for x in xs {
ys.push(Leaf(x.to_owned()))
ys.push(Leaf(Arc::new(x.to_owned())))
}

TernaryTreeList::Tree(TernaryTree::rebuild_list(xs.len(), 0, &ys))
Expand Down
56 changes: 28 additions & 28 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::util::{divide_ternary_sizes, rough_int_pow};

#[derive(Clone, Debug)]
pub enum TernaryTree<T> {
Leaf(T),
Leaf(Arc<T>),
Branch2 {
size: usize,
depth: u16,
Expand Down Expand Up @@ -175,7 +175,7 @@ where
pub fn index_of(&self, item: &T) -> Option<usize> {
match self {
Leaf(value) => {
if item == value {
if item == &**value {
Some(0)
} else {
None
Expand Down Expand Up @@ -283,7 +283,7 @@ where
match tree_parent {
Leaf(value) => {
if idx == 0 {
return Some(value.to_owned());
return Some((*value).to_owned());
} else {
println!("[warning] Cannot get from leaf with index {}", idx);
return None;
Expand Down Expand Up @@ -355,7 +355,7 @@ where
match self {
Leaf { .. } => {
if idx == 0 {
Ok(Leaf(item))
Ok(Leaf(Arc::new(item)))
} else {
Err(format!("Cannot assoc leaf into index {}", idx))
}
Expand Down Expand Up @@ -567,13 +567,13 @@ where
depth: 1,
size: 2,
left: Arc::new(self.to_owned()),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
})
} else {
Ok(Branch2 {
depth: 1,
size: 2,
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: Arc::new(self.to_owned()),
})
}
Expand All @@ -589,13 +589,13 @@ where
size: 2,
depth: 1,
left: left.to_owned(),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
});
} else {
return Ok(Branch2 {
size: 2,
depth: 1,
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: left.to_owned(),
});
}
Expand All @@ -608,7 +608,7 @@ where
size: 3,
depth: 1,
left: left.to_owned(),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
right: middle.to_owned(),
});
}
Expand All @@ -618,7 +618,7 @@ where
depth: 1,
left: left.to_owned(),
middle: middle.to_owned(),
right: Arc::new(Leaf(item)),
right: Arc::new(Leaf(Arc::new(item))),
});
} else {
return Err(String::from("cannot insert after position 2 since only 2 elements here"));
Expand All @@ -627,7 +627,7 @@ where
return Ok(Branch3 {
size: 3,
depth: 1,
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: left.to_owned(),
right: middle.to_owned(),
});
Expand All @@ -636,7 +636,7 @@ where
size: 3,
depth: 1,
left: left.to_owned(),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
right: middle.to_owned(),
});
} else {
Expand All @@ -654,7 +654,7 @@ where
return Ok(Branch3 {
size: *size + 1,
depth: *depth,
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: left.to_owned(),
right: middle.to_owned(),
});
Expand All @@ -666,7 +666,7 @@ where
depth: *depth,
left: left.to_owned(),
middle: middle.to_owned(),
right: Arc::new(Leaf(item)),
right: Arc::new(Leaf(Arc::new(item))),
});
}

Expand Down Expand Up @@ -704,13 +704,13 @@ where
size: 2,
depth: 1,
left: left.to_owned(),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
});
} else {
return Ok(Branch2 {
size: 2,
depth: 1,
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: left.to_owned(),
});
}
Expand All @@ -723,7 +723,7 @@ where
size: 3,
depth: 1,
left: left.to_owned(),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
right: middle.to_owned(),
});
}
Expand All @@ -733,7 +733,7 @@ where
depth: 1,
left: left.to_owned(),
middle: middle.to_owned(),
right: Arc::new(Leaf(item)),
right: Arc::new(Leaf(Arc::new(item))),
});
} else {
return Err(String::from("cannot insert after position 2 since only 2 elements here"));
Expand All @@ -742,7 +742,7 @@ where
return Ok(Branch3 {
size: 3,
depth: 1,
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: left.to_owned(),
right: middle.to_owned(),
});
Expand All @@ -751,7 +751,7 @@ where
size: 3,
depth: 1,
left: left.to_owned(),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
right: middle.to_owned(),
});
} else {
Expand All @@ -769,7 +769,7 @@ where
return Ok(Branch2 {
size: *size + 1,
depth: depth + 1,
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: Arc::new(self.to_owned()),
});
}
Expand All @@ -779,7 +779,7 @@ where
size: *size + 1,
depth: depth + 1,
left: Arc::new(self.to_owned()),
middle: Arc::new(Leaf(item)),
middle: Arc::new(Leaf(Arc::new(item))),
});
}

Expand All @@ -789,15 +789,15 @@ where
depth: depth.to_owned(),
left: left.to_owned(),
middle: middle.to_owned(),
right: Arc::new(Leaf(item)),
right: Arc::new(Leaf(Arc::new(item))),
});
}

if !after && idx == 0 && right.len() == 0 && middle.len() >= right.len() {
return Ok(Branch3 {
size: *size + 1,
depth: depth.to_owned(),
left: Arc::new(Leaf(item)),
left: Arc::new(Leaf(Arc::new(item))),
middle: left.to_owned(),
right: middle.to_owned(),
});
Expand Down Expand Up @@ -874,7 +874,7 @@ where
}
pub fn prepend(&self, item: T, disable_balancing: bool) -> Self {
if self.is_empty() {
return Leaf(item);
return Leaf(Arc::new(item));
}

let mut result = match self.insert(0, item, false) {
Expand All @@ -895,7 +895,7 @@ where
}
pub fn append(&self, item: T, disable_balancing: bool) -> Self {
if self.is_empty() {
return Leaf(item);
return Leaf(Arc::new(item));
}
let mut result = match self.insert(self.len() - 1, item, true) {
Ok(v) => v,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ where

pub fn push_right(&self, item: T) -> Self {
// start with 2 so its left child branch has capability of only 3^1
self.push_right_iter(Leaf(item), FingerMark::Main(2))
self.push_right_iter(Leaf(Arc::new(item)), FingerMark::Main(2))
}

pub fn drop_left(&self) -> Self {
Expand Down Expand Up @@ -1353,7 +1353,7 @@ where
}
pub fn map<V>(&self, f: Arc<dyn Fn(&T) -> V>) -> TernaryTree<V> {
match self {
Leaf(value) => Leaf(f(value)),
Leaf(value) => Leaf(Arc::new(f(value))),
Branch2 { left, middle, size, depth } => Branch2 {
size: *size,
depth: *depth,
Expand Down

0 comments on commit d635ffd

Please sign in to comment.