Adding First set of UnitTest
-Fixed: bugs in ArrayList implementation. -Added: Unit Tests for the following interfaces: Iterable/Collection/List -Added: ArrayList Test -Fixed: QuickSort was broken. -Added: Shuffle method. -Added: Shared random to SanityChecks
This commit is contained in:
@@ -237,7 +237,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 KEY_GENERIC_TYPE iter = c.iterator();
|
||||
while(add != 0) data[index++] = iter.NEXT();
|
||||
while(add-- != 0) data[index++] = iter.NEXT();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -256,7 +256,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
grow(size + add);
|
||||
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
|
||||
size+=add;
|
||||
c.getElements(0, data, index, size);
|
||||
c.getElements(0, data, index, c.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -678,14 +678,30 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
public KEY_TYPE REMOVE(int index) {
|
||||
checkRange(index);
|
||||
KEY_TYPE old = data[index];
|
||||
if(index != size - 1) System.arraycopy(data, index+1, data, index, size - index);
|
||||
size--;
|
||||
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
|
||||
#if TYPE_OBJECT
|
||||
data[size] = null;
|
||||
#endif
|
||||
size--;
|
||||
return old;
|
||||
}
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
/**
|
||||
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
|
||||
* It stops after finding the first element
|
||||
* @param e the element that is searched for
|
||||
* @return true if the element was found and removed.
|
||||
*/
|
||||
@Override
|
||||
public boolean REMOVE_KEY(KEY_TYPE type) {
|
||||
int index = indexOf(type);
|
||||
if(index == -1) return false;
|
||||
REMOVE(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
/**
|
||||
* A Type-Specific pop function to reduce (un)boxing
|
||||
* @param index the index of the element to fetch
|
||||
@@ -713,14 +729,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
boolean modified = false;
|
||||
int j = 0;
|
||||
for(int i = 0;i<size;i++) {
|
||||
if(!c.contains(KEY_TO_OBJ(data[i]))) {
|
||||
data[j++] = data[i];
|
||||
modified = true;
|
||||
}
|
||||
if(!c.contains(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
|
||||
else modified = true;
|
||||
}
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(data, j, size, null);
|
||||
#endif
|
||||
size = j;
|
||||
return modified;
|
||||
}
|
||||
|
||||
@@ -744,14 +759,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
boolean modified = false;
|
||||
int j = 0;
|
||||
for(int i = 0;i<size;i++) {
|
||||
if(c.contains(KEY_TO_OBJ(data[i]))) {
|
||||
data[j++] = data[i];
|
||||
modified = true;
|
||||
}
|
||||
if(c.contains(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
|
||||
else modified = true;
|
||||
}
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(data, j, size, null);
|
||||
#endif
|
||||
size = j;
|
||||
return modified;
|
||||
}
|
||||
|
||||
@@ -764,19 +778,17 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
@Override
|
||||
@Primitive
|
||||
public boolean removeIf(Predicate<? super CLASS_TYPE> filter) {
|
||||
|
||||
Objects.requireNonNull(filter);
|
||||
boolean modified = false;
|
||||
int j = 0;
|
||||
for(int i = 0;i<size;i++) {
|
||||
if(!filter.test(KEY_TO_OBJ(data[i]))) {
|
||||
data[j++] = data[i];
|
||||
modified = true;
|
||||
}
|
||||
if(!filter.test(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
|
||||
else modified = true;
|
||||
}
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(data, j, size, null);
|
||||
#endif
|
||||
size = j;
|
||||
return modified;
|
||||
}
|
||||
|
||||
@@ -792,14 +804,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
boolean modified = false;
|
||||
int j = 0;
|
||||
for(int i = 0;i<size;i++) {
|
||||
if(!c.contains(data[i])) {
|
||||
data[j++] = data[i];
|
||||
modified = true;
|
||||
}
|
||||
if(!c.contains(data[i])) data[j++] = data[i];
|
||||
else modified = true;
|
||||
}
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(data, j, size, null);
|
||||
#endif
|
||||
size = j;
|
||||
return modified;
|
||||
}
|
||||
|
||||
@@ -820,14 +831,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
boolean modified = false;
|
||||
int j = 0;
|
||||
for(int i = 0;i<size;i++) {
|
||||
if(c.contains(data[i])) {
|
||||
data[j++] = data[i];
|
||||
modified = true;
|
||||
}
|
||||
if(c.contains(data[i])) data[j++] = data[i];
|
||||
else modified = true;
|
||||
}
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(data, j, size, null);
|
||||
#endif
|
||||
size = j;
|
||||
return modified;
|
||||
}
|
||||
|
||||
@@ -841,12 +851,12 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
public boolean remIf(JAVA_PREDICATE filter) {
|
||||
Objects.requireNonNull(filter);
|
||||
boolean modified = false;
|
||||
for(int i = 0, j = 0;i<size;i++) {
|
||||
if(!filter.test(data[i])) {
|
||||
data[j++] = data[i];
|
||||
modified = true;
|
||||
}
|
||||
int j = 0;
|
||||
for(int i = 0;i<size;i++) {
|
||||
if(!filter.test(data[i])) data[j++] = data[i];
|
||||
else modified = true;
|
||||
}
|
||||
size = j;
|
||||
return modified;
|
||||
}
|
||||
|
||||
@@ -909,6 +919,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
data = Arrays.copyOf(data, size);
|
||||
else if(size < size() && size >= 0)
|
||||
Arrays.fill(data, size, size(), EMPTY_VALUE);
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -929,7 +940,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
||||
@Override
|
||||
public boolean trim(int size) {
|
||||
if(size > size() || size() == data.length) return false;
|
||||
int value = Math.min(size, size());
|
||||
int value = Math.max(size, size());
|
||||
#if TYPE_OBJECT
|
||||
data = value == 0 ? (KEY_TYPE[])ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value);
|
||||
#else
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package speiger.src.collections.PACKAGE.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.RecursiveAction;
|
||||
#if !TYPE_OBJECT
|
||||
|
||||
@@ -105,6 +106,20 @@ public class ARRAYS
|
||||
}
|
||||
|
||||
#endif
|
||||
public static GENERIC_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array) {
|
||||
return shuffle(array, SanityChecks.getRandom());
|
||||
}
|
||||
|
||||
public static GENERIC_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, Random random) {
|
||||
for(int i = array.length-1; i>=0;i--) {
|
||||
int p = random.nextInt(i + 1);
|
||||
KEY_TYPE t = array[i];
|
||||
array[i] = array[p];
|
||||
array[p] = t;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the specified range of elements according to the order induced by the specified comparator,
|
||||
* potentially dynamically choosing an appropriate algorithm given the type and size of the array.
|
||||
@@ -796,7 +811,7 @@ public class ARRAYS
|
||||
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
|
||||
if(compare == 0) swap(array, a++, b);
|
||||
}
|
||||
for(;b>=c && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
|
||||
for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
|
||||
if(compare == 0) swap(array, c, d--);
|
||||
}
|
||||
if(b>c) break;
|
||||
@@ -849,7 +864,7 @@ public class ARRAYS
|
||||
for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;b++) {
|
||||
if(comp == 0) swap(array, a++, b);
|
||||
}
|
||||
for(;b>=c && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) {
|
||||
for(;c>=b && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) {
|
||||
if(comp == 0) swap(array, c, d--);
|
||||
}
|
||||
if(b>c) break;
|
||||
@@ -998,7 +1013,7 @@ public class ARRAYS
|
||||
for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;b++) {
|
||||
if(comp == 0) swap(array, a++, b);
|
||||
}
|
||||
for(;b>=c && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) {
|
||||
for(;c>=b && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) {
|
||||
if(comp == 0) swap(array, c, d--);
|
||||
}
|
||||
if(b>c) break;
|
||||
@@ -1041,7 +1056,7 @@ public class ARRAYS
|
||||
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
|
||||
if(compare == 0) swap(array, a++, b);
|
||||
}
|
||||
for(;b>=c && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
|
||||
for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
|
||||
if(compare == 0) swap(array, c, d--);
|
||||
}
|
||||
if(b>c) break;
|
||||
|
||||
Reference in New Issue
Block a user