Implement merge

This commit is contained in:
2021-08-29 22:43:32 +02:00
parent 9dd65d203e
commit effe0a39f9
2 changed files with 43 additions and 1 deletions

View File

@ -389,7 +389,30 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse
@Override @Override
public FullyUsedArray<T> merge(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) { public FullyUsedArray<T> merge(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) {
throw new UnsupportedOperationException(); var newArray = new FullyUsedArray<T>();
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 @Override

View File

@ -480,6 +480,25 @@ public abstract class SequenceProcessorTest<S> {
S s = processor.alternate(a, b); S s = processor.alternate(a, b);
assertTrue(processor.check(s));
assertIterableEquals(list, processor.iterate(s)); assertIterableEquals(list, processor.iterate(s));
} }
@ParameterizedTest
@ArgumentsSource(DoubleListProvider.class)
void testThat_mergeWorks(List<Integer> listA, List<Integer> 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));
}
} }