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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use prelude::*;
use std::marker::PhantomData;

/// A cursor that holds a shared reference to its tree.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct TreeCursor<'n: 'f, 'f, N: 'n> {
    root: PhantomData<&'n N>,
    frozen: PhantomData<&'f ()>,
    stack: Vec<(*const N, usize)>,
}

impl<'n, N: 'n> TreeCursor<'n, 'n, N> {
    /// Creates a new `TreeCursor` starting at `root`.
    pub fn new(root: &'n N) -> Self {
        Self {
            root: PhantomData,
            frozen: PhantomData,
            stack: vec![(root as *const N, 0)],
        }
    }
}

impl<'n: 'f, 'f, N: 'n> TreeCursor<'n, 'f, N> {
    fn top(&self) -> &(*const N, usize) {
        self.stack.last().unwrap()
    }

    fn top_mut(&mut self) -> &mut (*const N, usize) {
        self.stack.last_mut().unwrap()
    }

    fn down_map_ptr<F>(&mut self, f: F) -> Option<*const N>
    where
        F: Fn(&'n N, usize) -> Option<&'n N>,
    {
        let idx = self.top().1;
        let here_ptr = self.get() as *const N;
        let new_ptr =
            f(unsafe { here_ptr.as_ref().unwrap() }, idx)? as *const N;
        self.top_mut().1 += 1;
        Some(new_ptr)
    }

    /// Passes `f` the active node and the current value of the "next child"
    /// counter. If `f` returns a node, it's set as the active node, the old
    /// active node's "next child" counter is incremented, and this method
    /// returns true. Otherwise, this method returns false.
    pub fn down_map<F>(&mut self, f: F) -> bool
    where
        F: Fn(&'n N, usize) -> Option<&'n 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()
    }

    /// Like [`down_new`], except that it takes a closure like [`down_map`].
    ///
    /// [`down_new`]: TreeCursor::down_new
    /// [`down_map`]: TreeCursor::down_map
    pub fn down_map_new<'s, F>(&'s mut self, f: F)
        -> Option<TreeCursor<'n, 's, N>>
    where
        F: Fn(&'n N, usize) -> Option<&'n N>,
    {
        let new_ptr = self.down_map_ptr(f)?;
        Some(Self {
            root: PhantomData,
            frozen: PhantomData,
            stack: vec![(new_ptr, 0)],
        })
    }

    /// Resets the active node's "next child" counter to 0.
    pub fn zero(&mut self) {
        self.top_mut().1 = 0;
    }

    /// Moves the cursor up one node. Returns true if there was a node to move
    /// to, and false otherwise. In both cases, the old active node's "next
    /// child" counter is reset, as if [`zero`] had been called.
    ///
    /// [`zero`]: TreeCursor::zero
    pub fn up(&mut self) -> bool {
        if self.stack.len() == 1 {
            self.stack[0].1 = 0;
            false
        } else {
            self.stack.pop().unwrap();
            true
        }
    }

    /// Takes the active node from this `TreeCursor` and returns a new
    /// `TreeCursor` at that position. `self` is frozen until the new cursor
    /// goes out of scope.
    pub fn take<'s>(&'s mut self) -> Option<TreeCursor<'n, 's, N>> {
        if self.stack.len() == 1 {
            None
        } else {
            let (old_ptr, old_idx) = self.stack.pop().unwrap();
            let old = unsafe { old_ptr.as_ref().unwrap() };
            Some(Self {
                root: PhantomData,
                frozen: PhantomData,
                stack: vec![(old, old_idx)],
            })
        }
    }

    /// Returns a shared reference to the active node.
    pub fn get(&self) -> &N {
        let here: *const N = self.top().0;
        unsafe { here.as_ref().unwrap() }
    }
}

impl<'n: 'f, 'f, N: 'n + Down> TreeCursor<'n, 'f, N> {
    fn down_ptr(&mut self) -> Option<*const N> {
        let idx = self.top().1;
        let new_ptr = self.get().down(idx)? as *const N;
        self.top_mut().1 += 1;
        Some(new_ptr)
    }

