All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deephaven.chunk.CharChunkChunk Maven / Gradle / Ivy

The newest version!
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.chunk;

import io.deephaven.chunk.attributes.Any;

public class CharChunkChunk extends ChunkChunkBase implements ChunkChunk {
    @SuppressWarnings({"unchecked", "rawtypes"})
    private static final CharChunkChunk EMPTY = new CharChunkChunk<>(new CharChunk[0], 0, 0);

    public static  CharChunkChunk getEmptyChunk() {
        // noinspection unchecked
        return EMPTY;
    }

    public static  CharChunk[] makeArray(int capacity) {
        // noinspection unchecked
        return new CharChunk[capacity];
    }

    public static  CharChunkChunk chunkWrap(CharChunk[] data) {
        return new CharChunkChunk<>(data, 0, data.length);
    }

    public static  CharChunkChunk chunkWrap(CharChunk[] data, int offset, int capacity) {
        return new CharChunkChunk<>(data, offset, capacity);
    }

    CharChunk[] data;
    /**
     * innerData[i] is a cached copy of data[i].data used for faster two-dimensional access.
     */
    char[][] innerData;
    /**
     * innerOffsets[i] is a cached copy of data[i].offset used for faster two-dimensional access.
     */
    int[] innerOffsets;

    CharChunkChunk(CharChunk[] data, int offset, int capacity) {
        super(data.length, offset, capacity);
        this.data = data;
        resetInnerCache(data, offset, 0, capacity);
    }

    /**
     * Update cached "inner" data structures.
     */
    final void resetInnerCache(CharChunk[] data, int offset, int oldCapacity, int newCapacity) {
        if (innerData == null || innerData.length < newCapacity) {
            innerData = new char[newCapacity][];
            innerOffsets = new int[newCapacity];
        }
        for (int ii = 0; ii < newCapacity; ++ii) {
            resetInnerCacheItem(ii, data[ii + offset]);
        }
        for (int ii = newCapacity; ii < oldCapacity; ++ii) {
            // Be friendly to the garbage collector
            innerData[ii] = null;
            innerOffsets[ii] = 0; // to be nice
        }
    }

    /**
     * Update a specific cached "inner" data structures.
     */
    final void resetInnerCacheItem(int index, CharChunk chunk) {
        if (chunk == null) {
            innerData[index] = null;
            innerOffsets[index] = 0;
        } else {
            innerData[index] = chunk.data;
            innerOffsets[index] = chunk.offset;
        }
    }

    public final CharChunk get(int index) {
        return data[offset + index];
    }

    public final CharChunk getChunk(int index) {
        return get(index);
    }

    public final char get(int j, int i) {
        return innerData[j][innerOffsets[j] + i];
    }

    @Override
    public CharChunkChunk slice(int offset, int capacity) {
        ChunkHelpers.checkSliceArgs(size, offset, capacity);
        return new CharChunkChunk<>(data, this.offset + offset, capacity);
    }

    // region AsType
    // endregion AsType
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy