ptolemy.actor.util
Class TotallyOrderedSet

java.lang.Object
  extended by ptolemy.actor.util.TotallyOrderedSet

public class TotallyOrderedSet
extends java.lang.Object

An object of this class is a totally ordered set with an increasing order. The order between any two elements in the set can be checked by calling the compare() method of a comparator associated with this object. An element, a, in this set is said to precede another one, b, if compare(a, b) returns -1.

The set does not contain repeated elements, which means comparing any two elements in this set never returns 0.

Since:
Ptolemy II 5.2
Version:
$Id: TotallyOrderedSet.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Jie Liu, Haiyang Zheng
Accepted Rating:
Green (hyzheng)
Proposed Rating:
Green (hyzheng)

Field Summary
private  java.util.Comparator _comparator
          The comparator for the order.
private  java.util.LinkedList _set
          The set.
 
Constructor Summary
TotallyOrderedSet(java.util.Comparator comparator)
          Construct a set with the given comparator.
 
Method Summary
 java.lang.Object at(int index)
          Return the element with the given index.
 void clear()
          Clear the set by removing all elements.
 boolean contains(java.lang.Object object)
          Return true if the given element is contained in this set.
 java.util.List elementList()
          Return a list of all the elements.
 java.util.Enumeration elements()
          Deprecated. Use elementList() instead.
 java.lang.Object first()
          Return the first element, ie. the smallest element.
 java.util.Comparator getComparator()
          Return the comparator.
 int indexOf(java.lang.Object obj)
          Return the index of the given object.
 void insert(java.lang.Object obj)
          Insert the given element while keeping the set sorted.
 boolean isEmpty()
          Return true if the set is empty.
 void removeAllLessThan(java.lang.Object obj)
          Remove all the elements that are (strictly) less than the argument.
 java.lang.Object removeAt(int index)
          Remove and return the element with the given index.
 java.lang.Object removeFirst()
          Remove and return the first element, ie. the smallest element in the set.
 int size()
          Return the size of the set.
 java.lang.Object take()
          Deprecated. Use removeFirst() instead.
 java.lang.String toString()
          Return a string that consists of the contents of the elements in the set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_comparator

private final java.util.Comparator _comparator
The comparator for the order. The comparator is a blank final field that can't be changed after creation.


_set

private java.util.LinkedList _set
The set.

Constructor Detail

TotallyOrderedSet

public TotallyOrderedSet(java.util.Comparator comparator)
Construct a set with the given comparator.

Parameters:
comparator - The Comparator with which to compare elements. Note that the comparator cannot be changed after this TotallyOrderedSet object is constructed.
See Also:
Comparator
Method Detail

at

public java.lang.Object at(int index)
Return the element with the given index. The index starts with 0.

Parameters:
index - The index of the element to return.
Returns:
The requested element.

clear

public void clear()
Clear the set by removing all elements.


contains

public boolean contains(java.lang.Object object)
Return true if the given element is contained in this set. The equivalence relation is defined by the comparator. If the type of given element is not comparable by the comparator, then a ClassCastException will be thrown.

Parameters:
object - The object to check for containment.
Returns:
True If the element is contained in this set.

elementList

public java.util.List elementList()
Return a list of all the elements.

Returns:
The list of all the elements.

elements

public java.util.Enumeration elements()
Deprecated. Use elementList() instead.

Return an enumeration of all the elements.

Returns:
The enumeration of all the elements.

first

public java.lang.Object first()
Return the first element, ie. the smallest element. If the set is empty, then return null.

Returns:
The smallest element.

getComparator

public java.util.Comparator getComparator()
Return the comparator.

Returns:
The comparator.

indexOf

public int indexOf(java.lang.Object obj)
Return the index of the given object. Return -1 if the object is not in the set.

Parameters:
obj - The objec to get index for.
Returns:
The index.

insert

public void insert(java.lang.Object obj)
Insert the given element while keeping the set sorted. If the set contains an element "equal to" the given element, then do nothing. The equivalence relation is defined by the comparator. If the type of the given element is not comparable, then a ClassCastException will be thrown.

Parameters:
obj - The element to be inserted.

isEmpty

public boolean isEmpty()
Return true if the set is empty.

Returns:
True if the set is empty.

removeAllLessThan

public void removeAllLessThan(java.lang.Object obj)
Remove all the elements that are (strictly) less than the argument. If the set is empty or all the elements are greater than the argument, then do nothing.

Parameters:
obj - The argument.

removeAt

public java.lang.Object removeAt(int index)
Remove and return the element with the given index.

Parameters:
index - The index of the element.
Returns:
The removed element.

removeFirst

public java.lang.Object removeFirst()
Remove and return the first element, ie. the smallest element in the set.

Returns:
The removed element.

size

public int size()
Return the size of the set.

Returns:
The size of the set.

take

public java.lang.Object take()
Deprecated. Use removeFirst() instead.

Return the first element, ie. the smallest element and remove it from the set.

Returns:
The smallest element.

toString

public java.lang.String toString()
Return a string that consists of the contents of the elements in the set. The elements are represented by there toString() value. This method is for test purpose.

Overrides:
toString in class java.lang.Object
Returns:
The string description of the set.