All Packages Class Hierarchy This Package Previous Next Index
Counting1 - Sum the values of a range.
Counting2 - Sum the values of a range using a binary function.
Counting3 - Calculate and sum the difference between adjacent pairs of values.
Counting4 - Calculate and sum the difference between adjacent pairs of values.
Counting5 - Calculate and sum the difference between adjacent pairs of values.
Counting1 Example Code
// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
/**
* Sum the values of a range.
*
* @see com.objectspace.jgl.algorithms.Counting
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Counting1
{
public static void main( String[] args )
{
int intArray[] = { 1, 2, 3, 4, 5 };
IntIterator begin = IntIterator.begin( intArray );
IntIterator end = IntIterator.end( intArray );
Number sum = Counting.accumulate( begin, end, new Integer( 0 ) );
System.out.println( "Sum = " + sum );
}
}
Sum = 15
Counting2 Example Code
// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.functions.*;
/**
* Sum the values of a range using a binary function.
*
* @see com.objectspace.jgl.algorithms.Counting
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Counting2
{
public static void main( String[] args )
{
Array array = new Array();
array.add( new Long( 5 ) );
array.add( new Long( 4 ) );
array.add( new Long( 3 ) );
array.add( new Long( 2 ) );
array.add( new Long( 1 ) );
Number prod = new Long( 1 );
prod = Counting.accumulate
(
array.start(),
array.finish(),
prod,
new TimesNumber( prod.getClass() )
);
System.out.println( "Product = " + prod );
}
}
Product = 120
Counting3 Example Code
// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
/**
* Calculate and sum the difference between adjacent pairs of values.
*
* @see com.objectspace.jgl.algorithms.Counting
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Counting3
{
public static void main( String[] args )
{
// create sample array
int intArray[] = { 1, 2, 4, 8, 16 };
IntIterator begin = IntIterator.begin( intArray );
IntIterator end = IntIterator.end( intArray );
Printing.println( begin, end );
// make sure destination hase enough space allocated
Array array = new Array( 5 );
Counting.adjacentDifference( begin, end, array.start() );
Printing.println( array );
}
}
( 1, 2, 4, 8, 16 )
( 1, 1, 2, 4, 8 )
Counting4 Example Code
// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
/**
* Calculate and sum the difference between adjacent pairs of values.
*
* @see com.objectspace.jgl.algorithms.Counting
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Counting4
{
public static void main( String[] args )
{
Array array = new Array();
for ( int i = 0; i < 10; ++i )
array.add( new Integer( i * i ) );
System.out.println( array );
// note that this writes over the original array
Counting.adjacentDifference( array, array.start() );
System.out.println( array );
}
}
Array( 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 )
Array( 0, 1, 3, 5, 7, 9, 11, 13, 15, 17 )
Counting5 Example Code
// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.functions.*;
/**
* Calculate and sum the difference between adjacent pairs of values.
*
* @see com.objectspace.jgl.algorithms.Counting
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Counting5
{
public static void main( String[] args )
{
Array array = new Array();
for ( long i = 1; i <= 10; ++i )
array.add( new Long( i ) );
System.out.println( array );
Array result = new Array();
Counting.adjacentDifference
(
array,
result,
new TimesNumber( Long.class )
);
System.out.println( result );
}
}
Array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
Array( 1, 2, 6, 12, 20, 30, 42, 56, 72, 90 )
All Packages Class Hierarchy This Package Previous Next Index