#1 Finished FileLinked implementation.
This commit is contained in:
parent
4252851b81
commit
feadba1a65
4 changed files with 63 additions and 28 deletions
|
@ -1,6 +1,7 @@
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
use super::tree;
|
use super::tree;
|
||||||
|
use super::file_linked::FileLinked;
|
||||||
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -40,15 +41,18 @@ fn build_tree(h: u32) -> Option<Box<tree::Tree<Uuid>>> {
|
||||||
/// TODO: Explain reasoning for bracket system against genetic algorithm.
|
/// TODO: Explain reasoning for bracket system against genetic algorithm.
|
||||||
pub fn run_bracket() {
|
pub fn run_bracket() {
|
||||||
let mut height = 1;
|
let mut height = 1;
|
||||||
let mut tree: tree::Tree<Uuid> = *build_tree(height).expect("Error getting result from build_tree.");
|
let mut tree = FileLinked::new(*build_tree(height).expect("Error getting result from build_tree"), "temp")
|
||||||
|
.expect("Unable to create file linked object from tree");
|
||||||
|
|
||||||
|
|
||||||
// Building tree one node at a time, appending to the top.
|
// Building tree one node at a time, appending to the top.
|
||||||
loop {
|
loop {
|
||||||
println!("=========================================");
|
println!("=========================================");
|
||||||
println!("Running bracket...");
|
println!("Running bracket...");
|
||||||
height += 1;
|
height += 1;
|
||||||
tree = tree::Tree::new(Uuid::new_v4(), Some(Box::new(tree)), build_tree(height));
|
tree.replace(tree::Tree::new(Uuid::new_v4(), Some(Box::new(tree.readonly().clone())), build_tree(height)))
|
||||||
tree.run_simulation();
|
.expect("Error building up tree node");
|
||||||
|
tree.readonly().run_simulation();
|
||||||
|
|
||||||
if height == 3 {
|
if height == 3 {
|
||||||
println!("{}\n\n", tree);
|
println!("{}\n\n", tree);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::fmt::Display;
|
use std::fmt;
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -10,12 +10,12 @@ pub struct FileLinked<T> {
|
||||||
path: String
|
path: String
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: FromStr + Display> FileLinked<T> {
|
impl<T> FileLinked<T> where T: FromStr + fmt::Display + Default {
|
||||||
pub fn from_file(path: &str) -> Result<FileLinked<T>, String> {
|
pub fn from_file(path: &str) -> Result<FileLinked<T>, String> {
|
||||||
let meta = fs::metadata(path)
|
let meta = fs::metadata(path);
|
||||||
.or(Err(format!("Path {} does not exist.", path)))?;
|
|
||||||
|
|
||||||
if meta.is_file() {
|
match &meta {
|
||||||
|
Ok(m) if m.is_file() => {
|
||||||
let mut file = fs::OpenOptions::new().read(true).open(path)
|
let mut file = fs::OpenOptions::new().read(true).open(path)
|
||||||
.or(Err(format!("Unable to open file {}", path)))?;
|
.or(Err(format!("Unable to open file {}", path)))?;
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
|
@ -28,26 +28,39 @@ impl<T: FromStr + Display> FileLinked<T> {
|
||||||
val,
|
val,
|
||||||
path: String::from(path)
|
path: String::from(path)
|
||||||
})
|
})
|
||||||
} else {
|
},
|
||||||
|
Ok(_) => {
|
||||||
Err(format!("{} is not a file.", path))
|
Err(format!("{} is not a file.", path))
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
let result = FileLinked {
|
||||||
|
val: T::default(),
|
||||||
|
path: String::from(path)
|
||||||
|
};
|
||||||
|
|
||||||
|
result.write_data()?;
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(val: T, path: &str) -> FileLinked<T> {
|
pub fn new(val: T, path: &str) -> Result<FileLinked<T>, String> {
|
||||||
let result = FileLinked {
|
let result = FileLinked {
|
||||||
val,
|
val,
|
||||||
path: String::from(path)
|
path: String::from(path)
|
||||||
};
|
};
|
||||||
|
|
||||||
result.write_data();
|
result.write_data()?;
|
||||||
|
|
||||||
result
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_data(&self) -> Result<(), String> {
|
pub fn write_data(&self) -> Result<(), String> {
|
||||||
let mut file = fs::OpenOptions::new()
|
let mut file = fs::OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.create_new(true)
|
.create(true)
|
||||||
|
.truncate(true)
|
||||||
.open(&self.path)
|
.open(&self.path)
|
||||||
.or(Err(format!("Unable to open path {}", self.path)))?;
|
.or(Err(format!("Unable to open path {}", self.path)))?;
|
||||||
|
|
||||||
|
@ -61,11 +74,23 @@ impl<T: FromStr + Display> FileLinked<T> {
|
||||||
&self.val
|
&self.val
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mutate<U, F: FnOnce(&mut T) -> U>(&mut self, op: F) -> U {
|
pub fn mutate<U, F: FnOnce(&mut T) -> U>(&mut self, op: F) -> Result<U, String> {
|
||||||
let result = op(&mut self.val);
|
let result = op(&mut self.val);
|
||||||
|
|
||||||
self.write_data();
|
self.write_data()?;
|
||||||
|
|
||||||
result
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn replace(&mut self, val: T) -> Result<(), String>{
|
||||||
|
self.val = val;
|
||||||
|
|
||||||
|
self.write_data()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Display> fmt::Display for FileLinked<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.val)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -28,6 +28,11 @@ fn main() {
|
||||||
println!("{} is a valid directory!", directory);
|
println!("{} is a valid directory!", directory);
|
||||||
println!("Building tree for {}.", directory);
|
println!("Building tree for {}.", directory);
|
||||||
bracket::run_bracket();
|
bracket::run_bracket();
|
||||||
|
|
||||||
|
println!("\n\nReading tree from temp file.");
|
||||||
|
let tree: file_linked::FileLinked<tree::Tree<uuid::Uuid>> = file_linked::FileLinked::from_file("temp")
|
||||||
|
.expect("Unable to read tree from existing file");
|
||||||
|
println!("Value read from file:\n{}", tree);
|
||||||
},
|
},
|
||||||
Ok(_) => println!("{} is not a valid directory!", directory),
|
Ok(_) => println!("{} is not a valid directory!", directory),
|
||||||
_ => println!("{} does not exist!", directory)
|
_ => println!("{} does not exist!", directory)
|
||||||
|
|
|
@ -2,6 +2,7 @@ use std::fmt;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
pub struct Tree<T> {
|
pub struct Tree<T> {
|
||||||
pub val: T,
|
pub val: T,
|
||||||
pub left: Option<Box<Tree<T>>>,
|
pub left: Option<Box<Tree<T>>>,
|
||||||
|
|
Loading…
Add table
Reference in a new issue