From effe0a39f950011fdf55beafb217ae974961130b Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sun, 29 Aug 2021 22:43:32 +0200 Subject: [PATCH] Implement merge --- .../prep/array/FullyUsedArrayProcessor.java | 25 ++++++++++++++++++- test/aud/exam/prep/SequenceProcessorTest.java | 19 ++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java index e63b8d3..d6d7ddb 100644 --- a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java +++ b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java @@ -389,7 +389,30 @@ public class FullyUsedArrayProcessor implements SequenceProcessor merge(FullyUsedArray a, FullyUsedArray b, Comparator cmp) { - throw new UnsupportedOperationException(); + 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 diff --git a/test/aud/exam/prep/SequenceProcessorTest.java b/test/aud/exam/prep/SequenceProcessorTest.java index 74114d4..de0273a 100644 --- a/test/aud/exam/prep/SequenceProcessorTest.java +++ b/test/aud/exam/prep/SequenceProcessorTest.java @@ -480,6 +480,25 @@ public abstract class SequenceProcessorTest { S s = processor.alternate(a, b); + assertTrue(processor.check(s)); assertIterableEquals(list, processor.iterate(s)); } + + @ParameterizedTest + @ArgumentsSource(DoubleListProvider.class) + void testThat_mergeWorks(List listA, List listB) { + listA.sort(cmp); + S a = processor.create(listA); + + listB.sort(cmp); + S b = processor.create(listB); + + listA.addAll(listB); + listA.sort(cmp); + + S s = processor.merge(a, b, cmp); + + assertTrue(processor.check(s)); + assertIterableEquals(listA, processor.iterate(s)); + } } \ No newline at end of file