com.samskivert.util
Class IntListUtil

java.lang.Object
  extended by com.samskivert.util.IntListUtil

public class IntListUtil
extends Object

This class manages arrays of ints. Some of those routines mimic the behavior of array lists, others provide other more specialized (generally faster but making requirements of the caller) list behavior.

An example is probably in order:

 int[] list = null;

 // add our ints to a list
 list = ListUtil.add(list, 2);
 list = ListUtil.add(list, 5);

 // remove 5 from the list (does so by clearing out that index, but it
 // doesn't slide subsequent elements down)
 ListUtil.clear(list, 5);

 // append our objects to the end of the list letting list util know
 // that we're tracking the list size
 list = ListUtil.add(list, 0, 2);
 list = ListUtil.add(list, 1, 5);

 // remove the elements from the list, compacting it to preserve
 // element continuity
 ListUtil.removeAt(list, 0);
 ListUtil.remove(list, 5);
 
The array is initially assumed to be populated with zeros and zero is assumed to be an emty slot.

See the documentation for the individual functions for their exact behavior.


Field Summary
protected static int DEFAULT_LIST_SIZE
          The size of a list to create if we have to create one entirely from scratch rather than just expand it.
 
Constructor Summary
IntListUtil()
           
 
Method Summary
protected static int[] accomodate(int[] list, int index)
          Creates a new list that will accomodate the specified index and copies the contents of the old list to the first.
static int[] add(int[] list, int value)
          Adds the specified value to the first empty slot in the specified list.
static int[] add(int[] list, int startIdx, int value)
          Adds the specified value to the next empty slot in the specified list.
static Integer[] box(int[] list)
          Covnerts an array of primitives to an array of objects.
static int clear(int[] list, int value)
          Clears out the first value that is equal to the supplied value.
static int[] compact(int[] list)
          Converts a sparse array (with zero-valued entries) into a compact array (where all elements contain non-zero values) with ordering preserved.
static boolean contains(int[] list, int value)
          Looks for an element that is equal to the supplied value.
static int[] getMaxIndexes(int[] values)
          Returns an array of the indexes in the given array of values that have the maximum value in the array, or a zero-length array if the supplied array of values is null or zero-length.
static int getMaxValue(int[] values)
          Returns the maximum value in the given array of values, or Integer.MIN_VALUE if the array is null or zero-length.
static int getMaxValueIndex(int[] values)
          Returns the index of the maximum value in the given array of values, or -1 if the array is null or zero-length.
static int[] getMinIndexes(int[] values)
          Returns an array of the indexes in the given array of values that have the minimum value in the array, or a zero-length array if the supplied array of values is null or zero-length.
static int getMinValue(int[] values)
          Returns the minimum value in the given array of values, or Integer.MAX_VALUE if the array is null or zero-length.
static int indexOf(int[] list, int value)
          Looks for an element that is equal to the supplied value and returns its index in the array.
static float[] normalize(int[] values)
          Normalizes an array of integers from the bounding [min,max] to [0.0, 1.0].
static int remove(int[] list, int value)
          Removes the first value that is equal to the supplied value.
static int removeAt(int[] list, int index)
          Removes the value at the specified index.
static int sum(int[] list)
          Returns the total of all of the values in the list.
static int[] testAndAdd(int[] list, int value)
          Searches through the list checking to see if the value supplied is already in the list and adds it if it is not.
static int[] unbox(Integer[] list)
          Converts an array of Integer objects to an array of primitives.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LIST_SIZE

protected static final int DEFAULT_LIST_SIZE
The size of a list to create if we have to create one entirely from scratch rather than just expand it.

See Also:
Constant Field Values
Constructor Detail

IntListUtil

public IntListUtil()
Method Detail

add

public static int[] add(int[] list,
                        int value)
Adds the specified value to the first empty slot in the specified list. Begins searching for empty slots at zeroth index.

Parameters:
list - the list to which to add the value. Can be null.
value - the value to add.
Returns:
a reference to the list with value added (might not be the list you passed in due to expansion, or allocation).

