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