- Timestamp:
- 06/18/15 12:18:21 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FACT++_scripts_refactoring/Hist1D.js
r14763 r18227 5 5 'use strict'; 6 6 7 /**8 *9 * @constructor10 *11 * @param {Interger} nx12 *13 * @param {Number} xmin14 *15 * @param {Number} xmax16 *17 * @returns18 * A sub-classed array with the Hist1D functions added as properties.19 *20 * @example21 * var hist = Hist1D(10, -0.5, 1.5);22 *23 */24 function Hist1D(nx, xmin, xmax)25 {26 /**27 *28 * Array29 *30 */31 var arr = new Array(nx);32 7 33 /** 34 * 35 * @exports arr.get as Hist1D.get 36 * 37 */ 38 arr.get = function(x) 39 { 40 var ix = parseInt(nx*(x-xmin)/(xmax-xmin)); 8 if (!("Hist1D" in this)){ 9 var Hist1D = { 10 /** 11 * 12 * @constructor 13 * 14 * @param {Interger} nx 15 * 16 * @param {Number} xmin 17 * 18 * @param {Number} xmax 19 * 20 * @returns 21 * A sub-classed array with the Hist1D functions added as properties. 22 * 23 * @example 24 * var hist = Hist1D(10, -0.5, 1.5); 25 * 26 */ 27 Hist1D : function(nx, xmin, xmax) 28 { 29 /** 30 * 31 * Array 32 * 33 */ 34 var arr = new Array(nx); 41 35 42 return arr[ix] ? arr[ix] : 0; 43 } 36 /** 37 * 38 * @exports arr.get as Hist1D.get 39 * 40 */ 41 arr.get = function(x) 42 { 43 var ix = parseInt(nx*(x-xmin)/(xmax-xmin)); 44 44 45 /** 46 * 47 * @exports arr.fill as Hist1D.fill 48 * 49 */ 50 arr.fill = function(x, w) 51 { 52 if (!x || x===NaN) 53 return false; 54 55 var ix = parseInt(nx*(x-xmin)/(xmax-xmin)); 56 if (ix<0 || ix>=nx) 57 return false; 58 59 if (!arr[ix]) 60 arr[ix] = 0; 61 62 arr[ix] += w ? w : 1; 63 64 return true; 65 } 66 67 /** 68 * 69 * @exports arr.print as Hist1D.print 70 * 71 */ 72 arr.print = function(len) 73 { 74 if (!len) 75 len = 40; 76 if (len<6) 77 len = 6; 78 79 var sum = arr.reduce(function(a,b){return a+b;}, 0); 80 var max = arr.reduce(function(a,b){return Math.max(a,b);}, 0); 81 82 console.out(""); 83 for (var ix=nx-1; ix>=0; ix--) 84 { 85 var entry = arr[ix] ? arr[ix] : 0; 86 87 var line ="%3d [%3d%] ".$(ix, sum==0 ? 0 : 100*entry/sum); 88 if (arr[ix]) 89 { 90 var val = parseInt(len*arr[ix]/max); 91 var entry = ("%"+val+"s").$(""); 92 entry = entry.replace(/ /g, "*"); 93 line += ("%-"+len+"s").$(entry)+" |"; 45 return arr[ix] ? arr[ix] : 0; 94 46 } 95 47 96 console.out(line); 97 } 48 /** 49 * 50 * @exports arr.fill as Hist1D.fill 51 * 52 */ 53 arr.fill = function(x, w) 54 { 55 if (!x || x===NaN) 56 return false; 98 57 99 var entry = arr[ix] ? arr[ix] : 0; 100 console.out(" --------"+("%"+(len-5)+"s").$("")+"-------"); 58 var ix = parseInt(nx*(x-xmin)/(xmax-xmin)); 59 if (ix<0 || ix>=nx) 60 return false; 101 61 102 var line =" %9d ".$(sum); 103 line += ("%"+len+"d").$(max); 104 console.out(line); 105 console.out(""); 106 } 62 if (!arr[ix]) 63 arr[ix] = 0; 107 64 108 return arr; 65 arr[ix] += w ? w : 1; 66 67 return true; 68 } 69 70 /** 71 * 72 * @exports arr.print as Hist1D.print 73 * 74 */ 75 arr.print = function(len) 76 { 77 if (!len) 78 len = 40; 79 if (len<6) 80 len = 6; 81 82 var sum = arr.reduce(function(a,b){return a+b;}, 0); 83 var max = arr.reduce(function(a,b){return Math.max(a,b);}, 0); 84 85 console.out(""); 86 for (var ix=nx-1; ix>=0; ix--) 87 { 88 var entry = arr[ix] ? arr[ix] : 0; 89 90 var line ="%3d [%3d%] ".$(ix, sum==0 ? 0 : 100*entry/sum); 91 if (arr[ix]) 92 { 93 var val = parseInt(len*arr[ix]/max); 94 var entry = ("%"+val+"s").$(""); 95 entry = entry.replace(/ /g, "*"); 96 line += ("%-"+len+"s").$(entry)+" |"; 97 } 98 99 console.out(line); 100 } 101 102 var entry = arr[ix] ? arr[ix] : 0; 103 console.out(" --------"+("%"+(len-5)+"s").$("")+"-------"); 104 105 var line =" %9d ".$(sum); 106 line += ("%"+len+"d").$(max); 107 console.out(line); 108 console.out(""); 109 } 110 111 return arr; 112 }, 113 }; 109 114 } 115 else{ 116 console.out("multiple include of 'Hist1D.js'") 117 } 118
Note:
See TracChangeset
for help on using the changeset viewer.