Struct gdsl::sync_ungraph::Node

source ·
pub struct Node<K = usize, N = (), E = ()>where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone,{ /* private fields */ }
Expand description

A Node<K, N, E> is a key value pair smart-pointer, which includes inbound and outbound connections to other nodes. Nodes can be created individually and they don’t depend on a graph container. Generic parameters include K for the node’s key, N for the node’s value, and E for the edge’s value. Two nodes are equal if they have the same key.

Example

use gdsl::sync_ungraph::*;

let a = Node::new(0x1, "A");
let b = Node::new(0x2, "B");
let c = Node::new(0x4, "C");

a.connect(&b, 0.42);
a.connect(&c, 1.7);
b.connect(&c, 0.09);
c.connect(&b, 12.9);

let Edge(u, v, e) = a.iter().next().unwrap();

assert!(u == a);
assert!(v == b);
assert!(e == 0.42);

Implementations§

source§

impl<K, N, E> Node<K, N, E>where K: Clone + Hash + PartialEq + Eq + Display, N: Clone, E: Clone,

source

pub fn new(key: K, value: N) -> Self

Creates a new node with a given key and value. The key is used to identify the node in the graph. Two nodes with the same key are considered equal. Value is optional, node use’s () as default value type.

Example
use gdsl::sync_ungraph::*;

let n1 = Node::<i32, char, ()>::new(1, 'A');

assert!(*n1.key() == 1);
assert!(*n1.value() == 'A');
source

pub fn key(&self) -> &K

Returns a reference to the node’s key.

Example
use gdsl::sync_ungraph::*;

let n1 = Node::<i32, (), ()>::new(1, ());

assert!(*n1.key() == 1);
source

pub fn value(&self) -> &N

Returns a reference to the node’s value.

Example
use gdsl::sync_ungraph::*;

let n1 = Node::<i32, char, ()>::new(1, 'A');

assert!(*n1.value() == 'A');
source

pub fn degree(&self) -> usize

Returns the degree of the node. The degree is the number of adjacent edges.

Example
use gdsl::digraph::*;

let a = Node::new(0x1, "A");
let b = Node::new(0x2, "B");
let c = Node::new(0x4, "C");

a.connect(&b, 0.42);
a.connect(&c, 1.7);

assert!(a.out_degree() == 2);
source

pub fn connect(&self, other: &Self, value: E)

Connects this node to another node. The connection is created in both directions. The connection is created with the given edge value and defaults to (). This function allows for creating multiple connections between the same nodes.

Example
use gdsl::sync_ungraph::*;

let n1 = Node::new(1, ());
let n2 = Node::new(2, ());

n1.connect(&n2, 4.20);

assert!(n1.is_connected(n2.key()));
source

pub fn try_connect(&self, other: &Node<K, N, E>, value: E) -> Result<(), Error>

Connects this node to another node. The connection is created in both directions. The connection is created with the given edge value and defaults to (). This function doesn’t allow for creating multiple connections between the same nodes. Returns Ok(()) if the connection was created, Err(EdgeValue) if the connection already exists.

Example
use gdsl::sync_ungraph::*;

let n1 = Node::new(1, ());
let n2 = Node::new(2, ());

match n1.try_connect(&n2, ()) {
    Ok(_) => assert!(n1.is_connected(n2.key())),
    Err(_) => panic!("n1 should be connected to n2"),
}

match n1.try_connect(&n2, ()) {
    Ok(_) => panic!("n1 should be connected to n2"),
    Err(_) => assert!(n1.is_connected(n2.key())),
}
source

pub fn disconnect(&self, other: &K) -> Result<E, Error>

Disconnect two nodes from each other. The connection is removed in both directions. Returns Ok(EdgeValue) if the connection was removed, Err(()) if the connection doesn’t exist.

Example
use gdsl::sync_ungraph::*;

let n1 = Node::new(1, ());
let n2 = Node::new(2, ());

n1.connect(&n2, ());

assert!(n1.is_connected(n2.key()));

if n1.disconnect(n2.key()).is_err() {
    panic!("n1 should be connected to n2");
}

