pictorus-blocks 0.0.0

Implementations of Pictorus blocks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
extern crate alloc;
use alloc::vec::Vec;
use pictorus_block_data::{BlockData as OldBlockData, FromPass};
use pictorus_traits::{ByteSliceSignal, Matrix, Pass, PassBy, ProcessBlock};

use crate::traits::{CopyInto, DefaultStorage, Scalar};

/// Switches between multiple input signals based on a condition.
///
/// The condition is the first input, and the rest are the signals to switch between.
/// The block will output the signal that corresponds to the index of the `cases`` parameter
/// that matches the condition input. If no matches are found, it will output the last input.
/// For example:
/// ```
/// use core::time::Duration;
/// use pictorus_blocks::SwitchBlock;
/// use pictorus_traits::ProcessBlock;
/// use pictorus_block_data::BlockData as OldBlockData;
/// use pictorus_traits::Context;
///
/// #[derive(Default)]
/// struct StubContext {}
///
/// impl Context for StubContext {
///     fn time(&self) -> Duration {
///         Duration::from_secs(0)
///     }
///
///     fn timestep(&self) -> Option<Duration> {
///         None
///     }
///
///     fn fundamental_timestep(&self) -> Duration {
///         Duration::from_millis(100)
///     }
/// }
///
/// let ctxt = StubContext::default();
/// let mut block = SwitchBlock::<(f64, f64, f64)>::default();
/// // If condition is 0, output the signal at index 0
/// // If condition is 1, output the signal at index 1
/// // If condition is anything else, output the signal at index 1
/// let cases = OldBlockData::from_vector(&[0.0, 1.0]);
/// let parameters = <SwitchBlock<(f64, f64, f64)> as ProcessBlock>::Parameters::new(&cases);
/// // Here we have a condition of 0.0, and inputs of [1.0, 2.0]
/// // Since condition matches case 0, the output will be 1.0
/// let input = (0.0, 1.0, 2.0);
/// let output = block.process(&parameters, &ctxt, input);
/// assert_eq!(output, 1.0);
///
pub struct SwitchBlock<T: Apply>
where
    T::Output: DefaultStorage,
    OldBlockData: FromPass<T::Output>,
{
    pub data: OldBlockData,
    buffer: <T::Output as DefaultStorage>::Storage,
}

impl<T: Apply> Default for SwitchBlock<T>
where
    T::Output: DefaultStorage,
    OldBlockData: FromPass<T::Output>,
{
    fn default() -> Self {
        Self {
            data: <OldBlockData as FromPass<T::Output>>::from_pass(T::Output::from_storage(
                &T::Output::default_storage(),
            )),
            buffer: T::Output::default_storage(),
        }
    }
}

impl<T: Apply> ProcessBlock for SwitchBlock<T>
where
    T::Output: DefaultStorage,
    OldBlockData: FromPass<T::Output>,
{
    type Inputs = T;
    type Output = T::Output;
    type Parameters = T::Parameters;

    fn process<'b>(
        &'b mut self,
        parameters: &Self::Parameters,
        _context: &dyn pictorus_traits::Context,
        inputs: PassBy<'_, Self::Inputs>,
    ) -> PassBy<'b, Self::Output> {
        T::apply(inputs, parameters, &mut self.buffer);
        let res = T::Output::from_storage(&self.buffer);
        self.data = <OldBlockData as FromPass<T::Output>>::from_pass(res);
        res
    }
}

/// Parameters for the SwitchBlock
pub struct Parameters<C: Scalar, const N: usize> {
    /// The cases to compare the input condition against
    /// The cases array must be exactly the same length as the number of inputs
    /// The last case is the default value
    pub cases: [C; N],
}

// TODO: This is currently only implemented for f64 and is constructed from OldBlockData.
// In the future this should either accept an array of [C; N] or a &[C]
impl<const N: usize> Parameters<f64, N> {
    pub fn new(cases: &OldBlockData) -> Self {
        assert!(cases.len() == N, "Invalid number of switch cases");

        let mut case_arr: [f64; N] = [0.0; N];
        for (idx, case) in cases.iter().enumerate() {
            case_arr[idx] = *case;
        }
        Self { cases: case_arr }
    }
}

pub trait ApplyInto<C: Scalar, const N: usize>: Pass + DefaultStorage {
    fn apply_into(
        condition: C,
        cases: &[C; N],
        inputs: &[PassBy<Self>; N],
        dest: &mut Self::Storage,
    );
}

impl<C: Scalar, const N: usize> ApplyInto<C, N> for C {
    fn apply_into(condition: C, cases: &[C; N], inputs: &[PassBy<C>; N], dest: &mut C) {
        for (idx, case) in cases.iter().enumerate() {
            if condition == *case {
                let res = inputs[idx];
                *dest = res;
                return;
            }
        }
        let res = inputs[inputs.len() - 1];
        *dest = res;
    }
}

