mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 11:16:42 +01:00
Begining of wasm
This commit is contained in:
52
src/streamfromarray_txt.js
Normal file
52
src/streamfromarray_txt.js
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
const Readable = require("stream").Readable;
|
||||
|
||||
module.exports = function streamFromArrayTxt(ma) {
|
||||
const rs = Readable();
|
||||
|
||||
let curIndex = getFirstIdx(ma);
|
||||
|
||||
rs._read = function() {
|
||||
let res;
|
||||
res = objFromIdx(ma, curIndex);
|
||||
curIndex = nextIdx(curIndex);
|
||||
if (res!==null) {
|
||||
rs.push(res + "\n");
|
||||
} else {
|
||||
rs.push(null);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return rs;
|
||||
|
||||
|
||||
function getFirstIdx(ma) {
|
||||
if (!Array.isArray(ma)) return [];
|
||||
return [0, ...getFirstIdx(ma[0])];
|
||||
}
|
||||
|
||||
function nextIdx(idx) {
|
||||
if (idx == null) return null;
|
||||
if (idx.length == 0) return null;
|
||||
|
||||
const parentIdx = idx.slice(0,-1);
|
||||
|
||||
const itObj = objFromIdx(ma, parentIdx);
|
||||
const newLastIdx = idx[idx.length-1]+1;
|
||||
if (newLastIdx < itObj.length) {
|
||||
const resIdx = idx.slice();
|
||||
resIdx[resIdx.length-1] = newLastIdx;
|
||||
return [...resIdx, ...getFirstIdx(itObj[newLastIdx])];
|
||||
} else {
|
||||
return nextIdx(parentIdx);
|
||||
}
|
||||
}
|
||||
|
||||
function objFromIdx(ma, idx) {
|
||||
if (idx == null) return null;
|
||||
if (idx.length == 0) return ma;
|
||||
if (ma.length == 0) return "";
|
||||
return objFromIdx(ma[idx[0]], idx.slice(1));
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user