Algorithms Examples

Algorithms Examples

Algorithms 1 - No Description Available

Algorithms 2 - No Description Available

Algorithms 3 - No Description Available

Algorithms 4 - No Description Available

Algorithms 5 - No Description Available

Algorithms 6 - No Description Available

Algorithms 7 - No Description Available

Algorithms 8 - No Description Available

Algorithms 9 - No Description Available


Algorithms1 Example Code

// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; 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 PlusInteger() ); 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 ObjectSpace, Inc. import jgl.*; import java.util.Vector; 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 ObjectSpace, Inc. import jgl.*; 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 NegativeInteger() ); 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 ObjectSpace, Inc. import jgl.*; 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 NegativeInteger() ); System.out.println( "first negative = " + negative ); boolean some = Finding.some( array, new NegativeInteger() ); 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 ObjectSpace, Inc. import jgl.*; 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 GreaterInteger(), 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 ObjectSpace, Inc. import jgl.*; 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 NegativeInteger(); 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 ObjectSpace, Inc. import jgl.*; public class Algorithms7 { public static void main( String[] args ) { Deque deque = new Deque(); deque.add( "Batman" ); deque.add( "Superman" ); deque.add( "Phantom" ); deque.add( "Spiderman" ); System.out.println( "before reverse = " + deque ); Reversing.reverse( deque ); System.out.println( "after reverse = " + deque ); } }

Algorithms7 Example Output

before reverse = Deque( Batman, Superman, Phantom, Spiderman ) after reverse = Deque( Spiderman, Phantom, Superman, Batman )

Algorithms8 Example Code

// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; 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 ObjectSpace, Inc. import jgl.*; 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 NegateInteger(); 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 TimesInteger(); 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 )