Implement partly FullyUsedArrayProcessor

This commit is contained in:
2021-08-29 19:03:18 +02:00
parent 0f12449954
commit 923b6a8c2d
3 changed files with 437 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package aud.exam.prep.array;
public class Arrays {
public static <T> T[] newArray(int size) {
//noinspection unchecked
return (T[]) new Object[size];
}
}

View File

@ -0,0 +1,418 @@
package aud.exam.prep.array;
import aud.exam.prep.Pair;
import aud.exam.prep.SequenceProcessor;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
@SuppressWarnings("ManualArrayCopy")
public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUsedArray<T>> {
@Override
public boolean find(FullyUsedArray<T> array, T t) {
for (int i = 0; i < array.theArray.length; i++) {
if (Objects.equals(array.theArray[i], t)) {
return true;
}
}
return false;
}
@Override
public boolean findBinary(FullyUsedArray<T> array, T t, Comparator<T> cmp) {
if (array.theArray.length == 0) {
return false;
}
var low = 0;
var high = array.theArray.length-1;
while (low <= high) {
var index = (low + high) / 2;
var c = cmp.compare(t, array.theArray[index]);
if (c == 0) {
return true;
} else if (c < 0) {
high = index-1;
} else {
low = index+1;
}
}
return false;
}
@Override
public boolean override(FullyUsedArray<T> array, T from, T to) {
for (int i = 0; i < array.theArray.length; i++) {
if (Objects.equals(array.theArray[i], from)) {
array.theArray[i] = to;
return true;
}
}
return false;
}
@Override
public boolean overrideAll(FullyUsedArray<T> array, T from, T to) {
var modified = false;
for (int i = 0; i < array.theArray.length; i++) {
if (Objects.equals(array.theArray[i], from)) {
array.theArray[i] = to;
modified = true;
}
}
return modified;
}
@Override
public boolean overrideAt(FullyUsedArray<T> array, T to, int index) {
if (index < 0 || index >= array.theArray.length) {
return false;
}
array.theArray[index] = to;
return true;
}
@Override
public void insertInOrder(FullyUsedArray<T> array, T t, Comparator<T> cmp) {
T[] newArray = Arrays.newArray(array.theArray.length+1);
var index = 0;
while (index < array.theArray.length && cmp.compare(array.theArray[index], t) < 0) {
newArray[index] = array.theArray[index];
index++;
}
newArray[index] = t;
while (index < array.theArray.length) {
newArray[index+1] = array.theArray[index];
index++;
}
array.theArray = newArray;
}
@Override
public boolean remove(FullyUsedArray<T> array, T t) {
if (!find(array, t)) {
return false;
}
T[] newArray = Arrays.newArray(array.theArray.length-1);
var index = 0;
while (!Objects.equals(array.theArray[index], t)) {
newArray[index] = array.theArray[index];
index++;
}
index++;
while (index < array.theArray.length) {
newArray[index-1] = array.theArray[index];
index++;
}
array.theArray = newArray;
return true;
}
@Override
public boolean removeAll(FullyUsedArray<T> array, T t) {
int size = 0;
for (int i = 0; i < array.theArray.length; i++) {
if (!Objects.equals(array.theArray[i], t)) {
size++;
}
}
if (size == array.theArray.length) {
return false;
}
T[] newArray = Arrays.newArray(size);
int index = 0;
for (int i = 0; i < array.theArray.length; i++) {
if (!Objects.equals(array.theArray[i], t)) {
newArray[index++] = array.theArray[i];
}
}
array.theArray = newArray;
return true;
}
@Override
public boolean isAscending(FullyUsedArray<T> array, Comparator<T> cmp) {
for (int i = 0; i < array.theArray.length-1; i++) {
if (cmp.compare(array.theArray[i], array.theArray[i+1]) > 0) {
return false;
}
}
return true;
}
@Override
public T max(FullyUsedArray<T> array, Comparator<T> cmp) {
if (array.theArray.length == 0) {
throw new IllegalArgumentException("The sequence may not be empty");
}
var max = array.theArray[0];
for (int i = 1; i < array.theArray.length; i++) {
if (cmp.compare(max, array.theArray[i]) < 0) {
max = array.theArray[i];
}
}
return max;
}
@Override
public T secondMax(FullyUsedArray<T> array, Comparator<T> cmp) {
if (array.theArray.length < 2) {
throw new IllegalArgumentException("The sequence must contains at least 2 elements");
}
T secondMax, max;
if (cmp.compare(array.theArray[0], array.theArray[1]) < 0) {
secondMax = array.theArray[0];
max = array.theArray[1];
} else {
secondMax = array.theArray[1];
max = array.theArray[0];
}
for (int i = 2; i < array.theArray.length; i++) {
if (cmp.compare(max, array.theArray[i]) < 0) {
secondMax = max;
max = array.theArray[i];
} else if (cmp.compare(secondMax, array.theArray[i]) < 0) {
secondMax = array.theArray[i];
}
}
return secondMax;
}
@Override
public boolean check(FullyUsedArray<T> array) {
return true;
}
@Override
public boolean isItemWiseLessOrEqual(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) {
if (b.theArray.length < a.theArray.length) {
return false;
}
for (int i = 0; i < a.theArray.length; i++) {
if (cmp.compare(a.theArray[i], b.theArray[i]) > 0) {
return false;
}
}
return true;
}
@Override
public boolean isItemWiseLess(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) {
if (b.theArray.length < a.theArray.length) {
return false;
}
var actualLessElement = false;
for (int i = 0; i < a.theArray.length; i++) {
var c = cmp.compare(a.theArray[i], b.theArray[i]);
if (c > 0) {
return false;
} else if (c < 0) {
actualLessElement = true;
}
}
return a.theArray.length < b.theArray.length || actualLessElement;
}
@Override
public boolean isLexSmaller(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) {
var end = Integer.min(a.theArray.length, b.theArray.length);
for (int i = 0; i < end; i++) {
var c = cmp.compare(a.theArray[i], b.theArray[i]);
if (c < 0) {
return true;
}
if (c > 0) {
return false;
}
}
return a.theArray.length < b.theArray.length;
}
@Override
public void exchangePairs(FullyUsedArray<T> array) {
}
@Override
public void rotateTriples(FullyUsedArray<T> array) {
}
@Override
public void removeEverySecond(FullyUsedArray<T> array) {
throw new UnsupportedOperationException();
}
@Override
public void doubleAllKeys(FullyUsedArray<T> array) {
T[] newArray = Arrays.newArray(array.theArray.length*2);
for (int i = 0; i < array.theArray.length; i++) {
newArray[2*i] = newArray[2*i + 1] = array.theArray[i];
}
array.theArray = newArray;
}
@Override
public FullyUsedArray<T> rotateRight(FullyUsedArray<T> array) {
if (array.theArray.length < 2) {
return array;
}
var right = array.theArray[array.theArray.length-1];
for (int i = array.theArray.length-2; i >= 0; i--) {
array.theArray[i+1] = array.theArray[i];
}
array.theArray[0] = right;
return array;
}
@Override
public FullyUsedArray<T> rotateLeft(FullyUsedArray<T> array) {
if (array.theArray.length < 2) {
return array;
}
var left = array.theArray[0];
for (int i = 0; i < array.theArray.length-1; i++) {
array.theArray[i] = array.theArray[i+1];
}
array.theArray[array.theArray.length-1] = left;
return array;
}
@Override
public void removeDuplicates(FullyUsedArray<T> array) {
if (array.theArray.length < 2) {
return;
}
var distinctRuns = 1;
for (int i = 0; i < array.theArray.length-1; i++) {
if (array.theArray[i] != array.theArray[i+1]) {
distinctRuns++;
}
}
if (distinctRuns == array.theArray.length) {
return;
}
T[] newArray = Arrays.newArray(distinctRuns);
var index = 0;
for (int i = 0; i < array.theArray.length; i++) {
if (index == 0 || !Objects.equals(newArray[index-1], array.theArray[i])) {
newArray[index++] = array.theArray[i];
}
}
array.theArray = newArray;
}
@Override
public FullyUsedArray<T> invert(FullyUsedArray<T> array) {
return null;
}
@Override
public FullyUsedArray<T> clone(FullyUsedArray<T> array) {
return null;
}
@Override
public FullyUsedArray<T> alternate(FullyUsedArray<T> a, FullyUsedArray<T> b) {
throw new UnsupportedOperationException();
}
@Override
public FullyUsedArray<T> merge(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) {
throw new UnsupportedOperationException();
}
@Override
public Pair<FullyUsedArray<T>, FullyUsedArray<T>> divideAlternating(FullyUsedArray<T> array) {
throw new UnsupportedOperationException();
}
@Override
public Pair<FullyUsedArray<T>, FullyUsedArray<T>> divideAlternatingByRuns(FullyUsedArray<T> array, Comparator<T> cmp) {
throw new UnsupportedOperationException();
}
@Override
public Pair<FullyUsedArray<T>, FullyUsedArray<T>> divideByPivot(FullyUsedArray<T> array, T pivot, Comparator<T> cmp) {
throw new UnsupportedOperationException();
}
@Override
public FullyUsedArray<T> create(Iterable<T> iterable) {
var array = new FullyUsedArray<T>();
var size = 0;
for (T ignored : iterable) {
size++;
}
array.theArray = Arrays.newArray(size);
var index = 0;
for (T t : iterable) {
array.theArray[index] = t;
index++;
}
return array;
}
@Override
public Iterable<T> iterate(FullyUsedArray<T> array) {
return () -> new Iterator<>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < array.theArray.length;
}
@Override
public T next() {
return array.theArray[index++];
}
};
}
}

View File

@ -0,0 +1,10 @@
package aud.exam.prep.array;
import aud.exam.prep.SequenceProcessorTest;
public class FullyUsedArrayProcessorTest extends SequenceProcessorTest<FullyUsedArray<Integer>> {
protected FullyUsedArrayProcessorTest() {
super(new FullyUsedArrayProcessor<>());
}
}