-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizable-uint8array.js
55 lines (55 loc) · 1.75 KB
/
resizable-uint8array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var ResizableUint8Array=function(){
this._buffer=new ArrayBuffer(8);
this._data=new Uint8Array(this._buffer);
this._size=0;
};
ResizableUint8Array.prototype.size=function(){
return this._size;
};
ResizableUint8Array.prototype.get=function(index){
return this._data[index];
};
ResizableUint8Array.prototype.set=function(index,value){
this._data[index]=value;
};
ResizableUint8Array.prototype.reserve_extra=function(amount){
if(this._size+amount>this._data.length){
var new_buffer=new ArrayBuffer(Math.max(this._data.length*2,this._size+amount));
var new_data=new Uint8Array(new_buffer);
for(var i=0;i<this._size;++i){
new_data[i]=this._data[i];
}
this._buffer=new_buffer;
this._data=new_data;
}
};
ResizableUint8Array.prototype.push=function(value){
this.reserve_extra(1);
this._data[this._size++]=value;
};
ResizableUint8Array.prototype.append=function(value_uint8array){
this.reserve_extra(value_uint8array.length);
for(var i=0;i<value_uint8array.length;++i){
this._data[this._size++]=value_uint8array[i];
}
};
ResizableUint8Array.prototype.pop=function(){
return this._data[--this._size];
};
ResizableUint8Array.prototype.insert_arr=function(index,value_uint8array){
this.reserve_extra(value_uint8array.length);
for(var i=this._size-1;i>=index;--i){
this._data[i+value_uint8array.length]=this._data[i];
}
for(var i=0;i<value_uint8array.length;++i){
this._data[index+i]=value_uint8array[i];
}
this._size+=value_uint8array.length;
};
ResizableUint8Array.prototype.toUint8Array=function(){
var ret_arr=new Uint8Array(this._size);
for(var i=0;i<this._size;++i){
ret_arr[i]=this._data[i];
}
return ret_arr;
};