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