org.foray.common
Class ByteVectorPacked

java.lang.Object
  extended by org.foray.common.ByteVectorPacked
All Implemented Interfaces:
Serializable

public class ByteVectorPacked
extends Object
implements Serializable

A vector of nibbles, or half bytes. The purpose of this class is to reduce by approximately half the amount of memory needed to store a data structure which only needs 4 bits for each element.

See Also:
Serialized Form

Constructor Summary
ByteVectorPacked()
          No-argument constructor.
ByteVectorPacked(int capacity)
          Constructor.
 
Method Summary
 void add(byte value)
          Adds one element to the vector.
 void add(byte[] value)
          Adds the content of a byte array to this vector.
 int capacity()
          Returns the current capacity of the vector, in half-bytes.
 int computeByteIndex(int nibbleIndex)
          Converts an index specified for nibbles into the correct index to use to retrieve the byte containing that nibble.
 byte get(int index)
          Retrieve a value from a specific index in the vector.
 int indexOf(byte value, int startingIndex)
          Returns the index of the first occurrence of a specified value in the vector.
 int length()
          Returns the number of half-byte items in the vector.
 int lengthInBytes()
          Returns the number of bytes actually used to store the vector content.
 void set(int index, byte value)
          Place an item in the vector at a specific index.
 void trimToSize()
          Sets the capacity of this vector equal to its size, to that there is no unused capacity.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteVectorPacked

public ByteVectorPacked()
No-argument constructor.


ByteVectorPacked

public ByteVectorPacked(int capacity)
Constructor.

Parameters:
capacity - The initial capacity, in half-bytes, of the new vector.
Method Detail

length

public int length()
Returns the number of half-byte items in the vector.

Returns:
The number of items in the vector.

lengthInBytes

public int lengthInBytes()
Returns the number of bytes actually used to store the vector content.

Returns:
The number of bytes used to store the vector content.

capacity

public int capacity()
Returns the current capacity of the vector, in half-bytes.

Returns:
The currect capacity of the vector.

computeByteIndex

public int computeByteIndex(int nibbleIndex)
Converts an index specified for nibbles into the correct index to use to retrieve the byte containing that nibble.

Parameters:
nibbleIndex - The index to the nibble.
Returns:
The index to the matching byte.

add

public void add(byte value)
Adds one element to the vector.

Parameters:
value - The half-byte value to be added to the vector. This must be in the range 0x0 thru 0xF.

add

public void add(byte[] value)
Adds the content of a byte array to this vector.

Parameters:
value - The array of half-byte values to be added to the vector. Each element must be in the range 0x0 thru 0xF.

set

public void set(int index,
                byte value)
Place an item in the vector at a specific index.

Parameters:
index - The 0-based index into the vector.
value - The half-byte value to be placed in the vector. This must be in the range 0x0 thru 0xF.

get

public byte get(int index)
Retrieve a value from a specific index in the vector.

Parameters:
index - The 0-based index whose value should be returned.
Returns:
The value at index.

trimToSize

public void trimToSize()
Sets the capacity of this vector equal to its size, to that there is no unused capacity.


indexOf

public int indexOf(byte value,
                   int startingIndex)
Returns the index of the first occurrence of a specified value in the vector.

Parameters:
value - The value whose index is sought.
startingIndex - The index in the vector at which the search should be started.
Returns:
The index to the first occurrence of value at or after startingIndex, or -1 if it does not occur in the vector.


Copyright © 2017. All rights reserved.