Search in sources :

Example 1 with Scalar

use of org.ojalgo.scalar.Scalar in project ojAlgo by optimatika.

the class BufferArray method create.

private static BasicArray<Double> create(final File file, final long... structure) {
    final long tmpCount = StructureAnyD.count(structure);
    DoubleBuffer tmpDoubleBuffer = null;
    try {
        final RandomAccessFile tmpRandomAccessFile = new RandomAccessFile(file, "rw");
        final FileChannel tmpFileChannel = tmpRandomAccessFile.getChannel();
        final long tmpSize = DOUBLE_ELEMENT_SIZE * tmpCount;
        if (tmpCount > (1L << 8)) {
            final DenseArray.Factory<Double> tmpFactory = new DenseArray.Factory<Double>() {

                long offset = 0L;

                @Override
                public AggregatorSet<Double> aggregator() {
                    return PrimitiveAggregator.getSet();
                }

                @Override
                public FunctionSet<Double> function() {
                    return PrimitiveFunction.getSet();
                }

                @Override
                public Scalar.Factory<Double> scalar() {
                    return PrimitiveScalar.FACTORY;
                }

                @Override
                long getElementSize() {
                    return DOUBLE_ELEMENT_SIZE;
                }

                @Override
                PlainArray<Double> make(final long size) {
                    final long tmpSize2 = size * DOUBLE_ELEMENT_SIZE;
                    try {
                        final MappedByteBuffer tmpMap = tmpFileChannel.map(MapMode.READ_WRITE, offset, tmpSize2);
                        tmpMap.order(ByteOrder.nativeOrder());
                        return new DoubleBufferArray(tmpMap.asDoubleBuffer(), tmpRandomAccessFile);
                    } catch (final IOException exception) {
                        throw new RuntimeException(exception);
                    } finally {
                        offset += tmpSize2;
                    }
                }
            };
            return tmpFactory.makeSegmented(structure);
        } else {
            final MappedByteBuffer tmpMappedByteBuffer = tmpFileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, tmpSize);
            tmpMappedByteBuffer.order(ByteOrder.nativeOrder());
            tmpDoubleBuffer = tmpMappedByteBuffer.asDoubleBuffer();
            return new DoubleBufferArray(tmpDoubleBuffer, tmpRandomAccessFile);
        }
    } catch (final FileNotFoundException exception) {
        throw new RuntimeException(exception);
    } catch (final IOException exception) {
        throw new RuntimeException(exception);
    }
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) PrimitiveScalar(org.ojalgo.scalar.PrimitiveScalar) Scalar(org.ojalgo.scalar.Scalar) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 DoubleBuffer (java.nio.DoubleBuffer)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 PrimitiveScalar (org.ojalgo.scalar.PrimitiveScalar)1 Scalar (org.ojalgo.scalar.Scalar)1