Skip to content

Commit be034de

Browse files
committed
add .last_index_of and .format_debug ; tag 0.0.13
1 parent abfb96d commit be034de

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
[package]
33
name = "im_ternary_tree"
4-
version = "0.0.12"
4+
version = "0.0.13"
55
edition = "2021"
66
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
77
license = "MIT"

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ where
6262
}
6363
}
6464

65+
/// items in debug display
66+
pub fn format_debug(&self) -> String {
67+
let mut s = String::from("(TernaryTreeList debug");
68+
for x in self.iter() {
69+
s.push_str(&format!(" {:?}", x));
70+
}
71+
s.push(')');
72+
s
73+
}
74+
6575
/// get element in list by reference
6676
/// PERF: recursive function is slower than iterative loop with Cell in bench(using `usize`),
6777
/// however, Calcit is heavy in cloning(reference though... according real practice),
@@ -90,6 +100,14 @@ where
90100
}
91101
}
92102

103+
/// index of element from end, return 0 if found at last
104+
pub fn last_index_of(&self, item: &T) -> Option<usize> {
105+
match self {
106+
Empty => None,
107+
Tree(t) => t.last_index_of(item),
108+
}
109+
}
110+
93111
/// recursively check structure
94112
pub fn eq_shape(&self, ys: &Self) -> bool {
95113
match (self, ys) {

src/tree.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,26 @@ where
180180
}
181181
}
182182

183+
// index from end, returns 0 when item found at end of original list
184+
pub fn last_index_of(&self, item: &T) -> Option<usize> {
185+
match self {
186+
Leaf(value) => {
187+
if item == value {
188+
Some(0)
189+
} else {
190+
None
191+
}
192+
}
193+
Branch2 { left, middle, .. } => middle
194+
.last_index_of(item)
195+
.or_else(|| left.last_index_of(item).map(|pos| pos + middle.len())),
196+
Branch3 { left, middle, right, .. } => right
197+
.last_index_of(item)
198+
.or_else(|| middle.last_index_of(item).map(|pos| pos + right.len()))
199+
.or_else(|| left.last_index_of(item).map(|pos| pos + middle.len() + right.len())),
200+
}
201+
}
202+
183203
/// recursively check structure
184204
pub fn eq_shape(&self, ys: &Self) -> bool {
185205
if self.len() != ys.len() {
@@ -1059,7 +1079,11 @@ where
10591079
T: Clone + Display + Eq + PartialEq + Debug + Ord + PartialOrd + Hash,
10601080
{
10611081
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1062-
write!(f, "TernaryTree[{}, ...]", self.len())
1082+
write!(f, "(TernaryTree")?;
1083+
for item in self.into_iter() {
1084+
write!(f, " {}", item)?;
1085+
}
1086+
write!(f, ")")
10631087
}
10641088
}
10651089

tests/list_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77
fn init_list() -> Result<(), String> {
88
assert_eq!(
99
TernaryTreeList::from(&[1, 2, 3, 4]).to_string(),
10-
String::from("TernaryTree[4, ...]")
10+
String::from("(TernaryTree 1 2 3 4)")
1111
);
1212

1313
let origin11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

0 commit comments

Comments
 (0)