    /// Moves the cursor down one node. The node to move to is determined by
    /// calling [`Down::down`] on the active node and passing it the "next
    /// child" counter. Returns true and increments the old active node's
    /// "next child" counter if there was a node to move to, and returns false
    /// otherwise.
    pub 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()
    }

    /// Like [`down`], except instead of moving the position of `self`, it
    /// returns a new `TreeCursor` whose root is the new position. `self` is
    /// frozen until the new cursor goes out of scope.
    ///
    /// [`down`]: TreeCursor::down
    pub fn down_new<'s>(&'s mut self) -> Option<TreeCursor<'n, 's, N>> {
        let new_ptr = self.down_ptr()?;
        Some(Self {
            root: PhantomData,
            frozen: PhantomData,
            stack: vec![(new_ptr, 0)],
        })
    }
}

impl<'n: 'f, 'f, N: 'n> From<TreeCursorMut<'n, 'f, N>>
    for TreeCursor<'n, 'f, N>
{
    fn from(mut cm: TreeCursorMut<'n, 'f, N>) -> Self {
        TreeCursor {
            root: PhantomData,
            frozen: PhantomData,
            stack: cm.stack.drain(..).map(|(p, n)| (p as *const N, n))
                .collect(), // TODO: speed this up
        }
    }
}

/// A cursor that holds a mutable reference to its tree.
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct TreeCursorMut<'n: 'f, 'f, N: 'n> {
    root: PhantomData<&'n mut N>,
    frozen: PhantomData<&'f ()>,
    stack: Vec<(*mut N, usize)>,
}

impl<'n, N: 'n> TreeCursorMut<'n, 'n, N> {
    /// Creates a new `TreeCursorMut` starting at `root`.
    pub fn new(root: &'n mut N) -> Self {
        Self {
            root: PhantomData,
            frozen: PhantomData,
            stack: vec![(root as *mut N, 0)],
        }
    }
}

impl<'n: 'f, 'f, N: 'n> TreeCursorMut<'n, 'f, N> {
    fn top(&self) -> &(*mut N, usize) {
        self.stack.last().unwrap()
    }

    fn top_mut(&mut self) -> &mut (*mut N, usize) {
        self.stack.last_mut().unwrap()
    }

    fn down_map_ptr<F>(&mut self, f: F) -> Option<*mut N>
    where
        F: Fn(&'n mut N, usize) -> Option<&'n mut N>,
    {
        let idx = self.top().1;
        let here_ptr = self.get_mut() as *mut N;
        let new_ptr = f(unsafe { here_ptr.as_mut().unwrap() }, idx)? as *mut N;
        self.top_mut().1 += 1;
        Some(new_ptr)
    }

    /// Passes `f` the active node and the current value of the "next child"
    /// counter. If `f` returns a node, it's set as the active node, the old
    /// active node's "next child" counter is incremented, and this method
    /// returns true. Otherwise, this method returns false.
    pub fn down_map<F>(&mut self, f: F) -> bool
    where
        F: Fn(&'n mut N, usize) -> Option<&'n 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()
    }

    /// Like [`down_new`], except that it takes a closure like [`down_map`].
    ///
    /// [`down_new`]: TreeCursorMut::down_new
    /// [`down_map`]: TreeCursorMut::down_map
    pub fn down_map_new<'s, F>(&'s mut self, f: F)
        -> Option<TreeCursorMut<'n, 's, N>>
    where
        F: Fn(&'n mut N, usize) -> Option<&'n mut N>,
    {
        let new_ptr = self.down_map_ptr(f)?;
        Some(Self {
            root: PhantomData,
            frozen: PhantomData,
            stack: vec![(new_ptr, 0)],
        })
    }

    /// Resets the active node's "next child" counter to 0.
    pub fn zero(&mut self) {
        self.top_mut().1 = 0;
    }

    /// Moves the cursor up one node. Returns true if there was a node to move
    /// to, and false otherwise. In both cases, the old active node's "next
    /// child" counter is reset, as if [`zero`] had been called.
    ///
    /// [`zero`]: TreeCursorMut::zero
    pub fn up(&mut self) -> bool {
        if self.stack.len() == 1 {
            self.stack[0].1 = 0;
            false
        } else {
            self.stack.pop().unwrap();
            true
        }
    }

    /// Takes the active node from this `TreeCursorMut` and returns a new
    /// `TreeCursorMut` at that position. `self` is frozen until the new cursor
    /// goes out of scope.
    pub fn take<'s>(&'s mut self) -> Option<TreeCursorMut<'n, 's, N>> {
        if self.stack.len() == 1 {
            None
        } else {
            let (old_ptr, old_idx) = self.stack.pop().unwrap();
            let old = unsafe { old_ptr.as_mut().unwrap() };
            Some(Self {
                root: PhantomData,
                frozen: PhantomData,
                stack: vec![(old, old_idx)],
            })
        }
    }

