/** *Returnsthismap'sentryforthegivenkey,or{@codenull}ifthemap *doesnotcontainanentryforthekey. * *@returnthismap'sentryforthegivenkey,or{@codenull}ifthemap *doesnotcontainanentryforthekey *@throwsClassCastExceptionifthespecifiedkeycannotbecompared *withthekeyscurrentlyinthemap *@throwsNullPointerExceptionifthespecifiedkeyisnull *andthismapusesnaturalordering,oritscomparator *doesnotpermitnullkeys
*/ final Entry<K,V> getEntry(Object key) { // Offload comparator-based version for sake of performance if (comparator != null) return getEntryUsingComparator(key);
Objects.requireNonNull(key);
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
Entry<K,V> p = root; while (p != null) { int cmp = k.compareTo(p.key); if (cmp < 0)
p = p.left; elseif (cmp > 0)
p = p.right; else return p;
} returnnull;
}
/** *VersionofgetEntryusingcomparator.SplitofffromgetEntry *forperformance.(Thisisnotworthdoingformostmethods, *thatarelessdependentoncomparatorperformance,butis *worthwhilehere.)
*/ final Entry<K,V> getEntryUsingComparator(Object key) {
@SuppressWarnings("unchecked")
K k = (K) key;
Comparator<? super K> cpr = comparator; if (cpr != null) {
Entry<K,V> p = root; while (p != null) { int cmp = cpr.compare(k, p.key); if (cmp < 0)
p = p.left; elseif (cmp > 0)
p = p.right; else return p;
}
} returnnull;
}
/** *Getstheentrycorrespondingtothespecifiedkey;ifnosuchentry *exists,returnstheentryfortheleastkeygreaterthanthespecified *key;ifnosuchentryexists(i.e.,thegreatestkeyintheTreeisless *thanthespecifiedkey),returns{@codenull}.
*/ final Entry<K,V> getCeilingEntry(K key) {
Entry<K,V> p = root; while (p != null) { int cmp = compare(key, p.key); if (cmp < 0) { if (p.left != null)
p = p.left; else return p;
} elseif (cmp > 0) { if (p.right != null) {
p = p.right;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p; while (parent != null && ch == parent.right) {
ch = parent;
parent = parent.parent;
} return parent;
}
} else return p;
} returnnull;
}
/** *Getstheentrycorrespondingtothespecifiedkey;ifnosuchentry *exists,returnstheentryforthegreatestkeylessthanthespecified *key;ifnosuchentryexists,returns{@codenull}.
*/ final Entry<K,V> getFloorEntry(K key) {
Entry<K,V> p = root; while (p != null) { int cmp = compare(key, p.key); if (cmp > 0) { if (p.right != null)
p = p.right; else return p;
} elseif (cmp < 0) { if (p.left != null) {
p = p.left;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p; while (parent != null && ch == parent.left) {
ch = parent;
parent = parent.parent;
} return parent;
}
} else return p;
} returnnull;
}
/** *Getstheentryfortheleastkeygreaterthanthespecified *key;ifnosuchentryexists,returnstheentryfortheleast *keygreaterthanthespecifiedkey;ifnosuchentryexists *returns{@codenull}.
*/ final Entry<K,V> getHigherEntry(K key) {
Entry<K,V> p = root; while (p != null) { int cmp = compare(key, p.key); if (cmp < 0) { if (p.left != null)
p = p.left; else return p;
} else { if (p.right != null) {
p = p.right;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p; while (parent != null && ch == parent.right) {
ch = parent;
parent = parent.parent;
} return parent;
}
}
} returnnull;
}
/** *Returnstheentryforthegreatestkeylessthanthespecifiedkey;if *nosuchentryexists(i.e.,theleastkeyintheTreeisgreaterthan *thespecifiedkey),returns{@codenull}.
*/ final Entry<K,V> getLowerEntry(K key) {
Entry<K,V> p = root; while (p != null) { int cmp = compare(key, p.key); if (cmp > 0) { if (p.right != null)
p = p.right; else return p;
} else { if (p.left != null) {
p = p.left;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p; while (parent != null && ch == parent.left) {
ch = parent;
parent = parent.parent;
} return parent;
}
}
} returnnull;
}
/** *Associatesthespecifiedvaluewiththespecifiedkeyinthismap. *Ifthemappreviouslycontainedamappingforthekey,theold *valueisreplaced. * *@paramkeykeywithwhichthespecifiedvalueistobeassociated *@paramvaluevaluetobeassociatedwiththespecifiedkey * *@returnthepreviousvalueassociatedwith{@codekey},or *{@codenull}iftherewasnomappingfor{@codekey}. *(A{@codenull}returncanalsoindicatethatthemap *previouslyassociated{@codenull}with{@codekey}.) *@throwsClassCastExceptionifthespecifiedkeycannotbecompared *withthekeyscurrentlyinthemap *@throwsNullPointerExceptionifthespecifiedkeyisnull *andthismapusesnaturalordering,oritscomparator *doesnotpermitnullkeys
*/ public V put(K key, V value) { return put(key, value, true);
}
@Override public V putIfAbsent(K key, V value) { return put(key, value, false);
}
/** *{@inheritDoc} * *<p>Thismethodwill,onabest-effortbasis,throwa *{@linkConcurrentModificationException}ifitisdetectedthatthe *mappingfunctionmodifiesthismapduringcomputation. * *@throwsConcurrentModificationExceptionifitisdetectedthatthe *mappingfunctionmodifiedthismap
*/
@Override public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V newValue;
Entry<K,V> t = root; if (t == null) {
newValue = callMappingFunctionWithCheck(key, mappingFunction); if (newValue != null) {
addEntryToEmptyMap(key, newValue); return newValue;
} else { returnnull;
}
} int cmp;
Entry<K,V> parent; // split comparator and comparable paths
Comparator<? super K> cpr = comparator; if (cpr != null) { do {
parent = t;
cmp = cpr.compare(key, t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; else { if (t.value == null) {
t.value = callMappingFunctionWithCheck(key, mappingFunction);
} return t.value;
}
} while (t != null);
} else {
Objects.requireNonNull(key);
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key; do {
parent = t;
cmp = k.compareTo(t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; else { if (t.value == null) {
t.value = callMappingFunctionWithCheck(key, mappingFunction);
} return t.value;
}
} while (t != null);
}
newValue = callMappingFunctionWithCheck(key, mappingFunction); if (newValue != null) {
addEntry(key, newValue, parent, cmp < 0); return newValue;
} returnnull;
}
/** *{@inheritDoc} * *<p>Thismethodwill,onabest-effortbasis,throwa *{@linkConcurrentModificationException}ifitisdetectedthatthe *remappingfunctionmodifiesthismapduringcomputation. * *@throwsConcurrentModificationExceptionifitisdetectedthatthe *remappingfunctionmodifiedthismap
*/
@Override public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<K,V> oldEntry = getEntry(key); if (oldEntry != null && oldEntry.value != null) { return remapValue(oldEntry, key, remappingFunction);
} else { returnnull;
}
}
/** *{@inheritDoc} * *<p>Thismethodwill,onabest-effortbasis,throwa *{@linkConcurrentModificationException}ifitisdetectedthatthe *remappingfunctionmodifiesthismapduringcomputation. * *@throwsConcurrentModificationExceptionifitisdetectedthatthe *remappingfunctionmodifiedthismap
*/
@Override public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V newValue;
Entry<K,V> t = root; if (t == null) {
newValue = callRemappingFunctionWithCheck(key, null, remappingFunction); if (newValue != null) {
addEntryToEmptyMap(key, newValue); return newValue;
} else { returnnull;
}
} int cmp;
Entry<K,V> parent; // split comparator and comparable paths
Comparator<? super K> cpr = comparator; if (cpr != null) { do {
parent = t;
cmp = cpr.compare(key, t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; else return remapValue(t, key, remappingFunction);
} while (t != null);
} else {
Objects.requireNonNull(key);
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key; do {
parent = t;
cmp = k.compareTo(t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; else return remapValue(t, key, remappingFunction);
} while (t != null);
}
newValue = callRemappingFunctionWithCheck(key, null, remappingFunction); if (newValue != null) {
addEntry(key, newValue, parent, cmp < 0); return newValue;
} returnnull;
}
/** *{@inheritDoc} * *<p>Thismethodwill,onabest-effortbasis,throwa *{@linkConcurrentModificationException}ifitisdetectedthatthe *remappingfunctionmodifiesthismapduringcomputation. * *@throwsConcurrentModificationExceptionifitisdetectedthatthe *remappingfunctionmodifiedthismap
*/
@Override public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
Entry<K,V> t = root; if (t == null) {
addEntryToEmptyMap(key, value); return value;
} int cmp;
Entry<K,V> parent; // split comparator and comparable paths
Comparator<? super K> cpr = comparator; if (cpr != null) { do {
parent = t;
cmp = cpr.compare(key, t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; elsereturn mergeValue(t, value, remappingFunction);
} while (t != null);
} else {
Objects.requireNonNull(key);
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key; do {
parent = t;
cmp = k.compareTo(t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; elsereturn mergeValue(t, value, remappingFunction);
} while (t != null);
}
addEntry(key, value, parent, cmp < 0); return value;
}
private V callMappingFunctionWithCheck(K key, Function<? super K, ? extends V> mappingFunction) { int mc = modCount;
V newValue = mappingFunction.apply(key); if (mc != modCount) { thrownew ConcurrentModificationException();
} return newValue;
}
private V callRemappingFunctionWithCheck(K key, V oldValue, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { int mc = modCount;
V newValue = remappingFunction.apply(key, oldValue); if (mc != modCount) { thrownew ConcurrentModificationException();
} return newValue;
}
privatevoid addEntry(K key, V value, Entry<K, V> parent, boolean addToLeft) {
Entry<K,V> e = new Entry<>(key, value, parent); if (addToLeft)
parent.left = e; else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
}
privatevoid addEntryToEmptyMap(K key, V value) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);
size = 1;
modCount++;
}
private V put(K key, V value, boolean replaceOld) {
Entry<K,V> t = root; if (t == null) {
addEntryToEmptyMap(key, value); returnnull;
} int cmp;
Entry<K,V> parent; // split comparator and comparable paths
Comparator<? super K> cpr = comparator; if (cpr != null) { do {
parent = t;
cmp = cpr.compare(key, t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; else {
V oldValue = t.value; if (replaceOld || oldValue == null) {
t.value = value;
} return oldValue;
}
} while (t != null);
} else {
Objects.requireNonNull(key);
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key; do {
parent = t;
cmp = k.compareTo(t.key); if (cmp < 0)
t = t.left; elseif (cmp > 0)
t = t.right; else {
V oldValue = t.value; if (replaceOld || oldValue == null) {
t.value = value;
} return oldValue;
}
} while (t != null);
}
addEntry(key, value, parent, cmp < 0); returnnull;
}
private V remapValue(Entry<K,V> t, K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
V newValue = callRemappingFunctionWithCheck(key, t.value, remappingFunction); if (newValue == null) {
deleteEntry(t); returnnull;
} else { // replace old mapping
t.value = newValue; return newValue;
}
}
private V mergeValue(Entry<K,V> t, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
V oldValue = t.value;
V newValue; if (t.value == null) {
newValue = value;
} else { int mc = modCount;
newValue = remappingFunction.apply(oldValue, value); if (mc != modCount) { thrownew ConcurrentModificationException();
}
} if (newValue == null) {
deleteEntry(t); returnnull;
} else { // replace old mapping
t.value = newValue; return newValue;
}
}
/** *RemovesthemappingforthiskeyfromthisTreeMapifpresent. * *@paramkeykeyforwhichmappingshouldberemoved *@returnthepreviousvalueassociatedwith{@codekey},or *{@codenull}iftherewasnomappingfor{@codekey}. *(A{@codenull}returncanalsoindicatethatthemap *previouslyassociated{@codenull}with{@codekey}.) *@throwsClassCastExceptionifthespecifiedkeycannotbecompared *withthekeyscurrentlyinthemap *@throwsNullPointerExceptionifthespecifiedkeyisnull *andthismapusesnaturalordering,oritscomparator *doesnotpermitnullkeys
*/ public V remove(Object key) {
Entry<K,V> p = getEntry(key); if (p == null) returnnull;
V oldValue = p.value;
deleteEntry(p); return oldValue;
}
@Override publicboolean replace(K key, V oldValue, V newValue) {
Entry<K,V> p = getEntry(key); if (p!=null && Objects.equals(oldValue, p.value)) {
p.value = newValue; returntrue;
} returnfalse;
}
@Override public V replace(K key, V value) {
Entry<K,V> p = getEntry(key); if (p!=null) {
V oldValue = p.value;
p.value = value; return oldValue;
} returnnull;
}
@Override publicvoid forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action); int expectedModCount = modCount; for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
action.accept(e.key, e.value);
if (expectedModCount != modCount) { thrownew ConcurrentModificationException();
}
}
}
@Override publicvoid replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function); int expectedModCount = modCount;
for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
e.value = function.apply(e.key, e.value);
if (expectedModCount != modCount) { thrownew ConcurrentModificationException();
}
}
}
// View class support
class Values extends AbstractCollection<V> { public Iterator<V> iterator() { returnnew ValueIterator(getFirstEntry());
}
publicfinalboolean hasNext() { return next != null;
}
final Entry<K,V> nextEntry() {
Entry<K,V> e = next; if (e == null) thrownew NoSuchElementException(); if (modCount != expectedModCount) thrownew ConcurrentModificationException();
next = successor(e);
lastReturned = e; return e;
}
final Entry<K,V> prevEntry() {
Entry<K,V> e = next; if (e == null) thrownew NoSuchElementException(); if (modCount != expectedModCount) thrownew ConcurrentModificationException();
next = predecessor(e);
lastReturned = e; return e;
}
publicvoid remove() { if (lastReturned == null) thrownew IllegalStateException(); if (modCount != expectedModCount) thrownew ConcurrentModificationException(); // deleted entries are replaced by their successors if (lastReturned.left != null && lastReturned.right != null)
next = lastReturned;
deleteEntry(lastReturned);
expectedModCount = modCount;
lastReturned = null;
}
}
publicfinal V put(K key, V value) { if (!inRange(key)) thrownew IllegalArgumentException("key out of range"); return m.put(key, value);
}
public V putIfAbsent(K key, V value) { if (!inRange(key)) thrownew IllegalArgumentException("key out of range"); return m.putIfAbsent(key, value);
}
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { if (!inRange(key)) thrownew IllegalArgumentException("key out of range"); return m.merge(key, value, remappingFunction);
}
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { if (!inRange(key)) { // Do not throw if mapping function returns null // to preserve compatibility with default computeIfAbsent implementation if (mappingFunction.apply(key) == null) returnnull; thrownew IllegalArgumentException("key out of range");
} return m.computeIfAbsent(key, mappingFunction);
}
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { if (!inRange(key)) { // Do not throw if remapping function returns null // to preserve compatibility with default computeIfAbsent implementation if (remappingFunction.apply(key, null) == null) returnnull; thrownew IllegalArgumentException("key out of range");
} return m.compute(key, remappingFunction);
}
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { return !inRange(key) ? null : m.computeIfPresent(key, remappingFunction);
}
publicfinal Map.Entry<K,V> pollFirstEntry() {
TreeMap.Entry<K,V> e = subLowest();
Map.Entry<K,V> result = exportEntry(e); if (e != null)
m.deleteEntry(e); return result;
}
publicfinal Map.Entry<K,V> pollLastEntry() {
TreeMap.Entry<K,V> e = subHighest();
Map.Entry<K,V> result = exportEntry(e); if (e != null)
m.deleteEntry(e); return result;
}
AscendingSubMap(TreeMap<K,V> m, boolean fromStart, K lo, boolean loInclusive, boolean toEnd, K hi, boolean hiInclusive) { super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
}
public Comparator<? super K> comparator() { return m.comparator();
}
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive) { if (!inRange(fromKey, fromInclusive)) thrownew IllegalArgumentException("fromKey out of range"); if (!inRange(toKey, toInclusive)) thrownew IllegalArgumentException("toKey out of range"); returnnew AscendingSubMap<>(m, false, fromKey, fromInclusive, false, toKey, toInclusive);
}
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) { if (!inRange(toKey, inclusive)) thrownew IllegalArgumentException("toKey out of range"); returnnew AscendingSubMap<>(m,
fromStart, lo, loInclusive, false, toKey, inclusive);
}
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) { if (!inRange(fromKey, inclusive)) thrownew IllegalArgumentException("fromKey out of range"); returnnew AscendingSubMap<>(m, false, fromKey, inclusive,
toEnd, hi, hiInclusive);
}
public Comparator<? super K> comparator() { return reverseComparator;
}
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive) { if (!inRange(fromKey, fromInclusive)) thrownew IllegalArgumentException("fromKey out of range"); if (!inRange(toKey, toInclusive)) thrownew IllegalArgumentException("toKey out of range"); returnnew DescendingSubMap<>(m, false, toKey, toInclusive, false, fromKey, fromInclusive);
}
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) { if (!inRange(toKey, inclusive)) thrownew IllegalArgumentException("toKey out of range"); returnnew DescendingSubMap<>(m, false, toKey, inclusive,
toEnd, hi, hiInclusive);
}
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) { if (!inRange(fromKey, inclusive)) thrownew IllegalArgumentException("fromKey out of range"); returnnew DescendingSubMap<>(m,
fromStart, lo, loInclusive, false, fromKey, inclusive);
}
// If strictly internal, copy successor's element to p and then make p // point to successor. if (p.left != null && p.right != null) {
Entry<K,V> s = successor(p);
p.key = s.key;
p.value = s.value;
p = s;
} // p has 2 children
// Start fixup at replacement node, if it exists.
Entry<K,V> replacement = (p.left != null ? p.left : p.right);
if (replacement != null) { // Link replacement to parent
replacement.parent = p.parent; if (p.parent == null)
root = replacement; elseif (p == p.parent.left)
p.parent.left = replacement; else
p.parent.right = replacement;
// Null out links so they are OK to use by fixAfterDeletion.
p.left = p.right = p.parent = null;
// Fix replacement if (p.color == BLACK)
fixAfterDeletion(replacement);
} elseif (p.parent == null) { // return if we are the only node.
root = null;
} else { // No children. Use self as phantom replacement and unlink. if (p.color == BLACK)
fixAfterDeletion(p);
/** *Savethestateofthe{@codeTreeMap}instancetoastream(i.e., *serializeit). * *@serialDataThe<em>size</em>oftheTreeMap(thenumberofkey-value *mappings)isemitted(int),followedbythekey(Object) *andvalue(Object)foreachkey-valuemappingrepresented *bytheTreeMap.Thekey-valuemappingsareemittedin *key-order(asdeterminedbytheTreeMap'sComparator, *orbythekeys'naturalorderingiftheTreeMaphasno *Comparator).
*/
@java.io.Serial privatevoid writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out the Comparator and any hidden stuff
s.defaultWriteObject();
// Write out size (number of Mappings)
s.writeInt(size);
// Write out keys and values (alternating) for (Map.Entry<K, V> e : entrySet()) {
s.writeObject(e.getKey());
s.writeObject(e.getValue());
}
}
/** *Reconstitutethe{@codeTreeMap}instancefromastream(i.e., *deserializeit).
*/
@java.io.Serial privatevoid readObject(final java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in the Comparator and any hidden stuff
s.defaultReadObject();
// Read in size int size = s.readInt();
buildFromSorted(size, null, s, null);
}
/** Intended to be called only from TreeSet.readObject */ void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal) throws java.io.IOException, ClassNotFoundException {
buildFromSorted(size, null, s, defaultVal);
}
/** Intended to be called only from TreeSet.addAll */ void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) { try {
buildFromSorted(set.size(), set.iterator(), null, defaultVal);
} catch (java.io.IOException | ClassNotFoundException cannotHappen) {
}
}
/** *Baseclassforspliterators.Iterationstartsatagiven *originandcontinuesuptobutnotincludingagivenfence(or *nullforend).Attop-level,forascendingcases,thefirst *splitusestherootasleft-fence/right-origin.Fromthere, *right-handsplitsreplacethecurrentfencewithitsleft *child,alsoservingasoriginforthesplit-offspliterator. *Left-handsaresymmetric.Descendingversionsplacetheorigin *attheendandinvertascendingsplitrules.Thisbaseclass *isnon-committalaboutdirectionality,orwhetherthetop-level *spliteratorcoversthewholetree.Thismeansthattheactual *splitmechanicsarelocatedinsubclasses.Someofthesubclass *trySplitmethodsareidentical(exceptforreturntypes),but *notnicelyfactorable. * *Currently,subclassversionsexistonlyforthefullmap *(includingdescendingkeysviaitsdescendingMap).Othersare *possiblebutcurrentlynotworthwhilebecausesubmapsrequire *O(n)computationstodeterminesize,whichsubstantiallylimits *potentialspeed-upsofusingcustomSpliteratorsversusdefault *mechanics. * *Tobootstrapinitialization,externalconstructorsuse *negativesizeestimates:-1forascend,-2fordescend.
*/ staticclass TreeMapSpliterator<K,V> { final TreeMap<K,V> tree;
TreeMap.Entry<K,V> current; // traverser; initially first node in range
TreeMap.Entry<K,V> fence; // one past last, or null int side; // 0: top, -1: is a left split, +1: right int est; // size estimate (exact only for top-level) int expectedModCount; // for CME checks
TreeMapSpliterator(TreeMap<K,V> tree,
TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence, int side, int est, int expectedModCount) { this.tree = tree; this.current = origin; this.fence = fence; this.side = side; this.est = est; this.expectedModCount = expectedModCount;
}
finalint getEstimate() { // force initialization int s; TreeMap<K,V> t; if ((s = est) < 0) { if ((t = tree) != null) {
current = (s == -1) ? t.getFirstEntry() : t.getLastEntry();
s = est = t.size;
expectedModCount = t.modCount;
} else
s = est = 0;
} return s;
}
staticfinalclass KeySpliterator<K,V> extends TreeMapSpliterator<K,V> implements Spliterator<K> {
KeySpliterator(TreeMap<K,V> tree,
TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence, int side, int est, int expectedModCount) { super(tree, origin, fence, side, est, expectedModCount);
}
public KeySpliterator<K,V> trySplit() { if (est < 0)
getEstimate(); // force initialization int d = side;
TreeMap.Entry<K,V> e = current, f = fence,
s = ((e == null || e == f) ? null : // empty
(d == 0) ? tree.root : // was top
(d > 0) ? e.right : // was right
(d < 0 && f != null) ? f.left : // was left null); if (s != null && s != e && s != f &&
tree.compare(e.key, s.key) < 0) { // e not already past s
side = 1; returnnew KeySpliterator<>
(tree, e, current = s, -1, est >>>= 1, expectedModCount);
} returnnull;
}
publicvoid forEachRemaining(Consumer<? super K> action) { if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization
TreeMap.Entry<K,V> f = fence, e, p, pl; if ((e = current) != null && e != f) {
current = f; // exhaust do {
action.accept(e.key); if ((p = e.right) != null) { while ((pl = p.left) != null)
p = pl;
} else { while ((p = e.parent) != null && e == p.right)
e = p;
}
} while ((e = p) != null && e != f); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException();
}
}
publicboolean tryAdvance(Consumer<? super K> action) {
TreeMap.Entry<K,V> e; if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization if ((e = current) == null || e == fence) returnfalse;
current = successor(e);
action.accept(e.key); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException(); returntrue;
}
publicfinal Comparator<? super K> getComparator() { return tree.comparator;
}
}
staticfinalclass DescendingKeySpliterator<K,V> extends TreeMapSpliterator<K,V> implements Spliterator<K> {
DescendingKeySpliterator(TreeMap<K,V> tree,
TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence, int side, int est, int expectedModCount) { super(tree, origin, fence, side, est, expectedModCount);
}
public DescendingKeySpliterator<K,V> trySplit() { if (est < 0)
getEstimate(); // force initialization int d = side;
TreeMap.Entry<K,V> e = current, f = fence,
s = ((e == null || e == f) ? null : // empty
(d == 0) ? tree.root : // was top
(d < 0) ? e.left : // was left
(d > 0 && f != null) ? f.right : // was right null); if (s != null && s != e && s != f &&
tree.compare(e.key, s.key) > 0) { // e not already past s
side = 1; returnnew DescendingKeySpliterator<>
(tree, e, current = s, -1, est >>>= 1, expectedModCount);
} returnnull;
}
publicvoid forEachRemaining(Consumer<? super K> action) { if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization
TreeMap.Entry<K,V> f = fence, e, p, pr; if ((e = current) != null && e != f) {
current = f; // exhaust do {
action.accept(e.key); if ((p = e.left) != null) { while ((pr = p.right) != null)
p = pr;
} else { while ((p = e.parent) != null && e == p.left)
e = p;
}
} while ((e = p) != null && e != f); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException();
}
}
publicboolean tryAdvance(Consumer<? super K> action) {
TreeMap.Entry<K,V> e; if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization if ((e = current) == null || e == fence) returnfalse;
current = predecessor(e);
action.accept(e.key); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException(); returntrue;
}
staticfinalclass ValueSpliterator<K,V> extends TreeMapSpliterator<K,V> implements Spliterator<V> {
ValueSpliterator(TreeMap<K,V> tree,
TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence, int side, int est, int expectedModCount) { super(tree, origin, fence, side, est, expectedModCount);
}
public ValueSpliterator<K,V> trySplit() { if (est < 0)
getEstimate(); // force initialization int d = side;
TreeMap.Entry<K,V> e = current, f = fence,
s = ((e == null || e == f) ? null : // empty
(d == 0) ? tree.root : // was top
(d > 0) ? e.right : // was right
(d < 0 && f != null) ? f.left : // was left null); if (s != null && s != e && s != f &&
tree.compare(e.key, s.key) < 0) { // e not already past s
side = 1; returnnew ValueSpliterator<>
(tree, e, current = s, -1, est >>>= 1, expectedModCount);
} returnnull;
}
publicvoid forEachRemaining(Consumer<? super V> action) { if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization
TreeMap.Entry<K,V> f = fence, e, p, pl; if ((e = current) != null && e != f) {
current = f; // exhaust do {
action.accept(e.value); if ((p = e.right) != null) { while ((pl = p.left) != null)
p = pl;
} else { while ((p = e.parent) != null && e == p.right)
e = p;
}
} while ((e = p) != null && e != f); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException();
}
}
publicboolean tryAdvance(Consumer<? super V> action) {
TreeMap.Entry<K,V> e; if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization if ((e = current) == null || e == fence) returnfalse;
current = successor(e);
action.accept(e.value); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException(); returntrue;
}
staticfinalclass EntrySpliterator<K,V> extends TreeMapSpliterator<K,V> implements Spliterator<Map.Entry<K,V>> {
EntrySpliterator(TreeMap<K,V> tree,
TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence, int side, int est, int expectedModCount) { super(tree, origin, fence, side, est, expectedModCount);
}
public EntrySpliterator<K,V> trySplit() { if (est < 0)
getEstimate(); // force initialization int d = side;
TreeMap.Entry<K,V> e = current, f = fence,
s = ((e == null || e == f) ? null : // empty
(d == 0) ? tree.root : // was top
(d > 0) ? e.right : // was right
(d < 0 && f != null) ? f.left : // was left null); if (s != null && s != e && s != f &&
tree.compare(e.key, s.key) < 0) { // e not already past s
side = 1; returnnew EntrySpliterator<>
(tree, e, current = s, -1, est >>>= 1, expectedModCount);
} returnnull;
}
publicvoid forEachRemaining(Consumer<? super Map.Entry<K, V>> action) { if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization
TreeMap.Entry<K,V> f = fence, e, p, pl; if ((e = current) != null && e != f) {
current = f; // exhaust do {
action.accept(e); if ((p = e.right) != null) { while ((pl = p.left) != null)
p = pl;
} else { while ((p = e.parent) != null && e == p.right)
e = p;
}
} while ((e = p) != null && e != f); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException();
}
}
publicboolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
TreeMap.Entry<K,V> e; if (action == null) thrownew NullPointerException(); if (est < 0)
getEstimate(); // force initialization if ((e = current) == null || e == fence) returnfalse;
current = successor(e);
action.accept(e); if (tree.modCount != expectedModCount) thrownew ConcurrentModificationException(); returntrue;
}
@Override public Comparator<Map.Entry<K, V>> getComparator() { // Adapt or create a key-based comparator if (tree.comparator != null) { return Map.Entry.comparingByKey(tree.comparator);
} else { return (Comparator<Map.Entry<K, V>> & Serializable) (e1, e2) -> {
@SuppressWarnings("unchecked")
Comparable<? super K> k1 = (Comparable<? super K>) e1.getKey(); return k1.compareTo(e2.getKey());
};
}
}
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.123Bemerkung:
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.