assert!(!n1.is_connected(n2.key()));
source

pub fn isolate(&self)

Removes all inbound and outbound connections to and from the node.

Example
use gdsl::sync_ungraph::*;

let n1 = Node::new(1, ());
let n2 = Node::new(2, ());
let n3 = Node::new(3, ());
let n4 = Node::new(4, ());

n1.connect(&n2, ());
n1.connect(&n3, ());
n1.connect(&n4, ());
n2.connect(&n1, ());
n3.connect(&n1, ());
n4.connect(&n1, ());

assert!(n1.is_connected(n2.key()));
assert!(n1.is_connected(n3.key()));
assert!(n1.is_connected(n4.key()));

n1.isolate();

assert!(n1.is_orphan());
source

pub fn is_orphan(&self) -> bool

Returns true if the node is an oprhan. Orphan nodes are nodes that have no connections.

source

pub fn is_connected(&self, other: &K) -> bool

Returns true if the node is connected to another node with a given key.

source

pub fn find_adjacent(&self, other: &K) -> Option<Node<K, N, E>>

Get a pointer to an adjacent node with a given key. Returns None if no node with the given key is found from the node’s adjacency list.

source

pub fn order(&self) -> Order<'_, K, N, E>

Returns an iterator-like object that can be used to map, filter and collect reachable nodes or edges in different orderings such as postorder or preorder.

source

pub fn dfs(&self) -> Dfs<'_, K, N, E>

Returns an iterator-like object that can be used to map, filter, search and collect nodes or edges resulting from a depth-first search.

source

pub fn bfs(&self) -> Bfs<'_, K, N, E>

Returns an iterator-like object that can be used to map, filter, search and collect nodes or edges resulting from a breadth-first search.

source

pub fn pfs(&self) -> Pfs<'_, K, N, E>where N: Ord,

Returns an iterator-like object that can be used to map, filter, search and collect nodes or edges resulting from a priotity-first search.

source

pub fn iter(&self) -> NodeIterator<'_, K, N, E>

Returns an iterator over the node’s adjacent edges.

source

pub fn sizeof(&self) -> usize

Trait Implementations§

source§

impl<K, N, E> Clone for Node<K, N, E>where K: Clone + Hash + PartialEq + Eq + Display + Clone, N: Clone + Clone, E: Clone + Clone,

source§

fn clone(&self) -> Node<K, N, E>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<K, N, E> Deref for Node<K, N, E>where K: Clone + Hash + Display + PartialEq + Eq, N: Clone, E: Clone,

§

type Target = N

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<'a, K, N, E> IntoIterator for &'a Node<K, N, E>where K: Clone + Hash + Display + PartialEq + Eq, N: Clone, E: Clone,

§

type Item = Edge<K, N, E>

The type of the elements being iterated over.
§

type IntoIter = NodeIterator<'a, K, N, E>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, N, E> Ord for Node<K, N, E>where K: Clone + Hash + PartialEq + Display + Eq, N: Clone + Ord, E: Clone,

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<K, N, E> PartialEq<Node<K, N, E>> for Node<K, N, E>where K: Clone + Hash + Display + PartialEq + Eq, N: Clone, E: Clone,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, N, E> PartialOrd<Node<K, N, E>> for Node<K, N, E>where K: Clone + Hash + PartialEq + Display + Eq, N: Clone + Ord, E: Clone,

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<K, N, E> Eq for Node<K, N, E>where K: Clone + Hash + Display + PartialEq + Eq, N: Clone, E: Clone,

Auto Trait Implementations§

§

impl<K, N, E> RefUnwindSafe for Node<K, N, E>where K: RefUnwindSafe, N: RefUnwindSafe,

§

impl<K, N, E> Send for Node<K, N, E>where E: Send + Sync, K: Send + Sync, N: Send + Sync,

§

impl<K, N, E> Sync for Node<K, N, E>where E: Send + Sync, K: Send + Sync, N: Send + Sync,

§

impl<K, N, E> Unpin for Node<K, N, E>

§

impl<K, N, E> UnwindSafe for Node<K, N, E>where K: RefUnwindSafe, N: RefUnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.