From f82dabae9d7884c5110b0ae7419a8e8ba916f6b6 Mon Sep 17 00:00:00 2001 From: Bazaah Date: Sun, 10 Apr 2022 14:28:23 +0000 Subject: [PATCH] node/graph: add Graph representation --- src/node/graph.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++ src/node/mod.rs | 1 + 2 files changed, 101 insertions(+) create mode 100644 src/node/graph.rs diff --git a/src/node/graph.rs b/src/node/graph.rs new file mode 100644 index 0000000..b081734 --- /dev/null +++ b/src/node/graph.rs @@ -0,0 +1,100 @@ +/* + * This Source Code Form is subject to the terms of the + * Mozilla Public License, v. 2.0. If a copy of the MPL + * was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +use slotmap::{SecondaryMap, SlotMap}; + +use super::nodes::{Node, NodeData, NodeIndex}; + +struct Graph<'a> +{ + store: Storage<'a>, + + head: Option, +} + +impl<'a> Graph<'a> +{ + /// Create a new, empty Graph + pub fn new() -> Self + { + Self { + store: Storage::new(), + head: None, + } + } + + pub fn insert(&mut self, f: F, data: NodeData<'a>) -> NodeIndex + where + F: FnOnce(NodeIndex) -> Node<'a>, + { + self.store.insert(f, data) + } + + pub fn nodes(&self) -> &SlotMap> + { + self.store.nodes() + } + + pub fn nodes_mut(&mut self) -> &mut SlotMap> + { + self.store.nodes_mut() + } + + pub fn node_data(&self) -> &SecondaryMap> + { + self.store.node_data() + } + + pub fn node_data_mut(&mut self) -> &mut SecondaryMap> + { + self.store.node_data_mut() + } +} + +#[derive(Default)] +pub(in crate::node) struct Storage<'a> +{ + nodes: SlotMap>, + node_data: SecondaryMap>, +} + +impl<'a> Storage<'a> +{ + pub fn new() -> Self + { + Self::default() + } + + pub fn nodes(&self) -> &SlotMap> + { + &self.nodes + } + + pub fn nodes_mut(&mut self) -> &mut SlotMap> + { + &mut self.nodes + } + + pub fn node_data(&self) -> &SecondaryMap> + { + &self.node_data + } + + pub fn node_data_mut(&mut self) -> &mut SecondaryMap> + { + &mut self.node_data + } + + pub fn insert(&mut self, f: F, data: NodeData<'a>) -> NodeIndex + where + F: FnOnce(NodeIndex) -> Node<'a>, + { + let id = self.nodes_mut().insert_with_key(f); + self.node_data_mut().insert(id, data); + + id + } +} diff --git a/src/node/mod.rs b/src/node/mod.rs index 35a3505..9db3b80 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -9,4 +9,5 @@ use crate::event::types::Slice; +mod graph; mod nodes;