js/src/parjs-benchmarks/rectarray.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 // -*- mode: js2; indent-tabs-mode: nil; -*-
michael@0 2
michael@0 3 // A RectArray<X,k> is a subclass of Array with 'width', 'height',
michael@0 4 // and 'payload' properties (where payload holds 'k').
michael@0 5 // properties, and 'width*height*k' elements (each an X) in the array.
michael@0 6 //
michael@0 7 // A RectTypedByteArray<k> is a subclass of ArrayBuffer with 'width'
michael@0 8 // and 'height' properties, and 'width*height*k' bytes in the buffer.
michael@0 9 //
michael@0 10 // The 'payload' property is initialized with the value of 'k'.
michael@0 11 //
michael@0 12 // (Felix would have used Array or ArrayView, but for the following bug:
michael@0 13 // Bug 695438 - TypedArrays don't support new named properties
michael@0 14 // https://bugzilla.mozilla.org/show_bug.cgi?id=695438
michael@0 15 // and so he is resorting to extending ArrayBuffer instead.)
michael@0 16 //
michael@0 17 // Both classes add a .get(x,y[,k]) method that eases access to the
michael@0 18 // contents, and a set(x,y[,k],value) method that eases modifying
michael@0 19 // their entries.
michael@0 20 //
michael@0 21 // In addition, for those who prefer functional-style,
michael@0 22 // RectArray.build,
michael@0 23 // and RectByteTypedArray.build
michael@0 24
michael@0 25 var RectArray, RectByteTypedArray;
michael@0 26
michael@0 27 // This is a variant of RectArray that supports the same interface,
michael@0 28 // but instead of attempting to extend Array (a practice fraught with
michael@0 29 // peril), it instead makes a wrapper around another array.
michael@0 30
michael@0 31 var WrapArray, WrapByteTypedArray;
michael@0 32
michael@0 33 (function (){
michael@0 34
michael@0 35 function defineReadOnly(x, name, value) {
michael@0 36 Object.defineProperty(x, name, {
michael@0 37 value: value,
michael@0 38 writable: false,
michael@0 39 enumerable: true,
michael@0 40 configurable: true });
michael@0 41 }
michael@0 42
michael@0 43 RectArray = function RectArray(w,h,k) {
michael@0 44 if (k === undefined)
michael@0 45 k = 1;
michael@0 46 this.length = w*h*k;
michael@0 47 defineReadOnly(this, "width", w);
michael@0 48 defineReadOnly(this, "height", h);
michael@0 49 defineReadOnly(this, "payload", k);
michael@0 50 };
michael@0 51
michael@0 52 RectByteTypedArray = function RectByteTypedArray(w,h,k) {
michael@0 53 if (k === undefined)
michael@0 54 k = 1;
michael@0 55 ArrayBuffer.call(this, w*h*k);
michael@0 56 defineReadOnly(this, "width", w);
michael@0 57 defineReadOnly(this, "height", h);
michael@0 58 defineReadOnly(this, "payload", k);
michael@0 59 };
michael@0 60
michael@0 61 WrapArray = function WrapArray(w,h,k) {
michael@0 62 if (k === undefined)
michael@0 63 k = 1;
michael@0 64 this.backingArray = new Array(w*h*k);
michael@0 65 defineReadOnly(this, "width", w);
michael@0 66 defineReadOnly(this, "height", h);
michael@0 67 defineReadOnly(this, "payload", k);
michael@0 68 };
michael@0 69
michael@0 70 WrapByteTypedArray = function WrapByteTypedArray(w,h,k) {
michael@0 71 if (k === undefined)
michael@0 72 k = 1;
michael@0 73 this.backingArray = new Uint8Array(new ArrayBuffer(w*h*k));
michael@0 74 defineReadOnly(this, "width", w);
michael@0 75 defineReadOnly(this, "height", h);
michael@0 76 defineReadOnly(this, "payload", k);
michael@0 77 };
michael@0 78
michael@0 79 WrapArray.prototype.slice = function(a,b) this.backingArray.slice(a,b);
michael@0 80 WrapArray.prototype.join = function(a) this.backingArray.join(a);
michael@0 81
michael@0 82 RectArray.prototype = new Array();
michael@0 83 RectByteTypedArray.prototype = new ArrayBuffer();
michael@0 84
michael@0 85 RectArray.prototype.get = function get(x,y,j) {
michael@0 86 if (j === undefined) j = 0;
michael@0 87 return this[(y*this.width+x)*this.payload+j];
michael@0 88 };
michael@0 89
michael@0 90 RectArray.prototype.set = function set(x,y,j,value) {
michael@0 91 if (value === undefined) {
michael@0 92 value = j;
michael@0 93 j = 0;
michael@0 94 }
michael@0 95 this[(y*this.width+x)*this.payload+j] = value;
michael@0 96 };
michael@0 97
michael@0 98 RectByteTypedArray.prototype.get = function get(x,y,j) {
michael@0 99 if (j === undefined) j = 0;
michael@0 100 return (new Uint8Array(this))[(y*this.width+x)*this.payload+j];
michael@0 101 };
michael@0 102
michael@0 103 RectByteTypedArray.prototype.set = function set(x,y,j,value) {
michael@0 104 if (value === undefined) {
michael@0 105 value = j;
michael@0 106 j = 0;
michael@0 107 }
michael@0 108 (new Uint8Array(this))[(y*this.width+x)*this.payload+j] = value;
michael@0 109 };
michael@0 110
michael@0 111 WrapArray.prototype.get = function get(x,y,j) {
michael@0 112 if (j === undefined) j = 0;
michael@0 113 return this.backingArray[(y*this.width+x)*this.payload+j];
michael@0 114 };
michael@0 115
michael@0 116 WrapArray.prototype.set = function set(x,y,j,value) {
michael@0 117 if (value === undefined) {
michael@0 118 value = j;
michael@0 119 j = 0;
michael@0 120 }
michael@0 121 this.backingArray[(y*this.width+x)*this.payload+j] = value;
michael@0 122 };
michael@0 123
michael@0 124 WrapByteTypedArray.prototype.get = function get(x,y,j) {
michael@0 125 if (j === undefined) j = 0;
michael@0 126 return this.backingArray[(y*this.width+x)*this.payload+j];
michael@0 127 };
michael@0 128
michael@0 129 WrapByteTypedArray.prototype.set = function set(x,y,j,value) {
michael@0 130 if (value === undefined) {
michael@0 131 value = j;
michael@0 132 j = 0;
michael@0 133 }
michael@0 134 this.backingArray[(y*this.width+x)*this.payload+j] = value;
michael@0 135 };
michael@0 136
michael@0 137 function viewToSource(view, width, height, payload) {
michael@0 138 var ret = "[";
michael@0 139 var i=0;
michael@0 140 var matrixNeedsNewline = false;
michael@0 141 for (var row=0; row < height; row++) {
michael@0 142 if (matrixNeedsNewline)
michael@0 143 ret += ",\n ";
michael@0 144 ret += "[";
michael@0 145 var rowNeedsComma = false;
michael@0 146 for (var x=0; x < width; x++) {
michael@0 147 if (rowNeedsComma)
michael@0 148 ret += ", ";
michael@0 149 if (payload == 1) {
michael@0 150 if (view[i] !== undefined)
michael@0 151 ret += view[i];
michael@0 152 i++;
michael@0 153 } else {
michael@0 154 var entryNeedsComma = false;
michael@0 155 ret += "(";
michael@0 156 for (var k=0; k < payload; k++) {
michael@0 157 // Might be inefficient (does JavaScript have
michael@0 158 // StringBuffers?, or use them internally, like Tamarin?)
michael@0 159 if (entryNeedsComma)
michael@0 160 ret += ", ";
michael@0 161 if (view[i] !== undefined)
michael@0 162 ret += view[i];
michael@0 163 entryNeedsComma = true;
michael@0 164 i++;
michael@0 165 }
michael@0 166 ret += ")";
michael@0 167 }
michael@0 168 rowNeedsComma = true;
michael@0 169 }
michael@0 170 ret += "]";
michael@0 171 matrixNeedsNewline = true;
michael@0 172 }
michael@0 173 ret += "]";
michael@0 174 return ret;
michael@0 175 }
michael@0 176
michael@0 177 RectArray.prototype.toSource = function toSource() {
michael@0 178 return viewToSource(this,
michael@0 179 this.width, this.height, this.payload);
michael@0 180 };
michael@0 181
michael@0 182 RectByteTypedArray.prototype.toSource = function toSource() {
michael@0 183 return viewToSource(new Uint8Array(this),
michael@0 184 this.width, this.height, this.payload);
michael@0 185 };
michael@0 186
michael@0 187 WrapArray.prototype.toSource = function toSource() {
michael@0 188 return viewToSource(this.backingArray,
michael@0 189 this.width, this.height, this.payload);
michael@0 190 };
michael@0 191
michael@0 192 WrapByteTypedArray.prototype.toSource = function toSource() {
michael@0 193 return viewToSource(this.backingArray,
michael@0 194 this.width, this.height, this.payload);
michael@0 195 };
michael@0 196
michael@0 197 RectArray.prototype.map = function map(f) {
michael@0 198 var ret = Array.map(this, f);
michael@0 199 ret.__proto__ = RectArray.prototype;
michael@0 200 defineReadOnly(ret, "width", this.width);
michael@0 201 defineReadOnly(ret, "height", this.height);
michael@0 202 defineReadOnly(ret, "payload", this.payload);
michael@0 203 return ret;
michael@0 204 };
michael@0 205
michael@0 206 WrapArray.prototype.map = function map(f) {
michael@0 207 var ret = new WrapArray(this.width, this.height, this.payload);
michael@0 208 ret.backingArray = this.backingArray.map(f);
michael@0 209 return ret;
michael@0 210 };
michael@0 211
michael@0 212 // (Array<X>|ArrayView<X>) Nat Nat Nat (Nat Nat Nat -> X) -> void
michael@0 213 function fillArrayView(view, width, height, k, fill) {
michael@0 214 var i = 0;
michael@0 215 for (var y=0; y < height; y++) {
michael@0 216 for (var x=0; x < width; x++) {
michael@0 217 for (var j=0; j < k; j++) {
michael@0 218 view[i++] = fill(x, y, j);
michael@0 219 }
michael@0 220 }
michael@0 221 }
michael@0 222 }
michael@0 223
michael@0 224
michael@0 225 // Nat Nat (Nat Nat [Nat] -> X) -> RectArray<X,1>
michael@0 226 RectArray.build =
michael@0 227 function buildRectArray1(width, height, fill) {
michael@0 228 var a = new RectArray(width, height, 1);
michael@0 229 fillArrayView(a, width, height, 1, fill);
michael@0 230 return a;
michael@0 231 };
michael@0 232
michael@0 233 RectArray.buildA =
michael@0 234 function buildRectArrayA(width, height, fill) {
michael@0 235 var a = new Array(width*height);
michael@0 236 fillArrayView(a, width, height, 1, fill);
michael@0 237 a.width = width;
michael@0 238 a.height = height;
michael@0 239 a.payload = 1;
michael@0 240 function F() { }
michael@0 241 F.prototype = RectArray.prototype;
michael@0 242 a.__proto__ = new F();
michael@0 243 return a;
michael@0 244 };
michael@0 245
michael@0 246 // Nat Nat (Nat Nat Nat -> X) -> RectArray<X,4>
michael@0 247 RectArray.build4 =
michael@0 248 function buildRectArray4(width, height, fill) {
michael@0 249 var a = new RectArray(width, height, 4);
michael@0 250 fillArrayView(a, width, height, 4, fill);
michael@0 251 return a;
michael@0 252 };
michael@0 253
michael@0 254 // Nat Nat (Nat Nat Nat -> X) -> RectArray<X,1>
michael@0 255 RectArray.buildN =
michael@0 256 function buildRectArrayN(width, height, n, fill) {
michael@0 257 var a = new RectArray(width, height, n);
michael@0 258 fillArrayView(a, width, height, n, fill);
michael@0 259 return a;
michael@0 260 };
michael@0 261
michael@0 262
michael@0 263 // Nat Nat (Nat Nat [Nat] -> Byte) -> RectTypedByteArray<4>
michael@0 264 RectByteTypedArray.build =
michael@0 265 function buildRectByteTypedArray1(width, height, fill) {
michael@0 266 var buf = new RectByteTypedArray(width, height, 1);
michael@0 267 fillArrayView(new Uint8Array(buf), width, height, 1, fill);
michael@0 268 return buf;
michael@0 269 };
michael@0 270
michael@0 271 // Nat Nat (Nat Nat Nat -> Byte) -> RectTypedByteArray<4>
michael@0 272 RectByteTypedArray.build4 =
michael@0 273 function buildRectByteTypedArray4(width, height, fill) {
michael@0 274 var buf = new RectByteTypedArray(width, height, 4);
michael@0 275 fillArrayView(new Uint8Array(buf), width, height, 4, fill);
michael@0 276 return buf;
michael@0 277 };
michael@0 278
michael@0 279 // Nat Nat (Nat Nat Nat -> Byte) -> RectTypedByteArray<4>
michael@0 280 RectByteTypedArray.buildN =
michael@0 281 function buildRectByteTypedArray4(width, height, n, fill) {
michael@0 282 var buf = new RectByteTypedArray(width, height, n);
michael@0 283 fillArrayView(new Uint8Array(buf), width, height, n, fill);
michael@0 284 return buf;
michael@0 285 };
michael@0 286
michael@0 287 WrapArray.build =
michael@0 288 function buildWrapArray1(width, height, fill) {
michael@0 289 var a = new WrapArray(width, height, 1);
michael@0 290 fillArrayView(a, width, height, 1, fill);
michael@0 291 return a;
michael@0 292 };
michael@0 293
michael@0 294 WrapArray.build =
michael@0 295 function buildWrapArray1(width, height, fill) {
michael@0 296 var a = new WrapArray(width, height, 1);
michael@0 297 fillArrayView(a.backingArray, width, height, 1, fill);
michael@0 298 return a;
michael@0 299 };
michael@0 300
michael@0 301 WrapArray.build4 =
michael@0 302 function buildWrapArray1(width, height, fill) {
michael@0 303 var a = new WrapArray(width, height, 4);
michael@0 304 fillArrayView(a.backingArray, width, height, 4, fill);
michael@0 305 return a;
michael@0 306 };
michael@0 307
michael@0 308 WrapArray.buildN =
michael@0 309 function buildWrapArray1(width, height, n, fill) {
michael@0 310 var a = new WrapArray(width, height, n);
michael@0 311 fillArrayView(a.backingArray, width, height, n, fill);
michael@0 312 return a;
michael@0 313 };
michael@0 314
michael@0 315 })();

mercurial