Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 19, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/ba4809449bf9adbfb523ab63ebcca45d to your computer and use it in GitHub Desktop.
Save rust-play/ba4809449bf9adbfb523ab63ebcca45d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
let mut g = Graph {
nodePointer: &mut 0,
edgePointer: &mut 0,
nodes: &mut Vec::new(),
edges: &mut Vec::new(),
};
let node_id_1 = g.add_node((1, 1));
let node_id_2 = g.add_node((1, 1));
{
let node_1 = g.get_node(node_id_1);
}
{
let node_2 = g.get_node(node_id_2);
}
}
pub struct Graph<'a> {
pub nodePointer: &'a mut usize,
pub edgePointer: &'a mut usize,
pub nodes: &'a mut Vec<Node>,
pub edges: &'a mut Vec<Edge>
}
impl<'a> Graph<'a> {
pub fn add_node(&mut self, data: (u64, u64)) -> usize {
let id: usize = *self.nodePointer;
self.nodes.push(Node {
id: id,
datum: data
});
*self.nodePointer += 1;
return id;
}
pub fn get_node(&'a mut self, id: usize) -> &Node {
return &self.nodes[id]
}
pub fn add_edge(&'a mut self, source: u64, target: u64, weight: u16) -> usize {
let id: usize = *self.nodePointer;
self.edges.push(Edge {
id: id,
source,
target,
weight
});
*self.edgePointer = *self.edgePointer + 1;
return id;
}
}
pub struct Node {
pub id: usize,
pub datum: (u64, u64)
}
pub struct Edge {
pub id: usize,
pub source: u64,
pub target: u64,
pub weight: u16
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment