circom/src/bigarray.js
src/bigarray.jsRaw
1const SUBARRAY_SIZE = 0x10000;
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 = [];
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 (element) {
30 this.setElement (this.length, element);
31 }
32 getElement(idx) {
33 idx = parseInt(idx);
34 const idx1 = Math.floor(idx / SUBARRAY_SIZE);
35 const idx2 = idx % SUBARRAY_SIZE;
36 return this.arr[idx1] ? this.arr[idx1][idx2] : undefined;
37 }
38 setElement(idx, value) {
39 idx = parseInt(idx);
40 const idx1 = Math.floor(idx / SUBARRAY_SIZE);
41 if (!this.arr[idx1]) {
42 this.arr[idx1] = [];
43 }
44 const idx2 = idx % SUBARRAY_SIZE;
45 this.arr[idx1][idx2] = value;
46 if (idx >= this.length) this.length = idx+1;
47 return true;
48 }
49}
50
51class BigArray {
52 constructor( initSize ) {
53 const obj = new _BigArray(initSize);
54 const extObj = new Proxy(obj, BigArrayHandler);
55 return extObj;
56 }
57}
58
59module.exports = BigArray;