use crate::gate::*;
use crate::mesh::meshes_equal;
use crate::mesh::*;
use crate::mesh_topo::topology::*;
#[derive(Clone)]
pub struct MeshWithTopology<'a, IntoIter>
where
IntoIter: Iterator<Item = Gate>,
{
mesh: &'a dyn Mesh<IntoIter = IntoIter>,
topology: &'a dyn Topology,
}
impl<'a, IntoIter> std::fmt::Debug for MeshWithTopology<'a, IntoIter>
where
IntoIter: Iterator<Item = Gate>,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MeshWithTopology")
.field("mesh", &"<dyn Mesh>")
.field("topology", &"<dyn Topology>")
.finish()
}
}
impl<'a, IntoIter> MeshWithTopology<'a, IntoIter>
where
IntoIter: Iterator<Item = Gate>,
{
pub fn new(
mesh: &'a impl Mesh<IntoIter = IntoIter>,
topology: &'a impl Topology,
) -> MeshWithTopology<'a, IntoIter> {
MeshWithTopology { mesh, topology }
}
}
impl<'a, IntoIter> Mesh for MeshWithTopology<'a, IntoIter>
where
IntoIter: Iterator<Item = Gate>,
{
type IntoIter = std::vec::IntoIter<Gate>;
fn successors(&self, from: usize, backward: bool) -> std::vec::IntoIter<Gate> {
let mut peers: Vec<Gate> = self
.mesh
.successors(from, backward)
.filter(|to| self.topology.allowed(from, to.target()).unwrap_or(false))
.collect();
if backward {
peers.reverse();
}
peers.into_iter()
}
fn len(&self) -> usize {
self.mesh.len()
}
}
impl<'a, IntoIter> PartialEq for MeshWithTopology<'a, IntoIter>
where
IntoIter: Iterator<Item = Gate>,
{
fn eq(&self, other: &Self) -> bool {
meshes_equal(self, other)
}
}
impl<'a, IntoIter> Eq for MeshWithTopology<'a, IntoIter> where IntoIter: Iterator<Item = Gate> {}