diff options
author | Muramasa <haydenkilloh@gmail.com> | 2016-09-15 15:48:47 +0100 |
---|---|---|
committer | Muramasa <haydenkilloh@gmail.com> | 2016-09-15 15:48:47 +0100 |
commit | b22bb2dfa6ec7d1e91d8657fcb8bd0389ff7e74a (patch) | |
tree | ed46bf1d37f8a2d518381dfb3bacf7b48bb7cee6 /src/main/java/gregtech/api/objects | |
parent | 1c195f5aab22c23aed4df944e49631e62312bd8b (diff) | |
parent | b10acf8244e3e69f7b37af2a1506eb71f57834c2 (diff) | |
download | GT5-Unofficial-b22bb2dfa6ec7d1e91d8657fcb8bd0389ff7e74a.tar.gz GT5-Unofficial-b22bb2dfa6ec7d1e91d8657fcb8bd0389ff7e74a.tar.bz2 GT5-Unofficial-b22bb2dfa6ec7d1e91d8657fcb8bd0389ff7e74a.zip |
Update Part 1
Diffstat (limited to 'src/main/java/gregtech/api/objects')
6 files changed, 275 insertions, 19 deletions
diff --git a/src/main/java/gregtech/api/objects/GT_ArrayList.java b/src/main/java/gregtech/api/objects/GT_ArrayList.java index 5bc781e281..d16974bc78 100644 --- a/src/main/java/gregtech/api/objects/GT_ArrayList.java +++ b/src/main/java/gregtech/api/objects/GT_ArrayList.java @@ -6,6 +6,7 @@ import java.util.Collection; public class GT_ArrayList<E> extends ArrayList<E> { private static final long serialVersionUID = 1L; + private int size_sS; private final boolean mAllowNulls; @@ -17,13 +18,13 @@ public class GT_ArrayList<E> extends ArrayList<E> { public GT_ArrayList(boolean aAllowNulls, E... aArray) { super(Arrays.asList(aArray)); mAllowNulls = aAllowNulls; - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} } public GT_ArrayList(boolean aAllowNulls, Collection<? extends E> aList) { super(aList); mAllowNulls = aAllowNulls; - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} } @Override @@ -46,14 +47,14 @@ public class GT_ArrayList<E> extends ArrayList<E> { @Override public boolean addAll(Collection<? extends E> aList) { boolean rReturn = super.addAll(aList); - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} return rReturn; } @Override public boolean addAll(int aIndex, Collection<? extends E> aList) { boolean rReturn = super.addAll(aIndex, aList); - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} return rReturn; } }
\ No newline at end of file diff --git a/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java b/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java index caf46b3980..12c2e28a37 100644 --- a/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java +++ b/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java @@ -36,7 +36,7 @@ public class GT_CopiedBlockTexture implements ITexture { this(aBlock, aSide, aMeta, Dyes._NULL.mRGBa); } - private IIcon getIcon(int aSide) { + private final IIcon getIcon(int aSide) { if (mSide == 6) return mBlock.getIcon(aSide, mMeta); return mBlock.getIcon(mSide, mMeta); } diff --git a/src/main/java/gregtech/api/objects/GT_HashSet.java b/src/main/java/gregtech/api/objects/GT_HashSet.java index cec006905b..32d8319cb5 100644 --- a/src/main/java/gregtech/api/objects/GT_HashSet.java +++ b/src/main/java/gregtech/api/objects/GT_HashSet.java @@ -8,26 +8,26 @@ import java.util.*; public class GT_HashSet<E extends GT_ItemStack> extends AbstractSet<E> { private static final Object OBJECT = new Object(); - private transient HashMap<GT_ItemStack, Object> map; + private transient Map<GT_ItemStack, Object> map; public GT_HashSet() { - map = new HashMap<GT_ItemStack, Object>(); + map = new WeakHashMap<GT_ItemStack, Object>(); GregTech_API.sItemStackMappings.add(map); } public GT_HashSet(Collection<? extends E> c) { - map = new HashMap<GT_ItemStack, Object>(Math.max((int) (c.size() / .75f) + 1, 16)); + map = new WeakHashMap<GT_ItemStack, Object>(Math.max((int) (c.size() / .75f) + 1, 16)); addAll(c); GregTech_API.sItemStackMappings.add(map); } public GT_HashSet(int initialCapacity, float loadFactor) { - map = new HashMap<GT_ItemStack, Object>(initialCapacity, loadFactor); + map = new WeakHashMap<GT_ItemStack, Object>(initialCapacity, loadFactor); GregTech_API.sItemStackMappings.add(map); } public GT_HashSet(int initialCapacity) { - map = new HashMap<GT_ItemStack, Object>(initialCapacity); + map = new WeakHashMap<GT_ItemStack, Object>(initialCapacity); GregTech_API.sItemStackMappings.add(map); } @@ -36,7 +36,7 @@ public class GT_HashSet<E extends GT_ItemStack> extends AbstractSet<E> { GregTech_API.sItemStackMappings.add(map); } - public HashMap getMap() { + public final Map getMap() { return map; } diff --git a/src/main/java/gregtech/api/objects/GT_ItemStack.java b/src/main/java/gregtech/api/objects/GT_ItemStack.java index f865b348a0..9791cadeeb 100644 --- a/src/main/java/gregtech/api/objects/GT_ItemStack.java +++ b/src/main/java/gregtech/api/objects/GT_ItemStack.java @@ -24,16 +24,16 @@ public class GT_ItemStack { this(GT_Utility.intToStack(aHashCode)); } - public ItemStack toStack() { + public final ItemStack toStack() { if (mItem == null) return null; return new ItemStack(mItem, 1, mMetaData); } - public boolean isStackEqual(ItemStack aStack) { + public final boolean isStackEqual(ItemStack aStack) { return GT_Utility.areStacksEqual(toStack(), aStack); } - public boolean isStackEqual(GT_ItemStack aStack) { + public final boolean isStackEqual(GT_ItemStack aStack) { return GT_Utility.areStacksEqual(toStack(), aStack.toStack()); } diff --git a/src/main/java/gregtech/api/objects/ItemData.java b/src/main/java/gregtech/api/objects/ItemData.java index c9c978d7ee..4609e8e613 100644 --- a/src/main/java/gregtech/api/objects/ItemData.java +++ b/src/main/java/gregtech/api/objects/ItemData.java @@ -93,26 +93,26 @@ public class ItemData { mByProducts = rList.toArray(new MaterialStack[rList.size()]); } - public boolean hasValidPrefixMaterialData() { + public final boolean hasValidPrefixMaterialData() { return mPrefix != null && mMaterial != null && mMaterial.mMaterial != null; } - public boolean hasValidPrefixData() { + public final boolean hasValidPrefixData() { return mPrefix != null; } - public boolean hasValidMaterialData() { + public final boolean hasValidMaterialData() { return mMaterial != null && mMaterial.mMaterial != null; } - public ArrayList<MaterialStack> getAllMaterialStacks() { + public final ArrayList<MaterialStack> getAllMaterialStacks() { ArrayList<MaterialStack> rList = new ArrayList(); if (hasValidMaterialData()) rList.add(mMaterial); rList.addAll(Arrays.asList(mByProducts)); return rList; } - public MaterialStack getByProduct(int aIndex) { + public final MaterialStack getByProduct(int aIndex) { return aIndex >= 0 && aIndex < mByProducts.length ? mByProducts[aIndex] : null; } diff --git a/src/main/java/gregtech/api/objects/XSTR.java b/src/main/java/gregtech/api/objects/XSTR.java new file mode 100644 index 0000000000..2f185ac3bb --- /dev/null +++ b/src/main/java/gregtech/api/objects/XSTR.java @@ -0,0 +1,255 @@ +package gregtech.api.objects; +/** + * A subclass of java.util.random that implements the Xorshift random number + * generator + * + * - it is 30% faster than the generator from Java's library - it produces + * random sequences of higher quality than java.util.Random - this class also + * provides a clone() function + * + * Usage: XSRandom rand = new XSRandom(); //Instantiation x = rand.nextInt(); + * //pull a random number + * + * To use the class in legacy code, you may also instantiate an XSRandom object + * and assign it to a java.util.Random object: java.util.Random rand = new + * XSRandom(); + * + * for an explanation of the algorithm, see + * http://demesos.blogspot.com/2011/09/pseudo-random-number-generators.html + * + * @author Wilfried Elmenreich University of Klagenfurt/Lakeside Labs + * http://www.elmenreich.tk + * + * This code is released under the GNU Lesser General Public License Version 3 + * http://www.gnu.org/licenses/lgpl-3.0.txt + */ + +import java.util.Random; +import java.util.concurrent.atomic.AtomicLong; + +/** + * XSTR - Xorshift ThermiteRandom + * Modified by Bogdan-G + * 03.06.2016 + * version 0.0.4 + */ +public class XSTR extends Random { + + private static final long serialVersionUID = 6208727693524452904L; + private long seed; + private long last; + private static final long GAMMA = 0x9e3779b97f4a7c15L; + private static final int PROBE_INCREMENT = 0x9e3779b9; + private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL; + private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) + private static final float FLOAT_UNIT = 0x1.0p-24f; // 1.0f / (1 << 24) + + /* + MODIFIED BY: Robotia + Modification: Implemented Random class seed generator + */ + /** + * Creates a new pseudo random number generator. The seed is initialized to + * the current time, as if by + * <code>setSeed(System.currentTimeMillis());</code>. + */ + public XSTR() { + this(seedUniquifier() ^ System.nanoTime()); + } + private static final AtomicLong seedUniquifier + = new AtomicLong(8682522807148012L); + + private static long seedUniquifier() { + // L'Ecuyer, "Tables of Linear Congruential Generators of + // Different Sizes and Good Lattice Structure", 1999 + for (;;) { + long current = seedUniquifier.get(); + long next = current * 181783497276652981L; + if (seedUniquifier.compareAndSet(current, next)) { + return next; + } + } + } + + /** + * Creates a new pseudo random number generator, starting with the specified + * seed, using <code>setSeed(seed);</code>. + * + * @param seed the initial seed + */ + public XSTR(long seed) { + this.seed = seed; + } + public boolean nextBoolean() { + return next(1) != 0; + } + + public double nextDouble() { + return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT; + } + /** + * Returns the current state of the seed, can be used to clone the object + * + * @return the current seed + */ + public synchronized long getSeed() { + return seed; + } + + /** + * Sets the seed for this pseudo random number generator. As described + * above, two instances of the same random class, starting with the same + * seed, produce the same results, if the same methods are called. + * + * @param seed the new seed + */ + public synchronized void setSeed(long seed) { + this.seed = seed; + } + + /** + * @return Returns an XSRandom object with the same state as the original + */ + @Override + public XSTR clone() { + return new XSTR(getSeed()); + } + + /** + * Implementation of George Marsaglia's elegant Xorshift random generator + * 30% faster and better quality than the built-in java.util.random see also + * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml + * + * @param nbits + * @return + */ + public int next(int nbits) { + long x = seed; + x ^= (x << 21); + x ^= (x >>> 35); + x ^= (x << 4); + seed = x; + x &= ((1L << nbits) - 1); + return (int) x; + } + boolean haveNextNextGaussian = false; + double nextNextGaussian = 0; + synchronized public double nextGaussian() { + // See Knuth, ACP, Section 3.4.1 Algorithm C. + if (haveNextNextGaussian) { + haveNextNextGaussian = false; + return nextNextGaussian; + } else { + double v1, v2, s; + do { + v1 = 2 * nextDouble() - 1; // between -1 and 1 + v2 = 2 * nextDouble() - 1; // between -1 and 1 + s = v1 * v1 + v2 * v2; + } while (s >= 1 || s == 0); + double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); + nextNextGaussian = v2 * multiplier; + haveNextNextGaussian = true; + return v1 * multiplier; + } + } + /** + * Returns a pseudorandom, uniformly distributed {@code int} value between 0 + * (inclusive) and the specified value (exclusive), drawn from this random + * number generator's sequence. The general contract of {@code nextInt} is + * that one {@code int} value in the specified range is pseudorandomly + * generated and returned. All {@code bound} possible {@code int} values are + * produced with (approximately) equal probability. The method + * {@code nextInt(int bound)} is implemented by class {@code Random} as if + * by: + * <pre> {@code + * public int nextInt(int bound) { + * if (bound <= 0) + * throw new IllegalArgumentException("bound must be positive"); + * + * if ((bound & -bound) == bound) // i.e., bound is a power of 2 + * return (int)((bound * (long)next(31)) >> 31); + * + * int bits, val; + * do { + * bits = next(31); + * val = bits % bound; + * } while (bits - val + (bound-1) < 0); + * return val; + * }}</pre> + * + * <p>The hedge "approx + * imately" is used in the foregoing description only because the next + * method is only approximately an unbiased source of independently chosen + * bits. If it were a perfect source of randomly chosen bits, then the + * algorithm shown would choose {@code int} values from the stated range + * with perfect uniformity. + * <p> + * The algorithm is slightly tricky. It rejects values that would result in + * an uneven distribution (due to the fact that 2^31 is not divisible by n). + * The probability of a value being rejected depends on n. The worst case is + * n=2^30+1, for which the probability of a reject is 1/2, and the expected + * number of iterations before the loop terminates is 2. + * <p> + * The algorithm treats the case where n is a power of two specially: it + * returns the correct number of high-order bits from the underlying + * pseudo-random number generator. In the absence of special treatment, the + * correct number of <i>low-order</i> bits would be returned. Linear + * congruential pseudo-random number generators such as the one implemented + * by this class are known to have short periods in the sequence of values + * of their low-order bits. Thus, this special case greatly increases the + * length of the sequence of values returned by successive calls to this + * method if n is a small power of two. + * + * @param bound the upper bound (exclusive). Must be positive. + * @return the next pseudorandom, uniformly distributed {@code int} value + * between zero (inclusive) and {@code bound} (exclusive) from this random + * number generator's sequence + * @throws IllegalArgumentException if bound is not positive + * @since 1.2 + */ + public int nextInt(int bound) { + //if (bound <= 0) { + //throw new RuntimeException("BadBound"); + //} + + /*int r = next(31); + int m = bound - 1; + if ((bound & m) == 0) // i.e., bound is a power of 2 + { + r = (int) ((bound * (long) r) >> 31); + } else { + for (int u = r; + u - (r = u % bound) + m < 0; + u = next(31)) + ; + } + return r;*/ + //speedup, new nextInt ~+40% + last = seed ^ (seed << 21); + last ^= (last >>> 35); + last ^= (last << 4); + seed = last; + int out = (int) last % bound; + return (out < 0) ? -out : out; + } + public int nextInt() { + return next(32); + } + + public float nextFloat() { + return next(24) * FLOAT_UNIT; + } + + public long nextLong() { + // it's okay that the bottom word remains signed. + return ((long)(next(32)) << 32) + next(32); + } + + public void nextBytes(byte[] bytes_arr) { + for (int iba = 0, lenba = bytes_arr.length; iba < lenba; ) + for (int rndba = nextInt(), + nba = Math.min(lenba - iba, Integer.SIZE/Byte.SIZE); + nba-- > 0; rndba >>= Byte.SIZE) + bytes_arr[iba++] = (byte)rndba; + } +}
\ No newline at end of file |