Skip to content

Commit 63b45a5

Browse files
author
zhangzhuang08
committed
improve Iterator implementation for skip list
1 parent 00351d0 commit 63b45a5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/data_structures/skip_list.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<'a, K: Ord, V> Iter<'a, K, V> {
264264
}
265265

266266
impl<'a, K: Ord, V> Iterator for Iter<'a, K, V> {
267-
type Item = &'a V;
267+
type Item = (&'a K, &'a V);
268268

269269
fn next(&mut self) -> Option<Self::Item> {
270270
unsafe {
@@ -275,13 +275,18 @@ impl<'a, K: Ord, V> Iterator for Iter<'a, K, V> {
275275
return None;
276276
}
277277
self.current_node = forward_0;
278-
return (*forward_0).value.as_ref();
278+
return match ((*forward_0).key.as_ref(), (*forward_0).value.as_ref()) {
279+
(Some(key), Some(value)) => Some((key, value)),
280+
_ => None,
281+
};
279282
}
280283
}
281284
}
282285

283286
#[cfg(test)]
284287
mod test {
288+
use std::collections::HashMap;
289+
285290
#[test]
286291
fn insert_and_delete() {
287292
let mut skip_list = super::SkipList::<&'static str, i32>::new(8);
@@ -310,8 +315,8 @@ mod test {
310315
skip_list.insert("a", 12);
311316
skip_list.insert("c", 11);
312317

313-
let result: Vec<&i32> = skip_list.iter().collect();
314-
assert_eq!(result, vec![&12, &11, &22]);
318+
let result: Vec<(&&'static str, &i32)> = skip_list.iter().collect();
319+
assert_eq!(result, vec![(&"a", &12), (&"c", &11), (&"h", &22)]);
315320
}
316321

317322
#[test]

0 commit comments

Comments
 (0)