orx_selfref_col/references/
node_idx.rs

1use super::NodePtr;
2use crate::{MemoryPolicy, MemoryState, Node, SelfRefCol, Variant};
3use core::fmt::Debug;
4use orx_pinned_vec::PinnedVec;
5
6/// A node index providing safe and constant time access to elements
7/// of the self referential collection.
8#[derive(Copy)]
9pub struct NodeIdx<V: Variant> {
10    ptr: *mut Node<V>,
11    state: MemoryState,
12}
13
14impl<V: Variant> Clone for NodeIdx<V> {
15    fn clone(&self) -> Self {
16        Self {
17            ptr: self.ptr,
18            state: self.state,
19        }
20    }
21}
22
23impl<V: Variant> Debug for NodeIdx<V> {
24    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25        f.debug_struct("NodeIdx")
26            .field("ptr", &self.ptr)
27            .field("state", &self.state)
28            .finish()
29    }
30}
31
32impl<V: Variant> PartialEq for NodeIdx<V> {
33    fn eq(&self, other: &Self) -> bool {
34        self.ptr == other.ptr && self.state == other.state
35    }
36}
37
38impl<V: Variant> Eq for NodeIdx<V> {}
39
40impl<V> NodeIdx<V>
41where
42    V: Variant,
43{
44    /// Creates a new index for the element at the given `node_ptr`
45    /// and the collection with the given `state`.
46    #[inline(always)]
47    pub fn new(state: MemoryState, node_ptr: &NodePtr<V>) -> Self {
48        Self {
49            ptr: node_ptr.ptr_mut(),
50            state,
51        }
52    }
53
54    /// Checks whether or not the `state` of the index matches that of this index.
55    #[inline(always)]
56    pub fn is_in_state(&self, state: MemoryState) -> bool {
57        self.state == state
58    }
59
60    #[inline(always)]
61    pub(crate) fn ptr(&self) -> *const Node<V> {
62        self.ptr
63    }
64
65    #[inline(always)]
66    pub(crate) fn ptr_mut(&self) -> *mut Node<V> {
67        self.ptr
68    }
69
70    /// Returns the node pointer if the index is in the same state as the `collection_state`,
71    /// None otherwise.
72    #[inline(always)]
73    pub fn get_ptr(&self, collection_state: MemoryState) -> Option<*mut Node<V>> {
74        self.state.eq(&collection_state).then_some(self.ptr)
75    }
76
77    /// Converts the node index into a node pointer.
78    #[inline(always)]
79    pub fn node_ptr(&self) -> NodePtr<V> {
80        NodePtr::new(self.ptr)
81    }
82
83    /// Returns true only if this index is valid for the given `collection`.
84    ///
85    /// A node index is valid iff it satisfies the following two conditions:
86    ///
87    /// * It is created from the given `collection`.
88    /// * Memory state of the `collection` has not changed since this index was created.
89    #[inline(always)]
90    pub fn is_valid_for<M, P>(&self, collection: &SelfRefCol<V, M, P>) -> bool
91    where
92        M: MemoryPolicy<V>,
93        P: PinnedVec<Node<V>>,
94    {
95        self.state == collection.memory_state()
96            && collection.nodes().contains_ptr(self.ptr)
97            && unsafe { &*self.ptr }.is_active()
98    }
99}