mod api;
mod test;
use alloc::collections::{BTreeMap, BTreeSet};
use core::default::Default;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub use api::*;
use crate::error::Error;
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BTreeNetwork<T>
where
T: Ord,
{
vertices: BTreeMap<T, BTreeSet<T>>,
}
impl<T> BTreeNetwork<T>
where
T: Ord,
{
pub fn new() -> Self {
let vertices: BTreeMap<T, BTreeSet<T>> = BTreeMap::new();
BTreeNetwork { vertices }
}
}
impl<T> Default for BTreeNetwork<T>
where
T: Ord,
{
fn default() -> Self {
Self::new()
}
}
impl<T> Vertices<T> for BTreeNetwork<T>
where
T: Ord,
{
fn vertices(&self) -> BTreeSet<&T> {
self.vertices.keys().collect()
}
}
impl<T> AddVertex<T> for BTreeNetwork<T>
where
T: Ord,
{
fn add_vertex(&mut self, x: T) -> Option<BTreeSet<T>> {
self.vertices.insert(x, BTreeSet::new())
}
}
impl<T> AddEdge<T> for BTreeNetwork<T>
where
T: Ord + Clone,
{
type Error = Error;
fn add_edge(&mut self, x: T, y: T) -> Result<(), Self::Error> {
if let Some(adj_y) = self.vertices.get(&y) {
if let Some(adj_x) = self.vertices.get(&x) {
let mut adj_y: BTreeSet<T> = adj_y.clone();
adj_y.insert(x.clone());
let mut adj_x: BTreeSet<T> = adj_x.clone();
adj_x.insert(y.clone());
self.vertices.insert(x, adj_x);
self.vertices.insert(y, adj_y);
return Ok(());
}
}
Err(Error::VertexDoesNotExist)
}
}
impl<T> GetVertexValue<T> for BTreeNetwork<T>
where
T: Ord,
{
fn get_vertex_value(&self, v: T) -> Option<&BTreeSet<T>> {
self.vertices.get(&v)
}
}
impl<T> RemoveEdge<T> for BTreeNetwork<T>
where
T: Ord + Clone,
{
type Error = Error;
fn remove_edge(&mut self, x: T, y: T) -> Result<(), Self::Error> {
if let Some(adj_x) = self.vertices.get(&x) {
if let Some(adj_y) = self.vertices.get(&y) {
let mut adj_x = adj_x.clone();
adj_x.remove(&y);
let mut adj_y = adj_y.clone();
adj_y.remove(&x);
self.vertices.insert(x, adj_x);
self.vertices.insert(y, adj_y);
return Ok(());
}
}
Err(Error::VertexDoesNotExist)
}
}
impl<T> RemoveVertex<T> for BTreeNetwork<T>
where
T: Ord + Clone,
{
type Error = Error;
fn remove_vertex(&mut self, x: T) -> Result<(), Self::Error> {
if let Some(adj_x) = self.vertices.get(&x) {
adj_x
.clone()
.into_iter()
.try_for_each(|y| self.remove_edge(x.clone(), y))?;
self.vertices.remove(&x);
return Ok(());
}
Err(Error::VertexDoesNotExist)
}
}
impl<T> Adjacent<T> for BTreeNetwork<T>
where
T: Ord,
{
type Error = Error;
fn adjacent(&self, x: T, y: T) -> Result<bool, Self::Error> {
if let Some(adj_y) = self.vertices.get(&y) {
if let Some(adj_x) = self.vertices.get(&x) {
if adj_y.contains(&x) && adj_x.contains(&y) {
return Ok(true);
}
return Ok(false);
}
}
Err(Error::VertexDoesNotExist)
}
}
impl<T> Connections<T> for BTreeNetwork<T>
where
T: Ord,
{
fn connections(&self, x: T) -> Option<&BTreeSet<T>> {
self.vertices.get(&x)
}
}