package electrosphere.mem;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import org.joml.Vector3d;
import org.joml.Vector3f;

/**
 * A pool for temporary vectors
 */
public class VectorPool {
    
    /**
     * Structure to store not-in-use objects
     */
    static List<Vector3f> vec3fPool = new LinkedList<Vector3f>();

    /**
     * Structure to store not-in-use objects
     */
    static List<Vector3d> vec3dPool = new LinkedList<Vector3d>();

    /**
     * Lock for thread-safeing operations
     */
    static ReentrantLock lock = new ReentrantLock();

    /**
     * Gets a Vector3f from the pool. Allocates if no free one is available.
     * @param type The type of the message
     * @return A Vector3f
     */
    public static Vector3f getF(){
        Vector3f rVal = null;
        lock.lock();
        if(vec3fPool.size() > 0){
            rVal = vec3fPool.remove(0);
        } else {
            rVal = new Vector3f();
        }
        lock.unlock();
        return rVal;
    }

    /**
     * Releases a Vector3f back into the pool
     * @param data The object to release
     */
    public static void release(Vector3f data){
        data.x = 0;
        data.y = 0;
        data.z = 0;
        lock.lock();
        VectorPool.vec3fPool.add(data);
        lock.unlock();
    }

    /**
     * Gets a Vector3d from the pool. Allocates if no free one is available.
     * @return A Vector3d
     */
    public static Vector3d getD(){
        Vector3d rVal = null;
        lock.lock();
        if(vec3dPool.size() > 0){
            rVal = vec3dPool.remove(0);
        } else {
            rVal = new Vector3d();
        }
        lock.unlock();
        return rVal;
    }

    /**
     * Releases a Vector3d back into the pool
     * @param data The object to release
     */
    public static void release(Vector3d data){
        data.x = 0;
        data.y = 0;
        data.z = 0;
        lock.lock();
        VectorPool.vec3dPool.add(data);
        lock.unlock();
    }

}
