public class DataBuffer
extends java.lang.Object
DataBuffer maintains three independent buffers for storing integer, double, and Object data. Data is added to each using a put() operation and can be read back using a get() operation. Each buffer has a size and an offset. When data is added using put(), it is added at the end of the buffer and the size is increased. When data is read back using put(), it is read from the current offset location, and the offset is then increased. The offset is not allowed to increase beyond the current size. The put() and get() operations therefore act like insert and remove operations for a queue. However, unlike a queue, the get() operation does not actually remove data; it simply advances the offset. Other set() and peek() operations allow data to be set and read within the buffer without affecting its offset or size.
The methods implementing the various operations (put(), get(), set(), size(), etc.) for each data type take the name of the operation prefaced with a single character identifying the data type:
z
d
o
zput()
and zget()
for integers and dput()
and dget()
for
doubles.
As an example, we consider storing and retrieving information about a vector. The store operation might look like this:
saveVector (DataBuffer data) {
data.zput (size); // store vector size as an integer
for (int i=0; i<size; i++) {
data.dout (vector.get(i)); // store vector data as doubles
}
}
while the restore operation might look like this:
loadVector (DataBuffer data) {
int size = data.zget (); // get the vector size
vector = new VectorNd(size);
for (int i=0; i<size; i++) {
vector.set (i, data.dget()); // restore the vector data
}
}
For convenience,put() and get() operations are also defined to
save and restore aggregate types, such as Vector3d
and Matrix3d
.
Internally, each buffer is implementing as an array, whose length defines the current buffer capacity. When the size increases beyond the capacity, the array is resized automatically and the capacity is increased. Attempting to move the offset beyond the current size will cause an exception to be thrown. The buffer structure is shown in the following diagram, in which entries marked with 'x' contain actual data.
|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| | | | | | | | | | | | | | | | | | offset-+ size-+ capacity-+
Modifier and Type | Class and Description |
---|---|
static class |
DataBuffer.Offsets |
Constructor and Description |
---|
DataBuffer() |
DataBuffer(int zcap,
int dcap,
int ocap)
Creates a new DataBuffer with specified capacities for its bytes,
double, integer, double and Object buffers.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
double[] |
dbuffer()
Returns the current array used to store the double buffer.
|
void |
dEnsureCapacity(int cap)
Ensures that the double buffer has a specified capacity.
|
boolean |
dequals(DataBuffer data)
Returns true if the double buffer contents of this DataBuffer
and another DataBuffer are equal.
|
double |
dget()
Returns the double value at the current double buffer offset, and
increases the offset.
|
void |
dget(Matrix3dBase M)
Gets nine doubles starting at the current double buffer offset,
places them into a Matrix3dBase, and increases the offset.
|
void |
dget(SymmetricMatrix3d M)
Gets six doubles starting at the current double buffer offset, places
them into a SymmetricMatrix3d, and increases the offset.
|
void |
dget(Vector3d vec)
Gets three doubles starting at the current double buffer offset,
places them into a Vector3d, and increases the offset.
|
void |
dget(VectorNd vec)
Gets doubles starting at the current double buffer offset, places them
into a VectorNd, and increases the offset.
|
int |
doffset()
Returns the current double buffer offset.
|
double |
dpeek()
Returns the double value at the current double buffer offset, but does
not increase the offset.
|
double |
dpeek(int i)
Returns the double value at a specified location
i . |
void |
dput(double d)
Adds a double to the double buffer, increasing its size.
|
void |
dput(Matrix3dBase M)
Adds nine doubles from a Matrix3dBase to the double buffer, increasing its
size.
|
void |
dput(SymmetricMatrix3d M)
Adds six doubles from a SymmetricMatrix3d to the double buffer,
increasing its size.
|
void |
dput(Vector3d vec)
Adds three doubles from a Vector3d to the double buffer, increasing its
size.
|
void |
dput(VectorNd vec)
Adds doubles from a VectorNd to the double buffer, increasing its
size.
|
void |
dset(int i,
double d)
Overwrites a value in the double buffer at a specified location
i . |
void |
dsetOffset(int off)
Sets the double buffer offset.
|
void |
dsetSize(int size)
Resets the size of the double buffer.
|
int |
dsize()
Returns the amount of data in the double buffer.
|
void |
dskip(int n)
Advances the double buffer offset forward by
n . |
boolean |
equals(DataBuffer data) |
DataBuffer.Offsets |
getNumericOffsets() |
DataBuffer.Offsets |
getNumericSizes() |
DataBuffer.Offsets |
getOffsets() |
DataBuffer.Offsets |
getSizes() |
boolean |
numericEquals(DataBuffer data) |
java.lang.Object[] |
obuffer()
Returns the current array used to store the Object buffer.
|
void |
oEnsureCapacity(int cap)
Ensures that the Object buffer has a specified capacity.
|
boolean |
oequals(DataBuffer data)
Returns true if the Object buffer contents of this DataBuffer
and another DataBuffer are equal.
|
java.lang.Object |
oget()
Returns the Object value at the current Object buffer offset, and
increases the offset.
|
int |
ooffset()
Returns the current Object buffer offset.
|
java.lang.Object |
opeek()
Returns the Object value at the current Object buffer offset, but does
not increase the offset.
|
java.lang.Object |
opeek(int i)
Returns the Object value at a specified location
i . |
void |
oput(java.lang.Object o)
Adds a Object to the Object buffer, increasing its size.
|
void |
oputs(java.util.Collection<? extends java.lang.Object> objs)
Adds a collection of Objects to the Object buffer, increasing its size.
|
void |
oset(int i,
java.lang.Object o)
Overwrites a value in the Object buffer at a specified location
i . |
void |
osetOffset(int off)
Sets the Object buffer offset.
|
void |
osetSize(int size)
Resets the size of the Object buffer.
|
int |
osize()
Returns the amount of data in the Object buffer.
|
void |
oskip(int n)
Advances the Object buffer offset forward by
n . |
void |
resetOffsets() |
void |
set(DataBuffer data) |
int[] |
zbuffer()
Returns the current array used to store the integer buffer.
|
void |
zEnsureCapacity(int cap)
Ensures that the integer buffer has a specified capacity.
|
boolean |
zequals(DataBuffer data)
Returns true if the integer buffer contents of this DataBuffer
and another DataBuffer are equal.
|
int |
zget()
Returns the integer value at the current integer buffer offset, and
increases the offset.
|
boolean |
zgetBool()
Returns the boolean value at the current int buffer offset, and
increases the offset.
|
int |
zoffset()
Returns the current integer buffer offset.
|
int |
zpeek()
Returns the integer value at the current integer buffer offset, but does
not increase the offset.
|
int |
zpeek(int i)
Returns the integer value at a specified location
i . |
void |
zput(int z)
Adds a integer to the integer buffer, increasing its size.
|
void |
zputBool(boolean bool)
Adds a boolean value to the int buffer, increasing its size.
|
void |
zset(int i,
int z)
Overwrites a value in the integer buffer at a specified location
i . |
void |
zsetOffset(int off)
Sets the integer buffer offset.
|
void |
zsetSize(int size)
Resets the size of the integer buffer.
|
int |
zsize()
Returns the amount of data in the integer buffer.
|
void |
zskip(int n)
Advances the integer buffer offset forward by
n . |
public DataBuffer()
public DataBuffer(int zcap, int dcap, int ocap)
public int zsize()
public void zput(int z)
z
- integer to addpublic void zset(int i, int z)
i
. If the location is outside the range 0
to
zsize()-1
, inclusive, an exception is thrown.i
- index at which to overwrite dataz
- new data value to setpublic void zputBool(boolean bool)
bool
- boolean value to addpublic boolean zgetBool()
false
if the underlying
int value is 0, and true
otherwise. If the current offset is
equal to zsize()
, an exception is thrown.public void zsetSize(int size)
size
- new size for the integer bufferpublic int zget()
zsize()
, an exception is thrown.public int zpeek()
zsize()
, an exception is thrown.public int zpeek(int i)
i
.
If the location is outside the range 0
to
zsize()-1
, inclusive, an exception is thrown.i
- index at which to obtain the datai
public void zskip(int n)
n
.
If this causes the offset to exceed zsize()
,
an exception is thrown.n
- amount to advance the integer bufferpublic int zoffset()
public void zsetOffset(int off)
0
to zsize()-1
, inclusive, an exception
is thrown.off
- new integer buffer offsetpublic boolean zequals(DataBuffer data)
public void zEnsureCapacity(int cap)
cap
- request integer buffer capacitypublic int[] zbuffer()
public int dsize()
public void dput(double d)
d
- double to addpublic void dput(Vector3d vec)
vec
- vector to addpublic void dput(Matrix3dBase M)
M
- matrix to addpublic void dput(SymmetricMatrix3d M)
M
- symmetric matrix to addpublic void dput(VectorNd vec)
vec
- vector to addpublic void dset(int i, double d)
i
. If the location is outside the range 0
to
dsize()-1
, inclusive, an exception is thrown.i
- index at which to overwrite datad
- new data value to setpublic void dsetSize(int size)
size
- new size for the double bufferpublic double dget()
dsize()
, an exception is thrown.public void dget(Vector3d vec)
vec
- returns vectorpublic void dget(Matrix3dBase M)
M
- returns the matrixpublic void dget(SymmetricMatrix3d M)
M
- returns the symmetric matrixpublic void dget(VectorNd vec)
vec
- returns the vectorpublic double dpeek()
dsize()
, an exception is thrown.public double dpeek(int i)
i
.
If the location is outside the range 0
to
dsize()-1
, inclusive, an exception is thrown.i
- index at which to obtain the datai
public void dskip(int n)
n
.
If this causes the offset to exceed dsize()
,
an exception is thrown.n
- amount to advance the double bufferpublic int doffset()
public void dsetOffset(int off)
0
to dsize()-1
, inclusive, an exception
is thrown.off
- new double buffer offsetpublic boolean dequals(DataBuffer data)
public void dEnsureCapacity(int cap)
cap
- request double buffer capacitypublic double[] dbuffer()
public int osize()
public void oput(java.lang.Object o)
o
- Object to addpublic void oputs(java.util.Collection<? extends java.lang.Object> objs)
objs
- Objects to addpublic void oset(int i, java.lang.Object o)
i
. If the location is outside the range 0
to
osize()-1
, inclusive, an exception is thrown.i
- index at which to overwrite datao
- new data value to setpublic void osetSize(int size)
null
s. If the size is decreased,
the offset will be adjusted to enusre that is does not exceed the size.size
- new size for the Object bufferpublic java.lang.Object oget()
osize()
, an exception is thrown.public java.lang.Object opeek()
osize()
, an exception is thrown.public java.lang.Object opeek(int i)
i
.
If the location is outside the range 0
to
osize()-1
, inclusive, an exception is thrown.i
- index at which to obtain the datai
public void oskip(int n)
n
.
If this causes the offset to exceed osize()
,
an exception is thrown.n
- amount to advance the Object bufferpublic int ooffset()
public void osetOffset(int off)
0
to osize()-1
, inclusive, an exception
is thrown.off
- new Object buffer offsetpublic boolean oequals(DataBuffer data)
public void oEnsureCapacity(int cap)
cap
- request Object buffer capacitypublic java.lang.Object[] obuffer()
public void clear()
public void set(DataBuffer data)
public boolean equals(DataBuffer data)
public boolean numericEquals(DataBuffer data)
public void resetOffsets()
public DataBuffer.Offsets getSizes()
public DataBuffer.Offsets getNumericSizes()
public DataBuffer.Offsets getOffsets()
public DataBuffer.Offsets getNumericOffsets()