nv-buf-resize-mthd 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +84 -0
- package/index.js +110 -0
- package/package.json +15 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type IntLike = number;
|
|
2
|
+
export type UintLike = number;
|
|
3
|
+
export type TypedArray =
|
|
4
|
+
| Int8Array
|
|
5
|
+
| Uint8Array
|
|
6
|
+
| Uint8ClampedArray
|
|
7
|
+
| Int16Array
|
|
8
|
+
| Uint16Array
|
|
9
|
+
| Int32Array
|
|
10
|
+
| Uint32Array
|
|
11
|
+
| Float32Array
|
|
12
|
+
| Float64Array
|
|
13
|
+
| BigInt64Array
|
|
14
|
+
| BigUint64Array;
|
|
15
|
+
|
|
16
|
+
export interface ResizeCfg {
|
|
17
|
+
stride: number
|
|
18
|
+
expand_ratio: number
|
|
19
|
+
aloc?: (oldBuf: TypedArray, newLen: number) => TypedArray
|
|
20
|
+
copy?: (src: TypedArray, dst: TypedArray, cnt: number) => void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface BufCore<TA extends TypedArray = TypedArray> {
|
|
24
|
+
buf: TA
|
|
25
|
+
ei: number
|
|
26
|
+
|
|
27
|
+
length: number
|
|
28
|
+
|
|
29
|
+
capacity(): number
|
|
30
|
+
|
|
31
|
+
get(unit_index: UintLike): any
|
|
32
|
+
|
|
33
|
+
_set(
|
|
34
|
+
unit_index: UintLike,
|
|
35
|
+
v: number | TA
|
|
36
|
+
): number
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface BufMethods<TA extends TypedArray = TypedArray> {
|
|
40
|
+
size(): number
|
|
41
|
+
empty(): boolean
|
|
42
|
+
is_full(): boolean
|
|
43
|
+
|
|
44
|
+
dflt_incr_cnt_when_expand(): number
|
|
45
|
+
dflt_decr_cnt_when_shrink(): number
|
|
46
|
+
|
|
47
|
+
get_lst(): any
|
|
48
|
+
|
|
49
|
+
_expand(incr_cnt: number): void
|
|
50
|
+
expand(): number
|
|
51
|
+
|
|
52
|
+
push(v: any): any
|
|
53
|
+
set(unit_index: number, v: any): any
|
|
54
|
+
pop(): any
|
|
55
|
+
|
|
56
|
+
_shrink(decr_cnt: number): void
|
|
57
|
+
|
|
58
|
+
shrink_to_fit(): number
|
|
59
|
+
shrink(decr_cnt: number): number
|
|
60
|
+
|
|
61
|
+
resize(cnt: number): number
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type BufLike<TA extends TypedArray = TypedArray> =
|
|
65
|
+
BufCore<TA> & BufMethods<TA> & ResizeCfg
|
|
66
|
+
|
|
67
|
+
export type NeedImplDesc = {
|
|
68
|
+
type: "accessor" | "getter" | "method"
|
|
69
|
+
value_type?: string
|
|
70
|
+
param_types?: Record<string, string>
|
|
71
|
+
rtrn_type?: string
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const NEED_IMPLS: Record<string, NeedImplDesc>
|
|
75
|
+
|
|
76
|
+
export const IMPLED_MTHDS: BufMethods
|
|
77
|
+
|
|
78
|
+
export const PX_HANDLES: ProxyHandler<any>
|
|
79
|
+
|
|
80
|
+
export function add_impled_mthds<T extends new (...args: any[]) => any>(
|
|
81
|
+
BufCls: T
|
|
82
|
+
): void
|
|
83
|
+
|
|
84
|
+
export function creat_px<T extends object>(target: T): T
|
package/index.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
|
|
2
|
+
const {
|
|
3
|
+
fmt_cfg,
|
|
4
|
+
DFLT_ALOC,DFLT_COPY,
|
|
5
|
+
DFLT_CFG,
|
|
6
|
+
_expand,
|
|
7
|
+
_shrink
|
|
8
|
+
} = require("nv-buf-resize-util")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const NEED_IMPLS = {
|
|
13
|
+
"buf" : {type:"accessor", value_type:'typed_array'}, //ta typed-array
|
|
14
|
+
"ei" : {type:"accessor", value_type:'intlike'},
|
|
15
|
+
"length" : {type:"getter" , value_type:'intlike'},
|
|
16
|
+
"capacity": {type:"method" , param_types:{/*{[name]:<value_type>}*/},rtrn_type:'intlike'},
|
|
17
|
+
"get" : {type:"method" , param_types:{unit_index:"uintlike"},rtrn_type:'intlike'},
|
|
18
|
+
"_set" : {type:"method" , param_types:{unit_index:"uintlike",v:"intlike@(if_stride_eq_1) | typed_array@(if_stride_gt_1)"},rtrn_type:'intlike'},
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
const IMPLED_MTHDS = {
|
|
24
|
+
size: function() {return this.ei;},
|
|
25
|
+
empty: function() {return this.ei === 0;},
|
|
26
|
+
is_full:function() {return this.ei === this.capacity();},
|
|
27
|
+
dflt_incr_cnt_when_expand : function() { return Math.ceil(this.capacity()*this.expand_ratio)},
|
|
28
|
+
dflt_decr_cnt_when_shrink : function() { return this.capacity() - this.ei;},
|
|
29
|
+
get_lst : function() {return this.get(this.ei -1);},
|
|
30
|
+
_expand: function(incr_cnt) {this.buf = _expand(this.buf,incr_cnt,this); },
|
|
31
|
+
expand: function() {var incr_cnt = this.dflt_incr_cnt_when_expand(); this._expand(incr_cnt); return incr_cnt;},
|
|
32
|
+
push: function(v) {
|
|
33
|
+
if(!this.is_full()) {} else {this._expand(this.dflt_incr_cnt_when_expand());}
|
|
34
|
+
this._set(this.ei,v);
|
|
35
|
+
++this.ei;
|
|
36
|
+
return v;
|
|
37
|
+
},
|
|
38
|
+
set: function(unit_index,v) {
|
|
39
|
+
while(unit_index>= this.capacity()) { this._expand(this.dflt_incr_cnt_when_expand());}
|
|
40
|
+
this._set(unit_index,v);
|
|
41
|
+
if(unit_index < this.ei) {} else {this.ei = unit_index +1;}
|
|
42
|
+
return v;
|
|
43
|
+
},
|
|
44
|
+
pop: function() {
|
|
45
|
+
var v = this.get_lst();
|
|
46
|
+
--this.ei;
|
|
47
|
+
return v;
|
|
48
|
+
},
|
|
49
|
+
_shrink: function(decr_cnt) {this.buf = _shrink(this.buf,decr_cnt,this);},
|
|
50
|
+
shrink_to_fit: function() {
|
|
51
|
+
var decr_cnt = this.decr_cnt_when_shrink();
|
|
52
|
+
this.buf = _shrink(this.buf,decr_cnt,this)
|
|
53
|
+
return decr_cnt;
|
|
54
|
+
},
|
|
55
|
+
shrink: function(decr_cnt) {
|
|
56
|
+
var mx_decr_cnt = this.decr_cnt_when_shrink();
|
|
57
|
+
decr_cnt = (decr_cnt < mx_decr_cnt)?decr_cnt:mx_decr_cnt;
|
|
58
|
+
this._shrink(decr_cnt);
|
|
59
|
+
return decr_cnt;
|
|
60
|
+
},
|
|
61
|
+
resize: function(cnt) {
|
|
62
|
+
var cap = this.capacity();
|
|
63
|
+
if(cnt>cap) {
|
|
64
|
+
this._expand(cnt-cap);
|
|
65
|
+
} else if(cnt === cap) {
|
|
66
|
+
} else {
|
|
67
|
+
this._shrink(cap-cnt);
|
|
68
|
+
}
|
|
69
|
+
return cnt;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
const PX_HANDLES = {
|
|
75
|
+
get (target, k, receiver) {
|
|
76
|
+
var i = Number(k);
|
|
77
|
+
if(Number.isInteger(i)) {
|
|
78
|
+
return target.get(i)
|
|
79
|
+
} else {
|
|
80
|
+
return Reflect.get(target,k);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
set (target, k, v,receiver) {
|
|
84
|
+
var i = Number(k);
|
|
85
|
+
if(Number.isInteger(i)) {
|
|
86
|
+
target.set(i,v);
|
|
87
|
+
} else {
|
|
88
|
+
Reflect.set(target,k);
|
|
89
|
+
}
|
|
90
|
+
return true
|
|
91
|
+
},
|
|
92
|
+
deleteProperty(target, k) {return false;},
|
|
93
|
+
getOwnPropertyDescriptor(target, prop) {return { configurable:true,enumerable: true,writable:true};},
|
|
94
|
+
ownKeys(target) {return Reflect.ownKeys(target);},
|
|
95
|
+
has(target, k) {return target.get(k) !== undefined;},
|
|
96
|
+
getPrototypeOf(target) {return null;}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const add_impled_mthds = (BufCls)=>{
|
|
100
|
+
for(let name in IMPLED_MTHDS) {
|
|
101
|
+
BufCls.prototype[name] = IMPLED_MTHDS[name];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = add_impled_mthds;
|
|
106
|
+
module.exports.creat_px = (target)=>new Proxy(target,PX_HANDLES);
|
|
107
|
+
////
|
|
108
|
+
module.exports.NEED_IMPLS = NEED_IMPLS;
|
|
109
|
+
module.exports.IMPLED_MTHDS = IMPLED_MTHDS;
|
|
110
|
+
module.exports.PX_HANDLES = PX_HANDLES;
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nv-buf-resize-mthd",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"nv-buf-resize-util": "^1.0.0"
|
|
7
|
+
},
|
|
8
|
+
"devDependencies": {},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"description": ""
|
|
15
|
+
}
|