Fixed recent found bugs

-Fixed: addAll with non Specific Collections was crashing lists.
-Fixed/Refactor: Clear and trim implementation was all over the place
-Fixed: Wrappers toString/hashCode/equals function wasn't implemented
-Added: Tests for addAll bug
-Refactor: Did small code style cleanups as I was fixing bugs.
This commit is contained in:
Speiger 2021-09-13 17:02:24 +02:00
parent ec817fb9c2
commit e30ca4103f
15 changed files with 197 additions and 151 deletions

View File

@ -68,7 +68,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
@Primitive
public boolean containsAny(Collection<?> c);
#if !TYPE_OBJECT
#if !TYPE_OBJECT
/**
* A Type-Specific remove function that reduces (un)boxing.
* @param o the element that should be removed

View File

@ -238,7 +238,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
size+=add;
Iterator<? extends CLASS_TYPE> iter = c.iterator();
while(add != 0) data[index++] = OBJ_TO_KEY(iter.next());
while(add-- != 0) data[index++] = OBJ_TO_KEY(iter.next());
return true;
}
@ -935,6 +935,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return;
}
data = size == 0 ? EMPTY_KEY_ARRAY : NEW_KEY_ARRAY(size);
this.size = size;
}
/**

View File

@ -423,6 +423,24 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
firstIndex = lastIndex = -1;
}
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(request + 1);
values = NEW_VALUE_ARRAY(request + 1);
links = new long[request + 1];
firstIndex = lastIndex = -1;
this.size = 0;
containsNull = false;
}
protected void moveToFirstIndex(int startPos) {
if(size == 1 || firstIndex == startPos) return;
if(lastIndex == startPos) {

View File

@ -534,8 +534,18 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
@Override
public void clearAndTrim(int size) {
clear();
trim(size);
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(request + 1);
values = NEW_VALUE_ARRAY(request + 1);
this.size = 0;
containsNull = false;
}
#if !TYPE_OBJECT

View File

@ -403,6 +403,24 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
firstIndex = lastIndex = -1;
}
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(request + 1);
values = NEW_VALUE_ARRAY(request + 1);
links = new long[request + 1];
firstIndex = lastIndex = -1;
this.size = 0;
containsNull = false;
}
protected void moveToFirstIndex(int startPos) {
if(size == 1 || firstIndex == startPos) return;
if(lastIndex == startPos) {

View File

@ -495,8 +495,18 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
@Override
public void clearAndTrim(int size) {
clear();
trim(size);
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(request + 1);
values = NEW_VALUE_ARRAY(request + 1);
this.size = 0;
containsNull = false;
}
#if !TYPE_OBJECT

View File

@ -1,5 +1,6 @@
package speiger.src.collections.PACKAGE.maps.impl.misc;
import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;
#if VALUE_OBJECT
@ -193,6 +194,14 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
return valuesC;
}
@Override
public void clear() {
if(size == 0) return;
size = 0;
Arrays.fill(present, 0L);
Arrays.fill(values, EMPTY_VALUE);
}
protected void onNodeAdded(int index) {
}

View File

@ -505,17 +505,19 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
@Override
public void clearAndTrim(int size) {
if(nullIndex <= size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = size;
mask = nullIndex - 1;
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(nullIndex + 1);
links = new long[nullIndex + 1];
firstIndex = -1;
lastIndex = -1;
keys = NEW_KEY_ARRAY(request + 1);
links = new long[request + 1];
firstIndex = lastIndex = -1;
this.size = 0;
containsNull = false;
}
@Override

View File

@ -486,17 +486,19 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
@Override
public void clearAndTrim(int size) {
if(nullIndex <= size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = size;
mask = nullIndex - 1;
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(nullIndex + 1);
links = new long[nullIndex + 1];
firstIndex = -1;
lastIndex = -1;
keys = NEW_KEY_ARRAY(request + 1);
links = new long[request + 1];
firstIndex = lastIndex = -1;
this.size = 0;
containsNull = false;
}
@Override

View File

@ -354,14 +354,17 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
@Override
public void clearAndTrim(int size) {
if(nullIndex <= size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = size;
mask = nullIndex - 1;
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(nullIndex + 1);
keys = NEW_KEY_ARRAY(request + 1);
this.size = 0;
containsNull = false;
}
private void ensureCapacity(int newCapacity) {

View File

@ -312,14 +312,17 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
@Override
public void clearAndTrim(int size) {
if(nullIndex <= size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(size >= request) {
clear();
return;
}
nullIndex = size;
mask = nullIndex - 1;
nullIndex = request;
mask = request-1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(nullIndex + 1);
keys = NEW_KEY_ARRAY(request + 1);
this.size = 0;
containsNull = false;
}
@Override

View File

@ -5,12 +5,14 @@ import java.util.function.Predicate;
#if PRIMITIVES
import java.util.function.JAVA_PREDICATE;
#endif
import java.util.function.Consumer;
import speiger.src.collections.PACKAGE.collections.ABSTRACT_COLLECTION;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.objects.utils.ObjectArrays;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#endif
@ -151,23 +153,32 @@ public class COLLECTIONS
#endif
@Override
public void clear() { synchronized(mutex) { c.clear(); } }
@Override
public Object[] toArray() { synchronized(mutex) { return c.toArray(); } }
#if !TYPE_OBJECT
@Override
public <T> T[] toArray(T[] a) { synchronized(mutex) { return c.toArray(a); } }
@Override
public KEY_TYPE[] TO_ARRAY() { synchronized(mutex) { return c.TO_ARRAY(); } }
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { synchronized(mutex) { return c.TO_ARRAY(a); } }
@Override
public void forEach(CONSUMER action) { synchronized(mutex) { c.forEach(action); } }
@Override
@Deprecated
public void forEach(Consumer<? super CLASS_TYPE> action) { synchronized(mutex) { c.forEach(action); } }
#else
@Override
public <E> E[] toArray(E[] a) { synchronized(mutex) { return c.toArray(a); } }
@Override
public void forEach(Consumer<? super CLASS_TYPE> action) { synchronized(mutex) { c.forEach(action); } }
#endif
@Override
public int hashCode() { synchronized(mutex) { return c.hashCode(); } }
@Override
public boolean equals(Object obj) { synchronized(mutex) { return c.equals(obj); } }
@Override
public String toString() { synchronized(mutex) { return c.toString(); } }
}
/**
@ -187,57 +198,29 @@ public class COLLECTIONS
public boolean addAll(Collection<? extends CLASS_TYPE> c) { throw new UnsupportedOperationException(); }
@Override
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) {
return c.contains(o);
}
public boolean contains(KEY_TYPE o) { return c.contains(o); }
#else
@Override
public boolean contains(Object o) {
return c.contains(o);
}
public boolean contains(Object o) { return c.contains(o); }
#endif
@Override
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) {
return this.c.containsAll(c);
}
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { return this.c.containsAll(c); }
@Override
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) {
return this.c.containsAny(c);
}
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { return this.c.containsAny(c); }
@Override
@Primitive
public boolean containsAny(Collection<?> c) {
return this.c.containsAny(c);
}
public boolean containsAny(Collection<?> c) { return this.c.containsAny(c); }
@Override
@Primitive
public boolean containsAll(Collection<?> c) {
return this.c.containsAll(c);
}
public boolean containsAll(Collection<?> c) { return this.c.containsAll(c); }
@Override
public int size() {
return c.size();
}
public int size() { return c.size(); }
@Override
public boolean isEmpty() {
return c.isEmpty();
}
public boolean isEmpty() { return c.isEmpty(); }
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.unmodifiable(c.iterator());
}
public ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.unmodifiable(c.iterator()); }
@Override
@Deprecated
public boolean remove(Object o) { throw new UnsupportedOperationException(); }
@ -264,35 +247,32 @@ public class COLLECTIONS
#endif
@Override
public void clear() { throw new UnsupportedOperationException(); }
@Override
public Object[] toArray() {
return c.toArray();
}
public Object[] toArray() { return c.toArray(); }
#if !TYPE_OBJECT
@Override
public <T> T[] toArray(T[] a) {
return c.toArray(a);
}
public <T> T[] toArray(T[] a) { return c.toArray(a); }
@Override
public KEY_TYPE[] TO_ARRAY() {
return c.TO_ARRAY();
}
public KEY_TYPE[] TO_ARRAY() { return c.TO_ARRAY(); }
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
return c.TO_ARRAY(a);
}
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { return c.TO_ARRAY(a); }
@Override
public void forEach(CONSUMER action) { c.forEach(action); }
@Override
@Deprecated
public void forEach(Consumer<? super CLASS_TYPE> action) { c.forEach(action); }
#else
@Override
public <E> E[] toArray(E[] a) {
return c.toArray(a);
}
public <E> E[] toArray(E[] a) { return c.toArray(a); }
@Override
public void forEach(Consumer<? super CLASS_TYPE> action) { c.forEach(action); }
#endif
@Override
public int hashCode() { return c.hashCode(); }
@Override
public boolean equals(Object obj) { return c.equals(obj); }
@Override
public String toString() { return c.toString(); }
}
/**
@ -308,43 +288,23 @@ public class COLLECTIONS
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) {
return false;
}
public boolean contains(KEY_TYPE o) { return false; }
@Override
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) {
return false;
}
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { return false; }
@Override
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) {
return false;
}
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { return false; }
#else
@Override
public boolean contains(Object o) {
return false;
}
public boolean contains(Object o) { return false; }
#endif
@Override
@Primitive
public boolean containsAny(Collection<?> c) {
return false;
}
public boolean containsAny(Collection<?> c) { return false; }
@Override
@Primitive
public boolean containsAll(Collection<?> c) {
return false;
}
public boolean containsAll(Collection<?> c) { return false; }
@Override
public int hashCode() {
return 0;
}
public int hashCode() { return 0; }
@Override
public boolean equals(Object o) {
@ -377,47 +337,24 @@ public class COLLECTIONS
@Override
public boolean remIf(JAVA_PREDICATE filter){ throw new UnsupportedOperationException(); }
#endif
@Override
public Object[] toArray() {
return ObjectArrays.EMPTY_ARRAY;
}
public Object[] toArray() { return ObjectArrays.EMPTY_ARRAY; }
#if !TYPE_OBJECT
@Override
public <T> T[] toArray(T[] a) {
return a;
}
public <T> T[] toArray(T[] a) { return a; }
@Override
public KEY_TYPE[] TO_ARRAY() {
return ARRAYS.EMPTY_ARRAY;
}
public KEY_TYPE[] TO_ARRAY() { return ARRAYS.EMPTY_ARRAY; }
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
return a;
}
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { return a; }
#else
@Override
public <E> E[] toArray(E[] a) {
return a;
}
public <E> E[] toArray(E[] a) { return a; }
#endif
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.empty();
}
public ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.empty(); }
@Override
public void clear() {
}
public void clear() {}
@Override
public int size() {
return 0;
}
public int size() { return 0; }
}
}

View File

@ -2,6 +2,7 @@ package speiger.src.collections.ints.base;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.IntStream;
import org.junit.Assert;
@ -12,6 +13,7 @@ import speiger.src.collections.ints.utils.IntArrays;
import speiger.src.collections.ints.utils.IntCollections;
import speiger.src.collections.ints.utils.IntCollections.SynchronizedCollection;
import speiger.src.collections.ints.utils.IntCollections.UnmodifiableCollection;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.tests.CollectionTest;
@SuppressWarnings("javadoc")
@ -47,6 +49,9 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
Assert.assertEquals(TEST_ARRAY.length, collection.size());
collection.addAll(create(BULK_ADD_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + BULK_ADD_ARRAY.length, collection.size());
// Testing if adding via non Type Specific work like they should
List<Integer> wrapper = new ObjectArrayList<>(create(ADD_ARRAY));
Assert.assertNotNull(create(ADD_ARRAY).addAll(wrapper));
}
@Test
@ -163,4 +168,14 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
collection = IntCollections.unmodifiable(collection);
Assert.assertTrue(collection instanceof UnmodifiableCollection);
}
@Test
public void testToString() {
if(!getValidCollectionTests().contains(CollectionTest.TO_STRING)) return;
String base = Arrays.toString(BULK_ADD_ARRAY);
IntCollection collection = create(BULK_ADD_ARRAY);
Assert.assertEquals(base, collection.toString());
Assert.assertEquals(base, IntCollections.synchronize(collection).toString());
Assert.assertEquals(base, IntCollections.unmodifiable(collection).toString());
}
}

View File

@ -6,6 +6,7 @@ import speiger.src.collections.ints.base.BaseIntCollectionTest;
import speiger.src.collections.ints.base.BaseIntSortedSetTest;
import speiger.src.collections.ints.collections.IntCollection;
import speiger.src.collections.ints.utils.IntStrategy;
import speiger.src.collections.tests.CollectionTest;
import speiger.src.collections.tests.SortedSetTest;
@SuppressWarnings("javadoc")
@ -21,6 +22,14 @@ public class IntHashSetTests
{
@Override
protected IntCollection create(int[] data) { return new IntOpenHashSet(data); }
@Override
protected EnumSet<CollectionTest> getValidCollectionTests()
{
EnumSet<CollectionTest> tests = super.getValidCollectionTests();
tests.remove(CollectionTest.TO_STRING);
return tests;
}
}
public static class IntLinkedOpenHashSetTests extends BaseIntOpenHashSetTests
@ -33,6 +42,14 @@ public class IntHashSetTests
{
@Override
protected IntCollection create(int[] data) { return new IntOpenCustomHashSet(data, new DefaultStrategy()); }
@Override
protected EnumSet<CollectionTest> getValidCollectionTests()
{
EnumSet<CollectionTest> tests = super.getValidCollectionTests();
tests.remove(CollectionTest.TO_STRING);
return tests;
}
}
public static class IntLinkedOpenCustomHashSetTests extends BaseIntOpenHashSetTests

View File

@ -14,5 +14,6 @@ public enum CollectionTest
RETAIN_ALL,
TO_ARRAY,
CLEAR,
WRAPPER;
WRAPPER,
TO_STRING;
}