impl<C: Scalar, const NROWS: usize, const NCOLS: usize, const N: usize> ApplyInto<C, N>
    for Matrix<NROWS, NCOLS, C>
{
    fn apply_into(
        condition: C,
        cases: &[C; N],
        inputs: &[PassBy<Matrix<NROWS, NCOLS, C>>; N],
        dest: &mut Matrix<NROWS, NCOLS, C>,
    ) {
        for (idx, case) in cases.iter().enumerate() {
            if condition == *case {
                let res = inputs[idx];
                Matrix::copy_into(res, dest);
                return;
            }
        }
        let res = inputs[inputs.len() - 1];
        Matrix::copy_into(res, dest);
    }
}

impl<C: Scalar, const N: usize> ApplyInto<C, N> for ByteSliceSignal {
    fn apply_into(
        condition: C,
        cases: &[C; N],
        inputs: &[PassBy<ByteSliceSignal>; N],
        dest: &mut Vec<u8>,
    ) {
        for (idx, case) in cases.iter().enumerate() {
            if condition == *case {
                let res = inputs[idx];
                dest.clear();
                dest.extend_from_slice(res);
                return;
            }
        }
        let res = inputs[inputs.len() - 1];
        // We use clear and extend rather than copy_from_slice because
        // copy_from_slice requires the destination to be the same length as the source
        dest.clear();
        dest.extend_from_slice(res);
    }
}

pub trait Apply: Pass {
    type Parameters;
    type Output: Pass + DefaultStorage;

    fn apply(
        input: PassBy<Self>,
        params: &Self::Parameters,
        buffer: &mut <Self::Output as DefaultStorage>::Storage,
    );
}

// SwitchBlock requires at least 3 inputs. The first is the condition,
// the rest are inputs to maybe pass through

// 1 condition + 2 inputs
impl<C: Scalar, T: Pass + DefaultStorage + ApplyInto<C, 2>> Apply for (C, T, T) {
    type Output = T;
    type Parameters = Parameters<C, 2>;

    fn apply(
        input: PassBy<Self>,
        params: &Self::Parameters,
        buffer: &mut <Self::Output as DefaultStorage>::Storage,
    ) {
        let condition = input.0;
        T::apply_into(condition, &params.cases, &[input.1, input.2], buffer);
    }
}

// 1 condition + 3 inputs
impl<C: Scalar, T: Pass + DefaultStorage + ApplyInto<C, 3>> Apply for (C, T, T, T) {
    type Output = T;
    type Parameters = Parameters<C, 3>;

    fn apply(
        input: PassBy<Self>,
        params: &Self::Parameters,
        buffer: &mut <Self::Output as DefaultStorage>::Storage,
    ) {
        let condition = input.0;
        T::apply_into(
            condition,
            &params.cases,
            &[input.1, input.2, input.3],
            buffer,
        );
    }
}

// 1 condition + 4 inputs
impl<C: Scalar, T: Pass + DefaultStorage + ApplyInto<C, 4>> Apply for (C, T, T, T, T) {
    type Output = T;
    type Parameters = Parameters<C, 4>;

    fn apply(
        input: PassBy<Self>,
        params: &Self::Parameters,
        buffer: &mut <Self::Output as DefaultStorage>::Storage,
    ) {
        let condition = input.0;
        T::apply_into(
            condition,
            &params.cases,
            &[input.1, input.2, input.3, input.4],
            buffer,
        );
    }
}

// 1 condition + 5 inputs
impl<C: Scalar, T: Pass + DefaultStorage + ApplyInto<C, 5>> Apply for (C, T, T, T, T, T) {
    type Output = T;
    type Parameters = Parameters<C, 5>;

    fn apply(
        input: PassBy<Self>,
        params: &Self::Parameters,
        buffer: &mut <Self::Output as DefaultStorage>::Storage,
    ) {
        let condition = input.0;
        T::apply_into(
            condition,
            &params.cases,
            &[input.1, input.2, input.3, input.4, input.5],
            buffer,
        );
    }
}

// 1 condition + 6 inputs
impl<C: Scalar, T: Pass + DefaultStorage + ApplyInto<C, 6>> Apply for (C, T, T, T, T, T, T) {
    type Output = T;
    type Parameters = Parameters<C, 6>;

    fn apply(
        input: PassBy<Self>,
        params: &Self::Parameters,
        buffer: &mut <Self::Output as DefaultStorage>::Storage,
    ) {
        let condition = input.0;
        T::apply_into(
            condition,
            &params.cases,
            &[input.1, input.2, input.3, input.4, input.5, input.6],
            buffer,
        );
    }
}

// 1 condition + 7 inputs
impl<C: Scalar, T: Pass + DefaultStorage + ApplyInto<C, 7>> Apply for (C, T, T, T, T, T, T, T) {
    type Output = T;
    type Parameters = Parameters<C, 7>;

    fn apply(
        input: PassBy<Self>,
        params: &Self::Parameters,
        buffer: &mut <Self::Output as DefaultStorage>::Storage,
    ) {
        let condition = input.0;
        T::apply_into(
            condition,
            &params.cases,
            &[
                input.1, input.2, input.3, input.4, input.5, input.6, input.7,
            ],
            buffer,
        );
    }
}

