Unfinished graph impl in rust.
parent
4463b79496
commit
7ce5794535
|
@ -1,19 +1,55 @@
|
|||
fn main() {
|
||||
let mut v = [4.1, -5.1, 1.1, -3.1, 2.1];
|
||||
v.sort_by(f64::total_cmp);
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// use std::borrow::BorrowMut;
|
||||
|
||||
println!("Hello, world!");
|
||||
for el in v {
|
||||
println!("Element {}.", el);
|
||||
}
|
||||
|
||||
let mut v = vec![5, 2, 1];
|
||||
v.push(2);
|
||||
v.push(5);
|
||||
|
||||
for (idx, elem) in v.iter().enumerate() {
|
||||
println!("Val: {}. {}.", idx, elem);
|
||||
}
|
||||
|
||||
println!("Done");
|
||||
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());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue