ai-0/lab3/src/algo/edges.rs

50 lines
1.1 KiB
Rust

use std::ops::{Index, IndexMut};
pub struct Edge {
pub vertex1_index: usize,
pub vertex2_index: usize,
pub length: f64,
pub ferment_intensity: f64,
}
enum EdgeCell {
Free { next_free_cell_id: Option<usize> },
Value { data: Edge },
}
pub struct EdgesVec {
storage: Vec<EdgeCell>,
}
impl EdgesVec {
pub fn new() -> Self {
return Self {
storage: Vec::new(),
};
}
}
impl Index<usize> for EdgesVec {
type Output = Edge;
fn index(&self, index: usize) -> &Self::Output {
match self.storage.get(index).expect("Edge index out of bounds") {
EdgeCell::Free { .. } => panic!("Referenced edge doesn't exists"),
EdgeCell::Value { data } => return data,
}
}
}
impl IndexMut<usize> for EdgesVec {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
match self
.storage
.get_mut(index)
.expect("Edge index out of bounds")
{
EdgeCell::Free { .. } => panic!("Referenced edge doesn't exists"),
EdgeCell::Value { data } => return data,
}
}
}