Skip to content

Instantly share code, notes, and snippets.

View gliese1337's full-sized avatar

Logan Kearsley gliese1337

View GitHub Profile
@gliese1337
gliese1337 / Matrix.js
Created June 22, 2011 21:16
Map a 2D array onto an underlying contiguous 1D array.
Matrix = (function(){
function id(){return this;}
function _set(idx,src){this[idx]=src;}
function _get(idx){return this[idx];}
Array.prototype.subarray = function(start,end){
var j,row = {};
for(j=0;start<end;j++,start++){
Object.defineProperty(row,j,{
set:_set.bind(this,start),
@gliese1337
gliese1337 / DataView-polyfill.js
Created June 21, 2011 05:06
A simple implementation of the DataView ArrayBuffer wrapper.
var DataView = (function(){
var littleEndian = (
(new Uint16Array((new Uint8Array([0x12, 0x34])).buffer)[0] === 0x3412)
?true:false); //Determine native endianness
function dv(buffer){
this.buffer = buffer;
this.bytes = new Uint8Array(buffer);
}
@gliese1337
gliese1337 / mozilla-readasarraybuffer.js
Created June 21, 2011 04:07 — forked from also/mozilla-readasarraybuffer.js
readAsArrayBuffer polyfill for Firefox 4 with caching
if (!FileReader.prototype.readAsArrayBuffer) {
FileReader.prototype.readAsArrayBuffer = function readAsArrayBuffer () {
this.readAsBinaryString.apply(this, arguments);
this.__defineGetter__('resultString', this.__lookupGetter__('result'));
Object.defineProperty(this, 'result', {
get : function () {
var string = this.resultString,
result = new Uint8Array(string.length);
for (var i = 0; i < string.length; i++) {
result[i] = string.charCodeAt(i);