    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.ArrayList;
    006    import java.util.List;
    007
    008    public 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            Vec3 var7;
    031
    032            if (this.nextFreeSpace >= this.vec3Cache.size())
    033            {
    034                var7 = new Vec3(par1, par3, par5);
    035                this.vec3Cache.add(var7);
    036            }
    037            else
    038            {
    039                var7 = (Vec3)this.vec3Cache.get(this.nextFreeSpace);
    040                var7.setComponents(par1, par3, par5);
    041            }
    042
    043            ++this.nextFreeSpace;
    044            return var7;
    045        }
    046
    047        /**
    048         * Will truncate the array everyN clears to the maximum size observed since the last truncation.
    049         */
    050        public void clear()
    051        {
    052            if (this.nextFreeSpace > this.maximumSizeSinceLastTruncation)
    053            {
    054                this.maximumSizeSinceLastTruncation = this.nextFreeSpace;
    055            }
    056
    057            if (this.resetCount++ == this.truncateArrayResetThreshold)
    058            {
    059                int var1 = Math.max(this.maximumSizeSinceLastTruncation, this.vec3Cache.size() - this.minimumSize);
    060
    061                while (this.vec3Cache.size() > var1)
    062                {
    063                    this.vec3Cache.remove(var1);
    064                }
    065
    066                this.maximumSizeSinceLastTruncation = 0;
    067                this.resetCount = 0;
    068            }
    069
    070            this.nextFreeSpace = 0;
    071        }
    072
    073        @SideOnly(Side.CLIENT)
    074        public void clearAndFreeCache()
    075        {
    076            this.nextFreeSpace = 0;
    077            this.vec3Cache.clear();
    078        }
    079    }