org.knime.core.data.vector.bytevector
Class DenseByteVector

java.lang.Object
  extended by org.knime.core.data.vector.bytevector.DenseByteVector

public class DenseByteVector
extends Object

A vector of fixed length holding byte counts at specific positions. Only positive values of counts are supported. Each index can store a number between 0 and 255 (both inclusive). Attempts to store negative numbers or numbers larger than 255 cause an exception.
The maximum length is 2147483645.
The implementation is not thread-safe.

Author:
ohl, University of Konstanz

Constructor Summary
DenseByteVector(byte[] counts)
          Creates a new vector initialized by the passed counts.
DenseByteVector(DenseByteVector byteVector)
          Creates a new vector initialized by the passed vector.
DenseByteVector(int length)
          Creates a new instance with the specified length.
 
Method Summary
 DenseByteVector add(DenseByteVector bv, boolean remainder)
          Returns a new vector with the sum of the counts at each position.
 int cardinality()
          Returns the number of counts larger than zero stored in this vector.
 void clear(int index)
          Sets the count at the specified index to zero.
 void clear(int startIdx, int endIdx)
          Resets the count at all positions from startIdx (inclusive) to endIdx (exclusive).
 DenseByteVector concatenate(DenseByteVector bv)
          Appends the argument at the end of this vector.
 boolean equals(Object obj)
          
 void fill(int startIdx, int endIdx, int count)
          Sets the new count value at all positions from startIdx (inclusive) to endIdx (exclusive).
 int get(int index)
          Returns the count stored at the specified position.
 int[] getAllCounts()
          Returns a copy of the internal array of counts.
 byte[] getAllCountsAsBytes()
          Returns a copy of the internal array of counts.
 int hashCode()
          
 boolean isEmpty()
          Checks all counts and returns true if they are all zero.
 int length()
          Returns the length of the vector.
 DenseByteVector max(DenseByteVector bv)
          Returns a new vector with the maximum of the counts at each position.
 DenseByteVector min(DenseByteVector bv)
          Returns a new vector with the minimum of the counts at each position.
 int nextCountIndex(int idx)
          Returns the index equal to or larger than the specified index that contains a count larger than zero.
 int nextZeroIndex(int idx)
          Returns the index equal to or larger than the specified index that contains a zero count.
 void set(int index, int count)
          Sets the new count value at the specified index.
 DenseByteVector subSequence(int startIdx, int endIdx)
          Creates and returns a new byte vector that contains a subsequence of this vector, beginning with the byte at index startIdx and with its last byte being this' byte at position endIdx - 1.
 long sumOfAllCounts()
          Calculates the checksum, the sum of all counts stored.
 String toString()
          Creates a string containing all counts of this vector (in braces ({ }) and comma separated), starting (on the left) with index 0.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DenseByteVector

DenseByteVector(int length)
Creates a new instance with the specified length. All bytes are set to zero.

Parameters:
length - the fixed length of the vector.

DenseByteVector

DenseByteVector(byte[] counts)
Creates a new vector initialized by the passed counts. The length of the new vector is the length of the argument array. The passed values are interpreted as unsigned positive values in the range of 0 ... 255.

Parameters:
counts - the value to initialize the new vector with.

DenseByteVector

DenseByteVector(DenseByteVector byteVector)
Creates a new vector initialized by the passed vector. The created vector is an identical copy.

Parameters:
byteVector - the byte vector to clone.
Method Detail

length

public int length()
Returns the length of the vector.

Returns:
the length of the vector.

set

public void set(int index,
                int count)
Sets the new count value at the specified index.

Parameters:
index - the index of the count value to change.
count - the new value for the specified position.
Throws:
ArrayIndexOutOfBoundsException - if the index is negative or larger than or equal to the length of the vector.
IllegalArgumentException - if the count is negative or larger than 255

fill

public void fill(int startIdx,
                 int endIdx,
                 int count)
Sets the new count value at all positions from startIdx (inclusive) to endIdx (exclusive).

Parameters:
startIdx - the first index to store the new value at.
endIdx - the first index the new value should not be stored anymore.
count - the new value that should be stored in the specified index range
Throws:
ArrayIndexOutOfBoundsException - if startIdx is negative or endIdx is larger than the length of the vector.
IllegalArgumentException - if the endIdx is smaller than the startIdx, or if the specified count is negative or larger than 255.

clear

public void clear(int index)
Sets the count at the specified index to zero. Equivalent to a call to set(int, int) with count zero.

Parameters:
index - the index of the position the count should be reset at.
Throws:
ArrayIndexOutOfBoundsException - if the index is negative or larger than or equal to the length of the vector.

clear

public void clear(int startIdx,
                  int endIdx)
Resets the count at all positions from startIdx (inclusive) to endIdx (exclusive). Equivalent to a call to fill(int, int, int) with count zero.

Parameters:
startIdx - the first index to set the count to zero.
endIdx - the first index the count not be reset anymore.
Throws:
ArrayIndexOutOfBoundsException - if startIdx is negative or endIdx is larger than the length of the vector.
IllegalArgumentException - if the endIdx is smaller than the startIdx.

