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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use prelude::*;
use std::marker::PhantomData;

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct TreeCursor<'n, N: 'n> {
    root: PhantomData<&'n N>,
    stack: Vec<(*const N, usize)>,
}

impl<'n, N: 'n + Down> TreeCursor<'n, N> {
    pub fn new(root: &'n N) -> Self {
        Self {
            root: PhantomData,
            stack: vec![(root as *const N, 0)],
        }
    }

    fn down_ptr(&mut self) -> Option<*const N> {
        let idx = self.stack.last().unwrap().1;
        let new_ptr = self.get().down(idx)? as *const N;
        self.stack.last_mut().unwrap().1 += 1;
        Some(new_ptr)
    }

    fn down_map_ptr<F>(&mut self, f: F) -> Option<*const N>
    where
        F: Fn(&N, usize) -> Option<&N>,
    {
        let idx = self.stack.last().unwrap().1;
        let new_ptr = f(self.get(), idx)? as *const N;
        self.stack.last_mut().unwrap().1 += 1;
        Some(new_ptr)
    }

    pub fn down_map<F>(&mut self, f: F) -> bool
    where
        F: Fn(&N, usize) -> Option<&N>,
    {
        let maybe_new_ptr = self.down_map_ptr(f);
        if let &Some(new_ptr) = &maybe_new_ptr {
            self.stack.push((new_ptr, 0));
        }
        maybe_new_ptr.is_some()
    }

    pub fn down_map_new<F>(&mut self, f: F) -> Option<Self>
    where
        F: Fn(&N, usize) -> Option<&N>,
    {
        let new_ptr = self.down_map_ptr(f)?;
        Some(Self::new(unsafe { new_ptr.as_ref().unwrap() }))
    }
}

impl<'n, N: 'n + Down> OpaqueVerticalCursor for TreeCursor<'n, N> {
    fn zero(&mut self) {
        self.stack.last_mut().unwrap().1 = 0;
    }

    fn down(&mut self) -> bool {
        let maybe_new_ptr = self.down_ptr();
        if let &Some(new_ptr) = &maybe_new_ptr {
            self.stack.push((new_ptr, 0));
        }
        maybe_new_ptr.is_some()
    }

    fn up(&mut self) -> bool {
        if self.stack.len() == 1 {
            self.stack[0].1 = 0;
            false
        } else {
            self.stack.pop().unwrap();
            true
        }
    }
}

impl<'n, N: 'n + Down> VerticalCursor for TreeCursor<'n, N> {
    type Item = N;

    fn get(&self) -> &N {
        let here: *const N = self.stack.last().unwrap().0;
        unsafe { here.as_ref().unwrap() }
    }

    fn down_new(&mut self) -> Option<Self> {
        let new_ptr = self.down_ptr()?;
        Some(Self::new(unsafe { new_ptr.as_ref().unwrap() }))
    }
}

#[derive(Debug, Eq, Hash, PartialEq)]
pub struct TreeCursorMut<'n, N: 'n> {
    root: PhantomData<&'n mut N>,
    stack: Vec<(*mut N, usize)>,
}

impl<'n, N: 'n + DownMut> TreeCursorMut<'n, N> {
    pub fn new(root: &'n mut N) -> Self {
        let root_ptr: *mut N = root;
        Self {
            root: PhantomData,
            stack: vec![(root_ptr, 0)],
        }
    }

    fn down_ptr(&mut self) -> Option<*mut N> {
        let idx = self.stack.last().unwrap().1;
        let new_ptr = self.get_mut().down_mut(idx)? as *mut N;
        self.stack.last_mut().unwrap().1 += 1;
        Some(new_ptr)
    }

    fn down_map_ptr<F>(&mut self, f: F) -> Option<*mut N>
    where
        F: Fn(&mut N, usize) -> Option<&mut N>,
    {
        let idx = self.stack.last().unwrap().1;
        let new_ptr = f(self.get_mut(), idx)? as *mut N;
        self.stack.last_mut().unwrap().1 += 1;
        Some(new_ptr)
    }

    pub fn down_map<F>(&mut self, f: F) -> bool
    where
        F: Fn(&mut N, usize) -> Option<&mut N>,
    {
        let maybe_new_ptr = self.down_map_ptr(f);
        if let &Some(new_ptr) = &maybe_new_ptr {
            self.stack.push((new_ptr, 0));
        }
        maybe_new_ptr.is_some()
    }

    pub fn down_map_new<F>(&mut self, f: F) -> Option<Self>
    where
        F: Fn(&mut N, usize) -> Option<&mut N>,
    {
        let new_ptr = self.down_map_ptr(f)?;
        Some(Self::new(unsafe { new_ptr.as_mut().unwrap() }))
    }
}

impl<'n, N: 'n + DownMut> OpaqueVerticalCursor for TreeCursorMut<'n, N> {
    fn zero(&mut self) {
        self.stack.last_mut().unwrap().1 = 0;
    }

    fn down(&mut self) -> bool {
        let maybe_new_ptr = self.down_ptr();
        if let &Some(new_ptr) = &maybe_new_ptr {
            self.stack.push((new_ptr, 0));
        }
        maybe_new_ptr.is_some()
    }

    fn up(&mut self) -> bool {
        if self.stack.len() == 1 {
            self.stack[0].1 = 0;
            false
        } else {
            self.stack.pop().unwrap();
            true
        }
    }
}

impl<'n, N: 'n + DownMut> VerticalCursor for TreeCursorMut<'n, N> {
    type Item = N;

    fn get(&self) -> &N {
        let here: *const N = self.stack.last().unwrap().0;
        (unsafe { here.as_ref() }).unwrap()
    }

    fn down_new(&mut self) -> Option<Self> {
        let new_ptr = self.down_ptr()?;
        Some(Self::new(unsafe { new_ptr.as_mut().unwrap() }))
    }
}

impl<'n, N: 'n + DownMut> VerticalCursorMut for TreeCursorMut<'n, N> {
    fn get_mut(&mut self) -> &mut N {
        let here = self.stack.last().unwrap().0;
        (unsafe { here.as_mut() }).unwrap()
    }
}