rshyper_core/node/impls/
impl_hyper_node.rs

1/*
2    appellation: impl_hyper_node <module>
3    authors: @FL03
4*/
5use crate::idx::{RawIndex, VertexId};
6use crate::node::Node;
7use crate::weight::{UnWeight, Weight};
8
9impl<T, Idx> Node<UnWeight<T>, Idx>
10where
11    Idx: RawIndex,
12{
13    /// returns a new, weightless node with the given index
14    pub const fn weightless(id: VertexId<Idx>) -> Self {
15        Self::new(id, UnWeight::new())
16    }
17    /// initialize the weight of the node with the given value
18    pub fn init_weight<F>(self, init: F) -> Node<T, Idx>
19    where
20        F: FnOnce() -> T,
21    {
22        Node::new(self.id, init())
23    }
24}
25
26impl<T, Idx> AsRef<Weight<T>> for Node<T, Idx>
27where
28    Idx: RawIndex,
29{
30    fn as_ref(&self) -> &Weight<T> {
31        self.weight()
32    }
33}
34
35impl<T, Idx> AsMut<Weight<T>> for Node<T, Idx>
36where
37    Idx: RawIndex,
38{
39    fn as_mut(&mut self) -> &mut Weight<T> {
40        self.weight_mut()
41    }
42}
43
44impl<T, Idx> core::borrow::Borrow<VertexId<Idx>> for Node<T, Idx>
45where
46    Idx: RawIndex,
47{
48    fn borrow(&self) -> &VertexId<Idx> {
49        self.id()
50    }
51}
52
53impl<T, Idx> core::borrow::Borrow<Weight<T>> for Node<T, Idx>
54where
55    Idx: RawIndex,
56{
57    fn borrow(&self) -> &Weight<T> {
58        self.weight()
59    }
60}
61
62impl<T, Idx> core::borrow::BorrowMut<Weight<T>> for Node<T, Idx>
63where
64    Idx: RawIndex,
65{
66    fn borrow_mut(&mut self) -> &mut Weight<T> {
67        self.weight_mut()
68    }
69}
70
71impl<T, Idx> core::ops::Deref for Node<T, Idx>
72where
73    Idx: RawIndex,
74{
75    type Target = T;
76
77    fn deref(&self) -> &Self::Target {
78        self.weight()
79    }
80}
81
82impl<T, Idx> core::ops::DerefMut for Node<T, Idx>
83where
84    Idx: RawIndex,
85{
86    fn deref_mut(&mut self) -> &mut Self::Target {
87        self.weight_mut()
88    }
89}
90
91impl<T, Idx> From<Weight<T>> for Node<T, Idx>
92where
93    Idx: Default + RawIndex,
94{
95    fn from(weight: Weight<T>) -> Self {
96        Node::from_weight(weight)
97    }
98}
99
100impl<T, Idx> From<VertexId<Idx>> for Node<T, Idx>
101where
102    Idx: RawIndex,
103    T: Default,
104{
105    fn from(index: VertexId<Idx>) -> Self {
106        Node::from_id(index)
107    }
108}
109
110impl<T, Idx> From<(VertexId<Idx>, Weight<T>)> for Node<T, Idx>
111where
112    Idx: RawIndex,
113{
114    fn from((index, Weight(weight)): (VertexId<Idx>, Weight<T>)) -> Self {
115        Node::new(index, weight)
116    }
117}
118
119impl<T, Idx> From<Node<T, Idx>> for (VertexId<Idx>, Weight<T>)
120where
121    Idx: RawIndex,
122{
123    fn from(node: Node<T, Idx>) -> Self {
124        (node.id, node.weight)
125    }
126}
127
128impl<T, Idx> From<Node<T, Idx>> for VertexId<Idx>
129where
130    Idx: RawIndex,
131{
132    fn from(node: Node<T, Idx>) -> Self {
133        node.id
134    }
135}