get

public int get(int index)
Returns the count stored at the specified position.

Parameters:
index - the index of the count to return
Returns:
the count stored at the specified index.
Throws:
ArrayIndexOutOfBoundsException - if the specified index is negative or too large.

nextCountIndex

public int nextCountIndex(int idx)
Returns the index equal to or larger than the specified index that contains a count larger than zero. If no such index exists (i.e. the vector contains only zeros from idx on) -1 is returned. I.e. if a value not equal -1 is returned, a call to get(int) with the result as argument returns a number larger than zero.

Parameters:
idx - the starting index to look for the next count larger than zero. It is okay to pass an index larger than the length of this vector (in which case -1 is returned).
Returns:
the next index with a count larger than zero that is equal to or larger than idx, or -1 if the vector contains only zeros on and after idx.

nextZeroIndex

public int nextZeroIndex(int idx)
Returns the index equal to or larger than the specified index that contains a zero count. If no such index exists (i.e. the vector contains only positive numbers from idx on) -1 is returned. I.e. if a value not equal -1 is returned, a call to get(int) with the result as argument returns zero.

Parameters:
idx - the starting index to look for the next zero count. It is okay to pass an index larger than the length of this vector (in which case -1 is returned).
Returns:
the next index with a zero count that is equal to or larger than idx, or -1 if the vector contains only counts larger than zero on and after idx.

add

public DenseByteVector add(DenseByteVector bv,
                           boolean remainder)
Returns a new vector with the sum of the counts at each position. The result's length is the maximum of this' and the argument's length. The value at position i in the result is the sum of the counts in this' and the arguments vector at position i.

Parameters:
bv - the vector to add to this one (position-wise).
remainder - if true and the result of the addition is larger than 255, the value in the result vector will be set to the remainder when divided by 255 - if false, the result vector is set to 255 if the sum is larger than 255. (Setting it to true performs slightly better.)
Returns:
a new instance holding at each position the sum of the counts.

max

public DenseByteVector max(DenseByteVector bv)
Returns a new vector with the maximum of the counts at each position. The result's length is the maximum of this' and the argument's length. The value at position i in the result is the maximum of the counts in this' and the arguments vector at position i.

Parameters:
bv - the vector to compute the maximum of (position-wise).
Returns:
a new instance holding at each position the maximum of the counts.

min

public DenseByteVector min(DenseByteVector bv)
Returns a new vector with the minimum of the counts at each position. The result's length is the maximum of this' and the argument's length. The value at position i in the result is the minimum of the counts in this' and the arguments vector at position i.

Parameters:
bv - the vector to compute the minimum of (position-wise).
Returns:
a new instance holding at each position the minimum of the counts.

concatenate

public DenseByteVector concatenate(DenseByteVector bv)
Appends the argument at the end of this vector. It creates and returns a new vector whose length is the sum of this' and the argument's length. The lower indices in the result contain the counts of this vector, the positions with indices beyond the length of this contain the argument's counts.

Parameters:
bv - the vector to concatenate with this.
Returns:
a new instance containing this vector with the argument appended

subSequence

public DenseByteVector subSequence(int startIdx,
                                   int endIdx)
Creates and returns a new byte vector that contains a subsequence of this vector, beginning with the byte at index startIdx and with its last byte being this' byte at position endIdx - 1. The length of the result vector is endIdx - startIdx. If startIdx equals endIdx a vector of length zero is returned.

Parameters:
startIdx - the startIdx of the subsequence
endIdx - the first byte in this vector after startIdx that is not included in the result sequence.
Returns:
a new vector of length endIdx - startIdx containing the subsequence of this vector from startIdx (included) to endIdx (not included anymore).

hashCode

public int hashCode()

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

toString

public String toString()
Creates a string containing all counts of this vector (in braces ({ }) and comma separated), starting (on the left) with index 0. If the vector is larger than 30000, the result is truncated (and ends with "... }" then).

Overrides:
toString in class Object
Returns:
a string containing all counts of this vector

getAllCountsAsBytes

public byte[] getAllCountsAsBytes()
Returns a copy of the internal array of counts. The length of the returned array is the same than this vector's length. Note, byte values are signed in Java, while the values stored in the vector are positive counts. Consider using getAllCounts(), which returns an int array only holding positive values from 0 ... 255.

Returns:
a copy of the internal byte array.

getAllCounts

public int[] getAllCounts()
Returns a copy of the internal array of counts. The returned array has the same length as this vector and holds values 0 ... 255

Returns:
a copy of the internal byte array.

sumOfAllCounts

public long sumOfAllCounts()
Calculates the checksum, the sum of all counts stored.

Returns:
the sum of all counts in this vector.

cardinality

public int cardinality()
Returns the number of counts larger than zero stored in this vector.

Returns:
the number of elements not equal to zero in this vector.

isEmpty

public boolean isEmpty()
Checks all counts and returns true if they are all zero.

Returns:
true if all counts are zero.


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.