arnaucube
arnaucube/circomPublic Self-hosted repository
circom/src/bigarray.js
src/bigarray.jsRaw
 1const SUBARRAY_SIZE = 0x40000;
 2
 3const BigArrayHandler = {
 4    get: function(obj, prop) {
 5        if (!isNaN(prop)) {
 6            return obj.getElement(prop);
 7        } else return obj[prop];
 8    },
 9    set: function(obj, prop, value) {
10        if (!isNaN(prop)) {
11            return obj.setElement(prop, value);
12        } else {
13            obj[prop] = value;
14            return true;
15        }
16    }
17};
18
19class _BigArray {
20    constructor (initSize) {
21        this.length = initSize || 0;
22        this.arr = new Array(SUBARRAY_SIZE);
23
24        for (let i=0; i<initSize; i+=SUBARRAY_SIZE) {
25            this.arr[i/SUBARRAY_SIZE] = new Array(Math.min(SUBARRAY_SIZE, initSize - i));
26        }
27        return this;
28    }
29    push () {
30        for (let i=0; i<arguments.length; i++) {
31            this.setElement (this.length, arguments[i]);
32        }
33    }
34    getElement(idx) {
35        idx = parseInt(idx);
36        const idx1 = Math.floor(idx / SUBARRAY_SIZE);
37        const idx2 = idx % SUBARRAY_SIZE;
38        return this.arr[idx1] ? this.arr[idx1][idx2] : undefined;
39    }
40    setElement(idx, value) {
41        idx = parseInt(idx);
42        const idx1 = Math.floor(idx / SUBARRAY_SIZE);
43        if (!this.arr[idx1]) {
44            this.arr[idx1] = new Array(SUBARRAY_SIZE);
45        }
46        const idx2 = idx % SUBARRAY_SIZE;
47        this.arr[idx1][idx2] = value;
48        if (idx >= this.length) this.length = idx+1;
49        return true;
50    }
51    getKeys() {
52        const newA = new BigArray();
53        for (let i=0; i<this.arr.length; i++) {
54            if (this.arr[i]) {
55                for (let j=0; j<this.arr[i].length; j++) {
56                    if (typeof this.arr[i][j] !== "undefined") {
57                        newA.push(i*SUBARRAY_SIZE+j);
58                    }
59                }
60            }
61        }
62        return newA;
63    }
64}
65
66class BigArray {
67    constructor( initSize ) {
68        const obj = new _BigArray(initSize);
69        const extObj = new Proxy(obj, BigArrayHandler);
70        return extObj;
71    }
72}
73
74module.exports = BigArray;