pub struct Node<W, N> {
pub data: N,
pub neighbors: Vec<(NodeId, EdgeId, W)>,
pub attributes: HashMap<String, String>,
}Expand description
A struct representing a graph node with data, neighbors, and attributes.
This struct is generic over the weight type W and node data type N, allowing flexibility
in how nodes are defined and used within a graph.
§Type Parameters
W: The type of the weight associated with edges to neighbors.N: The type of the data stored in the node (must implementDebug,Clone,PartialEq,Eq).
§Fields
data: The data associated with the node.neighbors: A list of neighboring nodes, represented as tuples of(NodeId, EdgeId, W). One entry is stored per incident edge, so parallel edges to the same neighbour appear as separate entries distinguished by their edge ID.attributes: A collection of additional attributes for the node, stored as key-value pairs.
§Examples
Creating a new node:
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let node = Node::<i32, &str>::new("City");
assert_eq!(node.data, "City");
assert!(node.neighbors.is_empty());
assert!(node.attributes.is_empty());Fields§
§data: NThe data associated with the node.
neighbors: Vec<(NodeId, EdgeId, W)>The list of neighbors, one entry per incident edge, as (neighbour ID, edge ID, weight).
attributes: HashMap<String, String>A collection of additional attributes for the node, represented as key-value pairs.
Implementations§
Source§impl<W, N> Node<W, N>
impl<W, N> Node<W, N>
Sourcepub fn new(data: N) -> Self
pub fn new(data: N) -> Self
Creates a new Node with the provided data, initializing neighbors and attributes as empty.
§Arguments
data: The data to associate with the node.
§Returns
A new Node instance with the given data, an empty neighbors list, and an empty attributes map.
§Examples
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let node = Node::<f64, i32>::new(42);
assert_eq!(node.data, 42);
assert!(node.neighbors.is_empty());
assert!(node.attributes.is_empty());Sourcepub fn add_neighbor(&mut self, neighbor_id: NodeId, edge_id: EdgeId, weight: W)
pub fn add_neighbor(&mut self, neighbor_id: NodeId, edge_id: EdgeId, weight: W)
Adds a neighbor to the node for one specific incident edge.
A separate entry is pushed for every edge, so calling this twice with the same
neighbor_id but different edge_ids records a pair of parallel edges.
§Arguments
neighbor_id: The ID of the neighboring node.edge_id: The ID of the edge connecting the two nodes.weight: The weight of the edge to the neighbor.
§Examples
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let mut node = Node::<i32, &str>::new("A");
node.add_neighbor(NodeId::from_index(1), EdgeId::from_index(0), 10);
assert_eq!(node.neighbors[0].0.index(), 1);Sourcepub fn neighbor_ids(&self) -> impl Iterator<Item = NodeId> + '_
pub fn neighbor_ids(&self) -> impl Iterator<Item = NodeId> + '_
Returns the IDs of the neighbours of this node, without edge IDs or weights.
A neighbour connected by several parallel edges is yielded once per edge; use a set if distinct neighbours are needed.
§Examples
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let mut node = Node::<i32, &str>::new("A");
node.add_neighbor(NodeId::from_index(1), EdgeId::from_index(0), 10);
node.add_neighbor(NodeId::from_index(1), EdgeId::from_index(1), 20);
let ids: Vec<_> = node.neighbor_ids().map(|id| id.index()).collect();
assert_eq!(ids, vec![1, 1]);Sourcepub fn degree(&self) -> usize
pub fn degree(&self) -> usize
Returns the degree of the node, counting parallel edges separately.
§Examples
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let mut node = Node::<i32, &str>::new("A");
node.add_neighbor(NodeId::from_index(1), EdgeId::from_index(0), 10);
node.add_neighbor(NodeId::from_index(1), EdgeId::from_index(1), 20);
assert_eq!(node.degree(), 2);Sourcepub fn get_attribute(&self, key: &str) -> Option<&String>
pub fn get_attribute(&self, key: &str) -> Option<&String>
Gets an attribute value by key.
§Arguments
key: The key of the attribute to retrieve.
§Returns
Some(&String)if the attribute exists.Noneif the attribute does not exist.
§Examples
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let mut node = Node::<i32, &str>::new("A");
node.attributes.insert("color".to_string(), "blue".to_string());
assert_eq!(node.get_attribute("color"), Some(&"blue".to_string()));Sourcepub fn set_attribute(&mut self, key: String, value: String) -> Option<String>
pub fn set_attribute(&mut self, key: String, value: String) -> Option<String>
Sets an attribute with the given key and value.
§Arguments
key: The key of the attribute to set.value: The value of the attribute to set.
§Returns
The value previously stored under key, if any.
§Examples
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let mut node = Node::<i32, &str>::new("A");
node.set_attribute("color".to_string(), "red".to_string());
assert_eq!(node.get_attribute("color"), Some(&"red".to_string()));Trait Implementations§
Source§impl<W, N: Default> Default for Node<W, N>
impl<W, N: Default> Default for Node<W, N>
Source§fn default() -> Self
fn default() -> Self
Returns a default-initialized Node.
The default node has:
dataset to the default value of typeN(requiresN: Default).neighborsas an empty vector.attributesas an emptyHashMap.
§Returns
A new Node instance with default values.
§Examples
use xgraph::core::{EdgeId, NodeId};
use xgraph::graph::node::Node;
let node: Node<i32, String> = Node::default();
assert_eq!(node.data, String::new());
assert!(node.neighbors.is_empty());
assert!(node.attributes.is_empty());Source§impl<'de, W, N> Deserialize<'de> for Node<W, N>where
W: Deserialize<'de>,
N: Deserialize<'de>,
impl<'de, W, N> Deserialize<'de> for Node<W, N>where
W: Deserialize<'de>,
N: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl<W: Eq, N: Eq> Eq for Node<W, N>
impl<W: PartialEq, N: PartialEq> StructuralPartialEq for Node<W, N>
Auto Trait Implementations§
impl<W, N> Freeze for Node<W, N>
impl<W, N> RefUnwindSafe for Node<W, N>
impl<W, N> Send for Node<W, N>
impl<W, N> Sync for Node<W, N>
impl<W, N> Unpin for Node<W, N>
impl<W, N> UnsafeUnpin for Node<W, N>
impl<W, N> UnwindSafe for Node<W, N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more