Set

Kind of class:public class
Package:com.bourre.collection
Inherits from:none
Implements:
Author:Olympe Dignat, Cédric Néhémie
Classpath:com.bourre.collection.Set
File last modified:Monday, 24 November 2008, 11:36:50
A collection that contains no duplicate elements. More formally, sets
contain no pair of elements e1 and e2 such that

e1 === e2, and at most one null element. As implied by
its name, this interface models the mathematical set abstraction.

The Set class places additional stipulations, beyond those
inherited from the Collection interface, on the contracts of the
constructor and on the contracts of the add method.
Declarations for other inherited methods are also included here for convenience.
(The specifications accompanying these declarations have been tailored to the

Set class, but they do not contain any additional stipulations.)

The additional stipulation on constructors is, not surprisingly,
that all constructors must create a set that contains no duplicate elements
(as defined above).

Some set implementations have restrictions on the elements that
they may contain. For example, some implementations prohibit null elements,
and some have restrictions on the types of their elements. Attempting to
add an ineligible element throws an unchecked exception, typically

NullPointerException or ClassCastException.
Attempting to query the presence of an ineligible element may throw an exception,
or it may simply return false; some implementations will exhibit the former
behavior and some will exhibit the latter. More generally, attempting an
operation on an ineligible element whose completion would not result in
the insertion of an ineligible element into the set may throw an
exception or it may succeed, at the option of the implementation.
Such exceptions are marked as "optional" in the specification for this
class.

Example:
  • Using an untyped Set
    var set : Set = new Set();

    set.add( "foo" );
    set.add( 25 );

    trace ( set.add( 25 ) ) // false, as the object already exist in this set

    set.add( false );

    trace( set.size() ); // 3


    Using a typed Set
    var set : Set = new Set( String );

    set.add( "foo" );

    try
    {
    set.add( 25 ); // throw an error, as 25 is not a String
    }
    catch( e : Error ) {}

    trace ( set.add( "foo" ); ) // false, as the object already exist in this set

    set.add( "hello" );

    trace( set.size() ); // 2

Summary


Constructor
  • Set (type:Class = null)
    • Creates a new set object.
Instance methods
  • add (o:Object) : Boolean
    • Adds the specified element to this set if it is not already
  • addAt (index:uint, o:Object) : void
    • Inserts the specified element at the specified position
  • addAll (c:Collection) : Boolean
    • Adds all of the elements in the specified Collection
  • addAllAt (index:uint, c:Collection) : Boolean
    • Inserts all of the elements in the specified Collection into
  • remove (o:Object) : Boolean
    • Removes the specified element from this set
  • removeAt (index:uint) : Boolean
    • Removes the element at the specified position in this set.
  • removeAll (c:Collection) : Boolean
    • Removes from this set all of its elements that are contained
  • retainAll (c:Collection) : Boolean
    • Retains only the elements in this set that are contained
  • contains (o:Object) : Boolean
    • Returns true if this set contains the
  • containsAll (c:Collection) : Boolean
    • Returns true if this set contains
  • equals (o:Object) : Boolean
    • Compares the specified object with this set for equality.
  • indexOf (o:Object) : int
    • Returns the index of the object in this set
  • lastIndexOf (o:Object) : int
    • Returns the index of the last occurrence of the specified
  • clear : void
    • Removes all of the elements from this set
  • iterator : Iterator
    • Returns an iterator over the elements in this set.
  • listIterator (index:uint = 0) : ListIterator
    • Returns a list iterator of the elements in this list
  • subList (fromIndex:uint, toIndex:uint) : List
    • Returns a view of the portion of this List between fromIndex,
  • size : uint
    • Returns the number of elements in this set (its cardinality).
  • get (index:uint) : Object
    • Returns the Object stored at the passed-in
  • set (index:uint, o:Object) : Object
    • Insert the passed-in Object in this set
  • isValidIndex (index:uint) : void
    • Verify that the passed-in uint index is a
  • isValidIndexForAdd (index:uint) : void
    • Verify that the passed-in uint index is a
  • isValidCollection (c:Collection) : Boolean
    • Verify that the passed-in Collection is a valid
  • isValidObject (o:Object) : Boolean
    • Verify that the passed-in object is valid for this Set(well-typed, not already present in the set).
  • isValidType (o:Object) : Boolean
    • Verify that the passed-in object type match the current
  • isEmpty : Boolean
    • Returns true if this set contains no elements.
  • matchType (o:*) : Boolean
    • Verify that the passed-in object type match the current
  • isTyped : Boolean
    • Returns true if this set perform a verification
  • getType : Class
    • Return the class type of element in this set object.
  • toArray : Array
    • Returns an array containing all the elements in this set.
  • swap (index1:uint, index2:uint) : void
    • Swap two objects stored in the passed-in index
  • toString : String
    • Returns the String representation of

Constructor

Set

public function Set (
type:Class = null)

Creates a new set object. If the typeargument is defined, the set is considered as typed, and then
the type of all elements inserted in this set is checked.
Parameters:
type:
Class type for elements of this set

Instance methods

add

public function add (
o:Object) : Boolean

Adds the specified element to this set if it is not already
present (optional operation). More formally, adds the specified
element, o, to this set if this set contains no
element e such that o === e. If this set
already contains the specified element, the call leaves this set
unchanged and returns false. In combination with the
restriction on constructors, this ensures that sets never contain
duplicate elements.

The stipulation above does not imply that sets must accept all
elements; sets may refuse to add any particular element, including

null, and throwing an exception, as described in the
specification for Collection.add. Individual set
implementations should clearly document any restrictions on the the
elements that they may contain.

Parameters:
o:
element to be added to this set.
Returns:
  • true if this set did not already contain the specified
    element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this set
Example:
  • How to use the Set.add method

    var set : Set = new Set( String );

    set.add( "foo" );

    set.add( "foo" );

    set.add( 25 );


    In comparison with Java, where object which have all of their properties
    equals are considered as equals, AS3 doesn't allow that, except if objects
    provides an equals method which is used instead of the

    == or === operators. Nevertheless, the Setclass use the native operator to perform comparison, thus two objects with
    equals properties are considered as differents.


    var set : Set = new Set( Object );
    var o : Object = { x : 50, y : 100 };

    set.add( o );
    set.add( o );

    set.add( { x : 50, y : 100 } );


Specified by:

addAll

public function addAll (
c:Collection) : Boolean

Adds all of the elements in the specified Collection
to this set if they're not already present (optional operation).
If the specified collection is also a set, the addAlloperation effectively modifies this set so that its value is the
union of the two sets. The behavior of this operation is
unspecified if the specified collection is modified while the
operation is in progress.

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

Parameters:
c:
Collection whose elements are to be added to this set.
Returns:
  • true if this set changed as a result of the call.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this set
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Example:
  • How the Set.addAll method works with types

    Let say that you have two typed sets typedSet1and typedSet1.


    var typedSet1 : Set = new Set( String );
    var typedSet2 : Set = new Set( String );

    typedSet1.add( "foo1" );
    typedSet1.add( "foo2" );
    typedSet1.add( "foo3" );
    typedSet1.add( "foo4" );

    typedSet2.add( "foo3" );
    typedSet2.add( "foo4" );
    typedSet2.add( "foo5" );
    typedSet2.add( "foo6" );


    And two untyped sets untypedSet1and untypedSet1.


    var untypedSet1 : Set = new Set();
    var untypedSet2 : Set = new Set();

    untypedSet1.add( 1 );
    untypedSet1.add( 2 );
    untypedSet1.add( 3 );
    untypedSet1.add( "foo1" );

    untypedSet1.add( 3 );
    untypedSet1.add( 4 );
    untypedSet1.add( 5 );
    untypedSet1.add( "foo1" );


    The two operations below will work as expected,
    realizing an union between Sets objects.



    typedSet1.addAll ( typedSet2 );








    untypedSet1.addAll ( untypedSet2 );









    As an untyped set can contain any types of objects at the
    same time, the code below is always valid.


    untypedSet1.addAll( typedSet2 );













    But if you try to add an untyped collection to a typed one
    the call will fail with an exception.


    try
    {
    typedSet2.addAll( untypedSet2 );
    }
    catch( e : IllegalArgumentException )
    {
    trace( e );


    }
Specified by:

addAllAt

public function addAllAt (
index:uint, c:Collection) : Boolean

Inserts all of the elements in the specified Collection into
this set at the specified position. Shifts the element
currently at that position (if any) and any subsequent
elements to the right (increases their indices). The new
elements will appear in the stack in the order that
they are returned by the specified Collection's iterator.

The rules which govern collaboration between typed and untyped

Collection are described in the isValidCollectiondescrition, all rules described there are supported by the

containsAll method.

Parameters:
index:
uint index at which to insert
first element from the specified collection.
c :
elements to be inserted into this stack.
Returns:
  • true if this set changed as a result of the call.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — index is out of range
    (index < 0 || index > size()).
  • {VISDOC_LINK_1}ClassCastException — if the class of an element of
    the specified collection prevents it from being added to this collection.
  • {VISDOC_LINK_2}NullPointerException — if the passed in collection is null.
Specified by:

addAt

public function addAt (
index:uint, o:Object) : void

Inserts the specified element at the specified position
in this set. Shifts the element currently at that
position (if any) and any subsequent elements to the
right (adds one to their indices).
Parameters:
index:
uint index at which the specified
element is to be inserted.
o :
element to be inserted.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — index is out of range
    (index < 0 || index > size()).
  • {VISDOC_LINK_1}ClassCastException — if the class of the specified
    element prevents it from being added to this list.
See also:
Specified by:

clear

public function clear (
) : void

Removes all of the elements from this set
(optional operation). This set will be empty
after this call returns (unless it throws an exception).
Throws:
  • {VISDOC_LINK_0}o === e — if the true method is
    not supported by this collection.
    #
Specified by:

contains

public function contains (
o:Object) : Boolean

Returns true if this set contains the
specified element. More formally, returns true if
and only if this set contains an element esuch that o === e.
Parameters:
o:
Object whose presence in this set
is to be tested.
Returns:
  • true if this set contains the specified
    element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this set
Specified by:

containsAll

public function containsAll (
c:Collection) : Boolean

Returns true if this set contains
all of the elements of the specified collection. If the specified
collection is also a Set, this method returns trueif it is a subset of this set.

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

Parameters:
c:
Collection to be checked for containment in this set.
Returns:
  • true if this set contains all of the elements of the
    specified collection.
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified collection is
    null.
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Specified by:

equals

public function equals (
o:Object) : Boolean

Compares the specified object with this set for equality.
Returns true if the specified object is also a Set,
the two sets have the same size, and every member of the specified

Set is contained in this set (or equivalently,
every member of this set is contained in the specified
set). This definition ensures that the equals method works
properly across different implementations of the set class.
Parameters:
o:
Object to be compared for equality with this
set.
Returns:
  • true if the specified Object is equal to this
    set.

get

public function get (
index:uint) : Object

Returns the Object stored at the passed-in

index in this set object.

If the passed-in index is not a valid index
for this set, the function throw an

IndexOutOfBoundsException exception.

Parameters:
index:
uint index of the entry to get.
Returns:
  • Object stored at the specified index
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — The passed-in
    index is not a valid index for this set
Specified by:

getType

public function getType (
) : Class

Return the class type of element in this set object.

An untyped set returns null, as the
wildcard type (*) is not a Classand Object class doesn't fit for primitive types.

Returns:
  • Class type of the set's elements
Specified by:

indexOf

public function indexOf (
o:Object) : int

Returns the index of the object in this set
Parameters:
o:
the object to find
Returns:
  • int index of the passed object in this
    set, either if the object isn't contained
    in this set the function return -1
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this stack
Specified by:

isEmpty

public function isEmpty (
) : Boolean

Returns true if this set contains no elements.
Returns:
  • true if this set contains no elements.
Specified by:

isTyped

public function isTyped (
) : Boolean

Returns true if this set perform a verification
of the type of elements.
Returns:
  • true if this set perform a verification
    of the type of elements.
Specified by:

isValidCollection

public function isValidCollection (
c:Collection) : Boolean

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 :

  • Two typed collection, which have the same type, can collaborate each other.
  • Two untyped collection can collaborate each other.
  • An untyped collection can add, remove, retain or contains any typed collection
    of any type without throwing errors.
  • A typed collection will always fail when attempting to add, remove, retain
    or contains an untyped collection.

If the passed-in Collection is null the method throw a

NullPointerException error.

Parameters:
c:
Collection to verify
Returns:
  • boolean true if the collection is valid,
    either false
Throws:
  • {VISDOC_LINK_0}NullPointerException — If the passed-in collection
    is null
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.

isValidIndex

public function isValidIndex (
index:uint) : void

Verify that the passed-in uint index is a
valid index for this Set. If not, an

IndexOutOfBoundsException exception is
thrown.
Parameters:
index:
uint<code> index to verify

@throws <code>IndexOutOfBoundsException
— The passed-in
index is not a valid index for this set

isValidIndexForAdd

public function isValidIndexForAdd (
index:uint) : void

Verify that the passed-in uint index is a
valid index for an insertion in this Set.
If not, an IndexOutOfBoundsException exception
is thrown.
Parameters:
index:
uint<code> index to verify

@throws <code>IndexOutOfBoundsException
— The passed-in
index is not a valid index for this set

isValidObject

public function isValidObject (
o:Object) : Boolean

Verify that the passed-in object is valid for this Set(well-typed, not already present in the set).

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

Returns:
  • true if the object is valid
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this set

isValidType

public function isValidType (
o:Object) : Boolean

Verify that the passed-in object type match the current

Set element's type.

In the case that the set 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 set the method will throw
a ClassCastException.

Parameters:
o:
Object to verify
Returns:
  • true if the object is elligible for this
    set object, either false.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this set

iterator

public function iterator (

Returns an iterator over the elements in this set.
The elements are returned in no particular order (unless this
set is an instance of some class that provides
a guarantee).
Returns:
  • an iterator over the elements in this set.
Specified by:

lastIndexOf

public function lastIndexOf (
o:Object) : int

Returns the index of the last occurrence of the specified
object in this Set.
Parameters:
o:
the desired component.
Returns:
  • the index of the first occurrence of the object argument
    in this stack, that is, the largest value k such that (elem === elementData[k]) is true;
    returns -1 if the object is not found.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this stack
Specified by:

listIterator

public function listIterator (
index:uint = 0) : ListIterator

Returns a list iterator of the elements in this list
(in proper sequence), starting at the specified position
in the list. The specified index indicates the first
element that would be returned by an initial call to
the next method. An initial call to the previous method
would return the element with the specified index minus one.

This implementation returns a straightforward implementation
of the ListIterator interface that extends the implementation
of the Iterator interface returned by the iterator() method.
The ListIterator implementation relies on the backing list's

get(int), set(int, Object),

add(int, Object) and remove(int)methods.

Parameters:
index:
uint index of the first element
to be returned from the list iterator (by
a call to the next method).
Returns:
  • a list iterator of the elements in this list (in proper sequence),
    starting at the specified position in the list.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — index is out of range
    (index < 0 || index > size()).
Specified by:

matchType

public function matchType (
o:*) : Boolean

Verify that the passed-in object type match the current
set element's type.
Returns:
  • true if the object is elligible for this
    set object, either false.

remove

public function remove (
o:Object) : Boolean

Removes the specified element from this set
if it is present (optional operation). More formally,
removes an element e such that

o === e, if the set contains
such an element. Returns true if the set
contained the specified element (or equivalently, if the
set changed as a result of the call).
(The set will not contain the specified element
once the call returns.)
Parameters:
o:
object to be removed from this Set,
if present.
Returns:
  • true if the set contained the specified element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this set
Example:
  • Using the Set.remove() method of an untyped set :

    var set : Set = new Set();
    set.add ( "foo" );

    trace( set.size() );
    trace( set.remove( "foo" ) );

    trace( set.size() );
    trace( set.remove( "foo" ) );


    Using the Set.remove() method of a typed set :


    var set : Set = new Set( String );
    set.add ( "foo" );

    trace( set.size() );
    trace( set.remove( "foo" ) );


    try
    {
    set.remove( 45 );
    }
    catch( e : ClassCastException )
    {
    trace ( e );
    }
Specified by:

removeAll

public function removeAll (
c:Collection) : Boolean

Removes from this set all of its elements that are contained
in the specified collection (optional operation). If the specified

Collection is also a Set, this operation
effectively modifies this set so that its value is the
asymmetric set difference of the two sets.

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

Parameters:
c:
Collection that defines which elements will be
removed from this set.
Returns:
  • true if this set changed as a result
    of the call.
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified collection is
    null.
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Example:
  • Using the Set.removeAll() with untyped sets

    var set1 : Set = new Set();
    var set2 : Set = new Set();

    set1.add( 1 );
    set1.add( 2 );
    set1.add( 3 );
    set1.add( 4 );
    set1.add( "foo1" );
    set1.add( "foo2" );
    set1.add( "foo3" );
    set1.add( "foo4" );

    set2.add( 1 );
    set2.add( 3 );
    set2.add( "foo1" );
    set2.add( "foo3" );

    trace ( set1.removeAll ( set2 ) ) ;


Specified by:

removeAt

public function removeAt (
index:uint) : Boolean

Removes the element at the specified position in this set.
Shifts any subsequent elements to the right (subtracts one from
their indices).
@copy com.bourre.collection.Set#remove()
Parameters:
index:
uint index at which to remove an element
from the specified Collection.
Returns:
  • true if the object have been removed, false otherwise.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — The passed-in
    index is not a valid index for this set
See also:
Example:
  • Using the Set.removeAt method with an untyped set

    var set : Set = new Set();
    set.add( "foo1" );
    set.add( "foo2" );
    set.add( "foo3" );

    trace ( set.removeAt( 2 ) );
    trace ( set.removeAt( 0 ) );


    try
    {
    set.removeAt( 1 );
    }
    catch( e : IndexOutOfBoundsException )
    {
    trace( e );
    }
Specified by:

retainAll

public function retainAll (
c:Collection) : Boolean

Retains only the elements in this set that are contained
in the specified collection (optional operation). In other words,
removes from this set all of its elements that are not
contained in the specified collection. If the specified collection
is also a Set, this operation effectively modifies this
set so that its value is the intersection of the
two sets.

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

Parameters:
c:
Collection that defines which elements this
set will retain.
Returns:
  • true if this collection changed as a result of the
    call.
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified collection is
    null.
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Example:
  • Using the Set.retainAll() with untyped sets

    var set1 : Set = new Set();
    var set2 : Set = new Set();

    set1.add( 1 );
    set1.add( 2 );
    set1.add( 3 );
    set1.add( 4 );
    set1.add( "foo1" );
    set1.add( "foo2" );
    set1.add( "foo3" );
    set1.add( "foo4" );

    set2.add( 1 );
    set2.add( 3 );
    set2.add( "foo1" );
    set2.add( "foo3" );

    trace ( set1.retainAll ( set2 ) ) ;


Specified by:

set

public function set (
index:uint, o:Object) : Object

Insert the passed-in Object in this set
at the specified index. The method returns
the object previously stored at this index.

If the passed-in index is not a valid index
for this set, the function throw an

IndexOutOfBoundsException exception.

If the passed-in object's type prevents it to be added
in this set the function will throw a

ClassCastException.

Parameters:
index:
uint index at which insert the
passed-in Object.
o :
Object to insert in this set
Returns:
  • Object previously stored at the specified
    index or null if the insertion haven't been
    done.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — The passed-in
    index is not a valid index for this set
  • {VISDOC_LINK_1}ClassCastException — If the object's type
    prevents it to be added into this set
Specified by:

size

public function size (
) : uint

Returns the number of elements in this set (its cardinality).
Returns:
  • Number of elements in this set (its cardinality).
Specified by:

subList

public function subList (
fromIndex:uint, toIndex:uint) : List

Returns a view of the portion of this List between fromIndex,
inclusive, and toIndex, exclusive. (If fromIndex and ToIndex
are equal, the returned List is empty.)
Parameters:
fromIndex:
low endpoint (inclusive) of the subList.
toIndex :
high endpoint (exclusive) of the subList.
Returns:
  • a view of the specified range within this List.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — fromIndex or toIndex are
    out of range (index < 0 || index > size()).
Specified by:

swap

public function swap (
index1:uint, index2:uint) : void

Swap two objects stored in the passed-in index

If the passed-in index are not valid index
for this set, the function throw an

IndexOutOfBoundsException exception.

Parameters:
index1:
uint index of the first Object.
index2:
uint index of the second Object.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — The passed-in
    index are not valid index for this set

toArray

public function toArray (
) : Array

Returns an array containing all the elements in this set.
Obeys the general contract of the Collection.toArraymethod.
Returns:
  • Array containing all of the elements in this set.
Specified by:

toString

public function toString (
) : String

Returns the String representation of
this object.

The function return a string like

com.bourre.collection::Set<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:
  • String representation of
    this object.