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
use super::*;

use std::collections::{HashMap, HashSet, BTreeMap};

#[derive(PartialEq, Eq, Clone, Copy)]
pub enum CleanupGen {
    Even, Odd
}

#[derive(PartialEq, Eq, Clone, Copy)]
pub struct MetaData {
    pub(crate) cleanup_gen : CleanupGen,
    pub(crate) store_index: usize,
}

impl CleanupGen {
    pub(crate) fn flip(&mut self) {
        if *self == CleanupGen::Even {
            *self = CleanupGen::Odd
        } else {
            *self = CleanupGen::Even
        }
    }
}

pub trait GraphNode : Sized {
    type Node;
    fn get(&self) -> &Self::Node;
    fn get_mut(&mut self) -> &mut Self::Node;

    fn meta(&self) -> &MetaData;
    fn meta_mut(&mut self) -> &mut MetaData;

    fn traverse(&self, cleanup : &mut CleanupState<Self>);

    fn from_data(data : Self::Node) -> Self;
}


/// Views into nodes allowing direct access to the nodes data and references.
pub mod node_views {
    use super::*;

    macro_rules! define_node_view {
        ($NodeType:ident, $Collection:ident) => {
            pub struct $NodeType<'id, N, E> {
                pub refs : $Collection<'id, super::$NodeType<N, E>, E>,
                pub data : N,
            }

            impl <'id, N, E> $NodeType<'id, N, E> {
                pub(crate) fn new(data : N) -> Self {
                    $NodeType { data, refs: $Collection::default() }
                }
            }
        }
    }

    define_node_view!{VecNode, NodeVec}
    define_node_view!{NamedNode, NodeNamedMap}
    define_node_view!{OptionNode, NodeOption}

    pub struct TreeNode<'id, K, N, E> {
        pub refs : NodeTreeMap<'id, K, super::TreeNode<K, N, E>, E>,
        pub data : N,
    }

    impl <'id, K : Ord, N, E> TreeNode<'id, K, N, E> {
        pub(crate) fn new(data : N) -> Self {
            TreeNode { data, refs: BTreeMap::default() }
        }
    }
}

macro_rules! impl_node_type {
    ($NodeType:ident) => {

        pub struct $NodeType<N, E> {
            pub(crate) internal: node_views::$NodeType<'static, N, E>,
            pub(crate) meta : MetaData,
        }

        impl <N, E> $NodeType<N, E> {
            pub (crate) fn get_view<'id>(&self) -> &node_views::$NodeType<'id, N, E> {
                unsafe {
                    transmute(&self.internal)
                }
            }

            pub (crate) fn get_view_mut<'id>(&mut self) -> &mut node_views::$NodeType<'id, N, E> {
                unsafe {
                    transmute(&mut self.internal)
                }
            }
        }

        impl <N, E> GraphNode for $NodeType<N, E> {
            type Node = N;

            fn get(&self) -> &Self::Node
            {
                &self.internal.data
            }

            fn get_mut(&mut self) -> &mut Self::Node
            {
                &mut self.internal.data
            }

            fn meta(&self) -> &MetaData {
                &self.meta
            }
            
            fn meta_mut(&mut self) -> &mut MetaData {
                &mut self.meta
            }

            fn traverse(&self, cleanup : &mut CleanupState<Self>) {
                NodeCollection::traverse(&self.internal.refs, cleanup);
            }

            fn from_data(data : Self::Node) -> Self
            {
                let meta = MetaData { cleanup_gen : CleanupGen::Even, store_index : 0 };
                Self { internal : node_views::$NodeType::new(data), meta }
            }
        }
    }
}

impl_node_type!{VecNode}
impl_node_type!{NamedNode}
impl_node_type!{OptionNode}

pub struct TreeNode<K, N, E> {
    pub(crate) internal: node_views::TreeNode<'static, K, N, E>,
    pub(crate) meta : MetaData,
}

impl <K, N, E> TreeNode<K, N, E> {
    pub (crate) fn get_view<'id>(&self) -> &node_views::TreeNode<'id, K, N, E> {
        unsafe {
            transmute(&self.internal)
        }
    }

