1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use super::node::Node;
use super::tree::SGTree;
use super::types::IdxVec;

// TODO: add pre-order and post-order iterators

// Immutable Reference iterator ----------------------------------------------------------------------------------------

/// Uses iterative in-order tree traversal algorithm.
/// Maintains a small stack of arena indexes (won't contain all indexes simultaneously for a balanced tree).
pub struct Iter<'a, K: Ord, V> {
    bst: &'a SGTree<K, V>,
    idx_stack: IdxVec,
}

impl<'a, K: Ord, V> Iter<'a, K, V> {
    pub fn new(bst: &'a SGTree<K, V>) -> Self {
        let mut ordered_iter = Iter {
            bst,
            idx_stack: IdxVec::new(),
        };

        if let Some(root_idx) = ordered_iter.bst.root_idx {
            let mut curr_idx = root_idx;
            loop {
                let node = ordered_iter.bst.arena.hard_get(curr_idx);
                match node.left_idx {
                    Some(lt_idx) => {
                        ordered_iter.idx_stack.push(curr_idx);
                        curr_idx = lt_idx;
                    }
                    None => {
                        ordered_iter.idx_stack.push(curr_idx);
                        break;
                    }
                }
            }
        }

        ordered_iter
    }
}

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

    fn next(&mut self) -> Option<Self::Item> {
        match self.idx_stack.pop() {
            Some(pop_idx) => {
                let node = self.bst.arena.hard_get(pop_idx);
                if let Some(gt_idx) = node.right_idx {
                    let mut curr_idx = gt_idx;
                    loop {
                        let node = self.bst.arena.hard_get(curr_idx);
                        match node.left_idx {
                            Some(lt_idx) => {
                                self.idx_stack.push(curr_idx);
                                curr_idx = lt_idx;
                            }
                            None => {
                                self.idx_stack.push(curr_idx);
                                break;
                            }
                        }
                    }
                }

                let node = self.bst.arena.hard_get(pop_idx);
                Some((&node.key, &node.val))
            }
            None => None,
        }
    }
}

// Mutable Reference iterator ----------------------------------------------------------------------------------------

pub struct IterMut<'a, K: Ord, V> {
    arena_iter_mut: core::slice::IterMut<'a, Option<Node<K, V>>>,
}

impl<'a, K: Ord, V> IterMut<'a, K, V> {
    pub fn new(bst: &'a mut SGTree<K, V>) -> Self {
        bst.sort_arena();
        IterMut {
            arena_iter_mut: bst.arena.iter_mut(),
        }
    }
}

impl<'a, K: Ord, V> Iterator for IterMut<'a, K, V> {
    type Item = (&'a K, &'a mut V);

    fn next(&mut self) -> Option<Self::Item> {
        match self.arena_iter_mut.next() {
            Some(Some(node)) => Some((&node.key, &mut node.val)),
            _ => None,
        }
    }
}

// Consuming iterator --------------------------------------------------------------------------------------------------

/// Cheats a little by using internal flattening logic to sort, instead of re-implementing proper traversal.
/// Maintains a shrinking list of arena indexes, initialized with all of them.
pub struct ConsumingIter<K: Ord, V> {
    bst: SGTree<K, V>,
    sorted_idxs: IdxVec,
}

impl<K: Ord, V> ConsumingIter<K, V> {
    pub fn new(bst: SGTree<K, V>) -> Self {
        let mut ordered_iter = ConsumingIter {
            bst,
            sorted_idxs: IdxVec::new(),
        };

        if let Some(root_idx) = ordered_iter.bst.root_idx {
            ordered_iter.sorted_idxs = ordered_iter.bst.flatten_subtree_to_sorted_idxs(root_idx);
            ordered_iter.sorted_idxs.reverse();
        }

        ordered_iter
    }
}

impl<K: Ord, V> Iterator for ConsumingIter<K, V> {
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        match self.sorted_idxs.pop() {
            Some(idx) => match self.bst.priv_remove_by_idx(idx) {
                Some(node) => Some((node.key, node.val)),
                None => {
                    debug_assert!(false, "Use of invalid index in consuming iterator!");
                    None
                }
            },
            None => None,
        }
    }
}