ObjectSpace Homepage

JGL - The Generic Collection Library for Java
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Algorithms examples

Algorithms1 - Applying provides algorithms for affecting every element in a container.

Algorithms2 - Copying provides algorithms for adding contents of one container to another.

Algorithms3 - Counting provides algorithms for tabulating the elements of a container.

Algorithms4 - Finding provides algorithms for locating elements in a container.

Algorithms5 - Filtering provides algorithms for creating a variation of a sequence.

Algorithms6 - Replacing provides algorithms for substituting elements in a container.

Algorithms7 - Reversing provides algorithms for changing the order of a sequence.

Algorithms8 - Sorting provides algorithms for sorting a sequence.

Algorithms9 - Transforming provides algorithms for modifying elements in a sequence.


Algorithms1 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.functions.*;

/**
 * Applying provides algorithms for affecting every element in a container.
 *
 * @see com.objectspace.jgl.algorithms.Applying
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms1
  {
  public static void main( String[] args )
    {
    Array array1 = new Array();
    array1.add( "cat" );
    array1.add( "monkey" );
    array1.add( "goat" );
    Applying.forEach( array1, new PrintFunction() );

    SList list = new SList();
    list.add( new Integer( 3 ) );
    list.add( new Integer( 7 ) );
    list.add( new Integer( 4 ) );
    Integer total = (Integer)Applying.inject( list, new Integer( 0 ), new PlusNumber() );
    System.out.println( "list = " + list + ", total = " + total );
    }
  }

class PrintFunction implements UnaryFunction
  {
  public Object execute( Object object )
    {
    System.out.println( "PRINT " + object );
    return null; // Not used.
    }
  }

Algorithms1 Example Output

PRINT cat
PRINT monkey
PRINT goat
list = SList( 3, 7, 4 ), total = 14

Algorithms2 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import java.util.Vector;

/**
 * Copying provides algorithms for adding contents of one container to another.
 *
 * @see com.objectspace.jgl.algorithms.Copying
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms2
  {
  public static void main( String[] args )
    {
    int ints[] = { 2, 6, 3, 7 };
    Vector vector = new Vector();
    vector.addElement( new Integer( 1 ) );
    vector.addElement( new Integer( 4 ) );

    // Create container adapters.
    IntArray intArray = new IntArray( ints );
    VectorArray vectorArray = new VectorArray( vector );

    System.out.println( "vector before copying = " + vector );
    Copying.copy( intArray, vectorArray );
    System.out.println( "vector after copying = " + vector );
    }
  }

Algorithms2 Example Output

vector before copying = [1, 4]
vector after copying = [1, 4, 2, 6, 3, 7]

Algorithms3 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.predicates.*;

/**
 * Counting provides algorithms for tabulating the elements of a container.
 *
 * @see com.objectspace.jgl.algorithms.Counting
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms3
  {
  public static void main( String[] args )
    {
    SList list = new SList();
    list.add( new Integer( -1 ) );
    list.add( new Integer( 1 ) );
    list.add( new Integer( -2 ) );
    list.add( new Integer( 1 ) );
    list.add( new Integer( -3 ) );
    System.out.println( "list = " + list );

    Object value = new Integer( 1 );
    int n1 = Counting.count( list, value );
    System.out.println( "Occurences of " + value + " = " + n1 );

    int n2 = Counting.countIf( list, new NegativeNumber() );
    System.out.println( "Occurences of a negative = " + n2 );
    }
  }

Algorithms3 Example Output

list = SList( -1, 1, -2, 1, -3 )
Occurences of 1 = 2
Occurences of a negative = 3

Algorithms4 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.predicates.*;

/**
 * Finding provides algorithms for locating elements in a container.
 *
 * @see com.objectspace.jgl.algorithms.Finding
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms4
  {
  public static void main( String[] args )
    {
    int ints[] = { 3, 7, 8, 2, -5, 8, 9, -2 };
    IntArray array = new IntArray( ints );
    System.out.println( "array = " + array );

    Integer negative = (Integer)Finding.detect( array, new NegativeNumber() );
    System.out.println( "first negative = " + negative );

    boolean some = Finding.some( array, new NegativeNumber() );
    System.out.println( "some items are negative = " + some );
    }
  }

Algorithms4 Example Output

array = int[]( 3, 7, 8, 2, -5, 8, 9, -2 )
first negative = -5
some items are negative = true

Algorithms5 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.functions.*;
import com.objectspace.jgl.predicates.*;

/**
 * Filtering provides algorithms for creating a variation of a sequence.
 *
 * @see com.objectspace.jgl.algorithms.Filtering
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms5
  {
  public static void main( String[] args )
    {
    Array array1 = new Array();
    array1.add( "cat" );
    array1.add( "monkey" );
    array1.add( "goat" );
    array1.add( "elephant" );
    System.out.println( "array1 = " + array1 );

    // Predicate that returns true if a string is greater than 4 characters long.
    UnaryPredicate predicate = new UnaryComposePredicate
      (
      new BindSecondPredicate( new GreaterNumber(), new Integer( 4 ) ),
      new LengthString()
      );

    Array array2 = (Array)Filtering.select( array1, predicate );
    System.out.println( "strings with length > 4 = " + array2 );

    Array array3 = (Array)Filtering.reject( array1, predicate );
    System.out.println( "strings with length <= 4 = " + array3 );
    }
  }

Algorithms5 Example Output

array1 = Array( cat, monkey, goat, elephant )
strings with length > 4 = Array( monkey, elephant )
strings with length <= 4 = Array( cat, goat )

Algorithms6 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.predicates.*;

/**
 * Replacing provides algorithms for substituting elements in a container.
 *
 * @see com.objectspace.jgl.algorithms.Replacing
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms6
  {
  public static void main( String[] args )
    {
    DList list = new DList();
    list.add( new Integer( -1 ) );
    list.add( new Integer( 1 ) );
    list.add( new Integer( -2 ) );
    list.add( new Integer( 1 ) );
    list.add( new Integer( -3 ) );
    System.out.println( "list = " + list );

    Object oldValue = new Integer( 1 );
    Object newValue = new Integer( 4 );
    int n1 = Replacing.replace( list, oldValue, newValue );
    System.out.println( "after 1 -> 4, list = " + list );

    Array array = new Array();
    UnaryPredicate predicate = new NegativeNumber();
    newValue = new Integer( 0 );
    Replacing.replaceCopyIf( list, array, predicate, newValue );
    System.out.println( "list = " + list );
    System.out.println( "array = " + array );
    }
  }

Algorithms6 Example Output

list = DList( -1, 1, -2, 1, -3 )
after 1 -> 4, list = DList( -1, 4, -2, 4, -3 )
list = DList( -1, 4, -2, 4, -3 )
array = Array( 0, 4, 0, 4, 0 )

Algorithms7 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;

/**
 * Reversing provides algorithms for changing the order of a sequence.
 *
 * @see com.objectspace.jgl.algorithms.Reversing
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms7
  {
  public static void main( String[] args )
    {
    Deque deque = new Deque();
    deque.add( "Batman" );
    deque.add( "Superman" );
    deque.add( "Phantom" );
    deque.add( "Spider-Man" );
    System.out.println( "before reverse = " + deque );
    Reversing.reverse( deque );
    System.out.println( "after reverse = " + deque );
    }
  }

Algorithms7 Example Output

before reverse = Deque( Batman, Superman, Phantom, Spider-Man )
after reverse = Deque( Spider-Man, Phantom, Superman, Batman )

Algorithms8 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.predicates.*;

/**
 * Sorting provides algorithms for sorting a sequence.
 *
 * @see com.objectspace.jgl.algorithms.Sorting
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms8
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( new Integer( 3 ) );
    array.add( new Integer( -2 ) );
    array.add( new Integer( 4 ) );
    array.add( new Integer( -5 ) );
    System.out.println( "unsorted array = " + array );
    Sorting.sort( array );
    System.out.println( "sorted array = " + array );

    Deque deque = new Deque();
    deque.add( "triangle" );
    deque.add( "square" );
    deque.add( "pentagon" );
    deque.add( "hexagon" );
    System.out.println( "unsorted deque = " + deque );
    Sorting.sort( deque, new LessString() );
    System.out.println( "sorted deque = " + deque );
    }
  }

Algorithms8 Example Output

unsorted array = Array( 3, -2, 4, -5 )
sorted array = Array( -5, -2, 3, 4 )
unsorted deque = Deque( triangle, square, pentagon, hexagon )
sorted deque = Deque( hexagon, pentagon, square, triangle )

Algorithms9 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.functions.*;

/**
 * Transforming provides algorithms for modifying elements in a sequence.
 *
 * @see com.objectspace.jgl.algorithms.Transforming
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Algorithms9
  {
  public static void main( String[] args )
    {
    int ints1[] = { 1, 3, 5, 2 };
    Array array = new Array();
    IntArray intArray1 = new IntArray( ints1 );
    UnaryFunction function = new NegateNumber();
    Transforming.transform( intArray1, array, function );
    System.out.println( "ints1 = " + intArray1 );
    System.out.println( "array = " + array );
    System.out.println();

    int ints2[] = { 2, 4, 2, 3 };
    int ints3[] = { 3, 6, 2, 1 };
    SList list = new SList();
    IntArray intArray2 = new IntArray( ints2 );
    IntArray intArray3 = new IntArray( ints3 );
    BinaryFunction function2 = new TimesNumber();
    Transforming.transform( intArray2, intArray3, list, function2 );
    System.out.println( "ints2 = " + intArray2 );
    System.out.println( "ints3 = " + intArray3 );
    System.out.println( "list = " + list );
    System.out.println();

    Array array1 = new Array();
    array1.add( "cat" );
    array1.add( "monkey" );
    array1.add( "goat" );
    System.out.println( "array1 = " + array1 );
    Array array2 = (Array)Transforming.collect( array1, new LengthString() );
    System.out.println( "array2 = " + array2 );
    }
  }

Algorithms9 Example Output

ints1 = int[]( 1, 3, 5, 2 )
array = Array( -1, -3, -5, -2 )

ints2 = int[]( 2, 4, 2, 3 )
ints3 = int[]( 3, 6, 2, 1 )
list = SList( 6, 24, 4, 3 )

array1 = Array( cat, monkey, goat )
array2 = Array( 3, 6, 4 )

All Packages  Class Hierarchy  This Package  Previous  Next  Index