blob: f063c65fce101f83ffa186177b8af4d9686bcfca (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.streamingvisitors;
import java.util.List;
import java.util.ListIterator;
import java.util.ArrayList;
/**
* A list merger that merges two sorted lists.
*
* @author Ulf Carlin
*/
public class ListMerger {
public static <T extends Comparable<? super T>> void mergeLinkedLists(List<T> to, List<T> from, int maxEntryCount) {
int entryCount = 0;
ListIterator<T> i = to.listIterator();
while (!from.isEmpty()) {
T fromElement = from.remove(0);
while (i.hasNext()) {
T toElement = i.next();
if (toElement.compareTo(fromElement) > 0) {
i.previous();
break;
} else {
entryCount++;
if (entryCount >= maxEntryCount) {
break;
}
}
}
if (entryCount >= maxEntryCount) {
break;
}
i.add(fromElement);
entryCount++;
if (entryCount >= maxEntryCount) {
break;
}
}
while (i.hasNext()) {
i.next();
i.remove();
}
}
public static <T extends Comparable<? super T>> List<T> mergeIntoArrayList(List<T> l1, List<T> l2, int maxEntryCount) {
List<T> mergedList = new ArrayList<>();
ListIterator<T> i1 = l1.listIterator();
ListIterator<T> i2 = l2.listIterator();
T e1 = null;
if (i1.hasNext()) {
e1 = i1.next();
}
T e2 = null;
if (i2.hasNext()) {
e2 = i2.next();
}
while (e1 != null && e2 != null && mergedList.size() < maxEntryCount) {
if (e1.compareTo(e2) <= 0) {
mergedList.add(e1);
if (i1.hasNext()) {
e1 = i1.next();
} else {
e1 = null;
}
} else {
mergedList.add(e2);
if (i2.hasNext()) {
e2 = i2.next();
} else {
e2 = null;
}
}
}
if (e2 == null) {
while (e1 != null && mergedList.size() < maxEntryCount) {
mergedList.add(e1);
if (i1.hasNext()) {
e1 = i1.next();
} else {
e1 = null;
}
}
} else if (e1 == null) {
while (e2 != null && mergedList.size() < maxEntryCount) {
mergedList.add(e2);
if (i2.hasNext()) {
e2 = i2.next();
} else {
e2 = null;
}
}
}
return mergedList;
}
public static <T extends Comparable<? super T>> List<T> mergeIntoArrayList(List<T> l1, List<T> l2) {
return mergeIntoArrayList(l1, l2, Integer.MAX_VALUE);
}
}
|