From fb5ae53e88757d83610991b268943f334f0ba2df Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 27 Aug 2021 22:31:25 +0200 Subject: [PATCH] add IndexBuffer --- nested/src/index/buffer.rs | 81 ++++++++++++++++++++++++++++++++++++++ nested/src/index/mod.rs | 2 +- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 nested/src/index/buffer.rs diff --git a/nested/src/index/buffer.rs b/nested/src/index/buffer.rs new file mode 100644 index 0000000..d48537b --- /dev/null +++ b/nested/src/index/buffer.rs @@ -0,0 +1,81 @@ + +use { + std::{ + sync::Arc, + collections::HashMap, + hash::Hash + }, + std::sync::RwLock, + crate::{ + core::{ + Observer, + ObserverBroadcast, + View, + InnerViewPort + }, + index::IndexView + } +}; + +pub struct IndexBufferView(Arc>>) +where Key: Clone + Hash + Eq + Send + Sync + 'static, + Item: Clone + Send + Sync + 'static; + +impl View for IndexBufferView +where Key: Clone + Hash + Eq + Send + Sync + 'static, + Item: Clone + Send + Sync + 'static +{ + type Msg = Key; +} + +impl IndexView for IndexBufferView +where Key: Clone + Hash + Eq + Send + Sync + 'static, + Item: Clone + Send + Sync + 'static +{ + type Item = Item; + + fn get(&self, key: &Key) -> Option { + self.0.read().unwrap().get(key).cloned() + } + + fn area(&self) -> Option> { + Some(self.0.read().unwrap().keys().cloned().collect()) + } +} + + +pub struct IndexBuffer +where Key: Clone + Hash + Eq + Send + Sync + 'static, + Item: Clone + Send + Sync + 'static +{ + data: Arc>>, + cast: Arc>>> +} + +impl IndexBuffer +where Key: Clone + Hash + Eq + Send + Sync + 'static, + Item: Clone + Send + Sync + 'static +{ + pub fn new(port: InnerViewPort>) -> Self { + let data = Arc::new(RwLock::new(HashMap::::new())); + port.set_view(Some(Arc::new(IndexBufferView(data.clone())))); + + IndexBuffer { + data, + cast: port.get_broadcast() + } + } + + pub fn insert(&mut self, key: Key, item: Item) { + self.data.write().unwrap().insert(key.clone(), item); + self.cast.notify(&key); + } + + pub fn insert_iter(&mut self, iter: T) + where T: IntoIterator { + for (key, item) in iter { + self.insert(key, item); + } + } +} + diff --git a/nested/src/index/mod.rs b/nested/src/index/mod.rs index 51d42cf..f639ef7 100644 --- a/nested/src/index/mod.rs +++ b/nested/src/index/mod.rs @@ -1,6 +1,7 @@ pub mod map_item; pub mod map_key; +pub mod buffer; use { std::{ @@ -46,7 +47,6 @@ impl IndexView for Arc where Key: Send + Sync, V: IndexView + ?Sized { - type Item = V::Item; fn get(&self, key: &Key) -> Option {