diff --git a/src/dorkbox/collections/Array.java b/src/dorkbox/collections/Array.java index d3e666a..d4b2fea 100644 --- a/src/dorkbox/collections/Array.java +++ b/src/dorkbox/collections/Array.java @@ -28,6 +28,8 @@ import java.util.NoSuchElementException; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "rawtypes", "SuspiciousSystemArraycopy", "unused", "NullableProblems", "DuplicatedCode"}) public class Array implements Iterable { + public static final String version = Collections.version; + /** Provides direct access to the underlying array. If the Array's generic type is not Object, this field may only be accessed * if the {@link Array#Array(boolean, int, Class)} constructor was used. */ public T[] items; @@ -438,7 +440,7 @@ public class Array implements Iterable { public void shuffle () { T[] items = this.items; for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); T temp = items[i]; items[i] = items[ii]; items[ii] = temp; @@ -477,7 +479,7 @@ public class Array implements Iterable { /** Returns a random item from the array, or null if the array is empty. */ public T random () { if (size == 0) return null; - return items[MathUtil.random(0, size - 1)]; + return items[Collections.random(0, size - 1)]; } /** Returns the items as an array. Note the array is typed, so the {@link #Array(Class)} constructor must have been used. diff --git a/src/dorkbox/collections/ArrayMap.java b/src/dorkbox/collections/ArrayMap.java index a7e3306..ca0f734 100644 --- a/src/dorkbox/collections/ArrayMap.java +++ b/src/dorkbox/collections/ArrayMap.java @@ -31,6 +31,8 @@ import dorkbox.collections.ObjectMap.Entry; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "rawtypes", "unused", "SuspiciousSystemArraycopy", "NullableProblems"}) public class ArrayMap implements Iterable> { + public static final String version = Collections.version; + public K[] keys; public V[] values; public int size; @@ -384,7 +386,7 @@ public class ArrayMap implements Iterable> { public void shuffle () { for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); K tempKey = keys[i]; keys[i] = keys[ii]; keys[ii] = tempKey; diff --git a/src/dorkbox/collections/BinarySearch.java b/src/dorkbox/collections/BinarySearch.java index 63edf94..140a290 100644 --- a/src/dorkbox/collections/BinarySearch.java +++ b/src/dorkbox/collections/BinarySearch.java @@ -43,6 +43,7 @@ import java.util.List; * @author Tim Boudreau */ public class BinarySearch { + public static final String version = Collections.version; private final Evaluator eval; private final Indexed indexed; diff --git a/src/dorkbox/collections/BooleanArray.java b/src/dorkbox/collections/BooleanArray.java index fb8a9df..edcd734 100644 --- a/src/dorkbox/collections/BooleanArray.java +++ b/src/dorkbox/collections/BooleanArray.java @@ -26,6 +26,8 @@ import java.util.BitSet; * removing elements (the last element is moved to the removed element's position). * @author Nathan Sweet */ public class BooleanArray { + public static final String version = Collections.version; + public boolean[] items; public int size; public boolean ordered; @@ -281,7 +283,7 @@ public class BooleanArray { public void shuffle () { boolean[] items = this.items; for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); boolean temp = items[i]; items[i] = items[ii]; items[ii] = temp; @@ -297,7 +299,7 @@ public class BooleanArray { /** Returns a random item from the array, or false if the array is empty. */ public boolean random () { if (size == 0) return false; - return items[MathUtil.random(0, size - 1)]; + return items[Collections.random(0, size - 1)]; } public boolean[] toArray () { diff --git a/src/dorkbox/collections/ByteArray.java b/src/dorkbox/collections/ByteArray.java index 42dcc53..007b182 100644 --- a/src/dorkbox/collections/ByteArray.java +++ b/src/dorkbox/collections/ByteArray.java @@ -24,6 +24,8 @@ import java.util.Arrays; * avoids a memory copy when removing elements (the last element is moved to the removed element's position). * @author Nathan Sweet */ public class ByteArray { + public static final String version = Collections.version; + public byte[] items; public int size; public boolean ordered; @@ -326,7 +328,7 @@ public class ByteArray { public void shuffle () { byte[] items = this.items; for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); byte temp = items[i]; items[i] = items[ii]; items[ii] = temp; @@ -342,7 +344,7 @@ public class ByteArray { /** Returns a random item from the array, or zero if the array is empty. */ public byte random () { if (size == 0) return 0; - return items[MathUtil.random(0, size - 1)]; + return items[Collections.random(0, size - 1)]; } public byte[] toArray () { diff --git a/src/dorkbox/collections/CharArray.java b/src/dorkbox/collections/CharArray.java index a43ad0b..5344197 100644 --- a/src/dorkbox/collections/CharArray.java +++ b/src/dorkbox/collections/CharArray.java @@ -24,6 +24,8 @@ import java.util.Arrays; * class avoids a memory copy when removing elements (the last element is moved to the removed element's position). * @author Nathan Sweet */ public class CharArray { + public static final String version = Collections.version; + public char[] items; public int size; public boolean ordered; @@ -326,7 +328,7 @@ public class CharArray { public void shuffle () { char[] items = this.items; for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); char temp = items[i]; items[i] = items[ii]; items[ii] = temp; @@ -342,7 +344,7 @@ public class CharArray { /** Returns a random item from the array, or zero if the array is empty. */ public char random () { if (size == 0) return 0; - return items[MathUtil.random(0, size - 1)]; + return items[Collections.random(0, size - 1)]; } public char[] toArray () { diff --git a/src/dorkbox/collections/Collections.kt b/src/dorkbox/collections/Collections.kt index 56751b6..7c5d4f4 100644 --- a/src/dorkbox/collections/Collections.kt +++ b/src/dorkbox/collections/Collections.kt @@ -13,30 +13,39 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dorkbox.collections; +package dorkbox.collections +import java.util.* -import java.util.Random; +object Collections { + /** + * Gets the version number. + */ + const val version = "1.0" -public -class MathUtil { - public static final Random random = new Random(); - - /** Returns a random number between 0 (inclusive) and the specified value (inclusive). */ - static public int random (int range) { - return random.nextInt(range + 1); + init { + // Add this project to the updates system, which verifies this class + UUID + version information + dorkbox.updates.Updates.add(java.util.Collections::class.java, "7a4be173d7fd48e4a09543cc572eb903", version) } - /** Returns a random number between start (inclusive) and end (inclusive). */ - static public int random (int start, int end) { - return start + random.nextInt(end - start + 1); + internal val random = Random() + + /** Returns a random number between 0 (inclusive) and the specified value (inclusive). */ + fun random(range: Int): Int { + return random.nextInt(range + 1) + } + + /** Returns a random number between start (inclusive) and end (inclusive). */ + @JvmStatic + fun random(start: Int, end: Int): Int { + return start + random.nextInt(end - start + 1) } /** * Returns the next power of two. Returns the specified value if the value is already a power of two. */ - public static - int nextPowerOfTwo(int value) { - return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); + @JvmStatic + fun nextPowerOfTwo(value: Int): Int { + return 1 shl 32 - Integer.numberOfLeadingZeros(value - 1) } } diff --git a/src/dorkbox/collections/ComparableTimSort.java b/src/dorkbox/collections/ComparableTimSort.java index 6860771..fb6b9fb 100644 --- a/src/dorkbox/collections/ComparableTimSort.java +++ b/src/dorkbox/collections/ComparableTimSort.java @@ -21,6 +21,8 @@ package dorkbox.collections; * conjunction with a comparator that simply returns {@code ((Comparable)first).compareTo(Second)}. If this is the case, you are * better off deleting ComparableTimSort to eliminate the code duplication. (See Arrays.java for details.) */ class ComparableTimSort { + public static final String version = Collections.version; + /** This is the minimum sized sequence that will be merged. Shorter sequences will be lengthened by calling binarySort. If the * entire array is less than this length, no merges will be performed. * diff --git a/src/dorkbox/collections/ConcurrentEntry.java b/src/dorkbox/collections/ConcurrentEntry.java index 9f28237..59c4c2c 100644 --- a/src/dorkbox/collections/ConcurrentEntry.java +++ b/src/dorkbox/collections/ConcurrentEntry.java @@ -25,6 +25,8 @@ package dorkbox.collections; */ public class ConcurrentEntry { + public static final String version = Collections.version; + private final T value; private volatile ConcurrentEntry next; diff --git a/src/dorkbox/collections/ConcurrentIterator.java b/src/dorkbox/collections/ConcurrentIterator.java index cb5ed2f..dc3af66 100644 --- a/src/dorkbox/collections/ConcurrentIterator.java +++ b/src/dorkbox/collections/ConcurrentIterator.java @@ -25,6 +25,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; @SuppressWarnings("unchecked") public class ConcurrentIterator { + public static final String version = Collections.version; + /** * Specifies the load-factor for the IdentityMap used */ diff --git a/src/dorkbox/collections/ConcurrentWeakIdentityHashMap.java b/src/dorkbox/collections/ConcurrentWeakIdentityHashMap.java index 7c14db4..09a03b2 100755 --- a/src/dorkbox/collections/ConcurrentWeakIdentityHashMap.java +++ b/src/dorkbox/collections/ConcurrentWeakIdentityHashMap.java @@ -36,6 +36,7 @@ import java.util.concurrent.ConcurrentMap; * @author zhanhb */ class ConcurrentWeakIdentityHashMap extends AbstractMap implements ConcurrentMap { + public static final String version = Collections.version; private final ConcurrentMap, V> map; private final ReferenceQueue queue = new ReferenceQueue<>(); diff --git a/src/dorkbox/collections/FloatArray.java b/src/dorkbox/collections/FloatArray.java index 490d4d2..02a31cd 100644 --- a/src/dorkbox/collections/FloatArray.java +++ b/src/dorkbox/collections/FloatArray.java @@ -19,12 +19,13 @@ package dorkbox.collections; import java.util.Arrays; -import java.util.Random; /** A resizable, ordered or unordered float array. Avoids the boxing that occurs with ArrayList. If unordered, this class * avoids a memory copy when removing elements (the last element is moved to the removed element's position). * @author Nathan Sweet */ public class FloatArray { + public static final String version = Collections.version; + public float[] items; public int size; public boolean ordered; @@ -327,7 +328,7 @@ public class FloatArray { public void shuffle () { float[] items = this.items; for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); float temp = items[i]; items[i] = items[ii]; items[ii] = temp; @@ -343,7 +344,7 @@ public class FloatArray { /** Returns a random item from the array, or zero if the array is empty. */ public float random () { if (size == 0) return 0; - return items[MathUtil.random(0, size - 1)]; + return items[Collections.random(0, size - 1)]; } public float[] toArray () { diff --git a/src/dorkbox/collections/IdentityMap.java b/src/dorkbox/collections/IdentityMap.java index ab5753e..754fdbf 100644 --- a/src/dorkbox/collections/IdentityMap.java +++ b/src/dorkbox/collections/IdentityMap.java @@ -31,6 +31,8 @@ import java.util.NoSuchElementException; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"}) public class IdentityMap implements Iterable> { + public static final String version = Collections.version; + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; @@ -66,7 +68,7 @@ public class IdentityMap implements Iterable> { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public IdentityMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -202,7 +204,7 @@ public class IdentityMap implements Iterable> { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random.nextInt(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -377,7 +379,7 @@ public class IdentityMap implements Iterable> { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -468,7 +470,7 @@ public class IdentityMap implements Iterable> { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/IntArray.java b/src/dorkbox/collections/IntArray.java index 6b1f801..4b92c44 100644 --- a/src/dorkbox/collections/IntArray.java +++ b/src/dorkbox/collections/IntArray.java @@ -24,6 +24,8 @@ import java.util.Arrays; * avoids a memory copy when removing elements (the last element is moved to the removed element's position). * @author Nathan Sweet */ public class IntArray { + public static final String version = Collections.version; + public int[] items; public int size; public boolean ordered; @@ -326,7 +328,7 @@ public class IntArray { public void shuffle () { int[] items = this.items; for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); int temp = items[i]; items[i] = items[ii]; items[ii] = temp; @@ -342,7 +344,7 @@ public class IntArray { /** Returns a random item from the array, or zero if the array is empty. */ public int random () { if (size == 0) return 0; - return items[MathUtil.random(0, size - 1)]; + return items[Collections.random(0, size - 1)]; } public int[] toArray () { diff --git a/src/dorkbox/collections/IntFloatMap.java b/src/dorkbox/collections/IntFloatMap.java index e39d0f9..f1a8049 100644 --- a/src/dorkbox/collections/IntFloatMap.java +++ b/src/dorkbox/collections/IntFloatMap.java @@ -30,7 +30,9 @@ import java.util.NoSuchElementException; * next higher POT size. * @author Nathan Sweet */ public class IntFloatMap implements Iterable { - private static final int PRIME1 = 0xbe1f14b1; + public static final String version = Collections.version; + + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; private static final int EMPTY = 0; @@ -68,7 +70,7 @@ public class IntFloatMap implements Iterable { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public IntFloatMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -218,7 +220,7 @@ public class IntFloatMap implements Iterable { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -424,7 +426,7 @@ public class IntFloatMap implements Iterable { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -506,7 +508,7 @@ public class IntFloatMap implements Iterable { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/IntIntMap.java b/src/dorkbox/collections/IntIntMap.java index 4c63e19..a4569eb 100644 --- a/src/dorkbox/collections/IntIntMap.java +++ b/src/dorkbox/collections/IntIntMap.java @@ -29,7 +29,9 @@ import java.util.NoSuchElementException; * next higher POT size. * @author Nathan Sweet */ public class IntIntMap implements Iterable { - private static final int PRIME1 = 0xbe1f14b1; + public static final String version = Collections.version; + + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; private static final int EMPTY = 0; @@ -66,7 +68,7 @@ public class IntIntMap implements Iterable { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public IntIntMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -216,7 +218,7 @@ public class IntIntMap implements Iterable { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -422,7 +424,7 @@ public class IntIntMap implements Iterable { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -492,7 +494,7 @@ public class IntIntMap implements Iterable { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/IntMap.java b/src/dorkbox/collections/IntMap.java index 30b50c3..5cdd74c 100644 --- a/src/dorkbox/collections/IntMap.java +++ b/src/dorkbox/collections/IntMap.java @@ -20,7 +20,6 @@ package dorkbox.collections; import java.util.Iterator; import java.util.NoSuchElementException; -import java.util.Random; /** An unordered map that uses int keys. This implementation is a cuckoo hash map using 3 hashes, random walking, and a small * stash for problematic keys. Null values are allowed. No allocation is done except when growing the table size.
@@ -31,6 +30,8 @@ import java.util.Random; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "rawtypes"}) public class IntMap implements Iterable> { + public static final String version = Collections.version; + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; @@ -69,7 +70,7 @@ public class IntMap implements Iterable> { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public IntMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -226,7 +227,7 @@ public class IntMap implements Iterable> { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random.nextInt(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -414,7 +415,7 @@ public class IntMap implements Iterable> { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -515,7 +516,7 @@ public class IntMap implements Iterable> { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/IntSet.java b/src/dorkbox/collections/IntSet.java index c6dcfab..218af39 100644 --- a/src/dorkbox/collections/IntSet.java +++ b/src/dorkbox/collections/IntSet.java @@ -19,7 +19,6 @@ package dorkbox.collections; import java.util.NoSuchElementException; -import java.util.Random; /** An unordered set that uses int keys. This implementation uses cuckoo hashing using 3 hashes, random walking, and a small stash * for problematic keys. No allocation is done except when growing the table size.
@@ -29,7 +28,9 @@ import java.util.Random; * size. * @author Nathan Sweet */ public class IntSet { - private static final int PRIME1 = 0xbe1f14b1; + public static final String version = Collections.version; + + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; private static final int EMPTY = 0; @@ -63,7 +64,7 @@ public class IntSet { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public IntSet (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -211,7 +212,7 @@ public class IntSet { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random.nextInt(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; keyTable[index1] = insertKey; @@ -336,7 +337,7 @@ public class IntSet { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -394,7 +395,7 @@ public class IntSet { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/LockFreeArrayList.java b/src/dorkbox/collections/LockFreeArrayList.java index 8645710..8b0d256 100644 --- a/src/dorkbox/collections/LockFreeArrayList.java +++ b/src/dorkbox/collections/LockFreeArrayList.java @@ -39,6 +39,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; */ public final class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializable { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater listRef = AtomicReferenceFieldUpdater.newUpdater(LockFreeArrayList.class, @@ -107,9 +109,9 @@ class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializ } + @SuppressWarnings("unchecked") public E get(int index) { - //noinspection unchecked return (E) listRef.get(this).get(index); } @@ -156,6 +158,7 @@ class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializ // lock-free get + @SuppressWarnings("unchecked") @Override public ListIterator listIterator() { @@ -163,6 +166,7 @@ class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializ } // lock-free get + @SuppressWarnings("unchecked") @Override public ListIterator listIterator(final int index) { @@ -170,6 +174,7 @@ class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializ } // lock-free get + @SuppressWarnings("unchecked") @Override public List subList(final int startIndex, final int endIndex) { @@ -185,10 +190,10 @@ class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializ } // lock-free get + @SuppressWarnings("unchecked") @Override public boolean containsAll(final Collection collection) { - //noinspection unchecked return listRef.get(this).containsAll(collection); } @@ -213,6 +218,7 @@ class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializ } // lock-free get + @SuppressWarnings("unchecked") @Override public Iterator iterator() { @@ -227,10 +233,10 @@ class LockFreeArrayList implements List, RandomAccess, Cloneable, Serializ } // lock-free get + @SuppressWarnings("unchecked") @Override public T[] toArray(final T[] targetArray) { - //noinspection unchecked return (T[]) listRef.get(this).toArray(targetArray); } diff --git a/src/dorkbox/collections/LockFreeBiMap.java b/src/dorkbox/collections/LockFreeBiMap.java index 0c2d2d8..2a21cba 100644 --- a/src/dorkbox/collections/LockFreeBiMap.java +++ b/src/dorkbox/collections/LockFreeBiMap.java @@ -39,6 +39,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; @SuppressWarnings("WeakerAccess") public final class LockFreeBiMap { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater forwardREF = AtomicReferenceFieldUpdater.newUpdater(LockFreeBiMap.class, diff --git a/src/dorkbox/collections/LockFreeHashMap.java b/src/dorkbox/collections/LockFreeHashMap.java index 0e15c1b..7d417ec 100644 --- a/src/dorkbox/collections/LockFreeHashMap.java +++ b/src/dorkbox/collections/LockFreeHashMap.java @@ -39,6 +39,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; */ public final class LockFreeHashMap implements Map, Cloneable, Serializable { + public static final String version = dorkbox.collections.Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater mapREF = AtomicReferenceFieldUpdater.newUpdater( LockFreeHashMap.class, diff --git a/src/dorkbox/collections/LockFreeHashSet.java b/src/dorkbox/collections/LockFreeHashSet.java index c54ef3a..0614ac7 100644 --- a/src/dorkbox/collections/LockFreeHashSet.java +++ b/src/dorkbox/collections/LockFreeHashSet.java @@ -38,6 +38,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; */ public final class LockFreeHashSet implements Set, Cloneable, Serializable { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater setREF = AtomicReferenceFieldUpdater.newUpdater(LockFreeHashSet.class, @@ -71,6 +73,7 @@ class LockFreeHashSet implements Set, Cloneable, Serializable { return hashSet.add(element); } + @SuppressWarnings("unchecked") @Override public boolean containsAll(final Collection collection) { @@ -133,10 +136,10 @@ class LockFreeHashSet implements Set, Cloneable, Serializable { } // lock-free get + @SuppressWarnings("unchecked") @Override public Iterator iterator() { - //noinspection unchecked return setREF.get(this).iterator(); } @@ -148,10 +151,10 @@ class LockFreeHashSet implements Set, Cloneable, Serializable { } // lock-free get + @SuppressWarnings("unchecked") @Override public T[] toArray(final T[] targetArray) { - //noinspection unchecked return (T[]) setREF.get(this).toArray(targetArray); } } diff --git a/src/dorkbox/collections/LockFreeIntBiMap.java b/src/dorkbox/collections/LockFreeIntBiMap.java index 3c0640f..4d8c6c3 100644 --- a/src/dorkbox/collections/LockFreeIntBiMap.java +++ b/src/dorkbox/collections/LockFreeIntBiMap.java @@ -40,6 +40,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; */ public class LockFreeIntBiMap { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater forwardREF = AtomicReferenceFieldUpdater.newUpdater( LockFreeIntBiMap.class, diff --git a/src/dorkbox/collections/LockFreeIntMap.java b/src/dorkbox/collections/LockFreeIntMap.java index 722a978..f1a8a73 100644 --- a/src/dorkbox/collections/LockFreeIntMap.java +++ b/src/dorkbox/collections/LockFreeIntMap.java @@ -45,6 +45,8 @@ import dorkbox.collections.IntMap.*; @SuppressWarnings("unchecked") public final class LockFreeIntMap implements Cloneable, Serializable { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater mapREF = AtomicReferenceFieldUpdater.newUpdater( LockFreeIntMap.class, diff --git a/src/dorkbox/collections/LockFreeIntStringMap.java b/src/dorkbox/collections/LockFreeIntStringMap.java index 0f73568..36dc6e1 100644 --- a/src/dorkbox/collections/LockFreeIntStringMap.java +++ b/src/dorkbox/collections/LockFreeIntStringMap.java @@ -24,6 +24,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; @SuppressWarnings("unchecked") public class LockFreeIntStringMap { + public static final String version = Collections.version; + private static final AtomicReferenceFieldUpdater mapREF = AtomicReferenceFieldUpdater.newUpdater( LockFreeIntStringMap.class, IntMap.class, diff --git a/src/dorkbox/collections/LockFreeObjectIntBiMap.java b/src/dorkbox/collections/LockFreeObjectIntBiMap.java index ac64f4d..ea122f0 100644 --- a/src/dorkbox/collections/LockFreeObjectIntBiMap.java +++ b/src/dorkbox/collections/LockFreeObjectIntBiMap.java @@ -40,6 +40,8 @@ import dorkbox.collections.IntMap.*; */ public class LockFreeObjectIntBiMap { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater forwardREF = AtomicReferenceFieldUpdater.newUpdater( LockFreeObjectIntBiMap.class, diff --git a/src/dorkbox/collections/LockFreeObjectIntMap.java b/src/dorkbox/collections/LockFreeObjectIntMap.java index ad05dfd..41244c5 100644 --- a/src/dorkbox/collections/LockFreeObjectIntMap.java +++ b/src/dorkbox/collections/LockFreeObjectIntMap.java @@ -34,6 +34,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; */ public class LockFreeObjectIntMap { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater mapREF = AtomicReferenceFieldUpdater.newUpdater( LockFreeObjectIntMap.class, diff --git a/src/dorkbox/collections/LockFreeObjectMap.java b/src/dorkbox/collections/LockFreeObjectMap.java index e005e14..7c2e34e 100644 --- a/src/dorkbox/collections/LockFreeObjectMap.java +++ b/src/dorkbox/collections/LockFreeObjectMap.java @@ -34,6 +34,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; @SuppressWarnings("unchecked") public final class LockFreeObjectMap implements Cloneable, Serializable { + public static final String version = Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater mapREF = AtomicReferenceFieldUpdater.newUpdater( LockFreeObjectMap.class, diff --git a/src/dorkbox/collections/LockFreeSet.java b/src/dorkbox/collections/LockFreeSet.java index ccbc6d3..9960862 100644 --- a/src/dorkbox/collections/LockFreeSet.java +++ b/src/dorkbox/collections/LockFreeSet.java @@ -38,6 +38,8 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; @SuppressWarnings("unchecked") public final class LockFreeSet implements Set, Cloneable, java.io.Serializable { + public static final String version = dorkbox.collections.Collections.version; + // Recommended for best performance while adhering to the "single writer principle". Must be static-final private static final AtomicReferenceFieldUpdater setREF = AtomicReferenceFieldUpdater.newUpdater(LockFreeSet.class, Set.class, diff --git a/src/dorkbox/collections/LongArray.java b/src/dorkbox/collections/LongArray.java index 44883b3..6a0638c 100644 --- a/src/dorkbox/collections/LongArray.java +++ b/src/dorkbox/collections/LongArray.java @@ -24,7 +24,9 @@ import java.util.Arrays; * avoids a memory copy when removing elements (the last element is moved to the removed element's position). * @author Nathan Sweet */ public class LongArray { - public long[] items; + public static final String version = Collections.version; + + public long[] items; public int size; public boolean ordered; @@ -326,7 +328,7 @@ public class LongArray { public void shuffle () { long[] items = this.items; for (int i = size - 1; i >= 0; i--) { - int ii = MathUtil.random(i); + int ii = Collections.INSTANCE.random(i); long temp = items[i]; items[i] = items[ii]; items[ii] = temp; @@ -342,7 +344,7 @@ public class LongArray { /** Returns a random item from the array, or zero if the array is empty. */ public long random () { if (size == 0) return 0; - return items[MathUtil.random(0,size - 1)]; + return items[Collections.random(0, size - 1)]; } public long[] toArray () { diff --git a/src/dorkbox/collections/LongMap.java b/src/dorkbox/collections/LongMap.java index 70f1901..dd77fa0 100644 --- a/src/dorkbox/collections/LongMap.java +++ b/src/dorkbox/collections/LongMap.java @@ -30,7 +30,9 @@ import java.util.NoSuchElementException; * @author Nathan Sweet */ @SuppressWarnings({"NullableProblems", "rawtypes", "unchecked"}) public class LongMap implements Iterable> { - private static final int PRIME1 = 0xbe1f14b1; + public static final String version = Collections.version; + + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; private static final int EMPTY = 0; @@ -68,7 +70,7 @@ public class LongMap implements Iterable> { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public LongMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -224,7 +226,7 @@ public class LongMap implements Iterable> { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -412,7 +414,7 @@ public class LongMap implements Iterable> { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -511,7 +513,7 @@ public class LongMap implements Iterable> { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/ObjectFloatMap.java b/src/dorkbox/collections/ObjectFloatMap.java index a89e872..b960324 100644 --- a/src/dorkbox/collections/ObjectFloatMap.java +++ b/src/dorkbox/collections/ObjectFloatMap.java @@ -20,7 +20,6 @@ package dorkbox.collections; import java.util.Iterator; import java.util.NoSuchElementException; -import java.util.Random; /** An unordered map where the values are floats. This implementation is a cuckoo hash map using 3 hashes, random walking, and a @@ -32,7 +31,9 @@ import java.util.Random; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "NullableProblems", "rawtypes"}) public class ObjectFloatMap implements Iterable> { - private static final int PRIME1 = 0xbe1f14b1; + public static final String version = Collections.version; + + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; @@ -68,7 +69,7 @@ public class ObjectFloatMap implements Iterable> { @SuppressWarnings("unchecked") public ObjectFloatMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -204,7 +205,7 @@ public class ObjectFloatMap implements Iterable> { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -392,7 +393,7 @@ public class ObjectFloatMap implements Iterable> { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -460,7 +461,7 @@ public class ObjectFloatMap implements Iterable> { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/ObjectIntMap.java b/src/dorkbox/collections/ObjectIntMap.java index aa2d4fa..a215279 100644 --- a/src/dorkbox/collections/ObjectIntMap.java +++ b/src/dorkbox/collections/ObjectIntMap.java @@ -20,7 +20,6 @@ package dorkbox.collections; import java.util.Iterator; import java.util.NoSuchElementException; -import java.util.Random; /** An unordered map where the values are ints. This implementation is a cuckoo hash map using 3 hashes, random walking, and a @@ -32,6 +31,8 @@ import java.util.Random; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "NullableProblems"}) public class ObjectIntMap implements Iterable> { + public static final String version = Collections.version; + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; @@ -67,7 +68,7 @@ public class ObjectIntMap implements Iterable> { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public ObjectIntMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -203,7 +204,7 @@ public class ObjectIntMap implements Iterable> { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -391,7 +392,7 @@ public class ObjectIntMap implements Iterable> { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -460,7 +461,7 @@ public class ObjectIntMap implements Iterable> { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/ObjectMap.java b/src/dorkbox/collections/ObjectMap.java index 838fea5..78bfffb 100644 --- a/src/dorkbox/collections/ObjectMap.java +++ b/src/dorkbox/collections/ObjectMap.java @@ -20,7 +20,6 @@ package dorkbox.collections; import java.util.Iterator; import java.util.NoSuchElementException; -import java.util.Random; /** An unordered map. This implementation is a cuckoo hash map using 3 hashes, random walking, and a small stash for problematic * keys. Null keys are not allowed. Null values are allowed. No allocation is done except when growing the table size.
@@ -34,7 +33,9 @@ import java.util.Random; * @author Nathan Sweet */ @SuppressWarnings("unchecked") public class ObjectMap implements Iterable> { - private static final int PRIME1 = 0xbe1f14b1; + public static final String version = Collections.version; + + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; @@ -69,7 +70,7 @@ public class ObjectMap implements Iterable> { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public ObjectMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -212,7 +213,7 @@ public class ObjectMap implements Iterable> { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random.nextInt(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; evictedValue = valueTable[index1]; @@ -393,7 +394,7 @@ public class ObjectMap implements Iterable> { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -487,7 +488,7 @@ public class ObjectMap implements Iterable> { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/ObjectSet.java b/src/dorkbox/collections/ObjectSet.java index cbe860d..c97a9e0 100644 --- a/src/dorkbox/collections/ObjectSet.java +++ b/src/dorkbox/collections/ObjectSet.java @@ -20,7 +20,6 @@ package dorkbox.collections; import java.util.Iterator; import java.util.NoSuchElementException; -import java.util.Random; /** An unordered set where the keys are objects. This implementation uses cuckoo hashing using 3 hashes, random walking, and a @@ -35,7 +34,9 @@ import java.util.Random; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "rawtypes", "NullableProblems", "SuspiciousSystemArraycopy"}) public class ObjectSet implements Iterable { - private static final int PRIME1 = 0xbe1f14b1; + public static final String version = Collections.version; + + private static final int PRIME1 = 0xbe1f14b1; private static final int PRIME2 = 0xb4b82e39; private static final int PRIME3 = 0xced1c241; @@ -67,7 +68,7 @@ public class ObjectSet implements Iterable { * @param initialCapacity If not a power of two, it is increased to the next nearest power of two. */ public ObjectSet (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); - initialCapacity = MathUtil.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); + initialCapacity = Collections.nextPowerOfTwo((int)Math.ceil(initialCapacity / loadFactor)); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = initialCapacity; @@ -204,7 +205,7 @@ public class ObjectSet implements Iterable { int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. - switch (MathUtil.random.nextInt(2)) { + switch (Collections.INSTANCE.random(2)) { case 0: evictedKey = key1; keyTable[index1] = insertKey; @@ -327,7 +328,7 @@ public class ObjectSet implements Iterable { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; - maximumCapacity = MathUtil.nextPowerOfTwo(maximumCapacity); + maximumCapacity = Collections.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); } @@ -402,7 +403,7 @@ public class ObjectSet implements Iterable { public void ensureCapacity (int additionalCapacity) { if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity); int sizeNeeded = size + additionalCapacity; - if (sizeNeeded >= threshold) resize(MathUtil.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); + if (sizeNeeded >= threshold) resize(Collections.nextPowerOfTwo((int)Math.ceil(sizeNeeded / loadFactor))); } private void resize (int newSize) { diff --git a/src/dorkbox/collections/OrderedMap.java b/src/dorkbox/collections/OrderedMap.java index ecf6814..644fa4b 100644 --- a/src/dorkbox/collections/OrderedMap.java +++ b/src/dorkbox/collections/OrderedMap.java @@ -28,7 +28,9 @@ import java.util.NoSuchElementException; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "NullableProblems"}) public class OrderedMap extends ObjectMap { - final Array keys; + public static final String version = Collections.version; + + final Array keys; private Entries entries1, entries2; private Values values1, values2; diff --git a/src/dorkbox/collections/OrderedSet.java b/src/dorkbox/collections/OrderedSet.java index 9af5580..6f60f7b 100644 --- a/src/dorkbox/collections/OrderedSet.java +++ b/src/dorkbox/collections/OrderedSet.java @@ -28,7 +28,9 @@ import java.util.NoSuchElementException; * @author Nathan Sweet */ @SuppressWarnings({"unchecked", "rawtypes"}) public class OrderedSet extends ObjectSet { - final Array items; + public static final String version = Collections.version; + + final Array items; OrderedSetIterator iterator1, iterator2; public OrderedSet () { diff --git a/src/dorkbox/collections/QuickSelect.java b/src/dorkbox/collections/QuickSelect.java index 53f2305..0cc00b3 100644 --- a/src/dorkbox/collections/QuickSelect.java +++ b/src/dorkbox/collections/QuickSelect.java @@ -25,7 +25,9 @@ import java.util.Comparator; * http://en.wikipedia.org/wiki/Quickselect * @author Jon Renner */ public class QuickSelect { - private T[] array; + public static final String version = Collections.version; + + private T[] array; private Comparator comp; public int select (T[] items, Comparator comp, int n, int size) { diff --git a/src/dorkbox/collections/Select.java b/src/dorkbox/collections/Select.java index ad43bee..12aaea1 100644 --- a/src/dorkbox/collections/Select.java +++ b/src/dorkbox/collections/Select.java @@ -34,7 +34,9 @@ import java.util.Comparator; * @author Jon Renner */ @SuppressWarnings("unchecked") public class Select { - private static Select instance; + public static final String version = Collections.version; + + private static Select instance; private QuickSelect quickSelect; /** Provided for convenience */ diff --git a/src/dorkbox/collections/Sort.java b/src/dorkbox/collections/Sort.java index 62921f2..fefb486 100644 --- a/src/dorkbox/collections/Sort.java +++ b/src/dorkbox/collections/Sort.java @@ -23,7 +23,9 @@ import java.util.Comparator; * @author Nathan Sweet */ @SuppressWarnings({"RedundantCast", "unchecked", "rawtypes"}) public class Sort { - static private Sort instance; + public static final String version = Collections.version; + + static private Sort instance; private TimSort timSort; private ComparableTimSort comparableTimSort; diff --git a/src/dorkbox/collections/TimSort.java b/src/dorkbox/collections/TimSort.java index 0881bbd..ecda838 100644 --- a/src/dorkbox/collections/TimSort.java +++ b/src/dorkbox/collections/TimSort.java @@ -39,7 +39,9 @@ import java.util.Comparator; * in place, using a binary insertion sort. */ @SuppressWarnings("unchecked") class TimSort { - /** This is the minimum sized sequence that will be merged. Shorter sequences will be lengthened by calling binarySort. If the + public static final String version = Collections.version; + + /** This is the minimum sized sequence that will be merged. Shorter sequences will be lengthened by calling binarySort. If the * entire array is less than this length, no merges will be performed. * * This constant should be a power of two. It was 64 in Tim Peter's C implementation, but 32 was empirically determined to work