Packagecom.bourre.structures
Classpublic class Grid
ImplementsCollection, TypedContainer

Player version: Flash Player 9.0
Language version: ActionScript 3.0

A Grid is basically a two dimensions data structure based on the Collection interface.

By default a Grid object is an untyped collection that allow duplicate and null elements. You can set your own default value instead of null by passing it to the grid constructor.

Its also possible to restrict the type of grid elements in the constructor as defined by the TypedContainer interface.

The Grid class don't support all the methods of the Collection interface. Here the list of the unsupported methods :

Instead of using the methods above there are several specific methods to insert data in the grid :

See also

../collection/Collection.html
../collection/TypedContainer.html


Public Methods
 MethodDefined by
  
Grid(size:Dimension, a:Array = null, dV:Object = null, t:Class = null)
Create a new grid of passed-in size.
Grid
  
add(o:Object):Boolean
The add method is unsupported by the Grid class.
Grid
  
addAll(c:Collection):Boolean
The addAll method is unsupported by the Grid class.
Grid
  
addAllAt(p:Point, c:Grid):Boolean
Copy the content of the passed-in grid at the specified coordinates.
Grid
  
clear():void
Removes all of the elements from this collection.
Grid
  
contains(o:Object):Boolean
Returns true if this grid contains at least one occurence of the specified element.
Grid
  
Returns true if this grid contains all of the elements of the specified collection.
Grid
  
fill(o:Object):void
Fill the current grid with the passed-in value.
Grid
  
getCoordinates(id:uint):Point
Returns a Point witch is the corresponding position of the passed-in value.
Grid
  
getDefaulValue():Object
Returns the current default value of this grid used to replace value when removing an element.
Grid
  
Returns the size of the grid as Dimension.
Grid
  
getType():Class
Return the current type allowed in the Grid
Grid
  
getVal(p:Point):Object
Returns the element stored at the passed-in coordinate of the grid.
Grid
  
isEmpty():Boolean
A Grid object is considered as empty if and only if all its cells contains null or the default value for the current Grid.
Grid
  
isGridCoords(p:Point):Boolean
Check if a Point object is a valid coordinate in the current grid.
Grid
  
isTyped():Boolean
Returns true if this grid perform a verification of the type of elements.
Grid
  
Verify that the passed-in Collection is a valid collection for use with the addAll, removeAll, retainAll and containsAll methods.
Grid
  
isValidType(o:Object):Boolean
Verify that the passed-in object type match the current Grid element's type.
Grid
  
Returns an iterator over the elements in this collection.
Grid
  
matchType(o:*):Boolean
Verify if the passed-in object can be inserted in the current Grid.
Grid
  
remove(o:Object):Boolean
Removes a single instance of the specified element from this grid, if this grid contains one or more such elements.
Grid
  
removeAll(c:Collection):Boolean
Removes from this grid all of its elements that are contained in the specified collection (optional operation).
Grid
  
removeAllAt(p:Point, c:Grid):Boolean
Removes the content of the passed-in grid at the specified coordinates.
Grid
  
removeAt(p:Point):Boolean
Removes the value located at the passed-in coordinate.
Grid
  
retainAll(c:Collection):Boolean
Retains only the elements in this queue that are contained in the specified collection (optional operation).
Grid
  
setContent(a:Array):Boolean
Fill the content with an array of witch length is equal to the grid size().
Grid
  
setDefaultValue(o:Object):Boolean
Defines the default value for this grid's cells content.
Grid
  
setVal(p:Point, o:Object):Boolean
Defines value of grid cell defining by passed-in Point coordinate.
Grid
  
size():uint
Returns the number of elements this collection can contains.
Grid
  
toArray():Array
Returns an array containing all the elements in this grid.
Grid
  
toString():String
Returns the String representation of this object.
Grid
Protected Methods
 MethodDefined by
  
initContent():void
Creates the internal two dimensional array used to store data of the grid.
Grid
Constructor detail
Grid()constructor
public function Grid(size:Dimension, a:Array = null, dV:Object = null, t:Class = null)

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Create a new grid of passed-in size.

If a is set, and if it have the same size that the grid, it's used to fill the collection at creation.

If dV is set, all null elements in the grid will be replaced by dV value.

Parameters
size:Dimension — Size of the grid.
 
a:Array (default = null) — An array to fill the grid with.
 
dV:Object (default = null) — dV The default value for null elements.
 
t:Class (default = null)

Throws
ArgumentError — Invalid size passed in Grid constructor.
 
ClassCastException — If objects type contained in the passed-in array prevents them to be added into this grid
 
IllegalArgumentException — If the passed-in array length doesn't match this grid size
Method detail
add()method
public function add(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

The add method is unsupported by the Grid class.

Parameters
o:Object

Returns
Boolean

Throws
UnsupportedOperationException — The add method of the Collection interface is unsupported by the Grid Class
addAll()method 
public function addAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

The addAll method is unsupported by the Grid class.

Parameters
c:Collection

Returns
Boolean

Throws
UnsupportedOperationException — The addAll method of the Collection interface is unsupported by the Grid Class
addAllAt()method 
public function addAllAt(p:Point, c:Grid):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Copy the content of the passed-in grid at the specified coordinates. The passed-in grid is paste into this grid such the top-left coordinates will start at the specified Point argument.

Parameters
p:Point — coordinates at which copy the grid
 
c:Grid — grid to copy in this grid object

Returns
Booleantrue if the passed-in grid have been successfully added at the specified coordinates in this grid

Throws
ClassCastException — If the object's type prevents it to be added into this grid
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.
 
IndexOutOfBoundsException — If the passed-in point is not valid coordinates for this grid.
 
IndexOutOfBoundsException — If the passed-in grid will overlap this grid when copying.

See also

clear()method 
public function clear():void

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes all of the elements from this collection. This collection will not be empty after this method.

If a default value have been defined for the grid then all cells of the grid contains that value.

contains()method 
public function contains(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns true if this grid contains at least one occurence of the specified element. Moreformally, returns true if and only if this grid contains at least an element e such that o === e.

Parameters
o:ObjectObject whose presence in this grid is to be tested.

Returns
Booleantrue if this grid contains the specified element.

Throws
ClassCastException — If the object's type prevents it to be added into this grid
containsAll()method 
public function containsAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns true if this grid contains all of the elements of the specified collection. If the specified collection is also a Grid, this method returns true if it is a subliset of this queue.

If the passed-in Collection is null the method throw a NullPointerException error.

If the passed-in Collection type is different than the current one the function will throw an IllegalArgumentException. However, if the type of this grid is null, the passed-in Collection can have any type.

The rules which govern collaboration between typed and untyped Collection are described in the isValidCollection descrition, all rules described there are supported by the containsAll method.

Parameters
c:Collection — c collection to be checked for containment in this collection.

Returns
Booleantrue if this collection contains all of the elements in the specified collection

Throws
ClassCastException — If the object's type prevents it to be added into this grid
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.

See also

fill()method 
public function fill(o:Object):void

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Fill the current grid with the passed-in value.

If the passed-in value is a "real" object (not a primitive) then all cells contains a reference to the same object.

Parameters
o:Object — Value used to fill the grid
getCoordinates()method 
public function getCoordinates(id:uint):Point

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns a Point witch is the corresponding position of the passed-in value.

Parameters
id:uint — id uint to convert in a two dimension location

Returns
PointPoint corresponding location

Throws
IndexOutOfBoundsException — the passed-in index is not a valid coordinates for this grid
getDefaulValue()method 
public function getDefaulValue():Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the current default value of this grid used to replace value when removing an element.

Returns
Object — element used as default value for the grid's cells
getSize()method 
public function getSize():Dimension

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the size of the grid as Dimension.

The returned Dimension is a clone of the internal one.

Returns
Dimension — the dimensions of the grid as Dimension
getType()method 
public function getType():Class

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Return the current type allowed in the Grid

Returns
ClassClass used to type checking.
getVal()method 
public function getVal(p:Point):Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the element stored at the passed-in coordinate of the grid.

Parameters
p:Point — p Coordinates Point in the grid

Returns
Object — Value stored at the coorespoding location or null if the passed-in coordinates is not a valid coordinates for this grid

Throws
IndexOutOfBoundsException — the passed-in point is not a valid coordinates for this grid
initContent()method 
protected function initContent():void

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Creates the internal two dimensional array used to store data of the grid.

isEmpty()method 
public function isEmpty():Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

A Grid object is considered as empty if and only if all its cells contains null or the default value for the current Grid.

Returns
Booleantrue if the grid is empty, either false.
isGridCoords()method 
public function isGridCoords(p:Point):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Check if a Point object is a valid coordinate in the current grid.

Parameters
p:PointPoint object to check

Returns
Booleantrue if passed-in Point is a valid coordinate for the current grid

Throws
IndexOutOfBoundsException — the passed-in point is not a valid coordinates for this grid
isTyped()method 
public function isTyped():Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns true if this grid perform a verification of the type of elements.

Returns
Booleantrue if this grid perform a verification of the type of elements.
isValidCollection()method 
public function isValidCollection(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Verify that the passed-in Collection is a valid collection for use with the addAll, removeAll, retainAll and containsAll methods.

When dealing with typed and untyped collection, the following rules apply :

If the passed-in Collection is null the method throw a NullPointerException error.

Parameters
c:CollectionCollection to verify

Returns
Boolean — boolean true if the collection is valid, either false

Throws
NullPointerException — If the passed-in collection is null
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one

See also

isValidType()method 
public function isValidType(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Verify that the passed-in object type match the current Grid element's type.

In the case that the grid is untyped the function will always returns true.

In the case that the object's type prevents it to be added as element for this grid the method will throw a ClassCastException.

Parameters
o:ObjectObject to verify

Returns
Booleantrue if the object is elligible for this grid object, either false

Throws
ClassCastException — If the object's type prevents it to be added into this grid
iterator()method 
public function iterator():Iterator

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns an iterator over the elements in this collection. Iterations are performed in the following order : columns first, rows after.

Result for a 2x2 grid :

Returns
Iterator — an Iterator over the elements in this collection.
matchType()method 
public function matchType(o:*):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Verify if the passed-in object can be inserted in the current Grid.

Parameters
o:* — Object to verify

Returns
Booleantrue if the object can be inserted in the Grid, either false.
remove()method 
public function remove(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes a single instance of the specified element from this grid, if this grid contains one or more such elements. Returns true if this grid contained the specified element (or equivalently, if this collection changed as a result of the call).

In order to remove all occurences of an element you have to call the remove method as long as the grid contains an occurrence of the passed-in element. Typically, the construct to remove all occurrences of an element should look like that :

      while( grid.contains( element ) ) grid.remove( element );
      

If the current grid object is typed and if the passed-in object's type prevents it to be added (and then removed) in this grid, the function throws a ClassCastException.

The Grid introduce a specific behavior for its default value, if the passed-in element is the default value for this grid the function return null and isnt't modified as result of the call.

Parameters
o:Objectobject to be removed from this grid, if present.

Returns
Booleantrue if the grid contained the specified element.

Throws
ClassCastException — If the object's type prevents it to be added into this grid
removeAll()method 
public function removeAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes from this grid all of its elements that are contained in the specified collection (optional operation). At the end of the call there's no occurences of any elements contained in the passed-in collection.

The only values which cannot be removed by a call to removeAll is the default value for this grid. It result that all cells which contained a value also contained in the passed-in collection are filled with the grid's default value.

The rules which govern collaboration between typed and untyped Collection are described in the isValidCollection descrition, all rules described there are supported by the removeAll method.

Parameters
c:CollectionCollection that defines which elements will be removed from this grid.

Returns
Booleantrue if this grid changed as a result of the call.

Throws
ClassCastException — If the object's type prevents it to be added into this grid
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.

See also

removeAllAt()method 
public function removeAllAt(p:Point, c:Grid):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes the content of the passed-in grid at the specified coordinates. The passed-in grid is removed from this grid such the top-left coordinates will start at the specified Point argument.

The content of the passed-in grid is only removed in the bounds of the grid in this grid space coordinates. Values which are also stored in the passed-in grid but whose coordinates aren't in the bounds of the operation aren't remove.

Parameters
p:Point — coordinates at which remove the grid
 
c:Grid — grid to remove from this grid object

Returns
Booleantrue if the passed-in grid have been successfully removed at the specified coordinates in this grid

Throws
ClassCastException — If the object's type prevents it to be added into this grid
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.
 
IndexOutOfBoundsException — If the passed-in point is not valid coordinates for this grid.
 
IndexOutOfBoundsException — If the passed-in grid will overlap this grid when removing.

See also

removeAt()method 
public function removeAt(p:Point):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes the value located at the passed-in coordinate.

If the grid changed after the call the function returns true. If the passed-in Point isn't a valid coordinate for this grid the function failed and return false.

If a default value is set, the cell contains that value instead of null after the call.

Parameters
p:PointPoint position of the value to remove

Returns
Booleantrue if the grid changed as result of the call

Throws
IndexOutOfBoundsException — the passed-in point is not a valid coordinates for this grid
retainAll()method 
public function retainAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Retains only the elements in this queue that are contained in the specified collection (optional operation). In other words, removes from this queue all of its elements that are not contained in the specified collection.

The only values which cannot be removed by a call to retainAll is the default value for this grid. It result that all cells which contained a value that are not contained in the passed-in collection are filled with the grid's default value.

The rules which govern collaboration between typed and untyped Collection are described in the isValidCollection descrition, all rules described there are supported by the retainAll method.

Parameters
c:Collection — c elements to be retained in this collection.

Returns
Booleantrue if this collection changed as a result of the call

Throws
ClassCastException — If the object's type prevents it to be added into this grid
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.

See also

setContent()method 
public function setContent(a:Array):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Fill the content with an array of witch length is equal to the grid size().

The call return true only if the Grid changed as results of the call.

Parameters
a:Array — a Array to fill the Grid

Returns
Booleantrue if the Grid changed as results of the call

Throws
ClassCastException — If the object's type prevents it to be added into this grid
 
IllegalArgumentException — If the passed-in array length doesn't match this grid size
setDefaultValue()method 
public function setDefaultValue(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Defines the default value for this grid's cells content. When changing the default value of a grid, the cells which previously contains the old default value will contains the new one at the end of the call.

Parameters
o:Object — new default value for this grid's cells

Returns
Booleantrue if the grid have change as result of the call

Throws
ClassCastException — If the object's type prevents it to be added into this grid
setVal()method 
public function setVal(p:Point, o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Defines value of grid cell defining by passed-in Point coordinate.

The call return true only if the Grid changed as results of the call.

Parameters
p:Point — p Point position of the cell
 
o:Object — o value to store in the grid

Returns
Booleantrue if the Grid changed as results of the call

Throws
IndexOutOfBoundsException — the passed-in point is not a valid coordinates for this grid
 
ClassCastException — If the object's type prevents it to be added into this grid
size()method 
public function size():uint

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the number of elements this collection can contains.

Returns
uint — the number of elements this collection can contains.
toArray()method 
public function toArray():Array

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns an array containing all the elements in this grid. Obeys the general contract of the Collection.toArray method.

Returns
ArrayArray containing all of the elements in this grid.

See also

Collection.toArray()
toString()method 
public function toString():String

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the String representation of this object.

The function return a string like com.bourre.structures::Grid<String> for a typed collection. The string between the < and > is the name of the type of the collection's elements. If the collection is an untyped collection the function will simply return the result of the PixlibStringifier.stringify call.

Returns
StringString representation of this object.