rshyper_core/node/
hyper_node.rs

1/*
2    Appellation: node <module>
3    Contrib: @FL03
4*/
5use crate::{VertexId, Weight};
6
7#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
8#[cfg_attr(
9    feature = "serde",
10    derive(serde::Deserialize, serde::Serialize),
11    serde(rename_all = "lowercase")
12)]
13pub struct Node<T = (), Idx = usize> {
14    pub(crate) index: VertexId<Idx>,
15    pub(crate) weight: Weight<T>,
16}
17
18impl<T, Idx> Node<T, Idx> {
19    /// initialize a new instance with the given index and weight
20    pub fn new(index: VertexId<Idx>, weight: T) -> Self {
21        Self {
22            index,
23            weight: Weight(weight),
24        }
25    }    
26    /// returns a new weighted node using the given value and the logical default for the index
27    pub fn from_index(index: VertexId<Idx>) -> Self
28    where
29        T: Default,
30    {
31        Self {
32            index,
33            weight: Weight::default(),
34        }
35    }    
36    /// creates a new node with the given index using the logical default for the weight.
37    pub fn from_weight(weight: Weight<T>) -> Self
38    where
39        Idx: Default,
40    {
41        Self {
42            index: VertexId::default(),
43            weight,
44        }
45    }
46    /// consumes the current instance to create another with the given index.
47    pub fn with_index<I2>(self, index: VertexId<I2>) -> Node<T, I2> {
48        Node {
49            index,
50            weight: self.weight,
51        }
52    }
53    /// consumes the current instance to create another with the given weight.
54    pub fn with_weight<U>(self, weight: Weight<U>) -> Node<U, Idx> {
55        Node {
56            index: self.index,
57            weight,
58        }
59    }
60    /// returns an immutable reference to the node index
61    pub const fn index(&self) -> &VertexId<Idx> {
62        &self.index
63    }
64    /// returns an immutable reference to the node weight
65    pub const fn weight(&self) -> &Weight<T> {
66        &self.weight
67    }
68    /// returns a mutable reference to the node weight
69    pub const fn weight_mut(&mut self) -> &mut Weight<T> {
70        &mut self.weight
71    }
72    /// update the weight and return a mutable reference to the current instance.
73    pub fn set_weight(&mut self, weight: T) -> &mut Self {
74        self.weight_mut().set(weight);
75        self
76    }
77    /// [`replace`](core::mem::replace) the weight of the current instance with the given weight,
78    /// returning the previous weight.
79    pub fn replace_weight(&mut self, weight: T) -> T {
80        self.weight_mut().replace(weight)
81    }
82    /// [`swap`](core::mem::swap) the weight of the current instance with the weight of
83    /// another instance.
84    pub fn swap_weight(&mut self, other: &mut Self) {
85        self.weight_mut().swap(other.weight_mut());
86    }
87    /// consumes the current instance and applies the given function onto the weight,
88    /// returning a new instance with the same index and the resulting weight.
89    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Node<U, Idx> {
90        Node {
91            index: self.index,
92            weight: self.weight.map(f),
93        }
94    }
95}
96
97impl<T, Idx> AsRef<Weight<T>> for Node<T, Idx> {
98    fn as_ref(&self) -> &Weight<T> {
99        &self.weight
100    }
101}
102
103impl<T, Idx> AsMut<Weight<T>> for Node<T, Idx> {
104    fn as_mut(&mut self) -> &mut Weight<T> {
105        &mut self.weight
106    }
107}
108
109impl<T, Idx> core::borrow::Borrow<VertexId<Idx>> for Node<T, Idx> {
110    fn borrow(&self) -> &VertexId<Idx> {
111        &self.index
112    }
113}
114
115impl<T, Idx> core::fmt::Display for Node<T, Idx>
116where
117    Idx: core::fmt::Display,
118    T: core::fmt::Display,
119{
120    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
121        write!(
122            f,
123            "{{ index: {}, weight: {} }}",
124            self.index(),
125            self.weight()
126        )
127    }
128}