Skip to content

Instantly share code, notes, and snippets.

@skrat
Last active December 18, 2015 16:39
Show Gist options
  • Save skrat/5813472 to your computer and use it in GitHub Desktop.
Save skrat/5813472 to your computer and use it in GitHub Desktop.
Per class storage
Function::property = (prop, desc) ->
Object.defineProperty @prototype, prop, desc
typed_concat = (type, a, b) ->
alen = a.length
result = new type(alen + b.length)
result.set(a)
result.set(b, alen)
result
class vec3i
X = 0
Y = 1
Z = 2
SIZE = 3
PREALLOCATE = 50
TYPE = Int32Array
DATA = new TYPE()
NREF = 0
@ensure_capacity: ->
if DATA.length < NREF * SIZE
console.log 'inflating universe...'
DATA = typed_concat(TYPE, DATA, new TYPE(PREALLOCATE * SIZE))
constructor: (x, y, z) ->
@offset = NREF * SIZE
NREF += 1
@constructor.ensure_capacity()
@x = x
@y = y
@z = z
@property 'x',
get: -> DATA[@offset + X]
set: (x) -> DATA[@offset + X] = x
@property 'y',
get: -> DATA[@offset + Y]
set: (y) -> DATA[@offset + Y] = y
@property 'z',
get: -> DATA[@offset + Z]
set: (z) -> DATA[@offset + Z] = z
for i in [0..120]
console.log(new vec3i(1,2,i).z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment