476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* Create and return a new SnapshotEntry holding current |
479 |
> |
* Create and return a new SimpleImmutableEntry holding current |
480 |
|
* mapping if this node holds a valid value, else null |
481 |
|
* @return new entry or null |
482 |
|
*/ |
483 |
< |
SnapshotEntry<K,V> createSnapshot() { |
483 |
> |
AbstractMap.SimpleImmutableEntry<K,V> createSnapshot() { |
484 |
|
V v = getValidValue(); |
485 |
|
if (v == null) |
486 |
|
return null; |
487 |
< |
return new SnapshotEntry(key, v); |
487 |
> |
return new AbstractMap.SimpleImmutableEntry(key, v); |
488 |
|
} |
489 |
|
} |
490 |
|
|
1220 |
|
|
1221 |
|
/** |
1222 |
|
* Remove first entry; return either its key or a snapshot. |
1223 |
< |
* @param keyOnly if true return key, else return SnapshotEntry |
1223 |
> |
* @param keyOnly if true return key, else return SimpleImmutableEntry |
1224 |
|
* (This is a little ugly, but avoids code duplication.) |
1225 |
|
* @return null if empty, first key if keyOnly true, else key,value entry |
1226 |
|
*/ |
1244 |
|
findFirst(); // retry |
1245 |
|
clearIndexToFirst(); |
1246 |
|
K key = n.key; |
1247 |
< |
return (keyOnly)? key : new SnapshotEntry<K,V>(key, (V)v); |
1247 |
> |
if (keyOnly) |
1248 |
> |
return key; |
1249 |
> |
else |
1250 |
> |
return new AbstractMap.SimpleImmutableEntry<K,V>(key, (V)v); |
1251 |
|
} |
1252 |
|
} |
1253 |
|
|
1329 |
|
|
1330 |
|
/** |
1331 |
|
* Specialized version of doRemove for last entry. |
1332 |
< |
* @param keyOnly if true return key, else return SnapshotEntry |
1332 |
> |
* @param keyOnly if true return key, else return SimpleImmutableEntry |
1333 |
|
* @return null if empty, last key if keyOnly true, else key,value entry |
1334 |
|
*/ |
1335 |
|
Object doRemoveLast(boolean keyOnly) { |
1369 |
|
if (head.right == null) |
1370 |
|
tryReduceLevel(); |
1371 |
|
} |
1372 |
< |
return (keyOnly)? key : new SnapshotEntry<K,V>(key, (V)v); |
1372 |
> |
if (keyOnly) |
1373 |
> |
return key; |
1374 |
> |
else |
1375 |
> |
return new AbstractMap.SimpleImmutableEntry<K,V>(key, (V)v); |
1376 |
|
} |
1377 |
|
} |
1378 |
|
} |
1460 |
|
} |
1461 |
|
|
1462 |
|
/** |
1463 |
< |
* Return SnapshotEntry for results of findNear. |
1463 |
> |
* Return SimpleImmutableEntry for results of findNear. |
1464 |
|
* @param kkey the key |
1465 |
|
* @param rel the relation -- OR'ed combination of EQ, LT, GT |
1466 |
|
* @return Entry fitting relation, or null if no such |
1467 |
|
*/ |
1468 |
< |
SnapshotEntry<K,V> getNear(K kkey, int rel) { |
1468 |
> |
AbstractMap.SimpleImmutableEntry<K,V> getNear(K kkey, int rel) { |
1469 |
|
for (;;) { |
1470 |
|
Node<K,V> n = findNear(kkey, rel); |
1471 |
|
if (n == null) |
1472 |
|
return null; |
1473 |
< |
SnapshotEntry<K,V> e = n.createSnapshot(); |
1473 |
> |
AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); |
1474 |
|
if (e != null) |
1475 |
|
return e; |
1476 |
|
} |
1491 |
|
} |
1492 |
|
|
1493 |
|
/** |
1494 |
< |
* Return SnapshotEntry or key for results of findNear ofter screening |
1495 |
< |
* to ensure result is in given range. Needed by submaps. |
1494 |
> |
* Return SimpleImmutableEntry or key for results of findNear |
1495 |
> |
* ofter screening to ensure result is in given range. Needed by |
1496 |
> |
* submaps. |
1497 |
|
* @param kkey the key |
1498 |
|
* @param rel the relation -- OR'ed combination of EQ, LT, GT |
1499 |
|
* @param least minimum allowed key value |
1500 |
|
* @param fence key greater than maximum allowed key value |
1501 |
< |
* @param keyOnly if true return key, else return SnapshotEntry |
1501 |
> |
* @param keyOnly if true return key, else return SimpleImmutableEntry |
1502 |
|
* @return Key or Entry fitting relation, or <tt>null</tt> if no such |
1503 |
|
*/ |
1504 |
|
Object getNear(K kkey, int rel, K least, K fence, boolean keyOnly) { |
1517 |
|
return null; |
1518 |
|
K k = n.key; |
1519 |
|
V v = n.getValidValue(); |
1520 |
< |
if (v != null) |
1521 |
< |
return keyOnly? k : new SnapshotEntry<K,V>(k, v); |
1520 |
> |
if (v != null) { |
1521 |
> |
if (keyOnly) |
1522 |
> |
return k; |
1523 |
> |
else |
1524 |
> |
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); |
1525 |
> |
} |
1526 |
|
} |
1527 |
|
} |
1528 |
|
|
1530 |
|
* Find and remove least element of subrange. |
1531 |
|
* @param least minimum allowed key value |
1532 |
|
* @param fence key greater than maximum allowed key value |
1533 |
< |
* @param keyOnly if true return key, else return SnapshotEntry |
1533 |
> |
* @param keyOnly if true return key, else return SimpleImmutableEntry |
1534 |
|
* @return least Key or Entry, or <tt>null</tt> if no such |
1535 |
|
*/ |
1536 |
|
Object removeFirstEntryOfSubrange(K least, K fence, boolean keyOnly) { |
1542 |
|
if (fence != null && compare(k, fence) >= 0) |
1543 |
|
return null; |
1544 |
|
V v = doRemove(k, null); |
1545 |
< |
if (v != null) |
1546 |
< |
return (keyOnly)? k : new SnapshotEntry<K,V>(k, v); |
1545 |
> |
if (v != null) { |
1546 |
> |
if (keyOnly) |
1547 |
> |
return k; |
1548 |
> |
else |
1549 |
> |
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); |
1550 |
> |
} |
1551 |
|
} |
1552 |
|
} |
1553 |
|
|
1555 |
|
* Find and remove greatest element of subrange. |
1556 |
|
* @param least minimum allowed key value |
1557 |
|
* @param fence key greater than maximum allowed key value |
1558 |
< |
* @param keyOnly if true return key, else return SnapshotEntry |
1558 |
> |
* @param keyOnly if true return key, else return SimpleImmutableEntry |
1559 |
|
* @return least Key or Entry, or <tt>null</tt> if no such |
1560 |
|
*/ |
1561 |
|
Object removeLastEntryOfSubrange(K least, K fence, boolean keyOnly) { |
1567 |
|
if (least != null && compare(k, least) < 0) |
1568 |
|
return null; |
1569 |
|
V v = doRemove(k, null); |
1570 |
< |
if (v != null) |
1571 |
< |
return (keyOnly)? k : new SnapshotEntry<K,V>(k, v); |
1570 |
> |
if (v != null) { |
1571 |
> |
if (keyOnly) |
1572 |
> |
return k; |
1573 |
> |
else |
1574 |
> |
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); |
1575 |
> |
} |
1576 |
|
} |
1577 |
|
} |
1578 |
|
|
2477 |
|
Node<K,V> n = findFirst(); |
2478 |
|
if (n == null) |
2479 |
|
return null; |
2480 |
< |
SnapshotEntry<K,V> e = n.createSnapshot(); |
2480 |
> |
AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); |
2481 |
|
if (e != null) |
2482 |
|
return e; |
2483 |
|
} |
2497 |
|
Node<K,V> n = findLast(); |
2498 |
|
if (n == null) |
2499 |
|
return null; |
2500 |
< |
SnapshotEntry<K,V> e = n.createSnapshot(); |
2500 |
> |
AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); |
2501 |
|
if (e != null) |
2502 |
|
return e; |
2503 |
|
} |
2513 |
|
* if the map is empty. |
2514 |
|
*/ |
2515 |
|
public Map.Entry<K,V> pollFirstEntry() { |
2516 |
< |
return (SnapshotEntry<K,V>)doRemoveFirst(false); |
2516 |
> |
return (AbstractMap.SimpleImmutableEntry<K,V>)doRemoveFirst(false); |
2517 |
|
} |
2518 |
|
|
2519 |
|
/** |
2526 |
|
* if the map is empty. |
2527 |
|
*/ |
2528 |
|
public Map.Entry<K,V> pollLastEntry() { |
2529 |
< |
return (SnapshotEntry<K,V>)doRemoveLast(false); |
2529 |
> |
return (AbstractMap.SimpleImmutableEntry<K,V>)doRemoveLast(false); |
2530 |
|
} |
2531 |
|
|
2532 |
|
|
3018 |
|
public Object[] toArray() { |
3019 |
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(); |
3020 |
|
for (Map.Entry e : this) |
3021 |
< |
c.add(new SnapshotEntry(e.getKey(), e.getValue())); |
3021 |
> |
c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue())); |
3022 |
|
return c.toArray(); |
3023 |
|
} |
3024 |
|
public <T> T[] toArray(T[] a) { |
3025 |
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(); |
3026 |
|
for (Map.Entry e : this) |
3027 |
< |
c.add(new SnapshotEntry(e.getKey(), e.getValue())); |
3027 |
> |
c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue())); |
3028 |
|
return c.toArray(a); |
3029 |
|
} |
3030 |
|
} |
3268 |
|
/* ---------------- Relational methods -------------- */ |
3269 |
|
|
3270 |
|
public Map.Entry<K,V> ceilingEntry(K key) { |
3271 |
< |
return (SnapshotEntry<K,V>) |
3271 |
> |
return (Map.Entry<K,V>) |
3272 |
|
m.getNear(key, m.GT|m.EQ, least, fence, false); |
3273 |
|
} |
3274 |
|
|
3278 |
|
} |
3279 |
|
|
3280 |
|
public Map.Entry<K,V> lowerEntry(K key) { |
3281 |
< |
return (SnapshotEntry<K,V>) |
3281 |
> |
return (Map.Entry<K,V>) |
3282 |
|
m.getNear(key, m.LT, least, fence, false); |
3283 |
|
} |
3284 |
|
|
3288 |
|
} |
3289 |
|
|
3290 |
|
public Map.Entry<K,V> floorEntry(K key) { |
3291 |
< |
return (SnapshotEntry<K,V>) |
3291 |
> |
return (Map.Entry<K,V>) |
3292 |
|
m.getNear(key, m.LT|m.EQ, least, fence, false); |
3293 |
|
} |
3294 |
|
|
3299 |
|
|
3300 |
|
|
3301 |
|
public Map.Entry<K,V> higherEntry(K key) { |
3302 |
< |
return (SnapshotEntry<K,V>) |
3302 |
> |
return (Map.Entry<K,V>) |
3303 |
|
m.getNear(key, m.GT, least, fence, false); |
3304 |
|
} |
3305 |
|
|
3331 |
|
} |
3332 |
|
|
3333 |
|
public Map.Entry<K,V> pollFirstEntry() { |
3334 |
< |
return (SnapshotEntry<K,V>) |
3334 |
> |
return (Map.Entry<K,V>) |
3335 |
|
m.removeFirstEntryOfSubrange(least, fence, false); |
3336 |
|
} |
3337 |
|
|
3338 |
|
public Map.Entry<K,V> pollLastEntry() { |
3339 |
< |
return (SnapshotEntry<K,V>) |
3339 |
> |
return (Map.Entry<K,V>) |
3340 |
|
m.removeLastEntryOfSubrange(least, fence, false); |
3341 |
|
} |
3342 |
|
|
3454 |
|
public Object[] toArray() { |
3455 |
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(); |
3456 |
|
for (Map.Entry e : this) |
3457 |
< |
c.add(new SnapshotEntry(e.getKey(), e.getValue())); |
3457 |
> |
c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue())); |
3458 |
|
return c.toArray(); |
3459 |
|
} |
3460 |
|
public <T> T[] toArray(T[] a) { |
3461 |
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(); |
3462 |
|
for (Map.Entry e : this) |
3463 |
< |
c.add(new SnapshotEntry(e.getKey(), e.getValue())); |
3463 |
> |
c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue())); |
3464 |
|
return c.toArray(a); |
3465 |
|
} |
3466 |
|
} |