    pub (crate) fn get_view_mut<'id>(&mut self) -> &mut node_views::TreeNode<'id, K, N, E> {
        unsafe {
            transmute(&mut self.internal)
        }
    }
}

impl <K : Ord, N, E> GraphNode for TreeNode<K, N, E> {
    type Node = N;

    fn get(&self) -> &Self::Node
    {
        &self.internal.data
    }

    fn get_mut(&mut self) -> &mut Self::Node
    {
        &mut self.internal.data
    }

    fn meta(&self) -> &MetaData {
        &self.meta
    }
    
    fn meta_mut(&mut self) -> &mut MetaData {
        &mut self.meta
    }

    fn traverse(&self, cleanup : &mut CleanupState<Self>) {
        NodeCollection::traverse(&self.internal.refs, cleanup);
    }

    fn from_data(data : Self::Node) -> Self
    {
        let meta = MetaData { cleanup_gen : CleanupGen::Even, store_index : 0 };
        Self { internal : node_views::TreeNode::new(data), meta }
    }
}

pub unsafe trait NodeCollection<'id, NodeType : GraphNode> : Default {
    fn traverse(this : &Self, cleanup : &mut CleanupState<NodeType>);
}

pub unsafe trait RootCollection<'id, NodeType : GraphNode> : Default {
    fn traverse(this : &Self, cleanup : &mut CleanupState<NodeType>);
}

fn traverse_touch<NodeType : GraphNode>(iter : impl Iterator<Item = *mut NodeType>, cleanup : &mut CleanupState<NodeType>) {
    for i in iter {
        cleanup.touch(i);
    }
}

pub type RootVec<'id, T> = Vec<GraphPtr<'id, T>>;
pub type RootNamedSet<'id, T> = HashSet<GraphPtr<'id, T>>;
pub type RootOption<'id, T> = Option<GraphPtr<'id, T>>;
pub type RootHashMap<'id, K, T> = HashMap<K, GraphPtr<'id, T>>;

pub type NodeVec<'id, NodeType, E> = Vec<(GraphPtr<'id, NodeType>, E)>;
pub type NodeNamedMap<'id, NodeType, E> = HashMap<GraphPtr<'id, NodeType>, E>;
pub type NodeOption<'id, NodeType, E> = Option<(GraphPtr<'id, NodeType>, E)>;
pub type NodeTreeMap<'id, K, NodeType, E> = BTreeMap<K, (GraphPtr<'id, NodeType>, E)>;

macro_rules! impl_root_collection {
    ($collection:ident) => {
        unsafe impl <'id, NodeType> RootCollection<'id, NodeType> for $collection<'id, NodeType>
        where NodeType : GraphNode
        {
            fn traverse(this : &Self, cleanup : &mut CleanupState<NodeType>) {
                traverse_touch(this.iter().map(|x| x.as_mut()), cleanup);
            }
        }
    }
}


unsafe impl <'id, K, NodeType> RootCollection<'id, NodeType> for RootHashMap<'id, K, NodeType>
where NodeType : GraphNode,
      K : Hash + Eq
{
    fn traverse(this : &Self, cleanup : &mut CleanupState<NodeType>) {
        traverse_touch(this.values().map(|x| x.as_mut()), cleanup);
    }
}

impl_root_collection!{RootVec}
impl_root_collection!{RootNamedSet}
impl_root_collection!{RootOption}

macro_rules! impl_node_collection {
    ($collection:ident) => {
        unsafe impl <'id, NodeType, E> NodeCollection<'id, NodeType> for $collection<'id, NodeType, E>
        where NodeType : GraphNode
        {
            fn traverse(this : &Self, cleanup : &mut CleanupState<NodeType>) {
                traverse_touch(this.iter().map(|x| x.0.as_mut()), cleanup);
            }
        }
    }
}

impl_node_collection!{NodeVec}
impl_node_collection!{NodeNamedMap}
impl_node_collection!{NodeOption}

unsafe impl <'id, K, NodeType, E> NodeCollection<'id, NodeType> for NodeTreeMap<'id, K, NodeType, E>
where NodeType : GraphNode,
      K : Ord
{
    fn traverse(this : &Self, cleanup : &mut CleanupState<NodeType>) {
        traverse_touch(this.values().map(|x| x.0.as_mut()), cleanup);
    }
}