tryReduceLevel(); return (keyOnly)? key : new SnapshotEntry<K,V>(key, (V)v);
/** * Return SnapshotEntry or key for results of findNear ofter screening * to ensure result is in given range. Needed by submaps. * @param kkey the key * @param rel the relation -- OR'ed combination of EQ, LT, GT * @param least minimum allowed key value * @param fence key greater than maximum allowed key value * @param keyOnly if true return key, else return SnapshotEntry * @return Key or Entry fitting relation, or <tt>null</tt> if no such */ Object getNear(K kkey, int rel, K least, K fence, boolean keyOnly) { K key = kkey; // Don't return keys less than least if ((rel & LT) == 0) { if (compare(key, least) < 0) { key = least; rel = rel | EQ; } } for (;;) { Node<K,V> n = findNear(key, rel); if (n == null || !inHalfOpenRange(n.key, least, fence)) return null; K k = n.key; V v = n.getValidValue(); if (v != null) return keyOnly? k : new SnapshotEntry<K,V>(k, v); } }
/** * Remove first entry; return either its key or a snapshot. * @param keyOnly if true return key, else return SnapshotEntry * (This is a little ugly, but avoids code duplication.) * @return null if empty, first key if keyOnly true, else key,value entry */ Object doRemoveFirst(boolean keyOnly) { for (;;) { Node<K,V> b = head.node; Node<K,V> n = b.next; if (n == null) return null; Node<K,V> f = n.next; if (n != b.next) continue; Object v = n.value; if (v == null) { n.helpDelete(b, f); continue; } if (!n.casValue(v, null)) continue; if (!n.appendMarker(f) || !b.casNext(n, f)) findFirst(); // retry clearIndexToFirst(); K key = n.key; return (keyOnly)? key : new SnapshotEntry<K,V>(key, (V)v); } }
/** * Find and remove greatest element of subrange. * @param least minimum allowed key value * @param fence key greater than maximum allowed key value * @param keyOnly if true return key, else return SnapshotEntry * @return least Key or Entry, or <tt>null</tt> if no such */ Object removeLastEntryOfSubrange(K least, K fence, boolean keyOnly) { for (;;) { Node<K,V> n = findLower(fence); if (n == null) return null; K k = n.key; if (least != null && compare(k, least) < 0) return null; V v = doRemove(k, null); if (v != null) return (keyOnly)? k : new SnapshotEntry<K,V>(k, v); } }
/** * Find and remove least element of subrange. * @param least minimum allowed key value * @param fence key greater than maximum allowed key value * @param keyOnly if true return key, else return SnapshotEntry * @return least Key or Entry, or <tt>null</tt> if no such */ Object removeFirstEntryOfSubrange(K least, K fence, boolean keyOnly) { for (;;) { Node<K,V> n = findCeiling(least); if (n == null) return null; K k = n.key; if (fence != null && compare(k, fence) >= 0) return null; V v = doRemove(k, null); if (v != null) return (keyOnly)? k : new SnapshotEntry<K,V>(k, v); } }
/** * Create and return a new SnapshotEntry holding current * mapping if this node holds a valid value, else null * @return new entry or null */ SnapshotEntry<K,V> createSnapshot() { V v = getValidValue(); if (v == null) return null; return new SnapshotEntry(key, v); } }