All files / src/gates nand.ts

100% Statements 35/35
100% Branches 5/5
100% Functions 6/6
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 498x       3x 3x 3x 1x 1x 1x   6x   3x 1x 1x   3x 7x 3x 10x 7x 17x     3x     3x 3x 23x 23x 23x 84x 84x 84x 84x   23x           3x     8x  
import {stde} from '../stde';
 
class Nand extends stde {
    constructor(group_count: number[] | number, in_count?: number) {
        let seed = 1;
        let in_carr: number[] = [];
        if (group_count instanceof Array) {
            in_carr = group_count;
            group_count = group_count.length;
            seed = Math.max(...in_carr);
        }  else {
            in_carr = Array.from({length: group_count}, () => 1);
        }
        if (in_count) {
            seed = in_count;
            in_carr = Array(group_count).fill(in_count).flat();
        }
        let in_arr: string[] = [];
        let out_arr: string[] = Array.from({length: seed}, (_, k) => String(k + 1));
        in_carr.forEach((i, idx) => {
            if (i > 1) {
                for (let j = 0; j < i; j++) {
                    in_arr.push(String.fromCharCode(idx + 65) + '_' + String(j + 1));
                }
            } else {
                in_arr.push(String.fromCharCode(idx + 65));
            }
        });
        let start = parseInt('1'.repeat(seed), 2);
        super('NAND', in_arr, out_arr, (a) => {
            let currentIndex = 0;
            let st = start;
            for (let i = 0; i < group_count; i++) {
                const groupSize = in_carr[i];
                const substring = a.slice(currentIndex, currentIndex + groupSize);
                st = st & parseInt(substring.padStart(seed, '1'), 2);
                currentIndex += groupSize;
            }
            return (~st >>> 0).toString(2).slice(-seed).padStart(seed, '1');
        });
    }
}
 
function nand(group_count: number | number[], in_count?: number) {
    return new Nand(group_count, in_count);
}
 
export {Nand, nand};