dlist_arena.rs 4.49 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
2
3
4
5
6
7
8
9
10
11
12
use crate::{GhostCell, GhostToken};
use typed_arena::Arena as TypedArena;

pub struct Node<'arena, 'id, T> {
    data: T,
    prev: Option<NodeRef<'arena, 'id, T>>,
    next: Option<NodeRef<'arena, 'id, T>>,
}

pub type NodeRef<'arena, 'id, T> = &'arena GhostCell<'id, Node<'arena, 'id, T>>;

impl<'arena, 'id, T> Node<'arena, 'id, T> {
Hai Dang's avatar
Hai Dang committed
13
    /// Create a new isolated node from T. Requires an arena.
Hai Dang's avatar
Hai Dang committed
14
15
16
17
18
19
20
21
22
23
24
    pub fn new(
        data: T,
        arena: &'arena TypedArena<Node<'arena, 'id, T>>
    ) -> NodeRef<'arena, 'id, T> {
        GhostCell::from_mut(arena.alloc(
            Self {
                data,
                prev: None,
                next: None,
            }
        ))
Ralf Jung's avatar
Ralf Jung committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    }

    pub fn prev(&self) -> Option<NodeRef<'arena, 'id, T>> {
        self.prev
    }

    pub fn next(&self) -> Option<NodeRef<'arena, 'id, T>> {
        self.next
    }

    /// Unlink the nodes adjacent to the provided node (but do not touch the node itself).
    fn unlink(node: NodeRef<'arena, 'id, T>, token: &mut GhostToken<'id>) {
        let old_prev = node.borrow(token).prev;
        let old_next = node.borrow(token).next;
        if let Some(next) = old_next {
            next.borrow_mut(token).prev = old_prev;
        }
        if let Some(prev) = old_prev {
            prev.borrow_mut(token).next = old_next;
        }
    }

Hai Dang's avatar
Hai Dang committed
47
    /// Insert `node2` right after `node1` in the list.
Ralf Jung's avatar
Ralf Jung committed
48
    pub fn insert_next(
Hai Dang's avatar
Hai Dang committed
49
50
        node1: NodeRef<'arena, 'id, T>,
        node2: NodeRef<'arena, 'id, T>,
Ralf Jung's avatar
Ralf Jung committed
51
52
        token: &mut GhostToken<'id>,
    ) {
Hai Dang's avatar
Hai Dang committed
53
54
        // Step 1: unlink the prev and next pointers nodes adjacent to node2.
        Self::unlink(node2, token);
Ralf Jung's avatar
Ralf Jung committed
55

Hai Dang's avatar
Hai Dang committed
56
57
58
59
        // Step 2: link node1.next pointer and node1.next.prev pointer to node2.
        let node1_old_next : Option<NodeRef<_>> = node1.borrow(token).next;
        if let Some(node1_old_next) = node1_old_next {
            node1_old_next.borrow_mut(token).prev = Some(node2);
Ralf Jung's avatar
Ralf Jung committed
60
        }
Hai Dang's avatar
Hai Dang committed
61
        node1.borrow_mut(token).next = Some(node2);
Ralf Jung's avatar
Ralf Jung committed
62
63

        // Step 3: link next to node and old_node_next (old node.next).
Hai Dang's avatar
Hai Dang committed
64
65
66
        let node2: &mut Node<_> = node2.borrow_mut(token);
        node2.prev = Some(node1);
        node2.next = node1_old_next;
Ralf Jung's avatar
Ralf Jung committed
67
68
    }

Hai Dang's avatar
Hai Dang committed
69
    /// Remove the links of this node to and from its adjacent nodes.
Ralf Jung's avatar
Ralf Jung committed
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
    pub fn remove(node: NodeRef<'arena, 'id, T>, token: &mut GhostToken<'id>) {
        // Step 1: unlink the prev and next pointers nodes adjacent to node.
        Self::unlink(node, token);

        // Step 2: null out the prev and next pointers.
        let node = node.borrow_mut(token);
        node.prev = None;
        node.next = None;
    }

    pub fn iter<'iter>(
        node: NodeRef<'arena, 'id, T>,
        token: &'iter GhostToken<'id>,
    ) -> Iter<'arena, 'id, 'iter, T> {
        Iter {
            cur: Some(node),
            token,
        }
    }

    /// Mutable iteration only works as "interior iteration", since we cannot hand out mutable references
    /// to multiple nodes at the same time.
    pub fn iter_mut(
        node: NodeRef<'arena, 'id, T>,
        token: &mut GhostToken<'id>,
        mut f: impl FnMut(&mut T),
    ) {
        let mut cur: Option<NodeRef<'arena, 'id, T>> = Some(node);
        while let Some(node) = cur {
            let node = node.borrow_mut(token); // mutably borrow `node` with `token`
            f(&mut node.data);
            cur = node.next;
        }
    }
Hai Dang's avatar
Hai Dang committed
104

Hai Dang's avatar
Hai Dang committed
105
    /// Immutable interior iteration
Hai Dang's avatar
Hai Dang committed
106
    pub fn iterate(
Hai Dang's avatar
Hai Dang committed
107
108
109
110
111
112
        node: NodeRef<'arena, 'id, T>,
        token: &GhostToken<'id>,
        f: impl Fn(&T),
    ) {
        let mut cur: Option<NodeRef<'arena, 'id, T>> = Some(node);
        while let Some(node) = cur {
Hai Dang's avatar
Hai Dang committed
113
            let node : &Node<_> = node.borrow(token); // immutably borrow `node` with `token`
Hai Dang's avatar
Hai Dang committed
114
115
116
117
            f(&node.data);
            cur = node.next;
        }
    }
Ralf Jung's avatar
Ralf Jung committed
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
}

pub struct Iter<'arena, 'id, 'iter, T> {
    // We could probably weaken this to make the outermost reference a `'iter`, but that
    // would make the types more complicated, so we don't.
    cur: Option<NodeRef<'arena, 'id, T>>,
    token: &'iter GhostToken<'id>,
}

impl<'arena, 'id, 'iter, T> Iterator for Iter<'arena, 'id, 'iter, T>
where
    T: 'iter,
    'arena: 'iter,
{
    type Item = &'iter T;

    fn next(&mut self) -> Option<Self::Item> {
        let cur: Option<NodeRef<'arena, 'id, T>> = self.cur;
        if let Some(node) = cur {
            let node: NodeRef<'arena, 'id, T> = node;
            let node = node.borrow(self.token);
            self.cur = node.next;
            Some(&node.data)
        } else {
            None
        }
    }
}