Implement merge
This commit is contained in:
@ -389,7 +389,30 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse
|
||||
|
||||
@Override
|
||||
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
|
||||
|
@ -480,6 +480,25 @@ public abstract class SequenceProcessorTest<S> {
|
||||
|
||||
S s = processor.alternate(a, b);
|
||||
|
||||
assertTrue(processor.check(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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user