orx_selfref_col/
core_col.rs

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
use crate::{node::Node, NodePtr, Refs, Utilization, Variant};
use orx_pinned_vec::PinnedVec;
use orx_split_vec::{Recursive, SplitVec};

/// Core collection of the self referential collection.
pub struct CoreCol<V, P>
where
    V: Variant,
    P: PinnedVec<Node<V>>,
{
    nodes: P,
    ends: V::Ends,
    len: usize,
}

impl<V, P> Default for CoreCol<V, P>
where
    V: Variant,
    P: PinnedVec<Node<V>> + Default,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<V, P> CoreCol<V, P>
where
    V: Variant,
    P: PinnedVec<Node<V>>,
{
    /// Creates a new empty collection.
    pub fn new() -> Self
    where
        P: Default,
    {
        Self {
            nodes: P::default(),
            ends: Refs::empty(),
            len: 0,
        }
    }

    pub(crate) fn from_raw_parts(nodes: P, ends: V::Ends, len: usize) -> Self {
        Self { nodes, ends, len }
    }

    /// Destructs the collection into its inner pinned vec, ends and length.
    pub fn into_inner(self) -> (P, V::Ends, usize) {
        (self.nodes, self.ends, self.len)
    }

    pub(crate) fn with_active_nodes(nodes: P) -> Self {
        debug_assert!(nodes.iter().all(|x| x.data().is_some()));
        Self {
            len: nodes.len(),
            nodes,
            ends: Refs::empty(),
        }
    }

    // get

    /// Returns current node utilization of the collection.
    pub fn utilization(&self) -> Utilization {
        Utilization {
            capacity: self.nodes.capacity(),
            num_active_nodes: self.len,
            num_closed_nodes: self.nodes.len() - self.len,
        }
    }

    /// Returns length of the self referential collection.
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns whether or not the self referential collection is empty.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns a reference to the underlying nodes storage.
    #[inline(always)]
    pub fn nodes(&self) -> &P {
        &self.nodes
    }

    /// Returns a reference to the node with the given `node_ptr`.
    #[inline(always)]
    pub fn node(&self, node_ptr: &NodePtr<V>) -> &Node<V> {
        unsafe { &*node_ptr.ptr_mut() }
    }

    /// Returns the position of the node with the given `node_ptr`,
    /// None if the pointer is not valid.
    #[inline(always)]
    pub fn position_of(&self, node_ptr: &NodePtr<V>) -> Option<usize> {
        self.nodes.index_of_ptr(node_ptr.ptr_mut())
    }

    /// Returns the position of the node with the given `node_ptr`.
    ///
    /// # Panics
    ///
    /// Panics if the pointer is not valid.
    #[inline(always)]
    pub fn position_of_unchecked(&self, node_ptr: &NodePtr<V>) -> usize {
        self.nodes
            .index_of_ptr(node_ptr.ptr_mut())
            .expect("Pointer does not belong to the collection")
    }

    /// Returns a reference to the data.
    ///
    /// # Panics
    ///
    /// Panics if the node is already closed.
    ///
    /// # Safety
    ///
    /// Does not perform bounds check; hence, the caller must guarantee that the
    /// `node_ptr` belongs to (created from) this collection.
    #[inline(always)]
    pub unsafe fn data_unchecked(&self, node_ptr: &NodePtr<V>) -> &V::Item {
        unsafe { &*node_ptr.ptr_mut() }
            .data()
            .expect("node is closed")
    }

    /// Returns a reference to the ends of the collection.
    #[inline(always)]
    pub fn ends(&self) -> &V::Ends {
        &self.ends
    }

    /// Returns the pointer of the element with the given `node_position`
    /// in the underlying nodes storage.
    ///
    /// # Panics
    ///
    /// Panics if the `node_position` is out of bounds.
    #[inline(always)]
    pub fn node_ptr_at_pos(&self, node_position: usize) -> NodePtr<V> {
        let ptr = self.nodes.get_ptr(node_position).expect("out-of-bounds");
        NodePtr::new(ptr as *mut Node<V>)
    }

    // mut

    pub(crate) fn clear_core(&mut self) {
        self.len = 0;
        self.ends.clear();
        self.nodes.clear();
    }

    /// Returns a mutable reference to the underlying nodes storage.
    #[inline(always)]
    pub fn nodes_mut(&mut self) -> &mut P {
        &mut self.nodes
    }

    /// Pushes the element with the given `data` and returns its pointer.
    pub fn push(&mut self, data: V::Item) -> NodePtr<V> {
        self.len += 1;
        let ptr = self.nodes.push_get_ptr(Node::new_free_node(data));
        NodePtr::new(ptr as *mut Node<V>)
    }

    /// Returns a mutable reference to the data.
    ///
    /// # Panics
    ///
    /// Panics if the node is already closed.
    ///
    /// # Safety
    ///
    /// Does not perform bounds check; hence, the caller must guarantee that the
    /// `node_ptr` belongs to (created from) this collection.
    #[inline(always)]
    pub unsafe fn data_mut_unchecked(&mut self, node_ptr: &NodePtr<V>) -> &mut V::Item {
        unsafe { &mut *node_ptr.ptr_mut() }
            .data_mut()
            .expect("node is closed")
    }

    /// Closes the node at the given `node_ptr` and returns its data.
    ///
    /// # Panics
    ///
    /// Panics if the node was already closed.
    #[inline(always)]
    pub fn close(&mut self, node_ptr: &NodePtr<V>) -> V::Item {
        self.len -= 1;
        unsafe { &mut *node_ptr.ptr_mut() }.close()
    }

    /// Closes the node at the given `node_ptr` and returns its data the node was active.
    /// Does nothing and returns None if the node was already closed.
    pub fn close_if_active(&mut self, node_ptr: &NodePtr<V>) -> Option<V::Item> {
        let node = unsafe { &mut *node_ptr.ptr_mut() };
        match node.is_active() {
            true => {
                self.len -= 1;
                Some(node.close())
            }
            false => None,
        }
    }

    /// Returns a mutable reference to the ends of the collection.
    pub fn ends_mut(&mut self) -> &mut V::Ends {
        &mut self.ends
    }

    /// Returns a mutable reference to the node with the given `node_ptr`.
    #[inline(always)]
    pub fn node_mut(&mut self, node_ptr: &NodePtr<V>) -> &mut Node<V> {
        unsafe { &mut *node_ptr.ptr_mut() }
    }

    /// Swaps the closed node at the `closed_position` with the active node
    /// at the `active_position`.
    pub fn move_node(&mut self, closed_position: usize, active_position: usize) {
        debug_assert!(closed_position < active_position);
        debug_assert!(self.nodes[closed_position].is_closed());
        debug_assert!(self.nodes[active_position].is_active());

        self.nodes_mut().swap(active_position, closed_position);
    }

    // data
    /// Swaps the underlying data of the element at the given `node_ptr` with the `new_value`,
    /// and returns the old value.
    ///
    /// # Panics
    ///
    /// Panics if the node was already closed.
    pub fn swap_data(&mut self, node_ptr: &NodePtr<V>, new_value: V::Item) -> V::Item {
        let node = unsafe { &mut *node_ptr.ptr_mut() };
        node.swap_data(new_value)
    }
}

impl<V> CoreCol<V, SplitVec<Node<V>, Recursive>>
where
    V: Variant,
{
    /// Appends the `nodes` to this collection.
    pub fn append_nodes(&mut self, nodes: SplitVec<Node<V>, Recursive>) {
        self.len += nodes.len();
        self.nodes.append(nodes)
    }
}