68 lines
1.8 KiB
Rust
68 lines
1.8 KiB
Rust
// use std::cell::RefCell;
|
|
// use std::rc::Rc;
|
|
mod graph;
|
|
use graph::Graph;
|
|
// use std::collections::HashMap;
|
|
// use std::iter::Map;
|
|
// use std::borrow::BorrowMut;
|
|
/*
|
|
struct Vertex {
|
|
name: String,
|
|
edges: RefCell<Vec<Vertex>>
|
|
}
|
|
|
|
struct Graph {
|
|
vertices: Vec<RefCell<Vertex>>
|
|
}
|
|
|
|
impl Graph {
|
|
fn new() -> Self{
|
|
return Graph { vertices: vec![] };
|
|
}
|
|
|
|
fn add_node(&mut self, name: String) {
|
|
self.vertices.push(Vertex { name: name, edges: RefCell::new(vec![]) }.into() );
|
|
}
|
|
|
|
fn vertices_count(&self) -> usize {
|
|
return self.vertices.len();
|
|
}
|
|
|
|
|
|
fn connect(&mut self, i: usize, _j: usize) {
|
|
// let nodej = Rc::clone(&self.vertices[j]);
|
|
// let nodei = Rc::clone(&self.vertices[i]);
|
|
// nodei.into_inner();
|
|
//nodei.edges.push(nodej);
|
|
// .unwrap().push(self.vertices[j]);
|
|
// Rc::into_inner(nodei).unwrap().edges.push(nodej);
|
|
// self.vertices[i].get.push(self.vertices[j]);
|
|
// let mut edges_new = self.vertices[i].edges.get_mut();
|
|
// let inner = self.vertices[j].take();
|
|
// self.vertices[i].get_mut().edges.borrow_mut().push(inner);
|
|
// let list = self.vertices[i].get_mut().edges.clone();
|
|
self.vertices[i].get_mut().edges.replace(vec![]);
|
|
}
|
|
|
|
}
|
|
*/
|
|
fn main() {
|
|
/*
|
|
let mut g = Graph::new();
|
|
g.add_node(String::from("Vertice #1"));
|
|
g.add_node(String::from("Vertice #2"));
|
|
g.add_node(String::from("Vertice #3"));
|
|
|
|
g.connect(0, 1);
|
|
g.connect(1, 2);
|
|
|
|
println!("Done, Vertices count: {}.", g.vertices_count());
|
|
*/
|
|
let mut g = Graph::new();
|
|
g.add_node(1, String::from("Node #1"));
|
|
g.add_node(2, String::from("Node #2"));
|
|
g.add_node(3, String::from("Node #3"));
|
|
g.connect(1,2);
|
|
g.connect(2,3);
|
|
}
|