From 96d1c11731cff24d58477d687c866388172ecb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=97=E9=9C=B2?= <69190413+illusory0x0@users.noreply.github.com> Date: Thu, 9 Jan 2025 16:57:33 +0800 Subject: [PATCH] improve eachi for sorted_set --- sorted_set/set.mbt | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/sorted_set/set.mbt b/sorted_set/set.mbt index 6f8b41a83..578047273 100644 --- a/sorted_set/set.mbt +++ b/sorted_set/set.mbt @@ -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) } ///|