smol-graph
A simple and to-the-point Rust graph implementation.
It uses Uuid for its indices (inside newtypes).
This means it is possible to generate an id that already exists,
but the chance is essentially 0.
This crate also provides a prelude that exports all of the
types used in this crate: Graph, NodeIndex,
andEdgeIndex.
Examples
Usage: First add it to your Cargo.toml, then use it in your project.
// Import the few types this library exports.
use *;
// Or do it explicitely.
use ;
Creating a new graph:
use Graph;
// Convience function:
let graph: = new;
// Or alternately, explicit field filling.
// This could be used for with capacity or prefilled maps, for example.
use HashMap;
let graph: = Graph ;
Inserting nodes and edges:
use ;
let mut graph: = new;
// Insert nodes and get their indices.
let one = graph.node;
let two = graph.node;
// or do it with field access
let three = ;
// And then for edges:
let a = graph.edge;
// Or with field access:
let b = ;
Removing nodes or edges:
use ;
let mut graph = new;
let a = graph.node;
let b = graph.node;
let edge = graph.edge;
// Watch out, the edge still points to the node that doesn't exist.
graph.r_node.unwrap;
graph.r_node.unwrap;
// And then remove the edge.
graph.r_edge.unwrap;
Notes
-
It uses Uuid for indices. This does mean that, though virtually impossible, a duplicate index could be generated. For more information on this, see https://en.wikipedia.org/wiki/Universally_unique_identifier.
-
The graph is backed by the std hashmap for both nodes and edges. While that may not be the best in all cases, it's certainly one of the easiest.
-
It doesn't check for edges pointing to valid nodes. While it would be possible to implement that, this library doesn't aim to do anything beyond have a small and easy type for a graph. Checking for nodes requires design decisions, and that is left up to the user.