|
AlgART Home | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectnet.algart.arrays.MatrixInfo
public abstract class MatrixInfo extends java.lang.Object
Full structural information about the AlgART matrix, consisting of elements
of some primitive types, in a form convenient for serialization.
This class contains information about the matrix element type, matrix dimensions and byte order,
and allows to pack this information, with special signature, into a byte array or String
(serialize, toBytes() / toChars() methods) and, vice versa, convert such byte array or String to an instance
of this class (deserialize, valueOf(byte[]) / valueOf(String) methods).
This tool helps to store AlgART matrices in external files. The "raw" matrix data, i.e. elements of the built-in AlgART array, can be stored / loaded via
LargeMemoryModel.asArray(Object, Class, long, long, ByteOrder),LargeMemoryModel.asUpdatableArray(Object, Class, long, long, boolean, ByteOrder)methods. However, if you want to load a matrix from a file, you need structural information
about it, that should be passed to these methods and to Matrices.matrix(Array, long...).
This class encapsulates all this information and allows to represent it
in a form of Java byte array byte[] or in a usual String,
that can be stored in a separate file or in a prefix
of the given data file (before the filePosition, passed to
asArray /
asUpdatableArray methods).
This class can be used together with higher-level methods
LargeMemoryModel.asMatrix(Object, MatrixInfo),LargeMemoryModel.asUpdatableMatrix(Object, MatrixInfo),LargeMemoryModel.getMatrixInfoForSavingInFile(Matrix, long).More precisely, this class represents the following information about the matrix:
elementType() method.byteOrder() method.dimensions() method.In addition to this information about a matrix, this class also contains the following data:
toBytes() method or to String by toChars() method.
The version is always stored in the serialized form returned by toBytes() / toChars() methods.
If the instance of this class was created by valueOf(Matrix, long) method,
its version is equal to the current default version DEFAULT_VERSION
(but you may specify some older version via valueOf(Matrix, long, String) method).
If the instance was created by valueOf(byte[]) or valueOf(String),
the version is equal to the version stored in this byte/char sequence.
Future implementations of this class will support all old serialization formats.asArray / asUpdatableArray methods
(it is their filePosition argument). For example, if you use this class to add
a fixed-size prefix to the file containg the matrix, this value may contain the prefix length.
You also may ignore this offset and always pass 0 (or any other value) as an argument
of valueOf(Matrix, long) method.tiling.
All properties are also serialized by toBytes() / toChars() methods,
deserialized by valueOf(byte[]) / valueOf(String) methods and accessed via
additionalProperties() and cloneWithOtherAdditionalProperties(Map) methods.This class does work with primitive element types only:
boolean, char, byte, short, int, long,
float and double.
The number of matrix dimensions (Matrix.dimCount()) must be not greater
than Matrix.MAX_DIM_COUNT_FOR_SOME_ALGORITHMS.
Below is an example of creating 1000x1000 matrix in a mapped file with the prefix based on this class:
final File file = new File("someFileName.dat");
DataFileModel<File> dfm = new DefaultDataFileModel(file, MatrixInfo.MAX_SERIALIZED_MATRIX_INFO_LENGTH, false);
LargeMemoryModel<File> lmm = LargeMemoryModel.getInstance(dfm);
Class elementType = float.class;
Matrix<UpdatablePArray> m = lmm.newMatrix(elementType, 1000, 1000);
MatrixInfo mi = MatrixInfo.valueOf(m, MatrixInfo.MAX_SERIALIZED_MATRIX_INFO_LENGTH);
UpdatableByteArray ba = lmm.asUpdatableByteArray(LargeMemoryModel.getDataFilePath(m.array()),
0, MatrixInfo.MAX_SERIALIZED_MATRIX_INFO_LENGTH, false, ByteOrder.nativeOrder());
ba.setData(0, mi.toBytes());
LargeMemoryModel.setTemporary(m.array(), false);
The following code opens this file as a matrix for read-write access:
final String file = new File("someFileName.dat");
LargeMemoryModel<File> lmm = LargeMemoryModel.getInstance();
ByteArray ba = lmm.asByteArray(file, 0,
Math.min(file.length(), MatrixInfo.MAX_SERIALIZED_MATRIX_INFO_LENGTH),
ByteOrder.nativeOrder());
// "min" here is added to be on the safe side: maybe, this file was created by another program with shorter prefix
MatrixInfo mi = MatrixInfo.valueOf(Arrays.toJavaArray(ba));
UpdatablePArray pa = lmm.asUpdatableArray(file,
mi.elementType(), mi.dataOffset(), LargeMemoryModel.ALL_FILE, false, mi.byteOrder());
pa = pa.subArr(0, Math.min(pa.length(), mi.size()));
// "min" here necessary because matrix info can be damaged: then subArr throws a non-informative exception
// mi.size() is necessary even for correct file: for example, bit arrays occupy uneven number of bytes
Matrix<UpdatablePArray> m = Matrices.matrix(pa, mi.dimensions());
This class is immutable and thread-safe: there are no ways to modify settings of the created instance.
AlgART Laboratory 2007-2013
DataFileModel.recommendedPrefixSize()| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEFAULT_VERSION
The current default version of the serialized format of the information about matrices: "1.2". |
static int |
MAX_NUMBER_OF_PROPERTIES_IN_MATRIX_INFO
The maximal number of additional properties, that can be stored in this object:
10000. |
static int |
MAX_SERIALIZED_MATRIX_INFO_LENGTH
The maximal allowed length of byte array or String, returned by toBytes() / toChars() methods: 65536. |
| Modifier and Type | Method and Description |
|---|---|
abstract java.util.Map<java.lang.String,java.lang.String> |
additionalProperties()
Returns additional string properties, stored in this instance. |
long |
bitsPerElement()
Returns the size in bits, required for each matrix element. |
java.nio.ByteOrder |
byteOrder()
Returns the byte order used by the matrix for storing data. |
abstract MatrixInfo |
cloneWithOtherAdditionalProperties(java.util.Map<java.lang.String,java.lang.String> additionalProperties)
Creates an instance of this class, identical to this one, with the only difference that the new instance have the specified map of named additional string properties. |
abstract MatrixInfo |
cloneWithOtherByteOrder(java.nio.ByteOrder byteOrder)
Creates an instance of this class, identical to this one, with the only difference that the new instance have the specified byte order. |
abstract MatrixInfo |
cloneWithOtherDataOffset(long dataOffset)
Creates an instance of this class, identical to this one, with the only difference that the new instance have the specified data offset. |
MatrixInfo |
cloneWithOtherVersion(java.lang.String version)
Creates an instance of this class, identical to this one, with the only difference that the new instance have the specified version
of the serialization format. |
long |
dataOffset()
Returns the data offset, passed to valueOf(Matrix, long) or valueOf(Matrix, long, String)
method or loaded from the serialized form by valueOf(byte[]) or valueOf(String) method. |
long |
dim(int n)
Returns the dimension #n of the matrix or 1 if n>= dimCount(). |
int |
dimCount()
Returns the number of the matrix dimensions. |
long[] |
dimensions()
Returns the array containing all matrix dimensions. |
java.lang.Class<?> |
elementType()
Returns the type of matrix elements. |
boolean |
equals(java.lang.Object obj)
Indicates whether some other matrix information is equal to this instance. |
int |
hashCode()
Returns the hash code of this matrix information. |
static boolean |
isCorrectAdditionalPropertyName(java.lang.String name)
Returns true if and only if the passed string is an allowed name for an additional property, that can be stored via cloneWithOtherAdditionalProperties(Map) method. |
boolean |
matches(Matrix<?> matrix)
Indicates whether this information correctly describes the given matrix. |
long |
size()
Returns the array length: total number of elements in the matrix. |
byte[] |
toBytes()
Serializes the content of this instance into a byte array and returns it. |
abstract java.lang.String |
toChars()
Serializes the content of this instance into a char sequence and returns it. |
java.lang.String |
toString()
Returns a brief string description of this object. |
static MatrixInfo |
valueOf(byte[] bytes)
Deserializes the byte array and creates new instance of this class on the base of information, stored in this byte array. |
static MatrixInfo |
valueOf(Matrix<? extends PArray> matrix,
long dataOffset)
Creates an instance of this class containing full structural information about the given matrix. |
static MatrixInfo |
valueOf(Matrix<? extends PArray> matrix,
long dataOffset,
java.lang.String version)
Creates an instance of this class containing full structural information about the given matrix with the specified version of serialization format. |
static MatrixInfo |
valueOf(java.lang.String chars)
Deserializes the char array (passed as String argument) and creates new instance of this class on the base of information, stored in this char array. |
java.lang.String |
version()
Returns the version of the serialization format implemented by this instance. |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int MAX_SERIALIZED_MATRIX_INFO_LENGTH
toBytes() / toChars() methods: 65536.
This length is enough to store
Matrix.MAX_DIM_COUNT_FOR_SOME_ALGORITHMS =
9
toBytes(),
toChars(),
Constant Field Valuespublic static final int MAX_NUMBER_OF_PROPERTIES_IN_MATRIX_INFO
additional properties, that can be stored in this object:
10000.
cloneWithOtherAdditionalProperties(Map),
Constant Field Valuespublic static java.lang.String DEFAULT_VERSION
| Method Detail |
|---|
public static MatrixInfo valueOf(Matrix<? extends PArray> matrix,
long dataOffset)
version, equal to the
default version supported by this class: DEFAULT_VERSION.
matrix - the matrix.dataOffset - the value that may be interpreted as an offset of some "main" data and
that will be stored in byte array returned by toBytes() method.java.lang.NullPointerException - if the argument is null.TooLargeArrayException - if the number of matrix dimensions is greater than
Matrix.MAX_DIM_COUNT_FOR_SOME_ALGORITHMS.java.lang.ClassCastException - if the built-in array in the matrix is not PArray.
public static MatrixInfo valueOf(Matrix<? extends PArray> matrix,
long dataOffset,
java.lang.String version)
version of serialization format.
The specified version must be supported by this class.
Current implementation supports the following versions: "1.1" and "1.2".
There is no guarantee that all previous format versions, that was supported by old implementations
of this class, will be supported by this method.
(However, all previous format versions are always supported by deserialization methods
valueOf(byte[]) and valueOf(String).)
matrix - the matrix.dataOffset - the value that may be interpreted as an offset of some "main" data and
that will be stored in byte array returned by toBytes() method.version - the version of the serialization format supported by the created instance.java.lang.NullPointerException - if the argument is null.TooLargeArrayException - if the number of matrix dimensions is greater than
Matrix.MAX_DIM_COUNT_FOR_SOME_ALGORITHMS.java.lang.ClassCastException - if the built-in array in the matrix is not PArray.java.lang.IllegalArgumentException - if the specified version is not supported by this class.
public static MatrixInfo valueOf(byte[] bytes)
throws IllegalInfoSyntaxException
The passed data must have format, corresponding to the current
default format version or to any previous format version,
that was somewhen implemented by this class in the past.
In other case, IllegalInfoSyntaxException will be thrown.
The passed array may contain extra elements after the end of the stored information. All versions of this class guarantee that random bytes after the end of the serialized information will not lead to problems: the serialization format always allows to detect the end of information in the sequence of bytes.
bytes - the byte array produced by some previous call of toBytes() method.java.lang.NullPointerException - if the argument is null.IllegalInfoSyntaxException - if the format of bytes argument is illegal.toBytes(),
valueOf(String)
public static MatrixInfo valueOf(java.lang.String chars)
throws IllegalInfoSyntaxException
The passed data must have format, corresponding to the current
default format version or to any previous format version,
that was somewhen implemented by this class in the past.
In other case, IllegalInfoSyntaxException will be thrown.
The passed char array may contain extra elements after the end of the stored information. All versions of this class guarantee that random characters after the end of the serialized information will not lead to problems: the serialization format always allows to detect the end of information in the sequence of characters.
chars - the string produced by some previous call of toChars() method.java.lang.NullPointerException - if the argument is null.IllegalInfoSyntaxException - if the format of chars argument is illegal.toChars(),
valueOf(byte[])public static boolean isCorrectAdditionalPropertyName(java.lang.String name)
cloneWithOtherAdditionalProperties(Map) method.
Namely, this method returns true if and only if:
There is a guarantee that the keys in the map, returned by additionalProperties() method,
always fulfil this condition.
name - the checked property name.java.lang.NullPointerException - if the argument is null.public final java.lang.String version()
If this instance was created by valueOf(byte[]) or valueOf(String) method,
this version corresponds to the format of the data in the argument of that method.
If this instance was created by valueOf(Matrix, long, String) method,
this version is equal to the last argument of that method.
If this instance was created by valueOf(Matrix, long) method,
this version is equal DEFAULT_VERSION.
The version format is traditional: "N.N" or "N.N.N" (every "N" is a single digit 0..9).
public final java.lang.Class<?> elementType()
Matrix.elementType()public long bitsPerElement()
Arrays.bitsPerElement(thisInstance.elementType()).
PArray.bitsPerElement()public final java.nio.ByteOrder byteOrder()
Array.byteOrder()public final long size()
Matrix.size(),
dimensions()public final long[] dimensions()
The returned array is a clone of the internal dimension array stored in this object.
Matrix.dimensions()public final int dimCount()
dimensions().length, but works faster.
Matrix.dimCount()public final long dim(int n)
dimCount().
Equivalent to n<dimCount()?dimensions()[n]:1, but works faster.
n - the index of matrix dimension.java.lang.IndexOutOfBoundsException - if n<0 (but not if n is too large).Matrix.dim(int)public final long dataOffset()
valueOf(Matrix, long) or valueOf(Matrix, long, String)
method or loaded from the serialized form by valueOf(byte[]) or valueOf(String) method.
public abstract java.util.Map<java.lang.String,java.lang.String> additionalProperties()
valueOf(byte[]) / valueOf(String)
methods or added by cloneWithOtherAdditionalProperties(Map) method.
There is a guarantee that the keys and values, returned by this method,
are always non-null String instances, and that the keys
always fulfil the isCorrectAdditionalPropertyName(String) method.
The returned map is a clone of the internal map stored in this object. The returned map is always mutable and allows to add/remove elements, alike HashMap or TreeMap classes. The returned object is never null.
Note that the first version 1.1 of serialization format does not support this feature.
If the current version of serialization format of this instance is 1.1,
this method always returns an empty map.
cloneWithOtherAdditionalProperties(Map)public abstract MatrixInfo cloneWithOtherByteOrder(java.nio.ByteOrder byteOrder)
byte order.
byteOrder - new value of byte order.public MatrixInfo cloneWithOtherVersion(java.lang.String version)
version
of the serialization format.
It is possible that the specified version does not support all features, supported by this object,
and cannot create fully identical instance.
In particular, it is possible if the specified version is 1.1 and the map of properties, returned by
additionalProperties() method, is not empty.
In this case, this method throws UnsupportedOperationException.
version - required version.java.lang.NullPointerException - if the argument is null.java.lang.IllegalArgumentException - if the specified version is not supported by this class.java.lang.UnsupportedOperationException - if the specified version cannot store all information, stored
in this object.public abstract MatrixInfo cloneWithOtherDataOffset(long dataOffset)
data offset.
dataOffset - new value of data offset.public abstract MatrixInfo cloneWithOtherAdditionalProperties(java.util.Map<java.lang.String,java.lang.String> additionalProperties)
toBytes() / toChars() methods and
retrieved by additionalProperties() method.
The names of all specified properties must fulfil the isCorrectAdditionalPropertyName(String)
method. In addition to this requirement, we recommend not to use (for your own goals) properties starting
with "net.algart.arrays." substring, because some methods of this package use such properties.
The values of all specified properties must be non-null String instances, that can contain any characters.
Please avoid very large number or size of properties. There are limits for the number of
properties (MAX_NUMBER_OF_PROPERTIES_IN_MATRIX_INFO)
and for the total size of the data that can be stored while serialization
(MAX_SERIALIZED_MATRIX_INFO_LENGTH).
Note that the first version 1.1 of serialization format does not support this feature.
So, if the current version of serialization format of this instance is 1.1,
this method throws UnsupportedOperationException.
The method additionalProperties() still works in the version 1.1 and returns an empty map.
The passed additionalProperties argument is cloned by this method: no references to it are maintained by the created instance.
additionalProperties - another additional properties.java.lang.UnsupportedOperationException - if the version of this instance is 1.1.java.lang.NullPointerException - if the argument is null
or if some keys or values are null.java.lang.ClassCastException - if some of keys or values are not String instances
(this situation is possible while passing raw Map type
without generics).java.lang.IllegalArgumentException - if one of the passed properties has incorrect name
(for which isCorrectAdditionalPropertyName(String) method
returns false)
or if the number of passed properties is greater than
MAX_NUMBER_OF_PROPERTIES_IN_MATRIX_INFO.additionalProperties()public byte[] toBytes()
version of this instance.
The length of returned array never exceeds MAX_SERIALIZED_MATRIX_INFO_LENGTH.
java.lang.IllegalStateException - if the summary size of all additional properties
is very large and, so, this instance cannot be serialized in
MAX_SERIALIZED_MATRIX_INFO_LENGTH bytes.valueOf(byte[]),
toChars()public abstract java.lang.String toChars()
version of this instance.
The length of returned string never exceeds MAX_SERIALIZED_MATRIX_INFO_LENGTH.
java.lang.IllegalStateException - if the summary size of all additional properties
is very large and, so, this instance cannot be serialized in
MAX_SERIALIZED_MATRIX_INFO_LENGTH characters.valueOf(String),
toBytes()public boolean matches(Matrix<?> matrix)
elementType().equals(this.elementType());array().byteOrder() == this.byteOrder();dimensions() and this dimensions()
arrays are identical.
matrix - the matrix to be compared with this matrix information.java.lang.NullPointerException - if the argument is null.public java.lang.String toString()
The result of this method may depend on implementation and usually contains a short description of all matrix dimensions.
toString in class java.lang.Objectpublic int hashCode()
element type, byte order,
all dimensions, data offset
and all additional properties,
but does not depend on the version.
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
MatrixInfo instance;element type and
byte order;dimension arrays;data offset;additional properties maps,
in terms of Map.equals method.Please note that this method, unlike Matrix.equals(Object),
compares the byte order in both objects. This method does not check
versions of the objects.
equals in class java.lang.Objectobj - the object to be compared for equality with this instance.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||