I wonder if there is an equivalent of c/++ union in Javascript? I need to use it as a library I use for an Html5 game wants some fixed variable names for the object I pass to a function of this library however it is much easier for me to keep the data in an array for easier calculation. To give an example, say there is a function 'F' in the library which takes a transformation matrix as a parameter. The parameter must have variable names 'a', 'b', ... 'f' which correspond to matrix elements(m[0][0], m[0][1] ...) consecutively. I have my own matrix class for calculations in which I use an array. I know that entering the parameter 'on the fly', as shown below, sorts out my problem however I don't want to do that every time I call the function nor I want to write a proxy function.
F({a:m[0][0], b:m[0][1], c:[0][2], d:m[1][0], e:m[1][1], f:[1][2]});
is there any way around that such as union?
I wonder if there is an equivalent of c/++ union in Javascript? I need to use it as a library I use for an Html5 game wants some fixed variable names for the object I pass to a function of this library however it is much easier for me to keep the data in an array for easier calculation. To give an example, say there is a function 'F' in the library which takes a transformation matrix as a parameter. The parameter must have variable names 'a', 'b', ... 'f' which correspond to matrix elements(m[0][0], m[0][1] ...) consecutively. I have my own matrix class for calculations in which I use an array. I know that entering the parameter 'on the fly', as shown below, sorts out my problem however I don't want to do that every time I call the function nor I want to write a proxy function.
F({a:m[0][0], b:m[0][1], c:[0][2], d:m[1][0], e:m[1][1], f:[1][2]});
is there any way around that such as union?
Share edited Apr 13, 2012 at 0:51 hevi asked Apr 12, 2012 at 23:29 hevihevi 2,7763 gold badges39 silver badges53 bronze badges 1- 5 Can you maybe show an example of what you're trying to do? – Michael Burr Commented Apr 12, 2012 at 23:32
3 Answers
Reset to default 6No, there's not.
There is no concept of a union, but since the language is loosely-typed you should never need anything of the sort.
You can change the type that is in a variable on the fly (and of course you can use an object with properties which are also loosely typed).
Until struct
support is available,
the most similar thing to struct
s / union
s which you can do for data access performance is to use a Uint8Array[8]
and cast it to the 8 bytes of a 64-bit Number
.
In fact you would have a much longer Uint8Array[n*8]
(or other TypedArray
), and access each 8-bit contiguous set of elements as a single fake struct
, or as a pair of fake
structs, for instance.
I'd suggest no bigger than 64 bits per fake struct
if possible, but you could also do 128-bit fake struct
s by reading twice, 192-bit by reading 3x etc. It just es down to more reads to pull the relevant data into CPU registers for use.
The benefit you gain, if you tightly pack data into each 64 or 128 bit struct
, is that you maximise the amount of relevant data you pull back from the CPU cache on every read, see cache line widths). To access more fine-grained information within those 64 bits, you can bitshift and bitmask to pull out desired bits only.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744818433a4595468.html
评论列表(0条)