All Packages Class Hierarchy This Package Previous Next Index
OrderedSet1 - Construction, enumeration, rejection of duplicates.
OrderedSet2 - Use of comparator to order elements.
OrderedSet3 - Union, intersection, difference, symmetric difference, subset.
OrderedSet4 - Counting, finding, erasing.
OrderedSet5 - Bounds.
OrderedSet6 - Construction, enumeration, acceptance of duplicates.
OrderedSet7 - Comparators.
OrderedSet8 - Counting, finding, erasing.
OrderedSet9 - Bounds.
OrderedSet1 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;
/**
* Construction, enumeration, rejection of duplicates.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet1
{
public static void main( String[] args )
{
OrderedSet set = new OrderedSet();
set.add( new Integer( 6 ) );
set.add( new Integer( 1 ) );
set.add( new Integer( 4 ) );
System.out.println( set );
System.out.println();
System.out.println( "Enumerate the OrderedSet" );
Enumeration e = set.elements();
while ( e.hasMoreElements() )
System.out.println( e.nextElement() );
System.out.println();
System.out.println( "Iterate through the OrderedSet" );
for ( OrderedSetIterator i = set.begin(); !i.atEnd(); i.advance() )
System.out.println( i.get() );
System.out.println();
System.out.println( "Show that duplicates cannot be added." );
Object value = set.add( new Integer( 8 ) );
if ( value != null )
System.out.println( "Could not add 8." );
else
{
System.out.println( "Added 8." );
System.out.println( "New contents are " + set );
}
value = set.add( new Integer( 4 ) );
if ( value != null )
System.out.println( "Could not add 4." );
else
{
System.out.println( "Added 4." );
System.out.println( "New contents are " + set );
}
}
}
OrderedSet( 1, 4, 6 )
Enumerate the OrderedSet
1
4
6
Iterate through the OrderedSet
1
4
6
Show that duplicates cannot be added.
Added 8.
New contents are OrderedSet( 1, 4, 6, 8 )
Could not add 4.
OrderedSet2 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
/**
* Use of comparator to order elements.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet2
{
public static void main( String[] args )
{
OrderedSet set = new OrderedSet( new LessString() );
set.add( "cat" );
set.add( "ape" );
set.add( "dog" );
set.add( "bat" );
System.out.println( set );
}
}
OrderedSet( ape, bat, cat, dog )
OrderedSet3 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
/**
* Union, intersection, difference, symmetric difference, subset.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet3
{
public static void main( String[] args )
{
OrderedSet set1 = new OrderedSet( new LessString() );
set1.add( "ape" );
set1.add( "cat" );
set1.add( "bat" );
OrderedSet set2 = new OrderedSet();
set2.add( "bat" );
set2.add( "fox" );
set2.add( "ape" );
System.out.println( "set1 = " + set1 + ", set2 = " + set2 );
OrderedSet set3 = set1.union( set2 );
System.out.println( "set3 = set1.union( set2 ) = " + set3 );
OrderedSet set4 = set1.intersection( set2 );
System.out.println( "set4 = set1.intersection( set2 ) = " + set4 );
OrderedSet set5 = set1.difference( set2 );
System.out.println( "set5 = set1.difference( set2 ) = " + set5 );
OrderedSet set6 = set1.symmetricDifference( set2 );
System.out.println( "set6 = set1.symmetricDifference( set2 ) = " + set6 );
System.out.println( "set4.subsetOf( set3 ) = " + set4.subsetOf( set3 ) );
System.out.println( "set3.subsetOf( set4 ) = " + set3.subsetOf( set4 ) );
}
}
set1 = OrderedSet( ape, bat, cat ), set2 = OrderedSet( ape, bat, fox )
set3 = set1.union( set2 ) = OrderedSet( ape, bat, cat, fox )
set4 = set1.intersection( set2 ) = OrderedSet( ape, bat )
set5 = set1.difference( set2 ) = OrderedSet( cat )
set6 = set1.symmetricDifference( set2 ) = OrderedSet( cat, fox )
set4.subsetOf( set3 ) = true
set3.subsetOf( set4 ) = false
OrderedSet4 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
/**
* Counting, finding, erasing.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet4
{
public static void main( String[] args )
{
OrderedSet set = new OrderedSet( new LessString() );
set.add( "cat" );
set.add( "ape" );
set.add( "dog" );
set.add( "bat" );
System.out.println( set );
System.out.println( "set.count( dog ) = " + set.count( "dog" ) );
OrderedSetIterator i = set.find( "dog" );
if ( i.equals( set.end() ) ) // A simpler way: if ( i.atEnd() ) ...
System.out.println( "Could not find dog." );
else
System.out.println( "Found " + i.get() );
System.out.println( "set.remove( dog ) = " + set.remove( "dog" ) );
OrderedSetIterator j = set.find( "dog" );
if ( j.atEnd() ) // A simpler way: if ( j.equals( set.end() ) ) ...
System.out.println( "Could not find dog." );
else
System.out.println( "Found " + j.get() );
}
}
OrderedSet( ape, bat, cat, dog )
set.count( dog ) = 1
Found dog
set.remove( dog ) = 1
Could not find dog.
OrderedSet5 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Bounds.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet5
{
public static void main( String[] args )
{
OrderedSet set = new OrderedSet();
set.add( new Integer( 3 ) );
set.add( new Integer( 8 ) );
set.add( new Integer( 2 ) );
set.add( new Integer( -2 ) );
set.add( new Integer( 10 ) );
System.out.println( set );
OrderedSetIterator lower = set.lowerBound( new Integer( 3 ) );
System.out.println( "First element that is not before 3 = " + lower.get() );
OrderedSetIterator upper = set.upperBound( new Integer( 3 ) );
System.out.println( "First element that is after 3 = " + upper.get() );
}
}
OrderedSet( -2, 2, 3, 8, 10 )
First element that is not before 3 = 3
First element that is after 3 = 8
OrderedSet6 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;
/**
* Construction, enumeration, acceptance of duplicates.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet6
{
public static void main( String[] args )
{
// make the ordered set allow duplicates
OrderedSet set = new OrderedSet( true );
set.add( new Integer( 6 ) );
set.add( new Integer( 1 ) );
set.add( new Integer( 4 ) );
set.add( new Integer( 1 ) );
System.out.println( set );
System.out.println();
System.out.println( "Enumerate the OrderedSet" );
Enumeration e = set.elements();
while ( e.hasMoreElements() )
System.out.println( e.nextElement() );
System.out.println();
System.out.println( "Iterate through the OrderedSet" );
for ( OrderedSetIterator i = set.begin(); !i.atEnd(); i.advance() )
System.out.println( i.get() );
System.out.println();
System.out.println( "Show that duplicates can be added." );
set.add( new Integer( 8 ) );
System.out.println( "set = " + set );
set.add( new Integer( 4 ) );
System.out.println( "set = " + set );
}
}
OrderedSet( 1, 1, 4, 6 )
Enumerate the OrderedSet
1
1
4
6
Iterate through the OrderedSet
1
1
4
6
Show that duplicates can be added.
set = OrderedSet( 1, 1, 4, 6, 8 )
set = OrderedSet( 1, 1, 4, 4, 6, 8 )
OrderedSet7 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
/**
* Comparators.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet7
{
public static void main( String[] args )
{
// make the ordered set allow duplicates
OrderedSet set = new OrderedSet( new LessString(), true );
set.add( "cat" );
set.add( "ape" );
set.add( "bat" );
set.add( "dog" );
set.add( "bat" );
set.add( "cat" );
set.add( "bat" );
System.out.println( set );
}
}
OrderedSet( ape, bat, bat, bat, cat, cat, dog )
OrderedSet8 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
/**
* Counting, finding, erasing.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet8
{
public static void main( String[] args )
{
// make the ordered set allow duplicates
OrderedSet set = new OrderedSet( new LessString(), true );
set.add( "cat" );
set.add( "ape" );
set.add( "dog" );
set.add( "bat" );
set.add( "dog" );
System.out.println( set );
System.out.println( "set.count( dog ) = " + set.count( "dog" ) );
OrderedSetIterator i = set.find( "dog" );
if ( i.equals( set.end() ) ) // A simpler way: if ( i.atEnd() ) ...
System.out.println( "Could not find dog." );
else
System.out.println( "Found " + i.get() );
System.out.println( "set.remove( dog ) = " + set.remove( "dog" ) );
OrderedSetIterator j = set.find( "dog" );
if ( j.atEnd() ) // A simpler way: if ( j.equals( set.end() ) ) ...
System.out.println( "Could not find dog." );
else
System.out.println( "Found " + j.get() );
}
}
OrderedSet( ape, bat, cat, dog, dog )
set.count( dog ) = 2
Found dog
set.remove( dog ) = 2
Could not find dog.
OrderedSet9 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Bounds.
*
* @see com.objectspace.jgl,OrderedSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class OrderedSet9
{
public static void main( String[] args )
{
OrderedSet set = new OrderedSet( true ); // allow duplicates
set.add( new Integer( 3 ) );
set.add( new Integer( 8 ) );
set.add( new Integer( 2 ) );
set.add( new Integer( -2 ) );
set.add( new Integer( 3 ) );
set.add( new Integer( 10 ) );
System.out.println( set );
OrderedSetIterator lower = set.lowerBound( new Integer( 3 ) );
System.out.println( "First element that is not before 3 = " + lower.get() );
OrderedSetIterator upper = set.upperBound( new Integer( 3 ) );
System.out.println( "First element that is after 3 = " + upper.get() );
Range range = set.equalRange( new Integer( 3 ) );
System.out.println( "equalRange.first = " + range.begin.get() );
System.out.println( "equalRange.second = " + range.end.get() );
System.out.println( "Iterating values in the range..." );
ForwardIterator begin = range.begin;
ForwardIterator end = range.end;
while ( ! begin.equals( end ) )
{
System.out.println( begin.get() );
begin.advance();
}
}
}
OrderedSet( -2, 2, 3, 3, 8, 10 )
First element that is not before 3 = 3
First element that is after 3 = 8
equalRange.first = 3
equalRange.second = 8
Iterating values in the range...
3
3
All Packages Class Hierarchy This Package Previous Next Index