From 0579ac3138169fb039c1d68020e06e2a2e74d33f Mon Sep 17 00:00:00 2001 From: tiye Date: Thu, 10 Mar 2022 12:56:27 +0800 Subject: [PATCH] fix dissoc of singleton to empty; tag 0.0.6 --- Cargo.toml | 2 +- examples/debug.rs | 16 ++++++++++------ src/lib.rs | 8 +++++++- src/tree.rs | 2 +- tests/list_tests.rs | 9 +++++++++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 70f2fae..0104d8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "im_ternary_tree" -version = "0.0.5" +version = "0.0.6" edition = "2021" authors = ["jiyinyiyong "] license = "MIT" diff --git a/examples/debug.rs b/examples/debug.rs index 54e7d80..b3888e7 100644 --- a/examples/debug.rs +++ b/examples/debug.rs @@ -47,12 +47,16 @@ pub fn main() -> Result<(), String> { // } // } - let mut data: Vec = vec![]; - for idx in 0..110 { - data.push(idx); - let tree = TernaryTreeList::from(&data); - println!("{}", tree.format_inline()); - } + // let mut data: Vec = vec![]; + // for idx in 0..110 { + // data.push(idx); + // let tree = TernaryTreeList::from(&data); + // println!("{}", tree.format_inline()); + // } + + let mut data: TernaryTreeList = TernaryTreeList::Empty; + data = data.push_right(0); + let _e = data.dissoc(0)?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 75b049d..a8953f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,7 +129,13 @@ where pub fn dissoc(&self, idx: usize) -> Result { match self { Empty => Err(String::from("calling dissoc from empty")), - Tree(t) => Ok(TernaryTreeList::Tree(t.dissoc(idx)?)), + Tree(t) => { + if idx == 0 && t.len() == 1 { + Ok(TernaryTreeList::Empty) + } else { + Ok(TernaryTreeList::Tree(t.dissoc(idx)?)) + } + } } } pub fn rest(&self) -> Result { diff --git a/src/tree.rs b/src/tree.rs index 96a4211..71d2ab7 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -404,7 +404,7 @@ where return Err(format!("Index too large {} for {}", idx, self.len())); } else if self.len() == 1 { // idx already == 0 - return Err(String::from("Cannot remove from singleton list")); + return Err(String::from("Cannot remove from singleton list to empty")); } match self { diff --git a/tests/list_tests.rs b/tests/list_tests.rs index 35b82ad..02ebe87 100644 --- a/tests/list_tests.rs +++ b/tests/list_tests.rs @@ -360,3 +360,12 @@ fn take_skip() -> Result<(), String> { Ok(()) } + +#[test] +fn dissoc_empty() -> Result<(), String> { + let data = TernaryTreeList::from(&[1]); + + assert_eq!(data.dissoc(0).unwrap(), TernaryTreeList::Empty); + + Ok(()) +}