de.lmu.ifi.dbs.elki.utilities
Class Util

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.utilities.Util

public final class Util
extends Object

This class collects various static helper methods. For helper methods related to special application fields see other utilities classes.

See Also:
de.lmu.ifi.dbs.elki.utilities

Nested Class Summary
static class Util.MaskedArrayList<T>
          This class is a virtual collection based on masking an array list using a bit mask.
 
Constructor Summary
Util()
           
 
Method Summary
static
<O> void
addToList(List<O> list, O[] array)
          Adds the entries of the specified array to the end of the given list.
static int arrayFind(String[] arr, Object ref)
          Search an (unsorted) array linearly for an object.
static List<Integer> convertBitSetToListInt(BitSet b, int off)
          Convert a bit set to a list of integers, representing bits that are set
static double[] convertToDoubles(float[] values)
          Converts the specified array of doubles to an array of floats.
static double[] convertToDoubles(List<Double> values)
          Converts the specified list of Double objects to an array of doubles.
static float[] convertToFloat(double[] values)
          Converts the specified array of doubles to an array of floats.
static List<Float> convertToFloat(List<Double> values)
          Converts the specified list of double objects to a list of float objects.
static double[] copy(double[] array)
          Returns a new double array containing the same objects as are contained in the given array.
static String[] copy(String[] array)
          Returns a new String array containing the same objects as are contained in the given array.
static String getFilePrefix(String fileName)
          Returns the prefix of the specified fileName (i.e. the name of the file without extension).
static int getIndexOfMaximum(double[] values)
          Returns the index of the maximum of the given values.
static int indexOfNthSetBit(BitSet bitset, int nthSetBit)
          Returns the index of the nth set bit in the given BitSet.
static BitSet int2Bit(int n)
          Converts the specified positive integer value into a bit representation, where bit 0 denotes 20, bit 1 denotes 21 etc.
static
<O> void
intersection(Set<O> s1, Set<O> s2, Set<O> result)
          Provides the intersection of the two specified sets in the given result set.
static String[] joinArray(String[] array1, String[] array2)
          Joins the specified arrays.
static int mixHashCodes(int... hash)
          Mix multiple hashcodes into one.
static BitSet parseBitSet(char[] s)
          Returns a new BitSet initialized to the values represented by the specified char array only containing '0' and '1' values.
static BitSet parseBitSet(String s)
          Returns a new BitSet initialized to the values represented by the specified String only containing 0 and 1 values.
static double[] parseDoubles(String s)
          Returns a new Double array initialized to the values represented by the specified String and separated by comma, as performed by the valueOf method of class Double.
static float[] parseFloats(String s)
          Returns a new Float array initialized to the values represented by the specified String and separated by comma, as performed by the valueOf method of class Float.
static String parseSelectedBits(BitSet b, String sep)
          Returns a string that represents the selected bits of the specified BitSet, while the first bit starts with 1.
static
<O> void
print(List<O> list, String separator, PrintStream out)
          Prints the given list to the specified PrintStream.
static DoubleVector project(DoubleVector v, BitSet selectedAttributes)
          Provides a new DoubleVector as a projection on the specified attributes.
static SparseFloatVector project(SparseFloatVector v, BitSet selectedAttributes)
          Provides a new SparseFloatVector as a projection on the specified attributes.
static BitSet randomBitSet(int cardinality, int capacity, Random random)
          Creates a new BitSet of fixed cardinality with randomly set bits.
static double[] unbox(Double[] array)
          Returns the unboxed double array of the given Object Double array.
static double[] unbox(Number[] array)
          Returns the unboxed double array of the given Object Number array.
static float[] unboxToFloat(Number[] array)
          Returns the unboxed float array of the given Object Number array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

getFilePrefix

public static String getFilePrefix(String fileName)
Returns the prefix of the specified fileName (i.e. the name of the file without extension).

Parameters:
fileName - the name of the file
Returns:
the prefix of the specified fileName

copy

public static String[] copy(String[] array)
Returns a new String array containing the same objects as are contained in the given array.

Parameters:
array - an array to copy
Returns:
the copied array

copy