add

public static int[] add(int[] list,
                        int startIdx,
                        int value)
Adds the specified value to the next empty slot in the specified list. Begins searching for empty slots at the specified index. This can be used to quickly add values to a list that preserves consecutivity by calling it with the size of the list as the first index to check.

Parameters:
list - the list to which to add the value. Can be null.
startIdx - the index at which to start looking for a spot.
value - the value to add.
Returns:
a reference to the list with the value added (might not be the list you passed in due to expansion, or allocation).

testAndAdd

public static int[] testAndAdd(int[] list,
                               int value)
Searches through the list checking to see if the value supplied is already in the list and adds it if it is not.

Parameters:
list - the list to which to add the value. Can be null.
value - the value to test and add.
Returns:
a reference to the list with value added (might not be the list you passed in due to expansion, or allocation) or null if the value was already in the original array.

contains

public static boolean contains(int[] list,
                               int value)
Looks for an element that is equal to the supplied value. Passing a zero value to this function will cleverly tell you whether or not there are any empty elements in the array which is probably not very useful.

Returns:
true if a matching value was found, false otherwise.

indexOf

public static int indexOf(int[] list,
                          int value)
Looks for an element that is equal to the supplied value and returns its index in the array. Passing a zero value to this function will cleverly tell you whether or not there are any empty elements in the array which is probably not very useful.

Returns:
the index of the first matching value if one was found, -1 otherwise.

clear

public static int clear(int[] list,
                        int value)
Clears out the first value that is equal to the supplied value. Passing a zero value to this function will cleverly tell you the index of the first empty element in the array which it will have kindly overwritten with zero just for good measure.

Returns:
the value that was removed or zero if it was not found.

remove

public static int remove(int[] list,
                         int value)
Removes the first value that is equal to the supplied value. The values after the removed value will be slid down the array one spot to fill the place of the removed value.

Returns:
the value that was removed from the array or zero if no matching object was found.

removeAt

public static int removeAt(int[] list,
                           int index)
Removes the value at the specified index. The values after the removed value will be slid down the array one spot to fill the place of the removed value. If a null array is supplied or one that is not large enough to accomodate this index, zero is returned.

Returns:
the value that was removed from the array or zero if no value existed at that location.

compact

public static int[] compact(int[] list)
Converts a sparse array (with zero-valued entries) into a compact array (where all elements contain non-zero values) with ordering preserved.


sum

public static int sum(int[] list)
Returns the total of all of the values in the list.


getMaxValue

public static int getMaxValue(int[] values)
Returns the maximum value in the given array of values, or Integer.MIN_VALUE if the array is null or zero-length.


getMinValue

public static int getMinValue(int[] values)
Returns the minimum value in the given array of values, or Integer.MAX_VALUE if the array is null or zero-length.


getMaxValueIndex

public static int getMaxValueIndex(int[] values)
Returns the index of the maximum value in the given array of values, or -1 if the array is null or zero-length.


getMaxIndexes

public static int[] getMaxIndexes(int[] values)
Returns an array of the indexes in the given array of values that have the maximum value in the array, or a zero-length array if the supplied array of values is null or zero-length.


getMinIndexes

public static int[] getMinIndexes(int[] values)
Returns an array of the indexes in the given array of values that have the minimum value in the array, or a zero-length array if the supplied array of values is null or zero-length.


normalize

public static float[] normalize(int[] values)
Normalizes an array of integers from the bounding [min,max] to [0.0, 1.0]. If min == max, all elements in the returned array will be 1f.


accomodate

protected static int[] accomodate(int[] list,
                                  int index)
Creates a new list that will accomodate the specified index and copies the contents of the old list to the first.


box

public static Integer[] box(int[] list)
Covnerts an array of primitives to an array of objects.


unbox

public static int[] unbox(Integer[] list)
Converts an array of Integer objects to an array of primitives.



Copyright © 2000-2008 Michael Bayne. All Rights Reserved.