add ListView and vec2list projection

This commit is contained in:
Michael Sippel 2024-03-19 17:19:00 +01:00
parent 6037e5f519
commit 27bb201b66
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 233 additions and 0 deletions

View file

@ -6,6 +6,7 @@ pub mod sgl2seq;
pub mod vec2seq;
pub mod vec2bin;
pub mod vec2json;
pub mod vec2list;
pub mod seq2idx;
pub mod enumerate_sequence;
pub mod filter_sequence;

169
src/projection/vec2list.rs Normal file
View file

@ -0,0 +1,169 @@
use {
crate::{
view::{
InnerViewPort, Observer, ObserverBroadcast, ObserverExt, OuterViewPort, View, ViewPort,
list::{ListView, ListDiff},
},
buffer::vec::VecDiff,
},
std::sync::Arc,
std::sync::RwLock,
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
/// Adapter View implementing `List` for `Vec`
pub struct Vec2List<T>
where
T: Clone + Send + Sync + 'static,
{
cur_len: usize,
src_view: Option<Arc<RwLock<Vec<T>>>>,
cast: Arc<RwLock<ObserverBroadcast<dyn ListView<T>>>>,
}
impl<T> Vec2List<T>
where
T: Clone + Send + Sync + 'static,
{
pub fn new(port: InnerViewPort<dyn ListView<T>>) -> Arc<RwLock<Self>> {
let v2l = Arc::new(RwLock::new(Vec2List {
cur_len: 0,
src_view: None,
cast: port.get_broadcast(),
}));
port.set_view(Some(v2l.clone()));
v2l
}
}
impl<T> Observer<RwLock<Vec<T>>> for Vec2List<T>
where
T: Clone + Send + Sync + 'static,
{
fn reset(&mut self, view: Option<Arc<RwLock<Vec<T>>>>) {
let old_len = self.cur_len;
self.src_view = view;
let new_len = if let Some(data) = self.src_view.as_ref() {
let data = data.read().unwrap();
self.cast.notify(&ListDiff::Clear);
self.cast.notify_each(
data.iter().cloned()
.enumerate()
.map(|(idx, val)|
ListDiff::Insert { idx, val }
)
);
data.len()
} else {
0
};
self.cur_len = new_len;
}
fn notify(&mut self, diff: &VecDiff<T>) {
match diff {
VecDiff::Clear => {
self.cast.notify(&ListDiff::Clear);
}
VecDiff::Push(val) => {
self.cast.notify(&ListDiff::Insert{
idx: self.cur_len + 1,
val: val.clone()
});
self.cur_len += 1;
}
VecDiff::Remove(idx) => {
self.cast.notify(&ListDiff::Remove(*idx));
self.cur_len -= 1;
}
VecDiff::Insert { idx, val } => {
self.cur_len += 1;
self.cast.notify(&ListDiff::Insert { idx: *idx, val: val.clone() });
}
VecDiff::Update { idx, val } => {
self.cast.notify(&ListDiff::Update { idx: *idx, val: val.clone() });
}
}
}
}
impl<T> View for Vec2List<T>
where
T: Clone + Send + Sync + 'static,
{
type Msg = ListDiff<T>;
}
impl<T> ListView<T> for Vec2List<T>
where
T: Clone + Send + Sync + 'static,
{
fn get(&self, idx: &usize) -> Option<T> {
self.src_view.as_ref()?.read().unwrap().get(*idx).cloned()
}
fn len(&self) -> Option<usize> {
Some(self.cur_len)
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<T> OuterViewPort<RwLock<Vec<T>>>
where
T: Clone + Send + Sync + 'static,
{
pub fn to_list(&self) -> OuterViewPort<dyn ListView<T>> {
let port = ViewPort::new();
port.add_update_hook(Arc::new(self.0.clone()));
let v2l = Vec2List::new(port.inner());
self.add_observer(v2l.clone());
port.into_outer()
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
#[cfg(test)]
mod tests {
use crate::buffer::vec::VecBuffer;
use crate::view::port::UpdateTask;
#[test]
fn vec_to_list() {
let mut buf = VecBuffer::<char>::new();
let list_view = buf.get_port().to_list();
assert_eq!(list_view.get_view().unwrap().len(), Some(0));
buf.push('a');
list_view.0.update();
assert_eq!(list_view.get_view().unwrap().len(), Some(1));
assert_eq!(list_view.get_view().unwrap().get(&0), Some('a'));
assert_eq!(list_view.get_view().unwrap().get(&1), None);
buf.push('b');
list_view.0.update();
assert_eq!(list_view.get_view().unwrap().len(), Some(2));
assert_eq!(list_view.get_view().unwrap().get(&0), Some('a'));
assert_eq!(list_view.get_view().unwrap().get(&1), Some('b'));
assert_eq!(list_view.get_view().unwrap().get(&2), None);
buf.push('c');
buf.remove(0);
list_view.0.update();
assert_eq!(list_view.get_view().unwrap().len(), Some(2));
assert_eq!(list_view.get_view().unwrap().get(&0), Some('b'));
assert_eq!(list_view.get_view().unwrap().get(&1), Some('c'));
assert_eq!(list_view.get_view().unwrap().get(&2), None);
}
}

62
src/view/list/mod.rs Normal file
View file

@ -0,0 +1,62 @@
use crate::view::View;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
#[derive(Clone)]
pub enum ListDiff<T>
where T: Clone + Send + Sync + 'static
{
Clear,
Remove(usize),
Insert{ idx: usize, val: T },
Update{ idx: usize, val: T },
}
pub trait ListView<Item>: View<Msg = ListDiff<Item>>
where Item: Clone + Send + Sync + 'static
{
fn len(&self) -> Option<usize>;
fn get(&self, idx: &usize) -> Option<Item>;
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
use std::sync::RwLock;
use std::{ops::Deref, sync::Arc};
impl<Item: Clone + Send + Sync + 'static, V: ListView<Item> + ?Sized> ListView<Item> for RwLock<V> {
fn get(&self, idx: &usize) -> Option<Item> {
self.read().unwrap().get(idx)
}
fn len(&self) -> Option<usize> {
self.read().unwrap().len()
}
}
impl<Item: Clone + Send + Sync + 'static, V: ListView<Item> + ?Sized> ListView<Item> for Arc<V> {
fn get(&self, idx: &usize) -> Option<Item> {
self.deref().get(idx)
}
fn len(&self) -> Option<usize> {
self.deref().len()
}
}
impl<Item: Clone + Send + Sync + 'static, V: ListView<Item>> ListView<Item> for Option<V> {
fn get(&self, idx: &usize) -> Option<Item> {
(self.as_ref()? as &V).get(idx)
}
fn len(&self) -> Option<usize> {
if let Some(v) = self.as_ref() {
v.len()
} else {
Some(0)
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>

View file

@ -43,4 +43,5 @@ pub mod singleton;
pub mod sequence;
pub mod index;
pub mod grid;
pub mod list;