Skip to main content

Graph

Struct Graph 

Source
pub struct Graph<'a> { /* private fields */ }
Expand description

The entity graph of a parsed file, with forward and back references.

Nodes are positions in Exchange::instances: node n is exchange.instances()[n]. Each index lists a neighbour once, in ascending node order, however many times the reference is repeated.

The back index is load-bearing, not an optimisation: a product’s shape, colours and placement all point at the product, so most questions about a product are answered by referenced_by. See docs/ARCHITECTURE.md.

Implementations§

Source§

impl<'a> Graph<'a>

Source

pub fn new(exchange: Exchange<'a>) -> Result<Self>

Builds both reference indices, failing on a dangling reference.

Use this for anything that transforms or extracts: a dangling reference means the file is already missing data, and every graph operation on it would silently produce a wrong answer.

§Errors

Returns Error::UnresolvedReference for the first reference, in file order, to an instance that is not defined.

Source

pub fn build(exchange: Exchange<'a>) -> Self

Builds both reference indices, recording dangling references instead of failing.

Use this where a partial answer is still useful, such as reporting every problem in a file. Dangling references are left out of both indices and listed by unresolved.

Source

pub fn exchange(&self) -> &Exchange<'a>

The parsed file this graph indexes.

Source

pub fn len(&self) -> usize

The number of nodes, which is the number of instances.

Source

pub fn is_empty(&self) -> bool

True if the file has no instances.

Source

pub fn node(&self, id: u64) -> Option<usize>

The node for instance #id.

Source

pub fn instance(&self, node: usize) -> &Instance

The instance at node.

§Panics

Panics if node >= self.len().

Source

pub fn references(&self, node: usize) -> &[usize]

The nodes node refers to.

§Panics

Panics if node >= self.len().

Source

pub fn referenced_by(&self, node: usize) -> &[usize]

The nodes that refer to node.

§Panics

Panics if node >= self.len().

Source

pub fn unresolved(&self) -> &[(usize, u64)]

References to undefined instances, as (node, missing #id) in file order. Always empty for a graph from new.

Trait Implementations§

Source§

impl Debug for Graph<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Graph<'a>

§

impl<'a> RefUnwindSafe for Graph<'a>

§

impl<'a> Send for Graph<'a>

§

impl<'a> Sync for Graph<'a>

§

impl<'a> Unpin for Graph<'a>

§

impl<'a> UnsafeUnpin for Graph<'a>

§

impl<'a> UnwindSafe for Graph<'a>

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 = !

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.