Skip to content

Commit

Permalink
Revert "flip the sort order for red black tree iter (#41)" (#44)
Browse files Browse the repository at this point in the history
This reverts commit 4c94d86.
  • Loading branch information
brittcyr authored Aug 29, 2024
1 parent 4c94d86 commit 10252b8
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions lib/src/red_black_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ impl<'a, V: TreeValue> RedBlackTreeReadOnly<'a, V> {
}
}

/// Sorted iterator starting from the max.
/// Sorted iterator starting from the min.
pub fn iter(&self) -> RedBlackTreeReadOnlyIterator<V> {
RedBlackTreeReadOnlyIterator {
tree: self,
index: if self.max_index == NIL {
self.get_max_index()
} else {
self.max_index
},
index: self.get_min_index::<V>(),
}
}
}
Expand Down Expand Up @@ -968,15 +964,12 @@ impl<'a, V: TreeValue> RedBlackTree<'a, V> {
}
}

/// Sorted iterator starting from the max.
// TODO: Go from max to min because that would be cheaper as we already store max
/// Sorted iterator starting from the min.
pub fn iter(&self) -> RedBlackTreeIterator<V> {
RedBlackTreeIterator {
tree: self,
index: if self.max_index == NIL {
self.get_max_index()
} else {
self.max_index
},
index: self.get_min_index::<V>(),
}
}

Expand Down Expand Up @@ -1105,12 +1098,12 @@ impl<'a, V: TreeValue> Iterator for RedBlackTreeIterator<'a, V> {

fn next(&mut self) -> Option<Self::Item> {
let index: DataIndex = self.index;
let predecessor_index: DataIndex = self.tree.get_predecessor_index::<V>(self.index);
let successor_index: DataIndex = self.tree.get_successor_index::<V>(self.index);
if index == NIL {
None
} else {
let result: &RBNode<V> = get_helper::<RBNode<V>>(self.tree.data, index);
self.index = predecessor_index;
self.index = successor_index;
Some((index, result))
}
}
Expand All @@ -1126,12 +1119,12 @@ impl<'a, V: TreeValue> Iterator for RedBlackTreeReadOnlyIterator<'a, V> {

fn next(&mut self) -> Option<Self::Item> {
let index: DataIndex = self.index;
let predecessor_index: DataIndex = self.tree.get_predecessor_index::<V>(self.index);
let successor_index: DataIndex = self.tree.get_successor_index::<V>(self.index);
if index == NIL {
None
} else {
let result: &RBNode<V> = get_helper::<RBNode<V>>(self.tree.data, index);
self.index = predecessor_index;
self.index = successor_index;
Some((index, result))
}
}
Expand Down

0 comments on commit 10252b8

Please sign in to comment.