Package ptolemy.actor.util
Class TotallyOrderedSet
- java.lang.Object
-
- 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$
- Author:
- Jie Liu, Haiyang Zheng
- Pt.AcceptedRating:
- Green (hyzheng)
- Pt.ProposedRating:
- Green (hyzheng)
-
-
Constructor Summary
Constructors Constructor Description TotallyOrderedSet(java.util.Comparator comparator)
Construct a set with the given comparator.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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.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.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.
-
-
-
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
@Deprecated 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 object 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
@Deprecated 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 classjava.lang.Object
- Returns:
- The string description of the set.
-
-