2021-04-29 01:32:59 +02:00
|
|
|
pub mod add;
|
2021-08-31 02:10:10 +02:00
|
|
|
pub mod editor;
|
2021-11-19 12:19:52 +01:00
|
|
|
pub mod radix;
|
2023-08-18 00:16:16 +02:00
|
|
|
pub mod ctx;
|
2021-04-29 01:32:59 +02:00
|
|
|
|
|
|
|
pub use {
|
2021-08-16 01:27:35 +02:00
|
|
|
add::Add,
|
2024-03-10 16:17:24 +01:00
|
|
|
editor::PosIntEditor,
|
2021-11-19 12:19:52 +01:00
|
|
|
radix::RadixProjection,
|
2023-08-21 15:49:07 +02:00
|
|
|
ctx::init_ctx
|
2021-04-29 01:32:59 +02:00
|
|
|
};
|
2023-03-25 08:56:38 +01:00
|
|
|
|
2024-05-25 00:39:47 +02:00
|
|
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
|
|
|
|
|
|
|
use {
|
|
|
|
r3vi::{
|
|
|
|
view::{
|
|
|
|
View, ViewPort, OuterViewPort,
|
|
|
|
Observer,
|
|
|
|
ObserverBroadcast,
|
|
|
|
sequence::*
|
|
|
|
}
|
|
|
|
},
|
|
|
|
crate::{
|
|
|
|
editors::integer::radix::{
|
|
|
|
PosIntProjections
|
|
|
|
}
|
|
|
|
},
|
|
|
|
std::sync::{Arc, RwLock}
|
|
|
|
};
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
pub trait PositionalUInt : SequenceView<Item = u64> {
|
|
|
|
fn get_radix(&self) -> u64;
|
|
|
|
fn get_value(&self) -> u64 {
|
2024-05-25 00:39:47 +02:00
|
|
|
let mut val = 0;
|
|
|
|
let mut r = 1;
|
|
|
|
for i in 0..self.len().unwrap_or(0) {
|
2024-09-02 00:07:57 +02:00
|
|
|
if let Some(digit_val) = self.get(&i) {
|
|
|
|
val += r * digit_val;
|
|
|
|
r *= self.get_radix();
|
|
|
|
}
|
2024-05-25 00:39:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: PositionalUInt> PositionalUInt for RwLock<V> {
|
2024-05-25 19:21:16 +02:00
|
|
|
fn get_radix(&self) -> u64 {
|
2024-05-25 00:39:47 +02:00
|
|
|
self.read().unwrap().get_radix()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PosUIntFromDigits {
|
2024-05-25 19:21:16 +02:00
|
|
|
radix: u64,
|
|
|
|
src_digits: Option<Arc<dyn SequenceView<Item = u64>>>,
|
2024-05-25 00:39:47 +02:00
|
|
|
cast: Arc<RwLock<ObserverBroadcast<dyn PositionalUInt>>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for PosUIntFromDigits {
|
|
|
|
type Msg = usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SequenceView for PosUIntFromDigits {
|
2024-05-25 19:21:16 +02:00
|
|
|
type Item = u64;
|
2024-05-25 00:39:47 +02:00
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
fn get(&self, idx: &usize) -> Option<u64> {
|
2024-05-25 00:39:47 +02:00
|
|
|
self.src_digits.get(idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> Option<usize> {
|
|
|
|
self.src_digits.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PositionalUInt for PosUIntFromDigits {
|
2024-05-25 19:21:16 +02:00
|
|
|
fn get_radix(&self) -> u64 {
|
2024-05-25 00:39:47 +02:00
|
|
|
self.radix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
impl Observer<dyn SequenceView<Item = u64>> for PosUIntFromDigits {
|
|
|
|
fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = u64>>>) {
|
2024-05-25 00:39:47 +02:00
|
|
|
self.src_digits = new_src;
|
|
|
|
// self.cast.write().unwrap().notify(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify(&mut self, idx: &usize) {
|
|
|
|
self.cast.write().unwrap().notify(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
|
|
|
|
|
|
|
pub trait DigitSeqProjection {
|
2024-05-25 19:21:16 +02:00
|
|
|
fn to_positional_uint(&self, radix: u64) -> OuterViewPort<dyn PositionalUInt>;
|
2024-05-25 00:39:47 +02:00
|
|
|
}
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
impl DigitSeqProjection for OuterViewPort<dyn SequenceView<Item = u64>> {
|
|
|
|
fn to_positional_uint(&self, radix: u64) -> OuterViewPort<dyn PositionalUInt> {
|
2024-05-25 00:39:47 +02:00
|
|
|
let port = ViewPort::new();
|
|
|
|
port.add_update_hook(Arc::new(self.0.clone()));
|
|
|
|
|
|
|
|
let proj = Arc::new(RwLock::new(PosUIntFromDigits {
|
|
|
|
radix,
|
|
|
|
src_digits: None,
|
|
|
|
cast: port.inner().get_broadcast()
|
|
|
|
}));
|
|
|
|
|
|
|
|
self.add_observer(proj.clone());
|
|
|
|
port.set_view(Some(proj));
|
|
|
|
port.into_outer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
|
|
|
|
|
|
|
struct PosUIntToDigits {
|
|
|
|
src: Option<Arc<dyn PositionalUInt>>,
|
2024-05-25 19:21:16 +02:00
|
|
|
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = u64>>>>
|
2024-05-25 00:39:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for PosUIntToDigits {
|
|
|
|
type Msg = usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SequenceView for PosUIntToDigits {
|
2024-05-25 19:21:16 +02:00
|
|
|
type Item = u64;
|
2024-05-25 00:39:47 +02:00
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
fn get(&self, idx: &usize) -> Option<u64> {
|
2024-05-25 00:39:47 +02:00
|
|
|
self.src.get(idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> Option<usize> {
|
|
|
|
self.src.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Observer<dyn PositionalUInt> for PosUIntToDigits {
|
|
|
|
fn reset(&mut self, view: Option<Arc<dyn PositionalUInt>>) {
|
|
|
|
self.src = view;
|
|
|
|
// self.cast.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify(&mut self, idx: &usize) {
|
|
|
|
self.cast.notify(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|