Skip to main content

Node

Struct Node 

Source
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 implement Debug, 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: N

The 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>

Source

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());
Source

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);
Source

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]);
Source

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);
Source

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.
  • None if 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()));
Source

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: Clone, N: Clone> Clone for Node<W, N>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<W: Debug, N: Debug> Debug for Node<W, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<W, N: Default> Default for Node<W, N>

Source§

fn default() -> Self

Returns a default-initialized Node.

The default node has:

  • data set to the default value of type N (requires N: Default).
  • neighbors as an empty vector.
  • attributes as an empty HashMap.
§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>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<W: Eq, N: Eq> Eq for Node<W, N>

Source§

impl<W: PartialEq, N: PartialEq> PartialEq for Node<W, N>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<W, N> Serialize for Node<W, N>
where W: Serialize, N: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<W: PartialEq, N: PartialEq> StructuralPartialEq for Node<W, N>

Auto Trait Implementations§

§

impl<W, N> Freeze for Node<W, N>
where N: Freeze, Vec<(NodeId, EdgeId, W)>: Freeze,

§

impl<W, N> RefUnwindSafe for Node<W, N>

§

impl<W, N> Send for Node<W, N>
where N: Send, Vec<(NodeId, EdgeId, W)>: Send,

§

impl<W, N> Sync for Node<W, N>
where N: Sync, Vec<(NodeId, EdgeId, W)>: Sync,

§

impl<W, N> Unpin for Node<W, N>
where N: Unpin, Vec<(NodeId, EdgeId, W)>: Unpin,

§

impl<W, N> UnsafeUnpin for Node<W, N>

§

impl<W, N> UnwindSafe for Node<W, N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V