Files
eacp_webapp/public/scripts/expand.js
T
2026-06-16 11:24:50 +08:00

26 lines
1.1 KiB
JavaScript

var array = {
expand : function(source,size){
var target = new Array(size);
var span = (size / source.length);
for(var i = 0 ; i < source.length ;i++ ){
var range = [ Math.floor(i * span) , Math.floor(( i + 1 ) * span)];
target.fill(source[i],...range);
}
return target;
},
extract : function(source,size){
var target = new Array(size);
var span = (source.length / size);
for(var i = 0 ; i < size ;i++ ){
var range = [ Math.floor(i * span) , Math.floor(( i + 1 ) * span)];
target[i] = Math.max.apply(null,source.slice(...range));
}
return target;
}
}
// 数组原型对象重新赋值一个函数, 新定义的expand与extract是要赋值的函数
Array.prototype.expand = function (size) { return array.expand(this,size);}
Array.prototype.extract = function (size) { return array.extract(this,size); }
Uint8Array.prototype.__proto__.expand = function (size) { return array.expand(this,size);}
Uint8Array.prototype.__proto__.extract = function (size) { return array.extract(this,size); }