Created
May 27, 2012 19:12
-
-
Save devongovett/2815567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DGArray | |
constructor: (Type, @lengths...) -> | |
if @lengths.length is 0 | |
throw new RangeError("Not enough lengths.") | |
length = 1 | |
for l in @lengths | |
length *= l | |
@data = new Type(length) | |
index = (indices, lengths) -> | |
if indices.length < lengths.length | |
throw new RangeError("Not enough indices.") | |
ret = 0 | |
for index, i in indices | |
for q in lengths[i + 1...] | |
index *= q | |
ret += index | |
return ret | |
get: (indices...) -> | |
return @data[index(indices, @lengths)] | |
set: (indices..., val) -> | |
@data[index(indices, @lengths)] = val | |
arr = new DGArray(Int32Array, 3, 4, 5) | |
arr.set(2, 3, 4, 5) | |
console.log arr.get(2, 3, 4) # 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment