Search:
Class Set
class
Set
A mathematical set, essentially nothing but a collection of objects. The class is essentially a wrapper for an array which defines
some traditional operations done on sets, such as cartesianProduct.
Defined in
Set.js
Variable Summary
int
cardinality
the cardinality ("dimension") of the set
Array
elements
the elements of the set
Function Summary
Adds a new element to the set and updates the cardinality.
Calculates the Cartesian product of two sets (the set of all ordered pairs, which are here treated as subsets).
Creates a new set that is the relative complement of two sets.
Tests whether or not two sets (including subsets, which must be defined as sets and not simply arrays) are equal.
Determines if a specified element is in the set of this group.
Finds the index of the first occurence of the specified element.
Creates a new set out of the intersection (shared elements) of two sets.
Tests if this set is a proper subset of a given set (i.e.
Tests whether or not this set is a proper superset of a given set.
Removes ALL OCCURENCES of an element from the set and updates the cardinality.
Tests to see if a set is a subset (not necessarily a proper subset; i.e.
Tests if the set is a superset of a given set.
Returns a string representation of the set (and subsets).
Variable Details
variable int cardinality
the cardinality ("dimension") of the set
variable Array elements
the elements of the set
variable public static Set empty
the empty set {}
Constructor Details
constructor Set
Set([Array
elements])
Set([Set
set])
Constructor function for a new set. If no parameters supplied, constructs an empty (null) set
Parameters:
fromArray
[elements]
- the elements of your set clone
[set]
- the set to clone Function Details
function addElement
public
Set
addElement(Object
el)
Adds a new element to the set and updates the cardinality.
Parameters:
el
- the element to be added
Returns:
this set, updated
function cartesianProduct
Calculates the Cartesian product of two sets (the set of all ordered pairs, which are here treated as subsets).
Parameters:
set
- the set with which to compute the product
Returns:
a new set that is the Cartesian product of the other two
function complement
Creates a new set that is the relative complement of two sets. This operation is analogous to subtracting the specified set from the original set.
Parameters:
set
- the set to which to find the complement
Returns:
a new set that is the specified complement
function equalTo
public
boolean
equalTo(Set
set)
Tests whether or not two sets (including subsets, which must be defined as sets and not simply arrays) are equal.
Parameters:
set
- the set to compare to
Returns:
true if the two sets are equal, false otherwise
function inSet
public
boolean
inSet()
Determines if a specified element is in the set of this group.
Returns:
true if element is in set, false otherwise
function indexOf
public
int
indexOf(Object
el)
Finds the index of the first occurence of the specified element. Should suffice since most sets should be in order and have no recurring elements.
Parameters:
el
- the element for which to find the index
Returns:
the index of the element in the set (NOT in the array of the set object)
function intersection
Creates a new set out of the intersection (shared elements) of two sets.
Parameters:
set
- the set with which to find the intersection
Returns:
the intersection of the two sets as a new set
function properSubsetOf
public
boolean
properSubsetOf(Set
set)
Tests if this set is a proper subset of a given set (i.e. there is at least one element in the superset not in the subset).
Parameters:
set
- the set which may or may not be a strict superset
Returns:
true if this is a proper subset of set, false otherwise
function properSupersetOf
public
boolean
properSupersetOf(Set
set)
Tests whether or not this set is a proper superset of a given set.
Parameters:
set
- the set which may or may not be a proper subset
Returns:
true if this is proper superset of set, false if not
function removeElement
public
Set
removeElement(Object
el)
Removes ALL OCCURENCES of an element from the set and updates the cardinality.
Parameters:
el
- the element to be removed
Returns:
this set, updated
function subsetOf
public
boolean
subsetOf(Set
set)
Tests to see if a set is a subset (not necessarily a proper subset; i.e. by this method, a set is a subset of itself) of a given set.
Parameters:
set
- the set that may or may not be a superset of the original
Returns:
true if this is a subset of set, false otherwise
function supersetOf
public
boolean
supersetOf(Set
set)
Tests if the set is a superset of a given set. Note: not strict definition of superset; any set is also a superset of itself by this definition.
Parameters:
set
- the set which may or may not be a subset of the original
Returns:
true if this is a superset of set, false otherwise
function toString
public
String
toString()
Returns a string representation of the set (and subsets).
Returns:
the string representatoin of the set.
function union
Creates a new set that is the union of two sets.
Parameters:
set
- the set to join with the current set
Returns:
a new set that is the union of the calling and given sets
function zero
public static
Set
zero(int
n)
Creates a set with n zero elements
Parameters:
n
- the number of elements in the zero set
Returns:
a set of n elements all equal to 0