diff --git a/hamsters.min.js b/hamsters.min.js index 6def32b5..04e672e1 100644 --- a/hamsters.min.js +++ b/hamsters.min.js @@ -6,4 +6,4 @@ * Copyright: 2015 Austin K. Smith - austin@asmithdev.com * License: Artistic License 2.0 */ -var hamsters={version:"2.0",debug:!1,maxThreads:Math.ceil(1.25*(navigator.hardwareConcurrency||1)),tools:{},runtime:{legacy:!1,queue:{running:[],pending:[]},tasks:[],errors:[],setup:{}}};hamsters.runtime.wakeUp=function(){"use strict";hamsters.tools.isIE=function(e){return new RegExp("msie"+(isNaN(e)?"":"\\s"+e),"i").test(navigator.userAgent)},hamsters.runtime.setup.isLegacy=function(){!window.Worker||-1!==navigator.userAgent.indexOf("Kindle/3.0")||-1!==navigator.userAgent.indexOf("Mobile/8F190")||-1!==navigator.userAgent.indexOf("IEMobile")||hamsters.tools.isIE(10)?hamsters.runtime.legacy=!0:-1!==navigator.userAgent.toLowerCase().indexOf("firefox")&&(window.firefox=window.firefox||!0)},hamsters.isLegacy=function(){return hamsters.runtime.legacy},hamsters.checkErrors=function(){var e=hamsters.runtime.errors||[];return{msg:"There are currently "+e.length+" errors captured in the runtime",total:e.length,errors:e}},hamsters.tools.splitArray=function(e,r){e.length&&!e.slice&&(e=hamsters.runtime.convertArray(e));var t=[],a=0;if(e){for(var n=e.length,s=Math.ceil(n/r);n>a;)t.push(e.slice(a,a+=s));return t}return[]},hamsters.tools.randomArray=function(e,r){if(!e||!r)return void(hamsters.runtime.errors=hamsters.runtime.errors.concat({msg:"Unable to generate random array, missing required params"}));var t={count:e};hamsters.run(t,function(){for(var e=t.count,r=0;e>r;)rtn.data[rtn.data.length]=Math.round(99*Math.random()+1),r++},function(e){r&&r(e)},1,!1,"Int32")},hamsters.runtime.setup.populateElements=function(e,r){for(var t=0,a=e;a>t;t++)hamsters.runtime.setup.getOrCreateElement(t);r&&r.call()},hamsters.runtime.setup.getOrCreateElement=function(e){var r=document.getElementById("hamster"+e)||null;if(!r){var t=hamsters.runtime.giveHamsterWork();return r=document.createElement("script"),r.type="javascript/worker",r.id="hamster"+e,r.text="("+t.toString()+"());",document.getElementsByTagName("head")[0].appendChild(r),r}return r},hamsters.runtime.giveHamsterWork=function(){var work=function(){var params,respond=function(e,r){if(params.dataType){var t=processDataType(params.dataType,e.data);e.data=t.buffer,e.dataType=params.dataType,self.postMessage({results:e||null,msg:r||""},[t.buffer])}else self.postMessage({results:e||null,msg:r||""})},processDataType=function(e,r){if(!e)return r;var t;switch(e.toLowerCase()){case"uint32":t=new Uint32Array(r);break;case"uint16":t=new Uint16Array(r);break;case"uint8":t=new Uint8Array(r);break;case"uint8clamped":t=new Uint8ClampedArray(r);break;case"int32":t=new Int32Array(r);break;case"int16":t=new Int16Array(r);break;case"int8":t=new Int8Array(r);break;case"float32":t=new Float32Array(r);break;case"float64":t=new Float64Array(r);break;default:t=r}return t};self.onmessage=function(e){var rtn={success:!0,data:[]};if(params=e.data,"string"==typeof params&&(params=JSON.parse(e.data)),params.dataType&¶ms.array&&"object"==typeof params.array&&(params.array=processDataType(params.dataType,params.array)),params.fn){var fn=eval("("+params.fn+")");fn&&"function"==typeof fn?(fn(),respond(rtn)):(rtn.success=!1,rtn.error="Missing function",rtn.msg="Error encounted check errors for details",respond(rtn))}}};return work},hamsters.runtime.sort=function(e){"verbose"===hamsters.debug&&console.info("Sorting array using index: "+e);var r=1;return"-"===e[0]&&(r=-1,e=e.substr(1)),function(t,a){var n=t[e]a[e]?1:0;return n*r}},hamsters.run=function(e,r,t,a,n,s){if(!e||!r)return"Error processing for loop, missing params or function";var o=hamsters.runtime.tasks.length;a=a||hamsters.maxThreads,hamsters.runtime.tasks.push({id:o,workers:[],count:0,threads:a,input:[],output:[],callback:t});var i=hamsters.runtime.tasks[o];t=t||null;var u,m={array:[]};for(u in e)e.hasOwnProperty(u)&&"array"!==u&&(m[u]=e[u]);m.fn=r.toString(),m.dataType=s||null;var c=e.array||null;e.array&&1!==i.threads&&(c=hamsters.tools.splitArray(e.array,i.threads));for(var l=0;l20)return e;if(r){for(var t=[],a=0,n=e.length;n>a;a++)t.push(hamsters.runtime.convertArray(e[a]));e=t}var s=[];return s=e.reduce(function(e,r){return e.concat(r)})},hamsters.runtime.getOutput=function(e,r,t){var a;return a=r?hamsters.tools.aggregate(e,t):e},hamsters.runtime.processQueue=function(e,r){var t=hamsters.runtime.queue.pending.shift();t&&hamsters.runtime.newWheel(t.input,t.params,t.aggregate,t.callback,t.taskid,t.workerid,e,r)},hamsters.runtime.terminateHamster=function(e){if(e){if(window.URL.revokeObjectURL(e.uri),e.blob){var r=e.blob.close||e.blob.msClose;r?r.call():delete e.blob}e=null}},hamsters.runtime.trainHamster=function(e,r,t,a,n,s,o){s.onmessage=function(i){var u=hamsters.runtime.queue;0===u.pending.length&&(s.terminate(),setTimeout(hamsters.runtime.terminateHamster(o),4)),u.running.splice(u.running.indexOf(e),1);var m=hamsters.runtime.tasks[a];if(!m)return hamsters.runtime.errors=hamsters.runtime.errors.concat({timeStamp:i.timeStamp,msg:"Error, unable to match thread to task, throwing exception",taskid:a,workerid:n,aggregate:r,callback:t}),void console.error("Fatal Exception, unable to match thread #"+n+" to task #"+a+", cannot continue. Check errors for more details");m.workers.splice(m.workers.indexOf(n),1);var c=!1;0===m.workers.length&&m.count===m.threads&&(c=!0);var l=i.data.results;l.dataType&&"object"==typeof l.data&&(l.data=hamsters.runtime.processDataType(l.dataType,l.data)),m.output[n]=l.data;var h=hamsters.debug;if("verbose"===h&&console.info("Hamster #"+e+" finished @ "+i.timeStamp),c){var p=hamsters.runtime.getOutput(m.output,r,l.dataType);hamsters.runtime.tasks[a]=null,t&&(h&&console.info("Execution Complete! Elapsed: "+(i.timeStamp-m.input[0].start)/1e3+"s"),t(p))}0!==u.pending.length&&hamsters.runtime.processQueue(s,o)},s.onerror=function(r){s.terminate();var t="Error Hamster #"+e+": Line "+r.lineno+" in "+r.filename+": "+r.message;hamsters.runtime.errors=hamsters.runtime.errors.concat({msg:t}),console.error(t)}},hamsters.runtime.convertArray=function(e){for(var r=[],t=0,a=e.length;a>t;t++)r.push(e[t]);return r},hamsters.runtime.processDataType=function(e,r){if(!e)return r;var t;switch(e.toLowerCase()){case"uint32":t=new Uint32Array(r);break;case"uint16":t=new Uint16Array(r);break;case"uint8":t=new Uint8Array(r);break;case"uint8clamped":t=new Uint8ClampedArray(r);break;case"int32":t=new Int32Array(r);break;case"int16":t=new Int16Array(r);break;case"int8":t=new Int8Array(r);break;case"float32":t=new Float32Array(r);break;case"float64":t=new Float64Array(r);break;default:t=r}return t},hamsters.runtime.feedHamster=function(e,r,t){if(t&&r.dataType?r.array=hamsters.runtime.processDataType(r.dataType,t):t&&(r.array=t),r.array&&r.array.buffer){var a=r.array.buffer;r.array=a,e.postMessage(r,[a])}else e.postMessage((window.chrome||window.firefox)&&r.array.length<=35e6?JSON.stringify(r):r)},hamsters.runtime.setup.isLegacy(),hamsters.runtime.setup.populateElements(hamsters.maxThreads)},hamsters.runtime.wakeUp(); \ No newline at end of file +var hamsters={version:"2.0",debug:false,maxThreads:Math.ceil((navigator.hardwareConcurrency||1)*1.25),tools:{},runtime:{legacy:false,queue:{running:[],pending:[]},tasks:[],errors:[],setup:{}}};hamsters.runtime.wakeUp=function(){hamsters.tools.isIE=function(version){return(new RegExp("msie"+(!isNaN(version)?("\\s"+version):""),"i").test(navigator.userAgent))};hamsters.runtime.setup.isLegacy=function(){if(!window.Worker||navigator.userAgent.indexOf("Kindle/3.0")!==-1||navigator.userAgent.indexOf("Mobile/8F190")!==-1||navigator.userAgent.indexOf("IEMobile")!==-1||hamsters.tools.isIE(10)){hamsters.runtime.legacy=true}else{if(navigator.userAgent.toLowerCase().indexOf("firefox")!==-1){window.firefox=window.firefox||true}}};hamsters.isLegacy=function(){return hamsters.runtime.legacy};hamsters.checkErrors=function(){var errors=hamsters.runtime.errors||[];return{msg:"There are currently "+errors.length+" errors captured in the runtime",total:errors.length,errors:errors}};hamsters.tools.splitArray=function(array,n){if(array.length&&!array.slice){array=hamsters.runtime.convertArray(array)}var tasks=[];var i=0;if(array){var len=array.length;var size=Math.ceil((len/n));while(ib[property])?1:0;return result*order}};hamsters.run=function(params,fn,callback,workers,aggregate,dataType){if(!params||!fn){return"Error processing for loop, missing params or function"}var taskid=hamsters.runtime.tasks.length;workers=workers||hamsters.maxThreads;hamsters.runtime.tasks.push({id:taskid,workers:[],count:0,threads:workers,input:[],output:[],callback:callback});var task=hamsters.runtime.tasks[taskid];callback=(callback||null);var hamsterfood={array:[]};var key;for(key in params){if(params.hasOwnProperty(key)){if(key!=="array"){hamsterfood[key]=params[key]}}}hamsterfood.fn=fn.toString();hamsterfood.dataType=dataType||null;var workArray=params.array||null;if(params.array&&task.threads!==1){workArray=hamsters.tools.splitArray(params.array,task.threads)}var i=0;while(i20){return input}if(dataType){var out=[];for(var i=0,len=input.length;i