Graph

Struct Graph 

Source
pub struct Graph<const SIZE: usize> { /* private fields */ }
Expand description

Compile time graphs.

use const_graphs::Graph;

const SIZE: usize = 1_000;
// You can use const.
const graph1: Graph<SIZE> = Graph::new();

// And, static.
static mut graph2: Graph<SIZE> = Graph::new();
unsafe {
  graph2.add_edge(0, 1);
  assert!(graph2.has_edge(0, 1));
}

// And, of course, let too:
let graph3 = Graph::<SIZE>::new();

Implementations§

Source§

impl<const SIZE: usize> Graph<SIZE>

Source

pub const fn add_edge(&mut self, i: usize, j: usize)

Add an edge to the graph between i and j.

use const_graphs::Graph;

let mut graph = Graph::<10>::new();
graph.add_edge(0, 1);
assert!(graph.has_edge(0, 1));

See also Graph::add_edge_undirected.

Source

pub const fn add_edge_undirected(&mut self, i: usize, j: usize)

Add an undirected edge to the graph between i and j.

use const_graphs::Graph;

let mut graph = Graph::<10>::new();
graph.add_edge_undirected(0, 1);
assert!(graph.has_edge(0, 1));
assert!(graph.has_edge(1, 0));

See also Graph::add_edge.

Source

pub const fn remove_edge(&mut self, i: usize, j: usize)

Remove an edge from the graph between i and j.

use const_graphs::Graph;

let mut graph = Graph::<10>::new();
graph.add_edge(0, 1);
graph.remove_edge(0, 1);
assert!(!graph.has_edge(0, 1));

See also Graph::remove_edge_undirected.

Source

pub const fn remove_edge_undirected(&mut self, i: usize, j: usize)

Remove an undirected edge from the graph between i and j.

use const_graphs::Graph;

let mut graph = Graph::<10>::new();
graph.add_edge_undirected(0, 1);
graph.remove_edge_undirected(0, 1);
assert!(!graph.has_edge(0, 1));
assert!(!graph.has_edge(1, 0));

See also Graph::remove_edge.

Source

pub const fn has_edge(&self, i: usize, j: usize) -> bool

Checks whether there is an edge between i and j.

use const_graphs::Graph;

let mut graph = Graph::<10>::new();
// The graph is initialized empty.
assert!(!graph.has_edge(0, 1));
Source

pub const fn get_edges(&self, vertex: usize) -> &[bool; SIZE]

Returns an array where the ith element is a boolean representing whether there is an edge between vertex and i.

use const_graphs::Graph;

let mut graph = Graph::<3>::new();
graph.add_edge(0, 2);
assert_eq!(graph.get_edges(0), &[false, false, true]);

See also Graph::get_inverse_edges.

Source

pub const fn get_inverse_edges(&self, vertex: usize) -> [bool; SIZE]

Returns an array where the ith element is a boolean representing whether there is an edge between i and vertex. This is useful in a few graph algorithms where you need to know which vertices “point” to the current, and not the contrary.

use const_graphs::Graph;

let mut graph = Graph::<3>::new();
graph.add_edge(0, 2);
assert_eq!(
  graph.get_inverse_edges(2),
  [true, false, false]
);

See also Graph::get_edges.

Source

pub const fn max_number_of_edges(&self) -> usize

Returns the maximum number of edges of a graph.

use const_graphs::Graph;

let graph = Graph::<3>::new();
// The possible edges are:
// 0 -> 1
// 0 -> 2
// 1 -> 0
// 1 -> 2
// 2 -> 0
// 2 -> 1
assert_eq!(graph.max_number_of_edges(), 6);
Source

pub const fn density(&self) -> f32

Returns the density of a graph, that is, the ratio between the number of edges and the maximum number of possible edges.

use const_graphs::Graph;

let mut graph = Graph::<3>::new();
graph.add_edge_undirected(0, 1);
graph.add_edge_undirected(0, 2);
graph.add_edge_undirected(1, 2);
assert_eq!(graph.density(), 1.0);
Source

pub const fn clear(&mut self)

Remove all edges from the graph.

use const_graphs::Graph;

let mut graph = Graph::<3>::new();
graph.add_edge_undirected(0, 1);
graph.add_edge_undirected(0, 2);
graph.add_edge_undirected(1, 2);
graph.clear();

assert_eq!(graph.density(), 0.0);
Source

pub const fn new() -> Graph<SIZE>

Creates a new graph.

use const_graphs::Graph;

const SIZE: usize = 10;
let graph = Graph::<SIZE>::new();

Auto Trait Implementations§

§

impl<const SIZE: usize> Freeze for Graph<SIZE>

§

impl<const SIZE: usize> RefUnwindSafe for Graph<SIZE>

§

impl<const SIZE: usize> Send for Graph<SIZE>

§

impl<const SIZE: usize> Sync for Graph<SIZE>

§

impl<const SIZE: usize> Unpin for Graph<SIZE>

§

impl<const SIZE: usize> UnwindSafe for Graph<SIZE>

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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