ptolemy.domains.ct.kernel.util
Class TotallyOrderedSet

java.lang.Object
  extended byptolemy.domains.ct.kernel.util.TotallyOrderedSet

public class TotallyOrderedSet
extends java.lang.Object

Totally ordered set with no repeated elements. A repeated element is an element that the comparator returns 0 for.

Since:
Ptolemy II 0.2
Version:
$Id: TotallyOrderedSet.java,v 1.33 2004/04/13 03:27:06 cxh Exp $
Author:
Jie Liu
Accepted Rating:
Red (johnr)
Proposed Rating:
Yellow (liuj)

Constructor Summary
TotallyOrderedSet(java.util.Comparator comparator)
          Construct the set with the given comparator. comparator is a blank final field that can't be changed after creation.
 
Method Summary
 java.lang.Object at(int index)
          Return the index-th element.
 void clear()
          Clear the set.
 boolean contains(java.lang.Object obj)
          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, 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 index-th element.
 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
 

Constructor Detail

TotallyOrderedSet

public TotallyOrderedSet(java.util.Comparator comparator)
Construct the set with the given comparator. comparator is a blank final field that can't be changed after creation.

Parameters:
comparator - The Comparator to compare elements.
See Also:
Comparator
Method Detail

at

public java.lang.Object at(int index)
Return the index-th element. If the index is out of range a NoSuchElementException will be thrown.

Returns:
The requested element.

clear

public void clear()
Clear the set. Remove all elements.


contains

public boolean contains(java.lang.Object obj)
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 uncomparable by the comparator, then a ClassCastException will be thrown.

Returns:
True If the elements is contained according to the comparator.

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.

Returns:
The index.

insert

public void insert(java.lang.Object obj)
Insert the given element, keeping the set sorted. If the set contains an element "equals" to the given element, then do nothing. The equivalence relation is defined by the comparator. If the type of given element is uncomparable by the comparator, 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 index-th element. Thrown an exception if the set is empty.

Parameters:
index - The index of the element.
Returns:
The removed element.
Throws:
NoSuchElementException - If the specified index is out of range.

removeFirst

public java.lang.Object removeFirst()
Remove and return the first element, ie. the "smallest" element in the set.. Thrown an exception if the set is empty.

Returns:
The removed element.
Throws:
NoSuchElementException - If the set is empty.

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.

Returns:
The String description of the set.