package com.srbenoit.geom;

import com.srbenoit.sparsearray.SparseArray;

/**
 * An array of vectors that supports addition and deletion of vectors, but will not change the
 * index of a vector once added. That is, deleting vectors makes the array sparse, and adding new
 * vectors may fill in gaps left by deleting vectors before appending to the end of the array.
 * Storage is allocated in blocks of fixed size as needed to add vectors.
 */
public class Vector2Array extends SparseArray<Vector2Int> {

    /**
     * Constructs a new <code>Vector2Array</code> with a default capacity.
     */
    public Vector2Array() {

        this(16);
    }

    /**
     * Constructs a new <code>Vector2Array</code> with capacity for a specified number of vectors.
     * Use this constructor if you know how many vectors will ultimately be added to the array.
     *
     * @param  initialSize  the initial capacity of array to allocate
     */
    public Vector2Array(final int initialSize) {

        super(Vector2Int.class, initialSize);
    }

    /**
     * Transforms the vectors in this list into another list using a given transformation matrix.
     * It is assumed that the target array contains the same number and arrangement of vectors as
     * this list.
     *
     * @param  target     the array into which to transform the vectors
     * @param  transform  the transformation matrix
     */
    public void transformInto(final Vector2Array target, final Transform2 transform) {

        int len;

        len = capacity();

        for (int i = 0; i < len; i++) {

            if (isFilled(i)) {
                transform.transformVec(get(i), target.get(i));
            }
        }
    }
}
