From 508e716a6a437f1566de68e5d630ecdcb1550c81 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 4 Aug 2024 19:27:36 +0200 Subject: [PATCH] repr tree: split leaf & node into separate files --- lib-nested-core/src/repr_tree/leaf.rs | 216 +++++++++++ lib-nested-core/src/repr_tree/mod.rs | 518 +------------------------- lib-nested-core/src/repr_tree/node.rs | 348 +++++++++++++++++ 3 files changed, 568 insertions(+), 514 deletions(-) create mode 100644 lib-nested-core/src/repr_tree/leaf.rs create mode 100644 lib-nested-core/src/repr_tree/node.rs diff --git a/lib-nested-core/src/repr_tree/leaf.rs b/lib-nested-core/src/repr_tree/leaf.rs new file mode 100644 index 0000000..6a12440 --- /dev/null +++ b/lib-nested-core/src/repr_tree/leaf.rs @@ -0,0 +1,216 @@ +use { + r3vi::{ + view::{ + ViewPort, OuterViewPort, + AnyViewPort, AnyInnerViewPort, AnyOuterViewPort, + port::UpdateTask, + View, Observer, + singleton::*, + sequence::*, + list::* + }, + buffer::{singleton::*, vec::*} + }, + laddertypes::{TypeTerm}, + std::{ + collections::HashMap, + sync::{Arc, RwLock}, + any::Any + }, +}; + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +#[derive(Clone)] +pub struct ReprLeaf { + out_port: AnyViewPort, + in_port: AnyInnerViewPort, + data: Option< Arc >, + + /// keepalive for the observer that updates the buffer from in_port + keepalive: Option>, + in_keepalive: Option>, +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +impl ReprLeaf { + pub fn from_view( src_port: OuterViewPort ) -> Self + where V: View + ?Sized + 'static, + V::Msg: Clone + { + let mut in_port = ViewPort::::new(); + let in_keepalive = in_port.attach_to(src_port); + + let mut out_port = ViewPort::::new(); + let out_keepalive = out_port.attach_to(in_port.outer()); + + ReprLeaf { + keepalive: Some(out_keepalive), + in_keepalive: Some(in_keepalive), + in_port: in_port.inner().into(), + out_port: out_port.into(), + data: None, + } + } + + pub fn detach(&mut self) + where V: View + ?Sized + 'static, + V::Msg: Clone + { + self.keepalive = None; + self.in_keepalive = None; + + let ip = self.in_port.clone() + .downcast::().ok() + .unwrap(); + ip.0.detach(); + + if self.data.is_none() { + let mut op = self.out_port.clone() + .downcast::().ok() + .unwrap(); + + op.detach(); + self.keepalive = Some(op.attach_to(ip.0.outer())); + } + } + + pub fn detach_vec(&mut self) + where Item: Clone + Send + Sync + 'static + { + self.keepalive = None; + self.in_keepalive = None; + + let ip = self.in_port.clone() + .downcast::>().ok() + .unwrap(); + + ip.0.detach(); + + if let Some(data) = self.data.as_mut() { + let mut op = self.out_port.clone() + .downcast::>>().ok() + .unwrap(); + op.detach(); + + let data = data.clone().downcast::< RwLock> >().ok().unwrap(); + let buffer = VecBuffer::with_data_arc_port(data, op.inner()); + self.keepalive = Some(buffer.attach_to(ip.0.outer())) + } + } + + pub fn attach_to(&mut self, src_port: OuterViewPort) + where V: View + ?Sized + 'static, + V::Msg: Clone + { + self.in_keepalive = Some(self.in_port.clone() + .downcast::().ok().unwrap() + .0.attach_to( src_port )); + } + + pub fn from_singleton_buffer( buffer: SingletonBuffer ) -> Self + where T: Clone + Send + Sync + 'static + { + let in_port = ViewPort::>::new(); + ReprLeaf { + in_keepalive: None, + keepalive: Some(buffer.attach_to(in_port.outer())), + in_port: in_port.inner().into(), + out_port: buffer.get_port().0.into(), + data: Some(buffer.into_inner()) + } + } + + pub fn from_vec_buffer( buffer: VecBuffer ) -> Self + where T: Clone + Send + Sync + 'static + { + let in_port = ViewPort::< dyn ListView >::new(); + ReprLeaf { + in_keepalive: None, + keepalive: Some(buffer.attach_to(in_port.outer())), + in_port: in_port.inner().into(), + out_port: buffer.get_port().0.into(), + data: Some(buffer.into_inner()) + } + } + + pub fn as_singleton_buffer(&mut self) -> Option> + where T: Clone + Send + Sync + 'static + { + let sgl_port = self.get_port::< dyn SingletonView >().unwrap().0; + + let data_arc = + if let Some(data) = self.data.as_ref() { + data.clone().downcast::>().ok() + } else { + sgl_port.update(); + let value = sgl_port.outer().get_view().unwrap().get(); + eprintln!("make new data ARC from old value"); + Some(Arc::new(RwLock::new( value ))) + }; + + if let Some(data_arc) = data_arc { + self.data = Some(data_arc.clone() as Arc); + let buf = SingletonBuffer { + value: data_arc, + port: sgl_port.inner() + }; + self.keepalive = Some(buf.attach_to( + self.in_port.0.clone() + .downcast::>() + .ok().unwrap() + .outer() + )); + Some(buf) + } else { + None + } + } + + pub fn as_vec_buffer(&mut self) -> Option> + where T: Clone + Send + Sync + 'static + { + let vec_port = self.get_port::< RwLock> >().unwrap().0; + + let data_arc = + if let Some(data) = self.data.as_ref() { + data.clone().downcast::>>().ok() + } else { + vec_port.update(); + if let Some(value) = vec_port.outer().get_view() { + let value = value.read().unwrap().clone(); + eprintln!("make new data ARC from old VECTOR-value"); + Some(Arc::new(RwLock::new( value ))) + } else { + eprintln!("no data vec"); + Some(Arc::new(RwLock::new( Vec::new() ))) +// None + } + }; + + if let Some(data_arc) = data_arc { + self.data = Some(data_arc.clone() as Arc); + let buf = VecBuffer::with_data_arc_port(data_arc, vec_port.inner()); + self.keepalive = Some(buf.attach_to( + self.in_port.0.clone() + .downcast::< dyn ListView >() + .ok().unwrap() + .outer() + )); + Some(buf) + } else { + None + } + } + + pub fn get_port(&self) -> Option> + where V: View + ?Sized + 'static, + V::Msg: Clone + { + self.out_port.clone().downcast::().ok().map(|p| p.outer()) + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + diff --git a/lib-nested-core/src/repr_tree/mod.rs b/lib-nested-core/src/repr_tree/mod.rs index ac1d18b..c2b71d4 100644 --- a/lib-nested-core/src/repr_tree/mod.rs +++ b/lib-nested-core/src/repr_tree/mod.rs @@ -1,3 +1,5 @@ +pub mod node; +pub mod leaf; pub mod context; pub mod morphism; @@ -6,6 +8,8 @@ mod tests; pub use { context::{Context}, + leaf::ReprLeaf, + node::ReprTree, morphism::{MorphismType, GenericReprTreeMorphism, MorphismBase} }; @@ -30,520 +34,6 @@ use { }, }; -//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - -#[derive(Clone)] -pub struct ReprLeaf { - out_port: AnyViewPort, - in_port: AnyInnerViewPort, - data: Option< Arc >, - - /// keepalive for the observer that updates the buffer from in_port - keepalive: Option>, - in_keepalive: Option>, -} - -#[derive(Clone)] -pub struct ReprTree { - halo: TypeTerm, - type_tag: TypeTerm, - branches: HashMap>>, - leaf: Option< ReprLeaf > -} - -//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - -impl std::fmt::Debug for ReprTree { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - writeln!(f, "| type: {:?}", self.type_tag)?; - - for (_k,x) in self.branches.iter() { - writeln!(f, "|--> child: {:?}", x)?; - } - - Ok(()) - } -} - -//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - -impl ReprLeaf { - pub fn from_view( src_port: OuterViewPort ) -> Self - where V: View + ?Sized + 'static, - V::Msg: Clone - { - let mut in_port = ViewPort::::new(); - let in_keepalive = in_port.attach_to(src_port); - - let mut out_port = ViewPort::::new(); - let out_keepalive = out_port.attach_to(in_port.outer()); - - ReprLeaf { - keepalive: Some(out_keepalive), - in_keepalive: Some(in_keepalive), - in_port: in_port.inner().into(), - out_port: out_port.into(), - data: None, - } - } - - pub fn detach(&mut self) - where V: View + ?Sized + 'static, - V::Msg: Clone - { - self.keepalive = None; - self.in_keepalive = None; - - let ip = self.in_port.clone() - .downcast::().ok() - .unwrap(); - ip.0.detach(); - - if self.data.is_none() { - let mut op = self.out_port.clone() - .downcast::().ok() - .unwrap(); - - op.detach(); - self.keepalive = Some(op.attach_to(ip.0.outer())); - } - } - - pub fn detach_vec(&mut self) - where Item: Clone + Send + Sync + 'static - { - self.keepalive = None; - self.in_keepalive = None; - - let ip = self.in_port.clone() - .downcast::>().ok() - .unwrap(); - - ip.0.detach(); - - if let Some(data) = self.data.as_mut() { - let mut op = self.out_port.clone() - .downcast::>>().ok() - .unwrap(); - op.detach(); - - let data = data.clone().downcast::< RwLock> >().ok().unwrap(); - let buffer = VecBuffer::with_data_arc_port(data, op.inner()); - self.keepalive = Some(buffer.attach_to(ip.0.outer())) - } - } - - pub fn attach_to(&mut self, src_port: OuterViewPort) - where V: View + ?Sized + 'static, - V::Msg: Clone - { - self.in_keepalive = Some(self.in_port.clone() - .downcast::().ok().unwrap() - .0.attach_to( src_port )); - } - - pub fn from_singleton_buffer( buffer: SingletonBuffer ) -> Self - where T: Clone + Send + Sync + 'static - { - let in_port = ViewPort::>::new(); - ReprLeaf { - in_keepalive: None, - keepalive: Some(buffer.attach_to(in_port.outer())), - in_port: in_port.inner().into(), - out_port: buffer.get_port().0.into(), - data: Some(buffer.into_inner()) - } - } - - pub fn from_vec_buffer( buffer: VecBuffer ) -> Self - where T: Clone + Send + Sync + 'static - { - let in_port = ViewPort::< dyn ListView >::new(); - ReprLeaf { - in_keepalive: None, - keepalive: Some(buffer.attach_to(in_port.outer())), - in_port: in_port.inner().into(), - out_port: buffer.get_port().0.into(), - data: Some(buffer.into_inner()) - } - } - - pub fn as_singleton_buffer(&mut self) -> Option> - where T: Clone + Send + Sync + 'static - { - let sgl_port = self.get_port::< dyn SingletonView >().unwrap().0; - - let data_arc = - if let Some(data) = self.data.as_ref() { - data.clone().downcast::>().ok() - } else { - sgl_port.update(); - let value = sgl_port.outer().get_view().unwrap().get(); - eprintln!("make new data ARC from old value"); - Some(Arc::new(RwLock::new( value ))) - }; - - if let Some(data_arc) = data_arc { - self.data = Some(data_arc.clone() as Arc); - let buf = SingletonBuffer { - value: data_arc, - port: sgl_port.inner() - }; - self.keepalive = Some(buf.attach_to( - self.in_port.0.clone() - .downcast::>() - .ok().unwrap() - .outer() - )); - Some(buf) - } else { - None - } - } - - pub fn as_vec_buffer(&mut self) -> Option> - where T: Clone + Send + Sync + 'static - { - let vec_port = self.get_port::< RwLock> >().unwrap().0; - - let data_arc = - if let Some(data) = self.data.as_ref() { - data.clone().downcast::>>().ok() - } else { - vec_port.update(); - if let Some(value) = vec_port.outer().get_view() { - let value = value.read().unwrap().clone(); - eprintln!("make new data ARC from old VECTOR-value"); - Some(Arc::new(RwLock::new( value ))) - } else { - eprintln!("no data vec"); - Some(Arc::new(RwLock::new( Vec::new() ))) -// None - } - }; - - if let Some(data_arc) = data_arc { - self.data = Some(data_arc.clone() as Arc); - let buf = VecBuffer::with_data_arc_port(data_arc, vec_port.inner()); - self.keepalive = Some(buf.attach_to( - self.in_port.0.clone() - .downcast::< dyn ListView >() - .ok().unwrap() - .outer() - )); - Some(buf) - } else { - None - } - } - - pub fn get_port(&self) -> Option> - where V: View + ?Sized + 'static, - V::Msg: Clone - { - self.out_port.clone().downcast::().ok().map(|p| p.outer()) - } -} - -//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - -impl ReprTree { - pub fn new(type_tag: impl Into) -> Self { - let type_tag = type_tag.into(); - - assert!(type_tag.is_flat()); - - ReprTree { - halo: TypeTerm::unit(), - type_tag: type_tag.clone(), - branches: HashMap::new(), - leaf: None - } - } - - pub fn new_arc(type_tag: impl Into) -> Arc> { - Arc::new(RwLock::new(Self::new(type_tag))) - } - - pub fn get_type(&self) -> &TypeTerm { - &self.type_tag - } - - pub fn set_halo(&mut self, halo_type: impl Into) { - self.halo = halo_type.into(); - for (branch_type, branch) in self.branches.iter() { - branch.write().unwrap().set_halo( TypeTerm::Ladder(vec![ - self.halo.clone(), - self.type_tag.clone() - ]).normalize() - ); - } - } - - pub fn get_halo_type(&self) -> &TypeTerm { - &self.halo - } - - pub fn get_leaf_types(&self) -> Vec< TypeTerm > { - let mut leaf_types = Vec::new(); - if self.leaf.is_some() { - leaf_types.push( self.get_type().clone() ); - } - for (branch_type, branch) in self.branches.iter() { - for t in branch.read().unwrap().get_leaf_types() { - leaf_types.push(TypeTerm::Ladder(vec![ - self.get_type().clone(), - t - ]).normalize()) - } - } - leaf_types - } - - pub fn insert_branch(&mut self, repr: Arc>) { - let branch_type = repr.read().unwrap().get_type().clone(); - - assert!(branch_type.is_flat()); - - repr.write().unwrap().set_halo( TypeTerm::Ladder(vec![ - self.halo.clone(), - self.type_tag.clone() - ]).normalize() ); - - self.branches.insert(branch_type, repr.clone()); - } - - pub fn from_char(ctx: &Arc>, c: char ) -> Arc> { - ReprTree::from_singleton_buffer( - Context::parse(ctx, "Char"), - SingletonBuffer::new(c) - ) - } - - pub fn from_view( type_tag: impl Into, view: OuterViewPort ) -> Arc> - where V: View + ?Sized + 'static, - V::Msg: Clone - { - let mut rt = ReprTree::new(type_tag); - rt.leaf = Some(ReprLeaf::from_view(view)); - Arc::new(RwLock::new(rt)) - } - - pub fn from_singleton_buffer( type_tag: impl Into, buf: SingletonBuffer ) -> Arc> - where T: Clone + Send + Sync + 'static - { - let mut rt = ReprTree::new(type_tag); - rt.leaf = Some(ReprLeaf::from_singleton_buffer(buf)); - Arc::new(RwLock::new(rt)) - } - - pub fn from_vec_buffer( type_tag: impl Into, buf: VecBuffer ) -> Arc> - where T: Clone + Send + Sync + 'static - { - let mut rt = ReprTree::new(type_tag); - rt.leaf = Some(ReprLeaf::from_vec_buffer(buf)); - Arc::new(RwLock::new(rt)) - } - - pub fn attach_to( - &mut self, - src_port: OuterViewPort - ) - where V: View + ?Sized + 'static, - V::Msg: Clone - { - if let Some(leaf) = self.leaf.as_mut() { - leaf.attach_to(src_port); - } else { - eprintln!("cant attach branch without leaf"); - } - } - - /// find, and if necessary, create corresponding path in repr-tree. - /// Attach src_port to input of that node - pub fn attach_leaf_to( - &mut self, - mut type_ladder: impl Iterator, - src_port: OuterViewPort - ) - where V: View + ?Sized + 'static, - V::Msg: Clone - { - while let Some(rung_type) = type_ladder.next() { - if &rung_type != self.get_type() { - if let Some(next_repr) = self.branches.get(&rung_type) { - next_repr.write().unwrap().attach_leaf_to(type_ladder, src_port); - } else { - let mut next_repr = ReprTree::new(rung_type.clone()); - next_repr.attach_leaf_to(type_ladder, src_port); - self.insert_branch(Arc::new(RwLock::new(next_repr))); - } - return; - } - } - - if let Some(leaf) = self.leaf.as_mut() { - leaf.attach_to(src_port); - } else { - self.leaf = Some(ReprLeaf::from_view(src_port)); - } - } - - pub fn detach(&mut self, ctx: &Arc>) { - if let Some(leaf) = self.leaf.as_mut() { - if self.type_tag == Context::parse(&ctx, "Char") { - leaf.detach::>(); - } - if self.type_tag == Context::parse(&ctx, "") { - leaf.detach_vec::(); - } - if self.type_tag == Context::parse(&ctx, "") { - leaf.detach::>(); - } - } - - for (t,b) in self.branches.iter_mut() { - b.write().unwrap().detach(&ctx); - } - } - - pub fn insert_leaf( - &mut self, - mut type_ladder: impl Iterator, - leaf: ReprLeaf - ) { - while let Some(type_term) = type_ladder.next() { - if &type_term != self.get_type() { - if let Some(next_repr) = self.branches.get(&type_term) { - next_repr.write().unwrap().insert_leaf(type_ladder, leaf.clone()); - } else { - let mut next_repr = ReprTree::new(type_term.clone()); - next_repr.insert_leaf(type_ladder, leaf.clone()); - self.insert_branch(Arc::new(RwLock::new(next_repr))); - } - return; - } - } - - self.leaf = Some(leaf); - } - - //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - - pub fn descend_one(&self, dst_type: impl Into) -> Option>> { - let dst_type = dst_type.into(); - assert!( dst_type.is_flat() ); - self.branches.get(&dst_type).cloned() - } - - pub fn descend_ladder(rt: &Arc>, mut repr_ladder: impl Iterator) -> Option>> { - if let Some(first) = repr_ladder.next() { - let rt = rt.read().unwrap(); - repr_ladder.fold( - rt.descend_one(first), - |s, t| s?.descend(t)) - } else { - Some(rt.clone()) - } - } - - pub fn descend(rt: &Arc>, dst_type: impl Into) -> Option>> { - let mut lnf = dst_type.into().get_lnf_vec(); - if lnf.len() > 0 { - if lnf[0] == rt.get_type() { - lnf.remove(0); - } - ReprTree::descend_ladder(rt, lnf.into_iter()) - } else { - Some(rt.clone()) - } - } - - pub fn ascend(rt: &Arc>, type_term: impl Into) -> Arc> { - let mut n = Self::new(type_term); - n.insert_branch(rt.clone()); - Arc::new(RwLock::new(n)) - } - - //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - - pub fn singleton_buffer(&mut self) -> Option> { - if let Some(leaf) = self.leaf.as_mut() { - leaf.as_singleton_buffer::() - } else { - // create new singleton buffer - /* - // default value?? - let buf = SingletonBuffer::::default(); - self.leaf = Some(ReprLeaf::from_singleton_buffer(buf.clone())); - Some(buf) - */ - None - } - } - - pub fn vec_buffer(&mut self) -> Option> { - if let Some(leaf) = self.leaf.as_mut() { - leaf.as_vec_buffer::() - } else { - None - } - } - - //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - - pub fn get_port(&self) -> Option> - where - V::Msg: Clone, - { - if let Some(leaf) = self.leaf.as_ref() { - leaf.get_port::() - } else { - None - } - } - - pub fn get_view(&self) -> Option> - where - V::Msg: Clone, - { - self.get_port::()? - .get_view() - } - - //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - - pub fn view_singleton(&self) -> OuterViewPort> { - self.get_port::>().expect("no singleton-view available") - } - - pub fn view_seq(&self) -> OuterViewPort> { - self.get_port::>().expect("no sequence-view available") - } - - pub fn view_list(&self) -> OuterViewPort> { - self.get_port::>().expect("no list-view available") - } - - pub fn view_char(&self) -> OuterViewPort> { - self.get_port::>().expect("no char-view available") - } - - pub fn view_u8(&self) -> OuterViewPort> { - self.get_port::>().expect("no u8-view available") - } - - pub fn view_u64(&self) -> OuterViewPort> { - self.get_port::>().expect("no u64-view available") - } - - pub fn view_usize(&self) -> OuterViewPort> { - self.get_port::>().expect("no usize-view available") - } -} - - - //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub trait ReprTreeExt { diff --git a/lib-nested-core/src/repr_tree/node.rs b/lib-nested-core/src/repr_tree/node.rs new file mode 100644 index 0000000..89dadf0 --- /dev/null +++ b/lib-nested-core/src/repr_tree/node.rs @@ -0,0 +1,348 @@ + + +use { + r3vi::{ + view::{ + ViewPort, OuterViewPort, + AnyViewPort, AnyInnerViewPort, AnyOuterViewPort, + port::UpdateTask, + View, Observer, + singleton::*, + sequence::*, + list::* + }, + buffer::{singleton::*, vec::*} + }, + laddertypes::{TypeTerm}, + std::{ + collections::HashMap, + sync::{Arc, RwLock}, + any::Any + }, + super::{Context, ReprLeaf, ReprTreeExt} +}; + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +#[derive(Clone)] +pub struct ReprTree { + halo: TypeTerm, + type_tag: TypeTerm, + branches: HashMap>>, + leaf: Option< ReprLeaf > +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +impl std::fmt::Debug for ReprTree { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "| type: {:?}", self.type_tag)?; + + for (_k,x) in self.branches.iter() { + writeln!(f, "|--> child: {:?}", x)?; + } + writeln!(f, ""); + + Ok(()) + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +impl ReprTree { + pub fn new(type_tag: impl Into) -> Self { + let type_tag = type_tag.into(); + + assert!(type_tag.is_flat()); + + ReprTree { + halo: TypeTerm::unit(), + type_tag: type_tag.clone(), + branches: HashMap::new(), + leaf: None + } + } + + pub fn new_arc(type_tag: impl Into) -> Arc> { + Arc::new(RwLock::new(Self::new(type_tag))) + } + + pub fn get_type(&self) -> &TypeTerm { + &self.type_tag + } + + pub fn set_halo(&mut self, halo_type: impl Into) { + self.halo = halo_type.into(); + for (branch_type, branch) in self.branches.iter() { + branch.write().unwrap().set_halo( TypeTerm::Ladder(vec![ + self.halo.clone(), + self.type_tag.clone() + ]).normalize() + ); + } + } + + pub fn get_halo_type(&self) -> &TypeTerm { + &self.halo + } + + pub fn get_leaf_types(&self) -> Vec< TypeTerm > { + let mut leaf_types = Vec::new(); + if self.leaf.is_some() { + leaf_types.push( self.get_type().clone() ); + } + for (branch_type, branch) in self.branches.iter() { + for t in branch.read().unwrap().get_leaf_types() { + leaf_types.push(TypeTerm::Ladder(vec![ + self.get_type().clone(), + t + ]).normalize()) + } + } + leaf_types + } + + pub fn insert_branch(&mut self, repr: Arc>) { + let branch_type = repr.read().unwrap().get_type().clone(); + + assert!(branch_type.is_flat()); + + repr.write().unwrap().set_halo( TypeTerm::Ladder(vec![ + self.halo.clone(), + self.type_tag.clone() + ]).normalize() ); + + self.branches.insert(branch_type, repr.clone()); + } + + pub fn from_char(ctx: &Arc>, c: char ) -> Arc> { + ReprTree::from_singleton_buffer( + Context::parse(ctx, "Char"), + SingletonBuffer::new(c) + ) + } + + pub fn from_view( type_tag: impl Into, view: OuterViewPort ) -> Arc> + where V: View + ?Sized + 'static, + V::Msg: Clone + { + let mut rt = ReprTree::new(type_tag); + rt.leaf = Some(ReprLeaf::from_view(view)); + Arc::new(RwLock::new(rt)) + } + + pub fn from_singleton_buffer( type_tag: impl Into, buf: SingletonBuffer ) -> Arc> + where T: Clone + Send + Sync + 'static + { + let mut rt = ReprTree::new(type_tag); + rt.leaf = Some(ReprLeaf::from_singleton_buffer(buf)); + Arc::new(RwLock::new(rt)) + } + + pub fn from_vec_buffer( type_tag: impl Into, buf: VecBuffer ) -> Arc> + where T: Clone + Send + Sync + 'static + { + let mut rt = ReprTree::new(type_tag); + rt.leaf = Some(ReprLeaf::from_vec_buffer(buf)); + Arc::new(RwLock::new(rt)) + } + + pub fn attach_to( + &mut self, + src_port: OuterViewPort + ) + where V: View + ?Sized + 'static, + V::Msg: Clone + { + if let Some(leaf) = self.leaf.as_mut() { + leaf.attach_to(src_port); + } else { + eprintln!("cant attach branch without leaf"); + } + } + + /// find, and if necessary, create corresponding path in repr-tree. + /// Attach src_port to input of that node + pub fn attach_leaf_to( + &mut self, + mut type_ladder: impl Iterator, + src_port: OuterViewPort + ) + where V: View + ?Sized + 'static, + V::Msg: Clone + { + while let Some(rung_type) = type_ladder.next() { + if &rung_type != self.get_type() { + if let Some(next_repr) = self.branches.get(&rung_type) { + next_repr.write().unwrap().attach_leaf_to(type_ladder, src_port); + } else { + let mut next_repr = ReprTree::new(rung_type.clone()); + next_repr.attach_leaf_to(type_ladder, src_port); + self.insert_branch(Arc::new(RwLock::new(next_repr))); + } + return; + } + } + + if let Some(leaf) = self.leaf.as_mut() { + leaf.attach_to(src_port); + } else { + self.leaf = Some(ReprLeaf::from_view(src_port)); + } + } + + pub fn detach(&mut self, ctx: &Arc>) { + if let Some(leaf) = self.leaf.as_mut() { + if self.type_tag == Context::parse(&ctx, "Char") { + leaf.detach::>(); + } + if self.type_tag == Context::parse(&ctx, "") { + leaf.detach_vec::(); + } + if self.type_tag == Context::parse(&ctx, "") { + leaf.detach::>(); + } + } + + for (t,b) in self.branches.iter_mut() { + b.write().unwrap().detach(&ctx); + } + } + + pub fn insert_leaf( + &mut self, + mut type_ladder: impl Iterator, + leaf: ReprLeaf + ) { + while let Some(type_term) = type_ladder.next() { + if &type_term != self.get_type() { + if let Some(next_repr) = self.branches.get(&type_term) { + next_repr.write().unwrap().insert_leaf(type_ladder, leaf.clone()); + } else { + let mut next_repr = ReprTree::new(type_term.clone()); + next_repr.insert_leaf(type_ladder, leaf.clone()); + self.insert_branch(Arc::new(RwLock::new(next_repr))); + } + return; + } + } + + self.leaf = Some(leaf); + } + + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + + pub fn descend_one(&self, dst_type: impl Into) -> Option>> { + let dst_type = dst_type.into(); + assert!( dst_type.is_flat() ); + self.branches.get(&dst_type).cloned() + } + + pub fn descend_ladder(rt: &Arc>, mut repr_ladder: impl Iterator) -> Option>> { + if let Some(first) = repr_ladder.next() { + let rt = rt.read().unwrap(); + repr_ladder.fold( + rt.descend_one(first), + |s, t| s?.descend(t)) + } else { + Some(rt.clone()) + } + } + + pub fn descend(rt: &Arc>, dst_type: impl Into) -> Option>> { + let mut lnf = dst_type.into().get_lnf_vec(); + if lnf.len() > 0 { + if lnf[0] == rt.get_type() { + lnf.remove(0); + } + ReprTree::descend_ladder(rt, lnf.into_iter()) + } else { + Some(rt.clone()) + } + } + + pub fn ascend(rt: &Arc>, type_term: impl Into) -> Arc> { + let mut n = Self::new(type_term); + n.insert_branch(rt.clone()); + Arc::new(RwLock::new(n)) + } + + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + + pub fn singleton_buffer(&mut self) -> Option> { + if let Some(leaf) = self.leaf.as_mut() { + leaf.as_singleton_buffer::() + } else { + // create new singleton buffer + /* + // default value?? + let buf = SingletonBuffer::::default(); + self.leaf = Some(ReprLeaf::from_singleton_buffer(buf.clone())); + Some(buf) + */ + None + } + } + + pub fn vec_buffer(&mut self) -> Option> { + if let Some(leaf) = self.leaf.as_mut() { + leaf.as_vec_buffer::() + } else { + None + } + } + + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + + pub fn get_port(&self) -> Option> + where + V::Msg: Clone, + { + if let Some(leaf) = self.leaf.as_ref() { + leaf.get_port::() + } else { + None + } + } + + pub fn get_view(&self) -> Option> + where + V::Msg: Clone, + { + self.get_port::()? + .get_view() + } + + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + + pub fn view_singleton(&self) -> OuterViewPort> { + self.get_port::>().expect("no singleton-view available") + } + + pub fn view_seq(&self) -> OuterViewPort> { + self.get_port::>().expect("no sequence-view available") + } + + pub fn view_list(&self) -> OuterViewPort> { + self.get_port::>().expect("no list-view available") + } + + pub fn view_char(&self) -> OuterViewPort> { + self.get_port::>().expect("no char-view available") + } + + pub fn view_u8(&self) -> OuterViewPort> { + self.get_port::>().expect("no u8-view available") + } + + pub fn view_u64(&self) -> OuterViewPort> { + self.get_port::>().expect("no u64-view available") + } + + pub fn view_usize(&self) -> OuterViewPort> { + self.get_port::>().expect("no usize-view available") + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> +