Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Jan 11, 2024
1 parent 9366cda commit 11541ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class VectorTile {
// TODO make these configurable
private static final int EXTENT = 4096;
private static final double SIZE = 256d;
// use a treemap to ensure that layers are encoded in a consistent order
private final Map<String, Layer> layers = new TreeMap<>();
private LayerAttrStats.Updater.ForZoom layerStatsTracker = LayerAttrStats.Updater.ForZoom.NOOP;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public static <T extends HasLongSortKey> Iterator<T> mergeIterators(List<? exten
};
}

private static <T extends HasLongSortKey> boolean lessThan(long ak, long bk, T a, T b, Comparator<T> tieBreaker) {
return ak < bk || (ak == bk && a != null && b != null && tieBreaker.compare(a, b) < 0);
}

private static class TwoWayMerge<T extends HasLongSortKey> implements Iterator<T> {

private final Comparator<T> tieBreaker;
Expand Down Expand Up @@ -67,7 +63,7 @@ public boolean hasNext() {
@Override
public T next() {
T result;
if (lessThan(ak, bk, a, b, tieBreaker)) {
if (lessThan(ak, bk, a, b)) {
result = a;
if (inputA.hasNext()) {
a = inputA.next();
Expand All @@ -90,6 +86,10 @@ public T next() {
}
return result;
}

private boolean lessThan(long ak, long bk, T a, T b) {
return ak < bk || (ak == bk && a != null && b != null && tieBreaker.compare(a, b) < 0);
}
}

private static class ThreeWayMerge<T extends HasLongSortKey> implements Iterator<T> {
Expand Down Expand Up @@ -127,8 +127,8 @@ public boolean hasNext() {
public T next() {
T result;
// use at most 2 comparisons to get the next item
if (lessThan(ak, bk, a, b, tieBreaker)) {
if (lessThan(ak, ck, a, c, tieBreaker)) {
if (lessThan(ak, bk, a, b)) {
if (lessThan(ak, ck, a, c)) {
// ACB / ABC
result = a;
if (inputA.hasNext()) {
Expand All @@ -149,7 +149,7 @@ public T next() {
ck = Long.MAX_VALUE;
}
}
} else if (lessThan(ck, bk, c, b, tieBreaker)) { /* (ck < bk) {*/
} else if (lessThan(ck, bk, c, b)) {
// CAB
result = c;
if (inputC.hasNext()) {
Expand All @@ -174,6 +174,21 @@ public T next() {
}
return result;
}

private boolean lessThan(long ak, long bk, T a, T b) {
return ak < bk || (ak == bk && lessThanCmp(a, b, tieBreaker));
}
}

private static <T> boolean lessThanCmp(T a, T b, Comparator<T> tieBreaker) {

Check warning on line 183 in planetiler-core/src/main/java/com/onthegomap/planetiler/collection/LongMerger.java

View workflow job for this annotation

GitHub Actions / Analyze with Sonar

MINOR CODE_SMELL

Move this method into "ThreeWayMerge". rule: java:S3398 (https://sonarcloud.io/organizations/onthegomap/rules?open=java%3AS3398&rule_key=java%3AS3398) issue url: https://sonarcloud.io/project/issues?pullRequest=785&open=AYz4kxdblkGXzLn6gFEp&id=onthegomap_planetiler
// nulls go at the end
if (a == null) {
return false;
} else if (b == null) {
return true;
} else {
return tieBreaker.compare(a, b) < 0;
}
}

private static class KWayMerge<T extends HasLongSortKey> implements Iterator<T> {
Expand All @@ -185,10 +200,7 @@ private static class KWayMerge<T extends HasLongSortKey> implements Iterator<T>
KWayMerge(List<? extends Iterator<T>> inputIterators, Comparator<T> tieBreaker) {
this.iterators = new Iterator[inputIterators.size()];
this.items = (T[]) new HasLongSortKey[inputIterators.size()];
final int size = inputIterators.size();
this.heap = LongMinHeap.newArrayHeap(inputIterators.size(), (a, b) -> {
return a >= size || b >= size ? 0 : tieBreaker.compare(items[a], items[b]);
});
this.heap = LongMinHeap.newArrayHeap(inputIterators.size(), (a, b) -> tieBreaker.compare(items[a], items[b]));
int outIdx = 0;
for (Iterator<T> iter : inputIterators) {
if (iter.hasNext()) {
Expand Down

0 comments on commit 11541ec

Please sign in to comment.