arnaucube
arnaucube/circomPublic Self-hosted repository
circom/src/streamfromarray_txt.js
src/streamfromarray_txt.jsRaw
 1
 2const Readable = require("stream").Readable;
 3
 4module.exports = function streamFromArrayTxt(ma) {
 5    const  rs = Readable();
 6
 7    let curIndex = getFirstIdx(ma);
 8
 9    rs._read = function() {
10        let res;
11        res = objFromIdx(ma, curIndex);
12        curIndex = nextIdx(curIndex);
13        if (res!==null) {
14            rs.push(res + "\n");
15        } else {
16            rs.push(null);
17        }
18    };
19
20
21    return rs;
22
23
24    function getFirstIdx(ma) {
25        if (!Array.isArray(ma)) return [];
26        return [0, ...getFirstIdx(ma[0])];
27    }
28
29    function nextIdx(idx) {
30        if (idx == null) return null;
31        if (idx.length == 0) return null;
32
33        const parentIdx = idx.slice(0,-1);
34
35        const itObj = objFromIdx(ma, parentIdx);
36        const newLastIdx = idx[idx.length-1]+1;
37        if (newLastIdx < itObj.length) {
38            const resIdx = idx.slice();
39            resIdx[resIdx.length-1] = newLastIdx;
40            return [...resIdx, ...getFirstIdx(itObj[newLastIdx])];
41        } else {
42            return nextIdx(parentIdx);
43        }
44    }
45
46    function objFromIdx(ma, idx) {
47        if (idx == null) return null;
48        if (idx.length == 0) return ma;
49        if (ma.length == 0) return "";
50        return objFromIdx(ma[idx[0]], idx.slice(1));
51    }
52};