Packageorg.flintparticles.common.utils
Classpublic class FastWeightedArray

A FastWeightedArray performes the same purpose as a WeightedArray but this array is optimized to select random items in a large array much more rapidly. In improving the speed if the array, it is necessary to loose the functionality to remove items from the FastWeightedArray, so the original WeightedArray also still available.

The FastWeightedArray is a collection of values that are weighted. When a random value is required from the collection, the value returned is randomly selkected based on the weightings.

Due to the nature of a FastWeightedArray, there are no facilities to push, unshift or splice items into the array. All items are added to the FastWeightedArray using the add method.



Public Properties
 PropertyDefined by
  length : uint
[read-only] The number of items in the FastWeightedArray
FastWeightedArray
  totalRatios : Number
[read-only] The sum of the weights of all the values.
FastWeightedArray
Public Methods
 MethodDefined by
  
Then constructor function is used to create a FastWeightedArray
FastWeightedArray
  
add(value:*, ratio:Number):uint
Adds a value to the FastWeightedArray.
FastWeightedArray
  
clear():void
Empties the FastWeightedArray.
FastWeightedArray
  
Returns a random value from the FastWeightedArray.
FastWeightedArray
Property detail
lengthproperty
length:uint  [read-only]

The number of items in the FastWeightedArray

Implementation
    public function get length():uint
totalRatiosproperty 
totalRatios:Number  [read-only]

The sum of the weights of all the values.

Implementation
    public function get totalRatios():Number
Constructor detail
FastWeightedArray()constructor
public function FastWeightedArray()

Then constructor function is used to create a FastWeightedArray

Method detail
add()method
public function add(value:*, ratio:Number):uint

Adds a value to the FastWeightedArray.

Parameters
value:* — the value to add
 
ratio:Number — the weighting to place on the item

Returns
uint — the length of the FastWeightedArray
clear()method 
public function clear():void

Empties the FastWeightedArray. After calling this method the FastWeightedArray contains no items.

getRandomValue()method 
public function getRandomValue():*

Returns a random value from the FastWeightedArray. The weighting of the values is used when selcting the random value, so items with a higher weighting are more likely to be seleted.

Returns
* — A randomly selected item from the array.