Skip to content

Instantly share code, notes, and snippets.

@devongovett
Created May 27, 2012 19:12
Show Gist options
  • Save devongovett/2815567 to your computer and use it in GitHub Desktop.
Save devongovett/2815567 to your computer and use it in GitHub Desktop.
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