    001package net.minecraft.util;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.ArrayList;
    006import java.util.List;
    007
    008public class Vec3Pool
    009{
    010    private final int truncateArrayResetThreshold;
    011    private final int minimumSize;
    012
    013    /** items at and above nextFreeSpace are assumed to be available */
    014    private final List vec3Cache = new ArrayList();
    015    private int nextFreeSpace = 0;
    016    private int maximumSizeSinceLastTruncation = 0;
    017    private int resetCount = 0;
    018
    019    public Vec3Pool(int par1, int par2)
    020    {
    021        this.truncateArrayResetThreshold = par1;
    022        this.minimumSize = par2;
    023    }
    024
    025    /**
    026     * extends the pool if all vecs are currently "out"
    027     */
    028    public Vec3 getVecFromPool(double par1, double par3, double par5)
    029    {
    030        if (this.func_82589_e())
    031        {
    032            return new Vec3(this, par1, par3, par5);
    033        }
    034        else
    035        {
    036            Vec3 var7;
    037
    038            if (this.nextFreeSpace >= this.vec3Cache.size())
    039            {
    040                var7 = new Vec3(this, par1, par3, par5);
    041                this.vec3Cache.add(var7);
    042            }
    043            else
    044            {
    045                var7 = (Vec3)this.vec3Cache.get(this.nextFreeSpace);
    046                var7.setComponents(par1, par3, par5);
    047            }
    048
    049            ++this.nextFreeSpace;
    050            return var7;
    051        }
    052    }
    053
    054    /**
    055     * Will truncate the array everyN clears to the maximum size observed since the last truncation.
    056     */
    057    public void clear()
    058    {
    059        if (!this.func_82589_e())
    060        {
    061            if (this.nextFreeSpace > this.maximumSizeSinceLastTruncation)
    062            {
    063                this.maximumSizeSinceLastTruncation = this.nextFreeSpace;
    064            }
    065
    066            if (this.resetCount++ == this.truncateArrayResetThreshold)
    067            {
    068                int var1 = Math.max(this.maximumSizeSinceLastTruncation, this.vec3Cache.size() - this.minimumSize);
    069
    070                while (this.vec3Cache.size() > var1)
    071                {
    072                    this.vec3Cache.remove(var1);
    073                }
    074
    075                this.maximumSizeSinceLastTruncation = 0;
    076                this.resetCount = 0;
    077            }
    078
    079            this.nextFreeSpace = 0;
    080        }
    081    }
    082
    083    @SideOnly(Side.CLIENT)
    084    public void clearAndFreeCache()
    085    {
    086        if (!this.func_82589_e())
    087        {
    088            this.nextFreeSpace = 0;
    089            this.vec3Cache.clear();
    090        }
    091    }
    092
    093    public int getPoolSize()
    094    {
    095        return this.vec3Cache.size();
    096    }
    097
    098    public int func_82590_d()
    099    {
    100        return this.nextFreeSpace;
    101    }
    102
    103    private boolean func_82589_e()
    104    {
    105        return this.minimumSize < 0 || this.truncateArrayResetThreshold < 0;
    106    }
    107}