Skip to content

Commit

Permalink
More tests and docs about ByteArray
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Mar 31, 2016
1 parent ca27374 commit 33aeb6e
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 80 deletions.
Expand Up @@ -22,36 +22,135 @@
/** /**
* Abstraction of a {@code byte[]} so that different implementations can be plugged in, for example * Abstraction of a {@code byte[]} so that different implementations can be plugged in, for example
* off-heap, dynamically growing, or other implementations. This interface is slightly different than * off-heap, dynamically growing, or other implementations. This interface is slightly different than
* {@link IntArray} and {@link LongArray} in that one item in the array isn't necessarily just a byte, * {@link IntArray} and {@link LongArray} in that one index in the array isn't necessarily just a byte,
* instead item size can be set to any number and the bytes in an item can be read and written as other * instead item size can be set to any number and the bytes in an index can be read and written as other
* number representations like {@link #setInt(long, int, int) int} or {@link #setLong(long, int, long) long}, * number representations like {@link #setInt(long, int, int) int} or {@link #setLong(long, int, long) long},
* even a special {@link #set6BLong(long, int, long) 6B long}. More can easily be added on demand. * even a special {@link #set6ByteLong(long, int, long) 6B long}. More can easily be added on demand.
*
* Each index in the array can hold multiple values, each at its own offset (starting from 0 at each index), e.g.
* an array could have items holding values <pre>byte, int, short, long</pre>, where:
* - the byte would be accessed using offset=0
* - the int would be accessed using offset=1
* - the short would be accessed using offset=5
* - the long would be accessed using offset=7
* *
* @see NumberArrayFactory * @see NumberArrayFactory
*/ */
public interface ByteArray extends NumberArray<ByteArray> public interface ByteArray extends NumberArray<ByteArray>
{ {
/**
* Gets the data at the given {@code index}. The data is read into the given byte array.
*
* @param index array index to get.
* @param into byte[] to read into.
*/
void get( long index, byte[] into ); void get( long index, byte[] into );


/**
* Gets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will get a byte at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to get the value from.
* @return the byte at the given offset at the given array index.
*/
byte getByte( long index, int offset ); byte getByte( long index, int offset );


/**
* Gets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will get a short at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to get the value from.
* @return the short at the given offset at the given array index.
*/
short getShort( long index, int offset ); short getShort( long index, int offset );


/**
* Gets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will get a int at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to get the value from.
* @return the int at the given offset at the given array index.
*/
int getInt( long index, int offset ); int getInt( long index, int offset );


long get6BLong( long index, int offset ); /**
* Gets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will get a 6-byte long at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to get the value from.
* @return the 6-byte long at the given offset at the given array index.
*/
long get6ByteLong( long index, int offset );


/**
* Gets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will get a long at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to get the value from.
* @return the long at the given offset at the given array index.
*/
long getLong( long index, int offset ); long getLong( long index, int offset );


/**
* Sets the given {@code data} at the given {@code index}, overwriting all the values in it.
*
* @param index array index to get.
* @param value the byte[] to copy into the given offset at the given array index.
*/
void set( long index, byte[] value ); void set( long index, byte[] value );


/**
* Sets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will set a byte at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to set the value for.
* @param value the byte value to set at the given offset at the given array index.
*/
void setByte( long index, int offset, byte value ); void setByte( long index, int offset, byte value );


/**
* Sets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will set a short at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to set the value for.
* @param value the short value to set at the given offset at the given array index.
*/
void setShort( long index, int offset, short value ); void setShort( long index, int offset, short value );


/**
* Sets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will set a int at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to set the value for.
* @param value the int value to set at the given offset at the given array index.
*/
void setInt( long index, int offset, int value ); void setInt( long index, int offset, int value );


void set6BLong( long index, int offset, long value ); /**
* Sets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will set a 6-byte long at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to set the value for.
* @param value the 6-byte long value to set at the given offset at the given array index.
*/
void set6ByteLong( long index, int offset, long value );


/**
* Sets a part of an item, at the given {@code index}. An item in this array can consist of
* multiple values. This call will set a long at the given {@code offset}.
*
* @param index array index to get.
* @param offset offset into this index to set the value for.
* @param value the long value to set at the given offset at the given array index.
*/
void setLong( long index, int offset, long value ); void setLong( long index, int offset, long value );
} }
Expand Up @@ -86,10 +86,10 @@ public int getInt( long index, int offset )
} }


@Override @Override
public long get6BLong( long index, int offset ) public long get6ByteLong( long index, int offset )
{ {
ByteArray chunk = chunkOrNullAt( index ); ByteArray chunk = chunkOrNullAt( index );
return chunk != null ? chunk.get6BLong( index, offset ) : return chunk != null ? chunk.get6ByteLong( index, offset ) :
get6BLongFromByteBuffer( defaultValueConvenienceBuffer, offset ); get6BLongFromByteBuffer( defaultValueConvenienceBuffer, offset );
} }


Expand Down Expand Up @@ -125,9 +125,9 @@ public void setInt( long index, int offset, int value )
} }


@Override @Override
public void set6BLong( long index, int offset, long value ) public void set6ByteLong( long index, int offset, long value )
{ {
at( index ).set6BLong( index, offset, value ); at( index ).set6ByteLong( index, offset, value );
} }


@Override @Override
Expand Down
Expand Up @@ -88,19 +88,16 @@ public N at( long index )
return chunks[chunkIndex]; return chunks[chunkIndex];
} }


private void synchronizedAddChunk( long index ) private synchronized void synchronizedAddChunk( long index )
{ {
synchronized ( this ) if ( index >= length() )
{ {
if ( index >= length() ) N[] newChunks = Arrays.copyOf( chunks, chunkIndex( index )+1 );
for ( int i = chunks.length; i < newChunks.length; i++ )
{ {
N[] newChunks = Arrays.copyOf( chunks, chunkIndex( index )+1 ); newChunks[i] = addChunk( chunkSize, chunkSize * i );
for ( int i = chunks.length; i < newChunks.length; i++ )
{
newChunks[i] = addChunk( chunkSize, chunkSize * i );
}
chunks = newChunks;
} }
chunks = newChunks;
} }
} }


Expand Down
Expand Up @@ -89,7 +89,7 @@ public int getInt( long index, int offset )
} }


@Override @Override
public long get6BLong( long index, int offset ) public long get6ByteLong( long index, int offset )
{ {
return get6BLongFromByteBuffer( buffer, index( index, offset ) ); return get6BLongFromByteBuffer( buffer, index( index, offset ) );
} }
Expand Down Expand Up @@ -132,7 +132,7 @@ public void setInt( long index, int offset, int value )
} }


@Override @Override
public void set6BLong( long index, int offset, long value ) public void set6ByteLong( long index, int offset, long value )
{ {
int absIndex = index( index, offset ); int absIndex = index( index, offset );
buffer.putInt( absIndex, (int) value ); buffer.putInt( absIndex, (int) value );
Expand Down

0 comments on commit 33aeb6e

Please sign in to comment.