    /// Returns a shared reference to the active node.
    pub fn get(&self) -> &N {
        let here: *const N = self.top().0;
        (unsafe { here.as_ref() }).unwrap()
    }

    /// Returns a mutable reference to the active node.
    pub fn get_mut(&mut self) -> &mut N {
        let here = self.top().0;
        (unsafe { here.as_mut() }).unwrap()
    }

    pub fn as_cursor<'s>(&'s self) -> TreeCursor<'n, 's, N> {
        TreeCursor {
            root: PhantomData,
            frozen: PhantomData,
            stack: self.stack.iter()
                .map(|&(p, n)| (p as *const N, n)).collect(),
        }
    }
}

/// Stores a cursor's position at an earlier point in time.
pub struct TreeCursorPos(Vec<usize>);

impl<'n: 'f, 'f, N: 'n + DownMut> TreeCursorMut<'n, 'f, N> {
    /// Returns an opaque object that stores the current position of the cursor.
    /// Pass it to [`set_pos`] to restore that position.
    ///
    /// [`set_pos`]: TreeCursorMut::set_pos
    pub fn pos(&self) -> TreeCursorPos {
        TreeCursorPos(self.stack.iter().map(|&(_, idx)| idx).collect())
    }

    /// Moves the cursor to the given position, as long as tree mutation hasn't
    /// invalidated the position since it was retrieved.
    ///
    /// # Panics
    ///
    /// If the tree has changed such that the position is no longer valid, this
    /// method panics. However, since the position is stored using "next child"
    /// indices (not pointers), it remains valid as long as the tree has a node
    /// in that position, even if the node's value changes or it's replaced with
    /// another node. If this is a problem, you should track the position's
    /// validity yourself.
    ///
    /// [`pos`]: TreeCursorMut::pos
    pub fn set_pos(&mut self, pos: &TreeCursorPos) {
        self.stack.truncate(1);
        for &idx in pos.0.iter().rev().skip(1).rev() { // TODO: ugly
            self.top_mut().1 = idx - 1;
            if !self.down() {
                panic!("missing node in TreeCursorPos");
            }
        }
        let &idx = pos.0.last().unwrap();
        self.top_mut().1 = idx;
    }

    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)
    }

    /// Moves the cursor down one node. The node to move to is determined by
    /// calling [`DownMut::down_mut`] on the active node and passing it the
    /// "next child" counter. Returns true and increments the old active node's
    /// "next child" counter if there was a node to move to, and returns false
    /// otherwise.
    pub 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()
    }

    /// Like [`down`], except instead of moving the position of `self`, it
    /// returns a new `TreeCursorMut` whose root is the new position. `self` is
    /// frozen until the new cursor goes out of scope.
    ///
    /// [`down`]: TreeCursorMut::down
    pub fn down_new<'s>(&'s mut self) -> Option<TreeCursorMut<'n, 's, N>> {
        let new_ptr = self.down_ptr()?;
        Some(Self {
            root: PhantomData,
            frozen: PhantomData,
            stack: vec![(new_ptr, 0)],
        })
    }
}