public static double[] copy(double[] array)
Returns a new double array containing the same objects as are contained in the given array.

Parameters:
array - an array to copy
Returns:
the copied array

unbox

public static double[] unbox(Double[] array)
Returns the unboxed double array of the given Object Double array.

Parameters:
array - the array to be unboxed
Returns:
the unboxed double array

unbox

public static double[] unbox(Number[] array)
Returns the unboxed double array of the given Object Number array.

Parameters:
array - the array to be unboxed
Returns:
the unboxed double array

unboxToFloat

public static float[] unboxToFloat(Number[] array)
Returns the unboxed float array of the given Object Number array.

Parameters:
array - the array to be unboxed
Returns:
the unboxed float array

parseDoubles

public static double[] parseDoubles(String s)
Returns a new Double array initialized to the values represented by the specified String and separated by comma, as performed by the valueOf method of class Double.

Parameters:
s - the string to be parsed.
Returns:
a new Double array represented by s

parseFloats

public static float[] parseFloats(String s)
Returns a new Float array initialized to the values represented by the specified String and separated by comma, as performed by the valueOf method of class Float.

Parameters:
s - the string to be parsed.
Returns:
a new Float array represented by s

convertToFloat

public static List<Float> convertToFloat(List<Double> values)
Converts the specified list of double objects to a list of float objects.

Parameters:
values - the list of double objects to be converted
Returns:
the converted list of float objects

convertToFloat

public static float[] convertToFloat(double[] values)
Converts the specified array of doubles to an array of floats.

Parameters:
values - the array of doubles to be converted
Returns:
the converted array of floats

convertToDoubles

public static double[] convertToDoubles(float[] values)
Converts the specified array of doubles to an array of floats.

Parameters:
values - the array of doubles to be converted
Returns:
the converted array of floats

convertToDoubles

public static double[] convertToDoubles(List<Double> values)
Converts the specified list of Double objects to an array of doubles.

Parameters:
values - the list of Double objects to be converted
Returns:
the converted array of doubles

print

public static <O> void print(List<O> list,
                             String separator,
                             PrintStream out)
Prints the given list to the specified PrintStream. The list entries are separated by the specified separator. The last entry is not followed by a separator. Thus, if a newline is used as separator, it might make sense to print a newline to the PrintStream after calling this method.

Type Parameters:
O - object class
Parameters:
list - the list to be printed
separator - the separator to separate entries of the list
out - the target PrintStream

getIndexOfMaximum

public static int getIndexOfMaximum(double[] values)
                             throws ArrayIndexOutOfBoundsException
Returns the index of the maximum of the given values. If no value is bigger than the first, the index of the first entry is returned.

Parameters:
values - the values to find the index of the maximum
Returns:
the index of the maximum in the given values
Throws:
ArrayIndexOutOfBoundsException - if values.length==0

parseBitSet

public static BitSet parseBitSet(String s)
Returns a new BitSet initialized to the values represented by the specified String only containing 0 and 1 values.

Parameters:
s - the string to be parsed.
Returns:
a new BitSet represented by s

parseBitSet

public static BitSet parseBitSet(char[] s)
Returns a new BitSet initialized to the values represented by the specified char array only containing '0' and '1' values.

Parameters:
s - the char array to be parsed.
Returns:
a new BitSet represented by s

parseSelectedBits

public static String parseSelectedBits(BitSet b,
                                       String sep)
Returns a string that represents the selected bits of the specified BitSet, while the first bit starts with 1. The selected bits are separated by the specified separator sep. If sep is ",", the result is suitable as a parameter for an IntListParameter.

Parameters:
b - the bit set to be parsed
sep - the separator
Returns:
a string representing the selected bits of the specified BitSet

convertBitSetToListInt

public static List<Integer> convertBitSetToListInt(BitSet b,
                                                   int off)
Convert a bit set to a list of integers, representing bits that are set

Parameters:
b - Bitset
off - Offset, set to 0 to start counting at 0, 1 to start counting at 1.
Returns:
List

randomBitSet

public static BitSet randomBitSet(int cardinality,
                                  int capacity,
                                  Random random)
Creates a new BitSet of fixed cardinality with randomly set bits.

Parameters:
cardinality - the cardinality of the BitSet to create
capacity - the capacity of the BitSet to create - the randomly generated indices of the bits set to true will be uniformly distributed between 0 (inclusive) and capacity (exclusive)
random - a Random Object to create the sequence of indices set to true - the same number occurring twice or more is ignored but the already selected bit remains true
Returns:
a new BitSet with randomly set bits

project

public static DoubleVector project(DoubleVector v,
                                   BitSet selectedAttributes)
Provides a new DoubleVector as a projection on the specified attributes. If the given DoubleVector has already an ID not null, the same ID is set in the returned new DoubleVector. Nevertheless, the returned DoubleVector is not backed by the given DoubleVector, i.e., any changes affecting v after calling this method will not affect the newly returned DoubleVector.

Parameters:
v - a DoubleVector to project
selectedAttributes - the attributes selected for projection
Returns:
a new DoubleVector as a projection on the specified attributes
Throws:
IllegalArgumentException - if the given selected attributes specify an attribute as selected which is out of range for the given DoubleVector.
See Also:
DoubleVector.doubleValue(int)

project

public static SparseFloatVector project(SparseFloatVector v,
                                        BitSet selectedAttributes)
Provides a new SparseFloatVector as a projection on the specified attributes. If the given SparseFloatVector has already an ID not null, the same ID is set in the returned new SparseFloatVector. Nevertheless, the returned SparseFloatVector is not backed by the given SparseFloatVector, i.e., any changes affecting v after calling this method will not affect the newly returned SparseFloatVector.

Parameters:
v - a SparseFloatVector to project
selectedAttributes - the attributes selected for projection
Returns:
a new SparseFloatVector as a projection on the specified attributes
Throws:
IllegalArgumentException - if the given selected attributes specify an attribute as selected which is out of range for the given SparseFloatVector.

indexOfNthSetBit

public static int indexOfNthSetBit(BitSet bitset,
                                   int nthSetBit)
                            throws IllegalArgumentException
Returns the index of the nth set bit in the given BitSet. For the parameter nthSetBit, following condition is assumed: 1 ≤ nthSetBit ≤ bitset.cardinality(). Otherwise, i.e., if the Bitset contains less than nthSetBit set bits or nthSetBit is not a positive number, the method throws an IllegalArgumentException. The worstcase runtime complexity of this method is in O( bitset.cardinality()).

Parameters:
bitset - the BitSet to derive the index of the nth set bit in
nthSetBit - which set bit to derive the index of
Returns:
the index of the nth set bit in the given BitSet
Throws:
IllegalArgumentException - if the Bitset contains less than nthSetBit set bits or nthSetBit is not a positive number

intersection

public static <O> void intersection(Set<O> s1,
                                    Set<O> s2,
                                    Set<O> result)
Provides the intersection of the two specified sets in the given result set.

Type Parameters:
O - object class
Parameters:
s1 - the first set
s2 - the second set
result - the result set

int2Bit

public static BitSet int2Bit(int n)
Converts the specified positive integer value into a bit representation, where bit 0 denotes 20, bit 1 denotes 21 etc.

Parameters:
n - the positive integer value to be converted
Returns:
the specified integer value into a bit representation

joinArray

public static String[] joinArray(String[] array1,
                                 String[] array2)
Joins the specified arrays.

Parameters:
array1 - the first array
array2 - the second array
Returns:
a new array containing the entries of array1 and the array2.

addToList

public static <O> void addToList(List<O> list,
                                 O[] array)
Adds the entries of the specified array to the end of the given list.

Type Parameters:
O - object class
Parameters:
list - the list
array - the array containing the objects to be added to the list

arrayFind

public static int arrayFind(String[] arr,
                            Object ref)
Search an (unsorted) array linearly for an object.

Parameters:
arr - Array to search
ref - Object to search for
Returns:
Index of object or -1 if not found.

mixHashCodes

public static final int mixHashCodes(int... hash)
Mix multiple hashcodes into one.

Parameters:
hash - Hashcodes to mix
Returns:
Mixed hash code

Release 0.4.0 (2011-09-20_1324)