From 20d0bfbb348674a65b6c84826b549ab22ff22c13 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Fri, 8 Nov 2024 18:18:21 +0100 Subject: [PATCH] ssh: Minor simplification in SerialRangeSet Instead of set.headMap(x).lastKey() use set.floorKey(x) and instead of set.tailMap(x).firstKey() use set.ceilingKey(x). Change-Id: I22f44cbe82b9ead06d6ff517d609dfdbc89a758c --- .../internal/signing/ssh/SerialRangeSet.java | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/signing/ssh/SerialRangeSet.java b/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/signing/ssh/SerialRangeSet.java index 6a0cec8821..f4eb884239 100644 --- a/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/signing/ssh/SerialRangeSet.java +++ b/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/signing/ssh/SerialRangeSet.java @@ -9,7 +9,6 @@ */ package org.eclipse.jgit.internal.signing.ssh; -import java.util.SortedMap; import java.util.TreeMap; import org.eclipse.jgit.internal.transport.sshd.SshdText; @@ -50,14 +49,14 @@ class SerialRangeSet { } } - // We use the same data structure as OpenSSH,; basically a - // TreeSet of mutable elements. To get "mutability", the set is - // implemented as a TreeMap with the same elements as keys and values. + // We use the same data structure as OpenSSH; basically a TreeSet of mutable + // SerialRanges. To get "mutability", the set is implemented as a TreeMap + // with the same elements as keys and values. // // get(x) will return null if none of the serial numbers in the range x is // in the set, and some range (partially) overlapping with x otherwise. // - // containsKey will return true if there is any (partially) overlapping + // containsKey(x) will return true if there is any (partially) overlapping // range in the TreeMap. private final TreeMap ranges = new TreeMap<>( SerialRangeSet::compare); @@ -117,21 +116,15 @@ class SerialRangeSet { } // No overlapping range exists: check for coalescing with the // previous/next range - SortedMap head = ranges.headMap(newRange); - if (!head.isEmpty()) { - SerialRange prev = head.lastKey(); - if (newRange.from() - prev.to() == 1) { - ranges.remove(prev); - newRange = new Range(prev.from(), newRange.to()); - } + SerialRange prev = ranges.floorKey(newRange); + if (prev != null && newRange.from() - prev.to() == 1) { + ranges.remove(prev); + newRange = new Range(prev.from(), newRange.to()); } - SortedMap tail = ranges.tailMap(newRange); - if (!tail.isEmpty()) { - SerialRange next = tail.firstKey(); - if (next.from() - newRange.to() == 1) { - ranges.remove(next); - newRange = new Range(newRange.from(), next.to()); - } + SerialRange next = ranges.ceilingKey(newRange); + if (next != null && next.from() - newRange.to() == 1) { + ranges.remove(next); + newRange = new Range(newRange.from(), next.to()); } ranges.put(newRange, newRange); } -- 2.39.5