mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 03:06:42 +01:00
bigarray
This commit is contained in:
59
src/bigarray.js
Normal file
59
src/bigarray.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const SUBARRAY_SIZE = 0x10000;
|
||||
|
||||
const BigArrayHandler = {
|
||||
get: function(obj, prop) {
|
||||
if (!isNaN(prop)) {
|
||||
return obj.getElement(prop);
|
||||
} else return obj[prop];
|
||||
},
|
||||
set: function(obj, prop, value) {
|
||||
if (!isNaN(prop)) {
|
||||
return obj.setElement(prop, value);
|
||||
} else {
|
||||
obj[prop] = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class _BigArray {
|
||||
constructor (initSize) {
|
||||
this.length = initSize || 0;
|
||||
this.arr = [];
|
||||
|
||||
for (let i=0; i<initSize; i+=SUBARRAY_SIZE) {
|
||||
this.arr[i/SUBARRAY_SIZE] = new Array(Math.min(SUBARRAY_SIZE, initSize - i));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
push (element) {
|
||||
this.setElement (this.length, element);
|
||||
}
|
||||
getElement(idx) {
|
||||
idx = parseInt(idx);
|
||||
const idx1 = Math.floor(idx / SUBARRAY_SIZE);
|
||||
const idx2 = idx % SUBARRAY_SIZE;
|
||||
return this.arr[idx1] ? this.arr[idx1][idx2] : undefined;
|
||||
}
|
||||
setElement(idx, value) {
|
||||
idx = parseInt(idx);
|
||||
const idx1 = Math.floor(idx / SUBARRAY_SIZE);
|
||||
if (!this.arr[idx1]) {
|
||||
this.arr[idx1] = [];
|
||||
}
|
||||
const idx2 = idx % SUBARRAY_SIZE;
|
||||
this.arr[idx1][idx2] = value;
|
||||
if (idx >= this.length) this.length = idx+1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class BigArray {
|
||||
constructor( initSize ) {
|
||||
const obj = new _BigArray(initSize);
|
||||
const extObj = new Proxy(obj, BigArrayHandler);
|
||||
return extObj;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BigArray;
|
||||
Reference in New Issue
Block a user