Skip to content

Commit

Permalink
improve eachi for sorted_set
Browse files Browse the repository at this point in the history
  • Loading branch information
illusory0x0 authored and bobzhang committed Jan 10, 2025
1 parent 102e8a6 commit 96d1c11
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions sorted_set/set.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -348,29 +348,21 @@ fn each[V](self : Node[V], f : (V) -> Unit) -> Unit {
///|
/// Iterates the set with index.
pub fn eachi[V](self : T[V], f : (Int, V) -> Unit) -> Unit {
match self.root {
None => ()
Some(root) => root.eachi(f)
}
}

///|
fn eachi[V](self : Node[V], f : (Int, V) -> Unit) -> Unit {
let s = []
let mut p = Some(self)
let mut i = 0
while not(p.is_empty()) || not(s.is_empty()) {
while not(p.is_empty()) {
s.push(p)
p = p.unwrap().left
}
if not(s.is_empty()) {
p = s.unsafe_pop()
f(i, p.unwrap().value)
p = p.unwrap().right
i += 1
fn dfs(root : Node[V]?) -> Unit {
match root {
Some(root) => {
dfs(root.left)
f(i, root.value)
i = i + 1
dfs(root.right)
}
None => ()
}
()
}

dfs(self.root)
}

///|
Expand Down

0 comments on commit 96d1c11

Please sign in to comment.