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

com.github.stephengold.joltjni.IndexedTriangleList Maven / Gradle / Ivy

/*
Copyright (c) 2024 Stephen Gold

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */
package com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.template.Array;

/**
 * A variable-length list (array) of indexed triangles. (native type:
 * {@code Array})
 *
 * @author Stephen Gold [email protected]
 */
final public class IndexedTriangleList extends Array {
    // *************************************************************************
    // constructors

    /**
     * Instantiate an empty list.
     */
    public IndexedTriangleList() {
        long listVa = createIndexedTriangleList();
        setVirtualAddress(listVa, () -> free(listVa));
    }
    // *************************************************************************
    // Array methods

    /**
     * Count how many triangles the currently allocated storage can hold. The
     * list is unaffected.
     *
     * @return the number of triangles (≥size)
     */
    @Override
    public int capacity() {
        long listVa = va();
        int result = capacity(listVa);

        return result;
    }

    /**
     * Remove all triangles in the specified range of indices.
     *
     * @param startIndex the index of the first element to remove (≥0)
     * @param stopIndex one plus the index of the last element to remove (≥0)
     */
    @Override
    public void erase(int startIndex, int stopIndex) {
        long vectorVa = va();
        erase(vectorVa, startIndex, stopIndex);
    }

    /**
     * Access the triangle at the specified index.
     *
     * @param elementIndex the index from which to get the triangle (≥0)
     * @return a new JVM object with the pre-existing native object assigned
     */
    @Override
    public IndexedTriangle get(int elementIndex) {
        long listVa = va();
        long triangleVa = getTriangle(listVa, elementIndex);
        IndexedTriangle result = new IndexedTriangle(triangleVa);

        return result;
    }

    /**
     * Expand or truncate the list.
     *
     * @param numTriangles the desired size (number of triangles, ≥0)
     */
    @Override
    public void resize(int numTriangles) {
        long listVa = va();
        resize(listVa, numTriangles);
    }

    /**
     * Put the specified triangle at the specified index.
     *
     * @param elementIndex the index at which to put the triangle (≥0)
     * @param triangle the triangle to put (not null)
     */
    @Override
    public void set(int elementIndex, IndexedTriangle triangle) {
        long listVa = va();
        long triangleVa = triangle.va();
        setTriangle(listVa, elementIndex, triangleVa);
    }

    /**
     * Count how many triangles are in the list. The list is unaffected.
     *
     * @return the number of triangles (≥0, ≤capacity)
     */
    @Override
    public int size() {
        long listVa = va();
        int result = size(listVa);

        return result;
    }
    // *************************************************************************
    // native private methods

    native private static int capacity(long listVa);

    native private static long createIndexedTriangleList();

    native private static void erase(
            long vectorVa, int startIndex, int stopIndex);

    native private static void free(long listVa);

    native private static long getTriangle(long listVa, int elementIndex);

    native private static void resize(long listVa, int numTriangles);

    native private static void setTriangle(
            long listVa, int elementIndex, long triangleVa);

    native private static int size(long listVa);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy