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
//#![feature(const_generics, const_evaluatable_checked)]
#![feature(array_methods)]
#![feature(array_map)]

#![cfg_attr(not(feature = "std"), no_std)]

#![allow(dead_code)]

mod dimutils;
pub mod broadcast;


#[cfg(test)]
mod tests {
    use crate::dimutils::*;
    use crate::broadcast::*;


    fn linear_iter_test<const N : usize>(size:[usize;N]) {
        let index_coeffs = linearization_coeffs(size);
        let indices = ndim_iterator(size);
        for (i,index) in indices.enumerate() {
            let lin_index = linearize_index(index,index_coeffs);
            assert_eq!(i,lin_index);
            assert_eq!(delinearize_index(lin_index,size),index);
        }

    }

    #[test]
    fn linearization() {
        linear_iter_test([3,3]);
        linear_iter_test([3,4]);
        linear_iter_test([3,2,4,2]);
        assert_eq!(linearization_coeffs([4]), [1]);
        assert_eq!(linearization_coeffs([4,3]), [1,4]);
        let mut a = ndim_iterator([3,4]);
        let mut b = ndim_iterator([3,4]);
        b.next();
        b.next();
        assert_eq!(a.nth(2),b.next())
    }


    fn game_of_life_rule(cell : u8, nbsum : u8) -> u8 {
        match (cell,nbsum) {
          (0,3) | (1,3) | (1,2)=> 1,
          _ => 0  
        }
    }
    
    
    fn game_of_life_update<'a>(bc : &impl Broadcastable<2,Element=u8>,bc_target : &mut impl BroadcastReceiver<2,Element=u8> ) {
        const NBOFFSETS : [[isize;2];8] = [[1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]];
        let nbcells_separate = NBOFFSETS.map(|offset| bc.offset_mod(offset));
        let nbcells = broadcast_k(nbcells_separate.each_ref());
        let nb_sum = nbcells.bmap(|[a,b,c,d,e,f,g,h] : [u8;8]| a + b + c + d + e + f + g + h);
        let nbsum_cell = nb_sum.broadcast2(bc);
        let newcells = nbsum_cell.bmap(|(nbsum,cell) : (u8,u8)| game_of_life_rule(cell, nbsum));
        newcells.feedto(bc_target);
    }

    #[test]
    fn game_of_life_test() {
        let mut gliderboard = [0, 1, 0, 0, 0,
                               0, 0, 1, 0, 0,
                               1, 1, 1, 0, 0,
                               0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0].reshaped([5,5]);
        let mut scratchpad = [0;5*5].reshaped([5,5]);

        for _ in 0..8 {
            game_of_life_update(&gliderboard, &mut scratchpad);
            let tmp = gliderboard;
            gliderboard = scratchpad;
            scratchpad = tmp;
        }

        let glider_8th_gen = [0, 0, 0, 0, 0,
                              0, 0, 0, 0, 0,
                              0, 0, 0, 1, 0,
                              0, 0, 0, 0, 1,
                              0, 0, 1, 1, 1].reshaped([5,5]);
        assert_eq!(gliderboard,glider_8th_gen);
    }

    #[test]
    fn it_works() {
        let v = vec![1,2,3];
        let u = vec![2,2,2];
        let bv = (v).lazy_updim([3,4]).into_iter().collect::<Vec<i64>>();
        let vdotu : i64 = u.broadcast2(&v).bmap(|(x,y)|x*y).iter().sum();
        assert_eq!(vdotu,1*2 + 2*2 + 3*2);
    
        println!("The elems of bv are {:?}",bv);
        for x in &bv {
            println!("{}",x);
        }

        assert_eq!(2 + 2, 4);
    }
}