From ab87b2e96460eb7ef887571181b87534dabe5f69 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Mon, 30 Aug 2021 17:06:50 +0200 Subject: [PATCH] Split up IterativeFullyUsedArrayProcessor and RecursiveFullyUsedArrayProcessor --- src/aud/exam/prep/array/ArrayIterator.java | 26 + .../prep/array/FullyUsedArrayProcessor.java | 549 +----------------- .../IterativeFullyUsedArrayProcessor.java | 535 +++++++++++++++++ .../RecursiveFullyUsedArrayProcessor.java | 170 ++++++ test/aud/exam/prep/SequenceProcessorTest.java | 2 +- .../array/FullyUsedArrayProcessorTest.java | 10 - .../IterativeFullyUsedArrayProcessorTest.java | 10 + .../RecursiveFullyUsedArrayProcessorTest.java | 10 + 8 files changed, 756 insertions(+), 556 deletions(-) create mode 100644 src/aud/exam/prep/array/ArrayIterator.java create mode 100644 src/aud/exam/prep/array/IterativeFullyUsedArrayProcessor.java create mode 100644 src/aud/exam/prep/array/RecursiveFullyUsedArrayProcessor.java delete mode 100644 test/aud/exam/prep/array/FullyUsedArrayProcessorTest.java create mode 100644 test/aud/exam/prep/array/IterativeFullyUsedArrayProcessorTest.java create mode 100644 test/aud/exam/prep/array/RecursiveFullyUsedArrayProcessorTest.java diff --git a/src/aud/exam/prep/array/ArrayIterator.java b/src/aud/exam/prep/array/ArrayIterator.java new file mode 100644 index 0000000..08f5ec2 --- /dev/null +++ b/src/aud/exam/prep/array/ArrayIterator.java @@ -0,0 +1,26 @@ +package aud.exam.prep.array; + +import java.util.Iterator; + +public class ArrayIterator implements Iterator { + + private final T[] array; + private int index; + private final int end; + + public ArrayIterator(T[] array, int begin, int end) { + this.array = array; + this.index = begin; + this.end = end; + } + + @Override + public boolean hasNext() { + return index < end; + } + + @Override + public T next() { + return array[index++]; + } +} diff --git a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java index 38b63d7..cdbc923 100644 --- a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java +++ b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java @@ -1,73 +1,8 @@ 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 implements SequenceProcessor> { - @Override - public boolean find(FullyUsedArray 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 array, T t, Comparator 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 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 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; - } +abstract class FullyUsedArrayProcessor implements SequenceProcessor> { @Override public boolean overrideAt(FullyUsedArray array, T to, int index) { @@ -79,490 +14,14 @@ public class FullyUsedArrayProcessor implements SequenceProcessor array, T t, Comparator 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 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 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 array, Comparator 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 array, Comparator 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 array, Comparator 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 isItemWiseLessOrEqual(FullyUsedArray a, FullyUsedArray b, Comparator 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 a, FullyUsedArray b, Comparator 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 a, FullyUsedArray b, Comparator 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 array) { - for (int i = 1; i < array.theArray.length-1; i += 2) { - T t = array.theArray[i]; - array.theArray[i] = array.theArray[i+1]; - array.theArray[i+1] = t; - } - } - - @Override - public void rotateTriples(FullyUsedArray array) { - for (int i = 1; i < array.theArray.length-2; i += 3) { - T t = array.theArray[i+2]; - array.theArray[i+2] = array.theArray[i+1]; - array.theArray[i+1] = array.theArray[i]; - array.theArray[i] = t; - } - } - - @Override - public void removeEverySecond(FullyUsedArray array) { - T[] newArray = Arrays.newArray(array.theArray.length - array.theArray.length / 2); - - for (int i = 0; i < newArray.length; i++) { - newArray[i] = array.theArray[2*i]; - } - - array.theArray = newArray; - } - - @Override - public void doubleAllKeys(FullyUsedArray 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 rotateRight(FullyUsedArray 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 rotateLeft(FullyUsedArray 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 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 invert(FullyUsedArray array) { - for (int i = 0; i < array.theArray.length/2; i++) { - T t = array.theArray[i]; - array.theArray[i] = array.theArray[array.theArray.length-i-1]; - array.theArray[array.theArray.length-i-1] = t; - } - return array; - } - - @Override - public FullyUsedArray clone(FullyUsedArray array) { - var newArray = new FullyUsedArray(); - newArray.theArray = Arrays.newArray(array.theArray.length); - - for (int i = 0; i < array.theArray.length; i++) { - newArray.theArray[i] = array.theArray[i]; - } - - return newArray; - } - - @Override - public FullyUsedArray alternate(FullyUsedArray a, FullyUsedArray b) { - var newArray = new FullyUsedArray(); - newArray.theArray = Arrays.newArray(a.theArray.length + b.theArray.length); - - var index = 0; - var indexA = 0; - var indexB = 0; - - while (indexA < a.theArray.length || indexB < b.theArray.length) { - if (indexA < a.theArray.length) { - newArray.theArray[index++] = a.theArray[indexA++]; - } - if (indexB < b.theArray.length) { - newArray.theArray[index++] = b.theArray[indexB++]; - } - } - - return newArray; - } - - @Override - public FullyUsedArray merge(FullyUsedArray a, FullyUsedArray b, Comparator cmp) { - var newArray = new FullyUsedArray(); - newArray.theArray = Arrays.newArray(a.theArray.length + b.theArray.length); - - var index = 0; - var indexA = 0; - var indexB = 0; - - while (indexA < a.theArray.length && indexB < b.theArray.length) { - if (cmp.compare(a.theArray[indexA], b.theArray[indexB]) < 0) { - newArray.theArray[index++] = a.theArray[indexA++]; - } else { - newArray.theArray[index++] = b.theArray[indexB++]; - } - } - - while (indexA < a.theArray.length) { - newArray.theArray[index++] = a.theArray[indexA++]; - } - - while (indexB < b.theArray.length) { - newArray.theArray[index++] = b.theArray[indexB++]; - } - - return newArray; - } - - @Override - public Pair, FullyUsedArray> divideAlternating(FullyUsedArray array) { - var p = new Pair, FullyUsedArray>(); - p.fst = new FullyUsedArray<>(); - p.snd = new FullyUsedArray<>(); - - p.snd.theArray = Arrays.newArray(array.theArray.length / 2); - p.fst.theArray = Arrays.newArray(array.theArray.length - p.snd.theArray.length); - - for (int i = 0; i < array.theArray.length; i++) { - if (i % 2 == 0) { - p.fst.theArray[i/2] = array.theArray[i]; - } else { - p.snd.theArray[i/2] = array.theArray[i]; - } - } - - return p; - } - - @Override - public Pair, FullyUsedArray> divideAlternatingByRuns(FullyUsedArray array, Comparator cmp) { - var p = new Pair, FullyUsedArray>(); - p.fst = new FullyUsedArray<>(); - p.snd = new FullyUsedArray<>(); - - var runInFst = true; - var elementsInFst = 0; - - for (int i = 0; i < array.theArray.length; i++) { - if (runInFst) { - elementsInFst++; - } - - if (i < array.theArray.length-1 && cmp.compare(array.theArray[i], array.theArray[i + 1]) > 0) { - runInFst = !runInFst; - } - } - - p.fst.theArray = Arrays.newArray(elementsInFst); - p.snd.theArray = Arrays.newArray(array.theArray.length - elementsInFst); - - runInFst = true; - var indexA = 0; - var indexB = 0; - - for (int i = 0; i < array.theArray.length; i++) { - if (runInFst) { - p.fst.theArray[indexA++] = array.theArray[i]; - } else { - p.snd.theArray[indexB++] = array.theArray[i]; - } - - if (i < array.theArray.length-1 && cmp.compare(array.theArray[i], array.theArray[i + 1]) > 0) { - runInFst = !runInFst; - } - } - - return p; - } - - @Override - public Pair, FullyUsedArray> divideByPivot(FullyUsedArray array, T pivot, Comparator cmp) { - var p = new Pair, FullyUsedArray>(); - p.fst = new FullyUsedArray<>(); - p.snd = new FullyUsedArray<>(); - - var less = 0; - var greater = 0; - - for (int i = 0; i < array.theArray.length; i++) { - var c = cmp.compare(array.theArray[i], pivot); - if (c < 0) { - less++; - } - if (c > 0) { - greater++; - } - } - - p.fst.theArray = Arrays.newArray(less); - p.snd.theArray = Arrays.newArray(greater); - - var indexA = 0; - var indexB = 0; - - for (int i = 0; i < array.theArray.length; i++) { - var c = cmp.compare(array.theArray[i], pivot); - if (c < 0) { - p.fst.theArray[indexA++] = array.theArray[i]; - } - if (c > 0) { - p.snd.theArray[indexB++] = array.theArray[i]; - } - } - - return p; - } - @Override public boolean check(FullyUsedArray array) { return true; } - @Override - public FullyUsedArray create(Iterable iterable) { - var array = new FullyUsedArray(); - 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 iterate(FullyUsedArray 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++]; - } - }; + return () -> + new ArrayIterator<>(array.theArray, 0, array.theArray.length); } -} \ No newline at end of file +} diff --git a/src/aud/exam/prep/array/IterativeFullyUsedArrayProcessor.java b/src/aud/exam/prep/array/IterativeFullyUsedArrayProcessor.java new file mode 100644 index 0000000..668b81b --- /dev/null +++ b/src/aud/exam/prep/array/IterativeFullyUsedArrayProcessor.java @@ -0,0 +1,535 @@ +package aud.exam.prep.array; + +import aud.exam.prep.Pair; + +import java.util.Comparator; +import java.util.Objects; + +@SuppressWarnings("ManualArrayCopy") +public class IterativeFullyUsedArrayProcessor extends FullyUsedArrayProcessor { + + @Override + public boolean find(FullyUsedArray 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 array, T t, Comparator 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 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 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 void insertInOrder(FullyUsedArray array, T t, Comparator 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 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 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 array, Comparator 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 array, Comparator 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 array, Comparator 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 isItemWiseLessOrEqual(FullyUsedArray a, FullyUsedArray b, Comparator 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 a, FullyUsedArray b, Comparator 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 a, FullyUsedArray b, Comparator 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 array) { + for (int i = 1; i < array.theArray.length-1; i += 2) { + T t = array.theArray[i]; + array.theArray[i] = array.theArray[i+1]; + array.theArray[i+1] = t; + } + } + + @Override + public void rotateTriples(FullyUsedArray array) { + for (int i = 1; i < array.theArray.length-2; i += 3) { + T t = array.theArray[i+2]; + array.theArray[i+2] = array.theArray[i+1]; + array.theArray[i+1] = array.theArray[i]; + array.theArray[i] = t; + } + } + + @Override + public void removeEverySecond(FullyUsedArray array) { + T[] newArray = Arrays.newArray(array.theArray.length - array.theArray.length / 2); + + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array.theArray[2*i]; + } + + array.theArray = newArray; + } + + @Override + public void doubleAllKeys(FullyUsedArray 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 rotateRight(FullyUsedArray 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 rotateLeft(FullyUsedArray 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 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 invert(FullyUsedArray array) { + for (int i = 0; i < array.theArray.length/2; i++) { + T t = array.theArray[i]; + array.theArray[i] = array.theArray[array.theArray.length-i-1]; + array.theArray[array.theArray.length-i-1] = t; + } + return array; + } + + @Override + public FullyUsedArray clone(FullyUsedArray array) { + var newArray = new FullyUsedArray(); + newArray.theArray = Arrays.newArray(array.theArray.length); + + for (int i = 0; i < array.theArray.length; i++) { + newArray.theArray[i] = array.theArray[i]; + } + + return newArray; + } + + @Override + public FullyUsedArray alternate(FullyUsedArray a, FullyUsedArray b) { + var newArray = new FullyUsedArray(); + newArray.theArray = Arrays.newArray(a.theArray.length + b.theArray.length); + + var index = 0; + var indexA = 0; + var indexB = 0; + + while (indexA < a.theArray.length || indexB < b.theArray.length) { + if (indexA < a.theArray.length) { + newArray.theArray[index++] = a.theArray[indexA++]; + } + if (indexB < b.theArray.length) { + newArray.theArray[index++] = b.theArray[indexB++]; + } + } + + return newArray; + } + + @Override + public FullyUsedArray merge(FullyUsedArray a, FullyUsedArray b, Comparator cmp) { + var newArray = new FullyUsedArray(); + newArray.theArray = Arrays.newArray(a.theArray.length + b.theArray.length); + + var index = 0; + var indexA = 0; + var indexB = 0; + + while (indexA < a.theArray.length && indexB < b.theArray.length) { + if (cmp.compare(a.theArray[indexA], b.theArray[indexB]) < 0) { + newArray.theArray[index++] = a.theArray[indexA++]; + } else { + newArray.theArray[index++] = b.theArray[indexB++]; + } + } + + while (indexA < a.theArray.length) { + newArray.theArray[index++] = a.theArray[indexA++]; + } + + while (indexB < b.theArray.length) { + newArray.theArray[index++] = b.theArray[indexB++]; + } + + return newArray; + } + + @Override + public Pair, FullyUsedArray> divideAlternating(FullyUsedArray array) { + var p = new Pair, FullyUsedArray>(); + p.fst = new FullyUsedArray<>(); + p.snd = new FullyUsedArray<>(); + + p.snd.theArray = Arrays.newArray(array.theArray.length / 2); + p.fst.theArray = Arrays.newArray(array.theArray.length - p.snd.theArray.length); + + for (int i = 0; i < array.theArray.length; i++) { + if (i % 2 == 0) { + p.fst.theArray[i/2] = array.theArray[i]; + } else { + p.snd.theArray[i/2] = array.theArray[i]; + } + } + + return p; + } + + @Override + public Pair, FullyUsedArray> divideAlternatingByRuns(FullyUsedArray array, Comparator cmp) { + var p = new Pair, FullyUsedArray>(); + p.fst = new FullyUsedArray<>(); + p.snd = new FullyUsedArray<>(); + + var runInFst = true; + var elementsInFst = 0; + + for (int i = 0; i < array.theArray.length; i++) { + if (runInFst) { + elementsInFst++; + } + + if (i < array.theArray.length-1 && cmp.compare(array.theArray[i], array.theArray[i + 1]) > 0) { + runInFst = !runInFst; + } + } + + p.fst.theArray = Arrays.newArray(elementsInFst); + p.snd.theArray = Arrays.newArray(array.theArray.length - elementsInFst); + + runInFst = true; + var indexA = 0; + var indexB = 0; + + for (int i = 0; i < array.theArray.length; i++) { + if (runInFst) { + p.fst.theArray[indexA++] = array.theArray[i]; + } else { + p.snd.theArray[indexB++] = array.theArray[i]; + } + + if (i < array.theArray.length-1 && cmp.compare(array.theArray[i], array.theArray[i + 1]) > 0) { + runInFst = !runInFst; + } + } + + return p; + } + + @Override + public Pair, FullyUsedArray> divideByPivot(FullyUsedArray array, T pivot, Comparator cmp) { + var p = new Pair, FullyUsedArray>(); + p.fst = new FullyUsedArray<>(); + p.snd = new FullyUsedArray<>(); + + var less = 0; + var greater = 0; + + for (int i = 0; i < array.theArray.length; i++) { + var c = cmp.compare(array.theArray[i], pivot); + if (c < 0) { + less++; + } + if (c > 0) { + greater++; + } + } + + p.fst.theArray = Arrays.newArray(less); + p.snd.theArray = Arrays.newArray(greater); + + var indexA = 0; + var indexB = 0; + + for (int i = 0; i < array.theArray.length; i++) { + var c = cmp.compare(array.theArray[i], pivot); + if (c < 0) { + p.fst.theArray[indexA++] = array.theArray[i]; + } + if (c > 0) { + p.snd.theArray[indexB++] = array.theArray[i]; + } + } + + return p; + } + + @Override + public FullyUsedArray create(Iterable iterable) { + var array = new FullyUsedArray(); + 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; + } +} \ No newline at end of file diff --git a/src/aud/exam/prep/array/RecursiveFullyUsedArrayProcessor.java b/src/aud/exam/prep/array/RecursiveFullyUsedArrayProcessor.java new file mode 100644 index 0000000..1707e6c --- /dev/null +++ b/src/aud/exam/prep/array/RecursiveFullyUsedArrayProcessor.java @@ -0,0 +1,170 @@ +package aud.exam.prep.array; + +import aud.exam.prep.Pair; + +import java.util.Comparator; +import java.util.Iterator; + +public class RecursiveFullyUsedArrayProcessor extends FullyUsedArrayProcessor { + + @Override + public boolean find(FullyUsedArray array, T t) { + return false; + } + + @Override + public boolean findBinary(FullyUsedArray array, T t, Comparator cmp) { + return false; + } + + @Override + public boolean override(FullyUsedArray array, T from, T to) { + return false; + } + + @Override + public boolean overrideAll(FullyUsedArray array, T from, T to) { + return false; + } + + @Override + public void insertInOrder(FullyUsedArray array, T t, Comparator cmp) { + + } + + @Override + public boolean remove(FullyUsedArray array, T t) { + return false; + } + + @Override + public boolean removeAll(FullyUsedArray array, T t) { + return false; + } + + @Override + public boolean isAscending(FullyUsedArray array, Comparator cmp) { + return false; + } + + @Override + public T max(FullyUsedArray array, Comparator cmp) { + return null; + } + + @Override + public T secondMax(FullyUsedArray array, Comparator cmp) { + return null; + } + + @Override + public boolean isItemWiseLessOrEqual(FullyUsedArray a, FullyUsedArray b, Comparator cmp) { + return false; + } + + @Override + public boolean isItemWiseLess(FullyUsedArray a, FullyUsedArray b, Comparator cmp) { + return false; + } + + @Override + public boolean isLexSmaller(FullyUsedArray a, FullyUsedArray b, Comparator cmp) { + return false; + } + + @Override + public void exchangePairs(FullyUsedArray array) { + + } + + @Override + public void rotateTriples(FullyUsedArray array) { + + } + + @Override + public void removeEverySecond(FullyUsedArray array) { + + } + + @Override + public void doubleAllKeys(FullyUsedArray array) { + + } + + @Override + public FullyUsedArray rotateRight(FullyUsedArray array) { + return null; + } + + @Override + public FullyUsedArray rotateLeft(FullyUsedArray array) { + return null; + } + + @Override + public void removeDuplicates(FullyUsedArray array) { + + } + + @Override + public FullyUsedArray invert(FullyUsedArray array) { + return null; + } + + @Override + public FullyUsedArray clone(FullyUsedArray array) { + return null; + } + + @Override + public FullyUsedArray alternate(FullyUsedArray a, FullyUsedArray b) { + return null; + } + + @Override + public FullyUsedArray merge(FullyUsedArray a, FullyUsedArray b, Comparator cmp) { + return null; + } + + @Override + public Pair, FullyUsedArray> divideAlternating(FullyUsedArray array) { + return null; + } + + @Override + public Pair, FullyUsedArray> divideAlternatingByRuns(FullyUsedArray array, Comparator cmp) { + return null; + } + + @Override + public Pair, FullyUsedArray> divideByPivot(FullyUsedArray array, T pivot, Comparator cmp) { + return null; + } + + @Override + public FullyUsedArray create(Iterable iterable) { + var size = countElement(iterable.iterator()); + var array = new FullyUsedArray(); + + array.theArray = Arrays.newArray(size); + copyElements(array.theArray, iterable.iterator(), 0); + + return array; + } + + private void copyElements(T[] array, Iterator iter, int index) { + if (index < array.length) { + array[index] = iter.next(); + copyElements(array, iter, index+1); + } + } + + private int countElement(Iterator iter) { + if (iter.hasNext()) { + iter.next(); + return 1 + countElement(iter); + } + return 0; + } +} diff --git a/test/aud/exam/prep/SequenceProcessorTest.java b/test/aud/exam/prep/SequenceProcessorTest.java index 9bc9e9a..fb2769a 100644 --- a/test/aud/exam/prep/SequenceProcessorTest.java +++ b/test/aud/exam/prep/SequenceProcessorTest.java @@ -512,7 +512,7 @@ public abstract class SequenceProcessorTest { @ParameterizedTest @ArgumentsSource(DoubleListProvider.class) - void testThat_cloneWorks(List listA, List listB) { + void testThat_alternateWorks(List listA, List listB) { S a = processor.create(listA); S b = processor.create(listB); diff --git a/test/aud/exam/prep/array/FullyUsedArrayProcessorTest.java b/test/aud/exam/prep/array/FullyUsedArrayProcessorTest.java deleted file mode 100644 index a86ead4..0000000 --- a/test/aud/exam/prep/array/FullyUsedArrayProcessorTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package aud.exam.prep.array; - -import aud.exam.prep.SequenceProcessorTest; - -public class FullyUsedArrayProcessorTest extends SequenceProcessorTest> { - - protected FullyUsedArrayProcessorTest() { - super(new FullyUsedArrayProcessor<>()); - } -} \ No newline at end of file diff --git a/test/aud/exam/prep/array/IterativeFullyUsedArrayProcessorTest.java b/test/aud/exam/prep/array/IterativeFullyUsedArrayProcessorTest.java new file mode 100644 index 0000000..c106de0 --- /dev/null +++ b/test/aud/exam/prep/array/IterativeFullyUsedArrayProcessorTest.java @@ -0,0 +1,10 @@ +package aud.exam.prep.array; + +import aud.exam.prep.SequenceProcessorTest; + +public class IterativeFullyUsedArrayProcessorTest extends SequenceProcessorTest> { + + protected IterativeFullyUsedArrayProcessorTest() { + super(new IterativeFullyUsedArrayProcessor<>()); + } +} \ No newline at end of file diff --git a/test/aud/exam/prep/array/RecursiveFullyUsedArrayProcessorTest.java b/test/aud/exam/prep/array/RecursiveFullyUsedArrayProcessorTest.java new file mode 100644 index 0000000..7d769a7 --- /dev/null +++ b/test/aud/exam/prep/array/RecursiveFullyUsedArrayProcessorTest.java @@ -0,0 +1,10 @@ +package aud.exam.prep.array; + +import aud.exam.prep.SequenceProcessorTest; + +class RecursiveFullyUsedArrayProcessorTest extends SequenceProcessorTest> { + + protected RecursiveFullyUsedArrayProcessorTest() { + super(new RecursiveFullyUsedArrayProcessor<>()); + } +} \ No newline at end of file