cpm_rs/
path.rs

1use crate::customtask::CustomTask;
2
3/// Represents a path of tasks.
4/// It is a copy of an original path in the graph.
5#[derive(Debug, Clone)]
6pub struct Path<T>
7where T: From<i8>
8	+ std::clone::Clone
9	+ std::marker::Copy
10	+ std::ops::Sub::<Output = T>
11	+ std::ops::Add<Output = T>
12	+ std::fmt::Display
13	+ std::fmt::Debug
14	+ std::cmp::PartialOrd
15	+ std::ops::AddAssign
16{
17	/// Vector of tasks.
18	tasks: Vec<CustomTask<T>>,
19}
20
21impl <T> Path<T>
22where T: From<i8>
23	+ std::clone::Clone
24	+ std::marker::Copy
25	+ std::ops::Sub::<Output = T>
26	+ std::ops::Add<Output = T>
27	+ std::fmt::Display
28	+ std::fmt::Debug
29	+ std::cmp::PartialOrd
30	+ std::ops::AddAssign
31{
32	pub fn new() -> Self {
33		Path {
34			tasks: vec!{},
35		}
36	}
37
38	pub fn new_from_vec(_tasks: Vec<CustomTask<T>>) -> Self {
39		Path {
40			tasks: _tasks.clone(),
41		}
42	}
43
44	pub fn add_task(&mut self, task: &CustomTask<T>) {
45		self.tasks.push(task.clone());
46	}
47
48	pub fn join_path(&mut self, path: &Path<T>) {
49		for task in &path.tasks {
50			self.add_task(&task);
51		}
52	}
53
54	/// Gets the total float of the path.
55	/// If the total float is zero, than the path is probably a
56	/// critical path.
57	pub fn get_total_float(&self) -> T {
58		let mut total_float: T = 0.into();
59		for task in &self.tasks {
60			total_float += task.get_total_float().unwrap(); // TODO check this unwrap
61		}
62		total_float
63	}
64
65	/// Reverse the order of the tasks in the path.
66	/// It does not change the order of the tasks in the Scheduler!
67	pub fn reverse_tasks(&mut self) {
68		self.tasks.reverse();
69	}
70
71	/// Returns a text that shows the order of tasks in the path.
72	pub fn get_path_string(&self) -> String {
73		let mut path_string: String = "".to_string();
74		for task_idx in 0..self.tasks.len() {
75			path_string = format!(
76				"{}{}({})->"
77				, path_string
78				, self.tasks[task_idx].get_id()
79				, self.tasks[task_idx].get_duration()
80			);
81		}
82		path_string
83	}
84
85	/// Gets the duration of the path.
86	pub fn get_dur(&self) -> T {
87		let mut dur = 0.into();
88		for task in &self.tasks {
89			dur += task.get_duration()
90		}
91		dur
92	}
93
94}
95