views:

1668

answers:

3

I would like to take advantage of all the goodness of the newer Vector class for FP10, but it seems it is marked as final.

I am doing some intensive mathematical processing in Actionscript, and repeatedly process arrays of Numbers. I have previously been using my own subclass of Array(I call it NumericArray), with added functions such as sum(), mean(), add(), multiply(), etc. This works very well and allows for some clean OO code. However, I am finding through profiling that about 95% of my processing time occurs in the functions of these objects. I need more performance out of these arrays.

I want to use a Vector, as it provides some performance enhancements. I want to specifically use a Vector.<Number>. Unfortunately, I cannot subclass Vector as it is marked final.

What is the best and cleanest way to imitate what I was previously doing with a subclass of Array, to a Vector.<Number>?

I have thought about passing around Vector.<Number> variables instead of my custom class and just using utility functions to manipulate, but this is not good OO design and will be a pain to use, not to mention ugly.

+3  A: 

If adding your additional functionality doesn't require access to protected properties/methods of Vector, you could create a wrapper class for the Vector. Something along these lines?

import flash.utils.Proxy;
import flash.utils.flash_proxy;

use namespace flash_proxy;

public class NumericVector extends Proxy
{
     private var vector:Vector.<Number>;

     public function NumericVector(vector:Vector.<Number> = null)
     {
          if(vector == null)
          {
              this.vector = new Vector.<Number>();
          }
          else
          {
              this.vector = vector;
          }
     }

     override flash_proxy function nextName(index:int):String 
     {
         return vector[index - 1].toString();
     }

     override flash_proxy function nextNameIndex(index:int):int
     {
         // implementation
     }

     public function sum():Number
     {
         // do whatever you intend to do
     }

     ...
}
Stiggler
Yes, composition is the other option, it's limitation is that I cannot treat a NumericVector like a regular vector, for example, iteration, or accessing using the indices, like `myVec[0]`. I will have to come up with some clunky functions to do things like `myVec.set(0, newVal)` instead of `myVec[0] = newVal;` or `myVec.get(0)` instead of `myVec[0]`.
Kekoa
You can in fact implement this behavior by having the NumericVector extend AS3's Proxy class. I can't say how this will impact speed though...
Stiggler
Thanks, I was unaware of the Proxy class, I'll look into it. I'm not so concerned about the speed of adding/modifying items in the NumericVector class. I am concerned with the speed of all the auxiliary functions that require many iterations, and these will be done straight on the Vector within the class.
Kekoa
+1  A: 

A way to sidestep this issue might be to use the as3ds (short for actionscript 3 data structures). Whether they can be faster than using Vector, I'm not sure.

grapefrukt
Looks like a great library, thanks!
Kekoa
A: 

How come on this page http://help.adobe.com/en%5FUS/AS3LCR/Flash%5F10.0/Vector.html

it says: "Note: To override this method in a subclass of Vector, use ...args for the parameters, as this example shows:"

??

doesn't that imply that you can subclass a Vector?

James

Looks like flash 10 :-p
widgisoft