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,
impl<K, N, E> Node<K, N, E>where K: Clone + Hash + PartialEq + Eq + Display, N: Clone, E: Clone,
sourcepub fn new(key: K, value: N) -> Self
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');
sourcepub fn key(&self) -> &K
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);
sourcepub fn value(&self) -> &N
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');
sourcepub fn degree(&self) -> usize
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);
sourcepub fn connect(&self, other: &Self, value: E)
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()));
sourcepub fn try_connect(&self, other: &Node<K, N, E>, value: E) -> Result<(), Error>
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())),
}
sourcepub fn disconnect(&self, other: &K) -> Result<E, Error>
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()));
sourcepub fn isolate(&self)
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());
sourcepub fn is_orphan(&self) -> bool
pub fn is_orphan(&self) -> bool
Returns true if the node is an oprhan. Orphan nodes are nodes that have no connections.
sourcepub fn is_connected(&self, other: &K) -> bool
pub fn is_connected(&self, other: &K) -> bool
Returns true if the node is connected to another node with a given key.
sourcepub fn find_adjacent(&self, other: &K) -> Option<Node<K, N, E>>
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.
sourcepub fn order(&self) -> Order<'_, K, N, E>
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.
sourcepub fn dfs(&self) -> Dfs<'_, K, N, E>
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.
sourcepub fn bfs(&self) -> Bfs<'_, K, N, E>
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.
sourcepub fn pfs(&self) -> Pfs<'_, K, N, E>where
N: Ord,
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.
sourcepub fn iter(&self) -> NodeIterator<'_, K, N, E> ⓘ
pub fn iter(&self) -> NodeIterator<'_, K, N, E> ⓘ
Returns an iterator over the node’s adjacent edges.
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,
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§impl<K, N, E> Deref for Node<K, N, E>where
K: Clone + Hash + Display + PartialEq + Eq,
N: Clone,
E: Clone,
impl<K, N, E> Deref for Node<K, N, E>where K: Clone + Hash + Display + PartialEq + Eq, N: Clone, E: Clone,
source§impl<'a, K, N, E> IntoIterator for &'a Node<K, N, E>where
K: Clone + Hash + Display + PartialEq + Eq,
N: Clone,
E: Clone,
impl<'a, K, N, E> IntoIterator for &'a Node<K, N, E>where K: Clone + Hash + Display + PartialEq + Eq, N: Clone, E: Clone,
source§impl<K, N, E> Ord for Node<K, N, E>where
K: Clone + Hash + PartialEq + Display + Eq,
N: Clone + Ord,
E: Clone,
impl<K, N, E> Ord for Node<K, N, E>where K: Clone + Hash + PartialEq + Display + Eq, N: Clone + Ord, E: Clone,
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,
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§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,
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,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more