#[cfg(test)]
mod tests {
    use crate::traits::MatrixOps;

    use super::*;
    use crate::testing::StubContext;

    #[test]
    fn test_switch_block_2_scalars() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(f64, f64, f64)>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[0.0, 1.0]));

        let input = (0.0, 1.0, 2.0);
        let output = block.process(&parameters, &ctxt, input);
        assert_eq!(output, 1.0);
        assert_eq!(block.data.scalar(), 1.0);
    }

    #[test]
    fn test_switch_block_7_scalars() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(f64, f64, f64, f64, f64, f64, f64, f64)>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[
            0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
        ]));

        let input = (6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0);
        let output = block.process(&parameters, &ctxt, input);
        assert_eq!(output, 7.0);
        assert_eq!(block.data.scalar(), 7.0);
    }

    #[test]
    fn test_switch_block_scalar_default() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(f64, f64, f64)>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[0.0, 1.0]));

        // Should use the last value by default
        let input = (1.2345, 1.0, 2.0);
        let output = block.process(&parameters, &ctxt, input);
        assert_eq!(output, 2.0);
        assert_eq!(block.data.scalar(), 2.0);
    }

    #[test]
    fn test_switch_block_2_matrices() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(f64, Matrix<3, 3, f64>, Matrix<3, 3, f64>)>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[0.0, 1.0]));

        let input = (0.0, &Matrix::from_element(1.0), &Matrix::from_element(2.0));
        let output = block.process(&parameters, &ctxt, input);
        let expected = Matrix::from_element(1.0);
        assert_eq!(output, &expected);
        assert_eq!(
            block.data.get_data().as_slice(),
            expected.data.as_flattened()
        );
    }

    #[test]
    fn test_switch_block_7_matrices() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(
            f64,
            Matrix<3, 3, f64>,
            Matrix<3, 3, f64>,
            Matrix<3, 3, f64>,
            Matrix<3, 3, f64>,
            Matrix<3, 3, f64>,
            Matrix<3, 3, f64>,
            Matrix<3, 3, f64>,
        )>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[
            0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
        ]));

        let input = (
            6.0,
            &Matrix::from_element(1.0),
            &Matrix::from_element(2.0),
            &Matrix::from_element(3.0),
            &Matrix::from_element(4.0),
            &Matrix::from_element(5.0),
            &Matrix::from_element(6.0),
            &Matrix::from_element(7.0),
        );
        let output = block.process(&parameters, &ctxt, input);
        let expected = Matrix::from_element(7.0);
        assert_eq!(output, &expected);
        assert_eq!(
            block.data.get_data().as_slice(),
            expected.data.as_flattened()
        );
    }

    #[test]
    fn test_switch_block_matrix_default() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(f64, Matrix<3, 3, f64>, Matrix<3, 3, f64>)>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[0.0, 1.0]));

        // Should use the last value by default
        let input = (
            1.2345,
            &Matrix::from_element(1.0),
            &Matrix::from_element(2.0),
        );
        let output = block.process(&parameters, &ctxt, input);
        let expected = Matrix::from_element(2.0);
        assert_eq!(output, &expected);
        assert_eq!(
            block.data.get_data().as_slice(),
            expected.data.as_flattened()
        );
    }

    #[test]
    fn test_switch_block_2_bytes() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(f64, ByteSliceSignal, ByteSliceSignal)>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[0.0, 1.0]));

        let input = (0.0, b"foo".as_slice(), b"bar".as_slice());
        let output = block.process(&parameters, &ctxt, input);
        assert_eq!(output, b"foo");
        assert_eq!(block.data.raw_string().as_bytes(), b"foo".as_slice());
    }

    #[test]
    fn test_switch_block_2_bytes_default() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(f64, ByteSliceSignal, ByteSliceSignal)>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[0.0, 1.0]));

        // Should use the last value by default
        let input = (1.2345, b"foo".as_slice(), b"bar".as_slice());
        let output = block.process(&parameters, &ctxt, input);
        assert_eq!(output, b"bar");
        assert_eq!(block.data.raw_string().as_bytes(), b"bar".as_slice());
    }

    #[test]
    fn test_switch_block_7_bytes() {
        let ctxt = StubContext::default();

        let mut block = SwitchBlock::<(
            f64,
            ByteSliceSignal,
            ByteSliceSignal,
            ByteSliceSignal,
            ByteSliceSignal,
            ByteSliceSignal,
            ByteSliceSignal,
            ByteSliceSignal,
        )>::default();
        let parameters = Parameters::new(&OldBlockData::from_vector(&[
            0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
        ]));

        let input = (
            6.0,
            b"foo".as_slice(),
            b"bar".as_slice(),
            b"baz".as_slice(),
            b"qux".as_slice(),
            b"quux".as_slice(),
            b"corge".as_slice(),
            b"grault".as_slice(),
        );
        let output = block.process(&parameters, &ctxt, input);
        assert_eq!(output, b"grault");
        assert_eq!(block.data.raw_string().as_bytes(), b"grault".as_slice());
    }
}