1 7 8 package java.util.concurrent; 9 import java.util.*; 10 import java.util.concurrent.atomic.*; 11 12 64 public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> 65 implements ConcurrentNavigableMap <K,V>, 66 Cloneable , 67 java.io.Serializable { 68 293 294 private static final long serialVersionUID = -8627078645895051609L; 295 296 300 private static final Random seedGenerator = new Random(); 301 302 305 private static final Object BASE_HEADER = new Object (); 306 307 310 private transient volatile HeadIndex<K,V> head; 311 312 317 private final Comparator<? super K> comparator; 318 319 323 private transient int randomSeed; 324 325 326 private transient KeySet keySet; 327 328 private transient EntrySet entrySet; 329 330 private transient Values values; 331 332 private transient ConcurrentNavigableMap <K,V> descendingMap; 333 334 339 final void initialize() { 340 keySet = null; 341 entrySet = null; 342 values = null; 343 descendingMap = null; 344 randomSeed = seedGenerator.nextInt() | 0x0100; head = new HeadIndex<K,V>(new Node<K,V>(null, BASE_HEADER, null), 346 null, null, 1); 347 } 348 349 350 private static final 351 AtomicReferenceFieldUpdater<ConcurrentSkipListMap , HeadIndex> 352 headUpdater = AtomicReferenceFieldUpdater.newUpdater 353 (ConcurrentSkipListMap .class, HeadIndex.class, "head"); 354 355 358 private boolean casHead(HeadIndex<K,V> cmp, HeadIndex<K,V> val) { 359 return headUpdater.compareAndSet(this, cmp, val); 360 } 361 362 363 364 371 static final class Node<K,V> { 372 final K key; 373 volatile Object value; 374 volatile Node<K,V> next; 375 376 379 Node(K key, Object value, Node<K,V> next) { 380 this.key = key; 381 this.value = value; 382 this.next = next; 383 } 384 385 392 Node(Node<K,V> next) { 393 this.key = null; 394 this.value = this; 395 this.next = next; 396 } 397 398 399 static final AtomicReferenceFieldUpdater<Node, Node> 400 nextUpdater = AtomicReferenceFieldUpdater.newUpdater 401 (Node.class, Node.class, "next"); 402 403 404 static final AtomicReferenceFieldUpdater<Node, Object > 405 valueUpdater = AtomicReferenceFieldUpdater.newUpdater 406 (Node.class, Object .class, "value"); 407 408 411 boolean casValue(Object cmp, Object val) { 412 return valueUpdater.compareAndSet(this, cmp, val); 413 } 414 415 418 boolean casNext(Node<K,V> cmp, Node<K,V> val) { 419 return nextUpdater.compareAndSet(this, cmp, val); 420 } 421 422 431 boolean isMarker() { 432 return value == this; 433 } 434 435 439 boolean isBaseHeader() { 440 return value == BASE_HEADER; 441 } 442 443 448 boolean appendMarker(Node<K,V> f) { 449 return casNext(f, new Node<K,V>(f)); 450 } 451 452 459 void helpDelete(Node<K,V> b, Node<K,V> f) { 460 465 if (f == next && this == b.next) { 466 if (f == null || f.value != f) appendMarker(f); 468 else 469 b.casNext(this, f.next); 470 } 471 } 472 473 479 V getValidValue() { 480 Object v = value; 481 if (v == this || v == BASE_HEADER) 482 return null; 483 return (V)v; 484 } 485 486 491 AbstractMap.SimpleImmutableEntry<K,V> createSnapshot() { 492 V v = getValidValue(); 493 if (v == null) 494 return null; 495 return new AbstractMap.SimpleImmutableEntry<K,V>(key, v); 496 } 497 } 498 499 500 501 508 static class Index<K,V> { 509 final Node<K,V> node; 510 final Index<K,V> down; 511 volatile Index<K,V> right; 512 513 516 Index(Node<K,V> node, Index<K,V> down, Index<K,V> right) { 517 this.node = node; 518 this.down = down; 519 this.right = right; 520 } 521 522 523 static final AtomicReferenceFieldUpdater<Index, Index> 524 rightUpdater = AtomicReferenceFieldUpdater.newUpdater 525 (Index.class, Index.class, "right"); 526 527 530 final boolean casRight(Index<K,V> cmp, Index<K,V> val) { 531 return rightUpdater.compareAndSet(this, cmp, val); 532 } 533 534 538 final boolean indexesDeletedNode() { 539 return node.value == null; 540 } 541 542 550 final boolean link(Index<K,V> succ, Index<K,V> newSucc) { 551 Node<K,V> n = node; 552 newSucc.right = succ; 553 return n.value != null && casRight(succ, newSucc); 554 } 555 556 563 final boolean unlink(Index<K,V> succ) { 564 return !indexesDeletedNode() && casRight(succ, succ.right); 565 } 566 } 567 568 569 570 573 static final class HeadIndex<K,V> extends Index<K,V> { 574 final int level; 575 HeadIndex(Node<K,V> node, Index<K,V> down, Index<K,V> right, int level) { 576 super(node, down, right); 577 this.level = level; 578 } 579 } 580 581 582 583 598 static final class ComparableUsingComparator<K> implements Comparable <K> { 599 final K actualKey; 600 final Comparator<? super K> cmp; 601 ComparableUsingComparator(K key, Comparator<? super K> cmp) { 602 this.actualKey = key; 603 this.cmp = cmp; 604 } 605 public int compareTo(K k2) { 606 return cmp.compare(actualKey, k2); 607 } 608 } 609 610 615 private Comparable <? super K> comparable(Object key) throws ClassCastException { 616 if (key == null) 617 throw new NullPointerException (); 618 if (comparator != null) 619 return new ComparableUsingComparator<K>((K)key, comparator); 620 else 621 return (Comparable <? super K>)key; 622 } 623 624 628 int compare(K k1, K k2) throws ClassCastException { 629 Comparator<? super K> cmp = comparator; 630 if (cmp != null) 631 return cmp.compare(k1, k2); 632 else 633 return ((Comparable <? super K>)k1).compareTo(k2); 634 } 635 636 641 boolean inHalfOpenRange(K key, K least, K fence) { 642 if (key == null) 643 throw new NullPointerException (); 644 return ((least == null || compare(key, least) >= 0) && 645 (fence == null || compare(key, fence) < 0)); 646 } 647 648 652 boolean inOpenRange(K key, K least, K fence) { 653 if (key == null) 654 throw new NullPointerException (); 655 return ((least == null || compare(key, least) >= 0) && 656 (fence == null || compare(key, fence) <= 0)); 657 } 658 659 660 661 669 private Node<K,V> findPredecessor(Comparable <? super K> key) { 670 if (key == null) 671 throw new NullPointerException (); for (;;) { 673 Index<K,V> q = head; 674 Index<K,V> r = q.right; 675 for (;;) { 676 if (r != null) { 677 Node<K,V> n = r.node; 678 K k = n.key; 679 if (n.value == null) { 680 if (!q.unlink(r)) 681 break; r = q.right; continue; 684 } 685 if (key.compareTo(k) > 0) { 686 q = r; 687 r = r.right; 688 continue; 689 } 690 } 691 Index<K,V> d = q.down; 692 if (d != null) { 693 q = d; 694 r = d.right; 695 } else 696 return q.node; 697 } 698 } 699 } 700 701 745 private Node<K,V> findNode(Comparable <? super K> key) { 746 for (;;) { 747 Node<K,V> b = findPredecessor(key); 748 Node<K,V> n = b.next; 749 for (;;) { 750 if (n == null) 751 return null; 752 Node<K,V> f = n.next; 753 if (n != b.next) break; 755 Object v = n.value; 756 if (v == null) { n.helpDelete(b, f); 758 break; 759 } 760 if (v == n || b.value == null) break; 762 int c = key.compareTo(n.key); 763 if (c == 0) 764 return n; 765 if (c < 0) 766 return null; 767 b = n; 768 n = f; 769 } 770 } 771 } 772 773 785 private V doGet(Object okey) { 786 Comparable <? super K> key = comparable(okey); 787 Node<K,V> bound = null; 788 Index<K,V> q = head; 789 Index<K,V> r = q.right; 790 Node<K,V> n; 791 K k; 792 int c; 793 for (;;) { 794 Index<K,V> d; 795 if (r != null && (n = r.node) != bound && (k = n.key) != null) { 797 if ((c = key.compareTo(k)) > 0) { 798 q = r; 799 r = r.right; 800 continue; 801 } else if (c == 0) { 802 Object v = n.value; 803 return (v != null)? (V)v : getUsingFindNode(key); 804 } else 805 bound = n; 806 } 807 808 if ((d = q.down) != null) { 810 q = d; 811 r = d.right; 812 } else 813 break; 814 } 815 816 for (n = q.node.next; n != null; n = n.next) { 818 if ((k = n.key) != null) { 819 if ((c = key.compareTo(k)) == 0) { 820 Object v = n.value; 821 return (v != null)? (V)v : getUsingFindNode(key); 822 } else if (c < 0) 823 break; 824 } 825 } 826 return null; 827 } 828 829 835 private V getUsingFindNode(Comparable <? super K> key) { 836 841 for (;;) { 842 Node<K,V> n = findNode(key); 843 if (n == null) 844 return null; 845 Object v = n.value; 846 if (v != null) 847 return (V)v; 848 } 849 } 850 851 852 853 861 private V doPut(K kkey, V value, boolean onlyIfAbsent) { 862 Comparable <? super K> key = comparable(kkey); 863 for (;;) { 864 Node<K,V> b = findPredecessor(key); 865 Node<K,V> n = b.next; 866 for (;;) { 867 if (n != null) { 868 Node<K,V> f = n.next; 869 if (n != b.next) break;; 871 Object v = n.value; 872 if (v == null) { n.helpDelete(b, f); 874 break; 875 } 876 if (v == n || b.value == null) break; 878 int c = key.compareTo(n.key); 879 if (c > 0) { 880 b = n; 881 n = f; 882 continue; 883 } 884 if (c == 0) { 885 if (onlyIfAbsent || n.casValue(v, value)) 886 return (V)v; 887 else 888 break; } 890 } 892 893 Node<K,V> z = new Node<K,V>(kkey, value, n); 894 if (!b.casNext(n, z)) 895 break; int level = randomLevel(); 897 if (level > 0) 898 insertIndex(z, level); 899 return null; 900 } 901 } 902 } 903 904 913 private int randomLevel() { 914 int x = randomSeed; 915 x ^= x << 13; 916 x ^= x >>> 17; 917 randomSeed = x ^= x << 5; 918 if ((x & 0x8001) != 0) return 0; 920 int level = 1; 921 while (((x >>>= 1) & 1) != 0) ++level; 922 return level; 923 } 924 925 930 private void insertIndex(Node<K,V> z, int level) { 931 HeadIndex<K,V> h = head; 932 int max = h.level; 933 934 if (level <= max) { 935 Index<K,V> idx = null; 936 for (int i = 1; i <= level; ++i) 937 idx = new Index<K,V>(z, idx, null); 938 addIndex(idx, h, level); 939 940 } else { 949 level = max + 1; 950 Index<K,V>[] idxs = (Index<K,V>[])new Index[level+1]; 951 Index<K,V> idx = null; 952 for (int i = 1; i <= level; ++i) 953 idxs[i] = idx = new Index<K,V>(z, idx, null); 954 955 HeadIndex<K,V> oldh; 956 int k; 957 for (;;) { 958 oldh = head; 959 int oldLevel = oldh.level; 960 if (level <= oldLevel) { k = level; 962 break; 963 } 964 HeadIndex<K,V> newh = oldh; 965 Node<K,V> oldbase = oldh.node; 966 for (int j = oldLevel+1; j <= level; ++j) 967 newh = new HeadIndex<K,V>(oldbase, newh, idxs[j], j); 968 if (casHead(oldh, newh)) { 969 k = oldLevel; 970 break; 971 } 972 } 973 addIndex(idxs[k], oldh, k); 974 } 975 } 976 977 984 private void addIndex(Index<K,V> idx, HeadIndex<K,V> h, int indexLevel) { 985 int insertionLevel = indexLevel; 987 Comparable <? super K> key = comparable(idx.node.key); 988 if (key == null) throw new NullPointerException (); 989 990 for (;;) { 993 int j = h.level; 994 Index<K,V> q = h; 995 Index<K,V> r = q.right; 996 Index<K,V> t = idx; 997 for (;;) { 998 if (r != null) { 999 Node<K,V> n = r.node; 1000 int c = key.compareTo(n.key); 1002 if (n.value == null) { 1003 if (!q.unlink(r)) 1004 break; 1005 r = q.right; 1006 continue; 1007 } 1008 if (c > 0) { 1009 q = r; 1010 r = r.right; 1011 continue; 1012 } 1013 } 1014 1015 if (j == insertionLevel) { 1016 if (t.indexesDeletedNode()) { 1018 findNode(key); return; 1020 } 1021 if (!q.link(r, t)) 1022 break; if (--insertionLevel == 0) { 1024 if (t.indexesDeletedNode()) 1026 findNode(key); 1027 return; 1028 } 1029 } 1030 1031 if (--j >= insertionLevel && j < indexLevel) 1032 t = t.down; 1033 q = q.down; 1034 r = q.right; 1035 } 1036 } 1037 } 1038 1039 1040 1041 1060 final V doRemove(Object okey, Object value) { 1061 Comparable <? super K> key = comparable(okey); 1062 for (;;) { 1063 Node<K,V> b = findPredecessor(key); 1064 Node<K,V> n = b.next; 1065 for (;;) { 1066 if (n == null) 1067 return null; 1068 Node<K,V> f = n.next; 1069 if (n != b.next) break; 1071 Object v = n.value; 1072 if (v == null) { n.helpDelete(b, f); 1074 break; 1075 } 1076 if (v == n || b.value == null) break; 1078 int c = key.compareTo(n.key); 1079 if (c < 0) 1080 return null; 1081 if (c > 0) { 1082 b = n; 1083 n = f; 1084 continue; 1085 } 1086 if (value != null && !value.equals(v)) 1087 return null; 1088 if (!n.casValue(v, null)) 1089 break; 1090 if (!n.appendMarker(f) || !b.casNext(n, f)) 1091 findNode(key); else { 1093 findPredecessor(key); if (head.right == null) 1095 tryReduceLevel(); 1096 } 1097 return (V)v; 1098 } 1099 } 1100 } 1101 1102 1122 private void tryReduceLevel() { 1123 HeadIndex<K,V> h = head; 1124 HeadIndex<K,V> d; 1125 HeadIndex<K,V> e; 1126 if (h.level > 3 && 1127 (d = (HeadIndex<K,V>)h.down) != null && 1128 (e = (HeadIndex<K,V>)d.down) != null && 1129 e.right == null && 1130 d.right == null && 1131 h.right == null && 1132 casHead(h, d) && h.right != null) casHead(d, h); } 1136 1137 1138 1139 1143 Node<K,V> findFirst() { 1144 for (;;) { 1145 Node<K,V> b = head.node; 1146 Node<K,V> n = b.next; 1147 if (n == null) 1148 return null; 1149 if (n.value != null) 1150 return n; 1151 n.helpDelete(b, n.next); 1152 } 1153 } 1154 1155 1159 Map.Entry<K,V> doRemoveFirstEntry() { 1160 for (;;) { 1161 Node<K,V> b = head.node; 1162 Node<K,V> n = b.next; 1163 if (n == null) 1164 return null; 1165 Node<K,V> f = n.next; 1166 if (n != b.next) 1167 continue; 1168 Object v = n.value; 1169 if (v == null) { 1170 n.helpDelete(b, f); 1171 continue; 1172 } 1173 if (!n.casValue(v, null)) 1174 continue; 1175 if (!n.appendMarker(f) || !b.casNext(n, f)) 1176 findFirst(); clearIndexToFirst(); 1178 return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, (V)v); 1179 } 1180 } 1181 1182 1185 private void clearIndexToFirst() { 1186 for (;;) { 1187 Index<K,V> q = head; 1188 for (;;) { 1189 Index<K,V> r = q.right; 1190 if (r != null && r.indexesDeletedNode() && !q.unlink(r)) 1191 break; 1192 if ((q = q.down) == null) { 1193 if (head.right == null) 1194 tryReduceLevel(); 1195 return; 1196 } 1197 } 1198 } 1199 } 1200 1201 1202 1203 1204 1208 Node<K,V> findLast() { 1209 1214 Index<K,V> q = head; 1215 for (;;) { 1216 Index<K,V> d, r; 1217 if ((r = q.right) != null) { 1218 if (r.indexesDeletedNode()) { 1219 q.unlink(r); 1220 q = head; } 1222 else 1223 q = r; 1224 } else if ((d = q.down) != null) { 1225 q = d; 1226 } else { 1227 Node<K,V> b = q.node; 1228 Node<K,V> n = b.next; 1229 for (;;) { 1230 if (n == null) 1231 return (b.isBaseHeader())? null : b; 1232 Node<K,V> f = n.next; if (n != b.next) 1234 break; 1235 Object v = n.value; 1236 if (v == null) { n.helpDelete(b, f); 1238 break; 1239 } 1240 if (v == n || b.value == null) break; 1242 b = n; 1243 n = f; 1244 } 1245 q = head; } 1247 } 1248 } 1249 1250 1257 private Node<K,V> findPredecessorOfLast() { 1258 for (;;) { 1259 Index<K,V> q = head; 1260 for (;;) { 1261 Index<K,V> d, r; 1262 if ((r = q.right) != null) { 1263 if (r.indexesDeletedNode()) { 1264 q.unlink(r); 1265 break; } 1267 if (r.node.next != null) { 1269 q = r; 1270 continue; 1271 } 1272 } 1273 if ((d = q.down) != null) 1274 q = d; 1275 else 1276 return q.node; 1277 } 1278 } 1279 } 1280 1281 1286 Map.Entry<K,V> doRemoveLastEntry() { 1287 for (;;) { 1288 Node<K,V> b = findPredecessorOfLast(); 1289 Node<K,V> n = b.next; 1290 if (n == null) { 1291 if (b.isBaseHeader()) return null; 1293 else 1294 continue; } 1296 for (;;) { 1297 Node<K,V> f = n.next; 1298 if (n != b.next) break; 1300 Object v = n.value; 1301 if (v == null) { n.helpDelete(b, f); 1303 break; 1304 } 1305 if (v == n || b.value == null) break; 1307 if (f != null) { 1308 b = n; 1309 n = f; 1310 continue; 1311 } 1312 if (!n.casValue(v, null)) 1313 break; 1314 K key = n.key; 1315 Comparable <? super K> ck = comparable(key); 1316 if (!n.appendMarker(f) || !b.casNext(n, f)) 1317 findNode(ck); else { 1319 findPredecessor(ck); if (head.right == null) 1321 tryReduceLevel(); 1322 } 1323 return new AbstractMap.SimpleImmutableEntry<K,V>(key, (V)v); 1324 } 1325 } 1326 } 1327 1328 1329 1330 1332 private static final int EQ = 1; 1333 private static final int LT = 2; 1334 private static final int GT = 0; 1336 1342 Node<K,V> findNear(K kkey, int rel) { 1343 Comparable <? super K> key = comparable(kkey); 1344 for (;;) { 1345 Node<K,V> b = findPredecessor(key); 1346 Node<K,V> n = b.next; 1347 for (;;) { 1348 if (n == null) 1349 return ((rel & LT) == 0 || b.isBaseHeader())? null : b; 1350 Node<K,V> f = n.next; 1351 if (n != b.next) break; 1353 Object v = n.value; 1354 if (v == null) { n.helpDelete(b, f); 1356 break; 1357 } 1358 if (v == n || b.value == null) break; 1360 int c = key.compareTo(n.key); 1361 if ((c == 0 && (rel & EQ) != 0) || 1362 (c < 0 && (rel & LT) == 0)) 1363 return n; 1364 if ( c <= 0 && (rel & LT) != 0) 1365 return (b.isBaseHeader())? null : b; 1366 b = n; 1367 n = f; 1368 } 1369 } 1370 } 1371 1372 1378 AbstractMap.SimpleImmutableEntry<K,V> getNear(K key, int rel) { 1379 for (;;) { 1380 Node<K,V> n = findNear(key, rel); 1381 if (n == null) 1382 return null; 1383 AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); 1384 if (e != null) 1385 return e; 1386 } 1387 } 1388 1389 1390 1391 1392 1396 public ConcurrentSkipListMap() { 1397 this.comparator = null; 1398 initialize(); 1399 } 1400 1401 1409 public ConcurrentSkipListMap(Comparator<? super K> comparator) { 1410 this.comparator = comparator; 1411 initialize(); 1412 } 1413 1414 1425 public ConcurrentSkipListMap(Map<? extends K, ? extends V> m) { 1426 this.comparator = null; 1427 initialize(); 1428 putAll(m); 1429 } 1430 1431 1440 public ConcurrentSkipListMap(SortedMap<K, ? extends V> m) { 1441 this.comparator = m.comparator(); 1442 initialize(); 1443 buildFromSorted(m); 1444 } 1445 1446 1452 public ConcurrentSkipListMap <K,V> clone() { 1453 ConcurrentSkipListMap <K,V> clone = null; 1454 try { 1455 clone = (ConcurrentSkipListMap <K,V>) super.clone(); 1456 } catch (CloneNotSupportedException e) { 1457 throw new InternalError (); 1458 } 1459 1460 clone.initialize(); 1461 clone.buildFromSorted(this); 1462 return clone; 1463 } 1464 1465 1470 private void buildFromSorted(SortedMap<K, ? extends V> map) { 1471 if (map == null) 1472 throw new NullPointerException (); 1473 1474 HeadIndex<K,V> h = head; 1475 Node<K,V> basepred = h.node; 1476 1477 ArrayList<Index<K,V>> preds = new ArrayList<Index<K,V>>(); 1480 1481 for (int i = 0; i <= h.level; ++i) 1483 preds.add(null); 1484 Index<K,V> q = h; 1485 for (int i = h.level; i > 0; --i) { 1486 preds.set(i, q); 1487 q = q.down; 1488 } 1489 1490 Iterator<? extends Map.Entry<? extends K, ? extends V>> it = 1491 map.entrySet().iterator(); 1492 while (it.hasNext()) { 1493 Map.Entry<? extends K, ? extends V> e = it.next(); 1494 int j = randomLevel(); 1495 if (j > h.level) j = h.level + 1; 1496 K k = e.getKey(); 1497 V v = e.getValue(); 1498 if (k == null || v == null) 1499 throw new NullPointerException (); 1500 Node<K,V> z = new Node<K,V>(k, v, null); 1501 basepred.next = z; 1502 basepred = z; 1503 if (j > 0) { 1504 Index<K,V> idx = null; 1505 for (int i = 1; i <= j; ++i) { 1506 idx = new Index<K,V>(z, idx, null); 1507 if (i > h.level) 1508 h = new HeadIndex<K,V>(h.node, h, idx, i); 1509 1510 if (i < preds.size()) { 1511 preds.get(i).right = idx; 1512 preds.set(i, idx); 1513 } else 1514 preds.add(idx); 1515 } 1516 } 1517 } 1518 head = h; 1519 } 1520 1521 1522 1523 1532 private void writeObject(java.io.ObjectOutputStream s) 1533 throws java.io.IOException { 1534 s.defaultWriteObject(); 1536 1537 for (Node<K,V> n = findFirst(); n != null; n = n.next) { 1539 V v = n.getValidValue(); 1540 if (v != null) { 1541 s.writeObject(n.key); 1542 s.writeObject(v); 1543 } 1544 } 1545 s.writeObject(null); 1546 } 1547 1548 1551 private void readObject(final java.io.ObjectInputStream s) 1552 throws java.io.IOException , ClassNotFoundException { 1553 s.defaultReadObject(); 1555 initialize(); 1557 1558 1565 1566 HeadIndex<K,V> h = head; 1567 Node<K,V> basepred = h.node; 1568 ArrayList<Index<K,V>> preds = new ArrayList<Index<K,V>>(); 1569 for (int i = 0; i <= h.level; ++i) 1570 preds.add(null); 1571 Index<K,V> q = h; 1572 for (int i = h.level; i > 0; --i) { 1573 preds.set(i, q); 1574 q = q.down; 1575 } 1576 1577 for (;;) { 1578 Object k = s.readObject(); 1579 if (k == null) 1580 break; 1581 Object v = s.readObject(); 1582 if (v == null) 1583 throw new NullPointerException (); 1584 K key = (K) k; 1585 V val = (V) v; 1586 int j = randomLevel(); 1587 if (j > h.level) j = h.level + 1; 1588 Node<K,V> z = new Node<K,V>(key, val, null); 1589 basepred.next = z; 1590 basepred = z; 1591 if (j > 0) { 1592 Index<K,V> idx = null; 1593 for (int i = 1; i <= j; ++i) { 1594 idx = new Index<K,V>(z, idx, null); 1595 if (i > h.level) 1596 h = new HeadIndex<K,V>(h.node, h, idx, i); 1597 1598 if (i < preds.size()) { 1599 preds.get(i).right = idx; 1600 preds.set(i, idx); 1601 } else 1602 preds.add(idx); 1603 } 1604 } 1605 } 1606 head = h; 1607 } 1608 1609 1610 1611 1621 public boolean containsKey(Object key) { 1622 return doGet(key) != null; 1623 } 1624 1625 1639 public V get(Object key) { 1640 return doGet(key); 1641 } 1642 1643 1656 public V put(K key, V value) { 1657 if (value == null) 1658 throw new NullPointerException (); 1659 return doPut(key, value, false); 1660 } 1661 1662 1672 public V remove(Object key) { 1673 return doRemove(key, null); 1674 } 1675 1676 1686 public boolean containsValue(Object value) { 1687 if (value == null) 1688 throw new NullPointerException (); 1689 for (Node<K,V> n = findFirst(); n != null; n = n.next) { 1690 V v = n.getValidValue(); 1691 if (v != null && value.equals(v)) 1692 return true; 1693 } 1694 return false; 1695 } 1696 1697 1713 public int size() { 1714 long count = 0; 1715 for (Node<K,V> n = findFirst(); n != null; n = n.next) { 1716 if (n.getValidValue() != null) 1717 ++count; 1718 } 1719 return (count >= Integer.MAX_VALUE)? Integer.MAX_VALUE : (int)count; 1720 } 1721 1722 1726 public boolean isEmpty() { 1727 return findFirst() == null; 1728 } 1729 1730 1733 public void clear() { 1734 initialize(); 1735 } 1736 1737 1738 1739 1747 1748 1769 public NavigableSet<K> keySet() { 1770 KeySet ks = keySet; 1771 return (ks != null) ? ks : (keySet = new KeySet(this)); 1772 } 1773 1774 public NavigableSet<K> navigableKeySet() { 1775 KeySet ks = keySet; 1776 return (ks != null) ? ks : (keySet = new KeySet(this)); 1777 } 1778 1779 1797 public Collection<V> values() { 1798 Values vs = values; 1799 return (vs != null) ? vs : (values = new Values(this)); 1800 } 1801 1802 1826 public Set<Map.Entry<K,V>> entrySet() { 1827 EntrySet es = entrySet; 1828 return (es != null) ? es : (entrySet = new EntrySet(this)); 1829 } 1830 1831 public ConcurrentNavigableMap <K,V> descendingMap() { 1832 ConcurrentNavigableMap <K,V> dm = descendingMap; 1833 return (dm != null) ? dm : (descendingMap = new SubMap<K,V> 1834 (this, null, false, null, false, true)); 1835 } 1836 1837 public NavigableSet<K> descendingKeySet() { 1838 return descendingMap().navigableKeySet(); 1839 } 1840 1841 1842 1843 1855 public boolean equals(Object o) { 1856 if (o == this) 1857 return true; 1858 if (!(o instanceof Map)) 1859 return false; 1860 Map<?,?> m = (Map<?,?>) o; 1861 try { 1862 for (Map.Entry<K,V> e : this.entrySet()) 1863 if (! e.getValue().equals(m.get(e.getKey()))) 1864 return false; 1865 for (Map.Entry<?,?> e : m.entrySet()) { 1866 Object k = e.getKey(); 1867 Object v = e.getValue(); 1868 if (k == null || v == null || !v.equals(get(k))) 1869 return false; 1870 } 1871 return true; 1872 } catch (ClassCastException unused) { 1873 return false; 1874 } catch (NullPointerException unused) { 1875 return false; 1876 } 1877 } 1878 1879 1880 1881 1890 public V putIfAbsent(K key, V value) { 1891 if (value == null) 1892 throw new NullPointerException (); 1893 return doPut(key, value, true); 1894 } 1895 1896 1903 public boolean remove(Object key, Object value) { 1904 if (key == null) 1905 throw new NullPointerException (); 1906 if (value == null) 1907 return false; 1908 return doRemove(key, value) != null; 1909 } 1910 1911 1918 public boolean replace(K key, V oldValue, V newValue) { 1919 if (oldValue == null || newValue == null) 1920 throw new NullPointerException (); 1921 Comparable <? super K> k = comparable(key); 1922 for (;;) { 1923 Node<K,V> n = findNode(k); 1924 if (n == null) 1925 return false; 1926 Object v = n.value; 1927 if (v != null) { 1928 if (!oldValue.equals(v)) 1929 return false; 1930 if (n.casValue(v, newValue)) 1931 return true; 1932 } 1933 } 1934 } 1935 1936 1945 public V replace(K key, V value) { 1946 if (value == null) 1947 throw new NullPointerException (); 1948 Comparable <? super K> k = comparable(key); 1949 for (;;) { 1950 Node<K,V> n = findNode(k); 1951 if (n == null) 1952 return null; 1953 Object v = n.value; 1954 if (v != null && n.casValue(v, value)) 1955 return (V)v; 1956 } 1957 } 1958 1959 1960 1961 public Comparator<? super K> comparator() { 1962 return comparator; 1963 } 1964 1965 1968 public K firstKey() { 1969 Node<K,V> n = findFirst(); 1970 if (n == null) 1971 throw new NoSuchElementException(); 1972 return n.key; 1973 } 1974 1975 1978 public K lastKey() { 1979 Node<K,V> n = findLast(); 1980 if (n == null) 1981 throw new NoSuchElementException(); 1982 return n.key; 1983 } 1984 1985 1990 public ConcurrentNavigableMap <K,V> subMap(K fromKey, 1991 boolean fromInclusive, 1992 K toKey, 1993 boolean toInclusive) { 1994 if (fromKey == null || toKey == null) 1995 throw new NullPointerException (); 1996 return new SubMap<K,V> 1997 (this, fromKey, fromInclusive, toKey, toInclusive, false); 1998 } 1999 2000 2005 public ConcurrentNavigableMap <K,V> headMap(K toKey, 2006 boolean inclusive) { 2007 if (toKey == null) 2008 throw new NullPointerException (); 2009 return new SubMap<K,V> 2010 (this, null, false, toKey, inclusive, false); 2011 } 2012 2013 2018 public ConcurrentNavigableMap <K,V> tailMap(K fromKey, 2019 boolean inclusive) { 2020 if (fromKey == null) 2021 throw new NullPointerException (); 2022 return new SubMap<K,V> 2023 (this, fromKey, inclusive, null, false, false); 2024 } 2025 2026 2031 public ConcurrentNavigableMap <K,V> subMap(K fromKey, K toKey) { 2032 return subMap(fromKey, true, toKey, false); 2033 } 2034 2035 2040 public ConcurrentNavigableMap <K,V> headMap(K toKey) { 2041 return headMap(toKey, false); 2042 } 2043 2044 2049 public ConcurrentNavigableMap <K,V> tailMap(K fromKey) { 2050 return tailMap(fromKey, true); 2051 } 2052 2053 2054 2055 2064 public Map.Entry<K,V> lowerEntry(K key) { 2065 return getNear(key, LT); 2066 } 2067 2068 2072 public K lowerKey(K key) { 2073 Node<K,V> n = findNear(key, LT); 2074 return (n == null)? null : n.key; 2075 } 2076 2077 2087 public Map.Entry<K,V> floorEntry(K key) { 2088 return getNear(key, LT|EQ); 2089 } 2090 2091 2096 public K floorKey(K key) { 2097 Node<K,V> n = findNear(key, LT|EQ); 2098 return (n == null)? null : n.key; 2099 } 2100 2101 2110 public Map.Entry<K,V> ceilingEntry(K key) { 2111 return getNear(key, GT|EQ); 2112 } 2113 2114 2118 public K ceilingKey(K key) { 2119 Node<K,V> n = findNear(key, GT|EQ); 2120 return (n == null)? null : n.key; 2121 } 2122 2123 2133 public Map.Entry<K,V> higherEntry(K key) { 2134 return getNear(key, GT); 2135 } 2136 2137 2142 public K higherKey(K key) { 2143 Node<K,V> n = findNear(key, GT); 2144 return (n == null)? null : n.key; 2145 } 2146 2147 2153 public Map.Entry<K,V> firstEntry() { 2154 for (;;) { 2155 Node<K,V> n = findFirst(); 2156 if (n == null) 2157 return null; 2158 AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); 2159 if (e != null) 2160 return e; 2161 } 2162 } 2163 2164 2170 public Map.Entry<K,V> lastEntry() { 2171 for (;;) { 2172 Node<K,V> n = findLast(); 2173 if (n == null) 2174 return null; 2175 AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); 2176 if (e != null) 2177 return e; 2178 } 2179 } 2180 2181 2187 public Map.Entry<K,V> pollFirstEntry() { 2188 return doRemoveFirstEntry(); 2189 } 2190 2191 2197 public Map.Entry<K,V> pollLastEntry() { 2198 return doRemoveLastEntry(); 2199 } 2200 2201 2202 2203 2204 2207 abstract class Iter<T> implements Iterator<T> { 2208 2209 Node<K,V> lastReturned; 2210 2211 Node<K,V> next; 2212 2213 V nextValue; 2214 2215 2216 Iter() { 2217 for (;;) { 2218 next = findFirst(); 2219 if (next == null) 2220 break; 2221 Object x = next.value; 2222 if (x != null && x != next) { 2223 nextValue = (V) x; 2224 break; 2225 } 2226 } 2227 } 2228 2229 public final boolean hasNext() { 2230 return next != null; 2231 } 2232 2233 2234 final void advance() { 2235 if (next == null) 2236 throw new NoSuchElementException(); 2237 lastReturned = next; 2238 for (;;) { 2239 next = next.next; 2240 if (next == null) 2241 break; 2242 Object x = next.value; 2243 if (x != null && x != next) { 2244 nextValue = (V) x; 2245 break; 2246 } 2247 } 2248 } 2249 2250 public void remove() { 2251 Node<K,V> l = lastReturned; 2252 if (l == null) 2253 throw new IllegalStateException (); 2254 ConcurrentSkipListMap.this.remove(l.key); 2257 lastReturned = null; 2258 } 2259 2260 } 2261 2262 final class ValueIterator extends Iter<V> { 2263 public V next() { 2264 V v = nextValue; 2265 advance(); 2266 return v; 2267 } 2268 } 2269 2270 final class KeyIterator extends Iter<K> { 2271 public K next() { 2272 Node<K,V> n = next; 2273 advance(); 2274 return n.key; 2275 } 2276 } 2277 2278 final class EntryIterator extends Iter<Map.Entry<K,V>> { 2279 public Map.Entry<K,V> next() { 2280 Node<K,V> n = next; 2281 V v = nextValue; 2282 advance(); 2283 return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v); 2284 } 2285 } 2286 2287 2289 Iterator<K> keyIterator() { 2290 return new KeyIterator(); 2291 } 2292 2293 Iterator<V> valueIterator() { 2294 return new ValueIterator(); 2295 } 2296 2297 Iterator<Map.Entry<K,V>> entryIterator() { 2298 return new EntryIterator(); 2299 } 2300 2301 2302 2303 2308 2309 static final <E> List<E> toList(Collection<E> c) { 2310 List<E> list = new ArrayList<E>(); 2312 for (E e : c) 2313 list.add(e); 2314 return list; 2315 } 2316 2317 static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> { 2318 private final ConcurrentNavigableMap <E,Object > m; 2319 KeySet(ConcurrentNavigableMap <E,Object > map) { m = map; } 2320 public int size() { return m.size(); } 2321 public boolean isEmpty() { return m.isEmpty(); } 2322 public boolean contains(Object o) { return m.containsKey(o); } 2323 public boolean remove(Object o) { return m.remove(o) != null; } 2324 public void clear() { m.clear(); } 2325 public E lower(E e) { return m.lowerKey(e); } 2326 public E floor(E e) { return m.floorKey(e); } 2327 public E ceiling(E e) { return m.ceilingKey(e); } 2328 public E higher(E e) { return m.higherKey(e); } 2329 public Comparator<? super E> comparator() { return m.comparator(); } 2330 public E first() { return m.firstKey(); } 2331 public E last() { return m.lastKey(); } 2332 public E pollFirst() { 2333 Map.Entry<E,Object > e = m.pollFirstEntry(); 2334 return e == null? null : e.getKey(); 2335 } 2336 public E pollLast() { 2337 Map.Entry<E,Object > e = m.pollLastEntry(); 2338 return e == null? null : e.getKey(); 2339 } 2340 public Iterator<E> iterator() { 2341 if (m instanceof ConcurrentSkipListMap ) 2342 return ((ConcurrentSkipListMap <E,Object >)m).keyIterator(); 2343 else 2344 return ((ConcurrentSkipListMap.SubMap <E,Object >)m).keyIterator(); 2345 } 2346 public boolean equals(Object o) { 2347 if (o == this) 2348 return true; 2349 if (!(o instanceof Set)) 2350 return false; 2351 Collection<?> c = (Collection<?>) o; 2352 try { 2353 return containsAll(c) && c.containsAll(this); 2354 } catch (ClassCastException unused) { 2355 return false; 2356 } catch (NullPointerException unused) { 2357 return false; 2358 } 2359 } 2360 public Object [] toArray() { return toList(this).toArray(); } 2361 public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } 2362 public Iterator<E> descendingIterator() { 2363 return descendingSet().iterator(); 2364 } 2365 public NavigableSet<E> subSet(E fromElement, 2366 boolean fromInclusive, 2367 E toElement, 2368 boolean toInclusive) { 2369 return new ConcurrentSkipListSet <E> 2370 (m.subMap(fromElement, fromInclusive, 2371 toElement, toInclusive)); 2372 } 2373 public NavigableSet<E> headSet(E toElement, boolean inclusive) { 2374 return new ConcurrentSkipListSet <E>(m.headMap(toElement, inclusive)); 2375 } 2376 public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { 2377 return new ConcurrentSkipListSet <E>(m.tailMap(fromElement, inclusive)); 2378 } 2379 public NavigableSet<E> subSet(E fromElement, E toElement) { 2380 return subSet(fromElement, true, toElement, false); 2381 } 2382 public NavigableSet<E> headSet(E toElement) { 2383 return headSet(toElement, false); 2384 } 2385 public NavigableSet<E> tailSet(E fromElement) { 2386 return tailSet(fromElement, true); 2387 } 2388 public NavigableSet<E> descendingSet() { 2389 return new ConcurrentSkipListSet (m.descendingMap()); 2390 } 2391 } 2392 2393 static final class Values<E> extends AbstractCollection<E> { 2394 private final ConcurrentNavigableMap <Object , E> m; 2395 Values(ConcurrentNavigableMap <Object , E> map) { 2396 m = map; 2397 } 2398 public Iterator<E> iterator() { 2399 if (m instanceof ConcurrentSkipListMap ) 2400 return ((ConcurrentSkipListMap <Object ,E>)m).valueIterator(); 2401 else 2402 return ((SubMap<Object ,E>)m).valueIterator(); 2403 } 2404 public boolean isEmpty() { 2405 return m.isEmpty(); 2406 } 2407 public int size() { 2408 return m.size(); 2409 } 2410 public boolean contains(Object o) { 2411 return m.containsValue(o); 2412 } 2413 public void clear() { 2414 m.clear(); 2415 } 2416 public Object [] toArray() { return toList(this).toArray(); } 2417 public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } 2418 } 2419 2420 static final class EntrySet<K1,V1> extends AbstractSet<Map.Entry<K1,V1>> { 2421 private final ConcurrentNavigableMap <K1, V1> m; 2422 EntrySet(ConcurrentNavigableMap <K1, V1> map) { 2423 m = map; 2424 } 2425 2426 public Iterator<Map.Entry<K1,V1>> iterator() { 2427 if (m instanceof ConcurrentSkipListMap ) 2428 return ((ConcurrentSkipListMap <K1,V1>)m).entryIterator(); 2429 else 2430 return ((SubMap<K1,V1>)m).entryIterator(); 2431 } 2432 2433 public boolean contains(Object o) { 2434 if (!(o instanceof Map.Entry)) 2435 return false; 2436 Map.Entry<K1,V1> e = (Map.Entry<K1,V1>)o; 2437 V1 v = m.get(e.getKey()); 2438 return v != null && v.equals(e.getValue()); 2439 } 2440 public boolean remove(Object o) { 2441 if (!(o instanceof Map.Entry)) 2442 return false; 2443 Map.Entry<K1,V1> e = (Map.Entry<K1,V1>)o; 2444 return m.remove(e.getKey(), 2445 e.getValue()); 2446 } 2447 public boolean isEmpty() { 2448 return m.isEmpty(); 2449 } 2450 public int size() { 2451 return m.size(); 2452 } 2453 public void clear() { 2454 m.clear(); 2455 } 2456 public boolean equals(Object o) { 2457 if (o == this) 2458 return true; 2459 if (!(o instanceof Set)) 2460 return false; 2461 Collection<?> c = (Collection<?>) o; 2462 try { 2463 return containsAll(c) && c.containsAll(this); 2464 } catch (ClassCastException unused) { 2465 return false; 2466 } catch (NullPointerException unused) { 2467 return false; 2468 } 2469 } 2470 public Object [] toArray() { return toList(this).toArray(); } 2471 public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } 2472 } 2473 2474 2486 static final class SubMap<K,V> extends AbstractMap<K,V> 2487 implements ConcurrentNavigableMap <K,V>, Cloneable , 2488 java.io.Serializable { 2489 private static final long serialVersionUID = -7647078645895051609L; 2490 2491 2492 private final ConcurrentSkipListMap <K,V> m; 2493 2494 private final K lo; 2495 2496 private final K hi; 2497 2498 private final boolean loInclusive; 2499 2500 private final boolean hiInclusive; 2501 2502 private final boolean isDescending; 2503 2504 private transient KeySet<K> keySetView; 2506 private transient Set<Map.Entry<K,V>> entrySetView; 2507 private transient Collection<V> valuesView; 2508 2509 2512 SubMap(ConcurrentSkipListMap <K,V> map, 2513 K fromKey, boolean fromInclusive, 2514 K toKey, boolean toInclusive, 2515 boolean isDescending) { 2516 if (fromKey != null && toKey != null && 2517 map.compare(fromKey, toKey) > 0) 2518 throw new IllegalArgumentException ("inconsistent range"); 2519 this.m = map; 2520 this.lo = fromKey; 2521 this.hi = toKey; 2522 this.loInclusive = fromInclusive; 2523 this.hiInclusive = toInclusive; 2524 this.isDescending = isDescending; 2525 } 2526 2527 2528 2529 private boolean tooLow(K key) { 2530 if (lo != null) { 2531 int c = m.compare(key, lo); 2532 if (c < 0 || (c == 0 && !loInclusive)) 2533 return true; 2534 } 2535 return false; 2536 } 2537 2538 private boolean tooHigh(K key) { 2539 if (hi != null) { 2540 int c = m.compare(key, hi); 2541 if (c > 0 || (c == 0 && !hiInclusive)) 2542 return true; 2543 } 2544 return false; 2545 } 2546 2547 private boolean inBounds(K key) { 2548 return !tooLow(key) && !tooHigh(key); 2549 } 2550 2551 private void checkKeyBounds(K key) throws IllegalArgumentException { 2552 if (key == null) 2553 throw new NullPointerException (); 2554 if (!inBounds(key)) 2555 throw new IllegalArgumentException ("key out of range"); 2556 } 2557 2558 2561 private boolean isBeforeEnd(ConcurrentSkipListMap.Node <K,V> n) { 2562 if (n == null) 2563 return false; 2564 if (hi == null) 2565 return true; 2566 K k = n.key; 2567 if (k == null) return true; 2569 int c = m.compare(k, hi); 2570 if (c > 0 || (c == 0 && !hiInclusive)) 2571 return false; 2572 return true; 2573 } 2574 2575 2579 private ConcurrentSkipListMap.Node <K,V> loNode() { 2580 if (lo == null) 2581 return m.findFirst(); 2582 else if (loInclusive) 2583 return m.findNear(lo, m.GT|m.EQ); 2584 else 2585 return m.findNear(lo, m.GT); 2586 } 2587 2588 2592 private ConcurrentSkipListMap.Node <K,V> hiNode() { 2593 if (hi == null) 2594 return m.findLast(); 2595 else if (hiInclusive) 2596 return m.findNear(hi, m.LT|m.EQ); 2597 else 2598 return m.findNear(hi, m.LT); 2599 } 2600 2601 2604 private K lowestKey() { 2605 ConcurrentSkipListMap.Node <K,V> n = loNode(); 2606 if (isBeforeEnd(n)) 2607 return n.key; 2608 else 2609 throw new NoSuchElementException(); 2610 } 2611 2612 2615 private K highestKey() { 2616 ConcurrentSkipListMap.Node <K,V> n = hiNode(); 2617 if (n != null) { 2618 K last = n.key; 2619 if (inBounds(last)) 2620 return last; 2621 } 2622 throw new NoSuchElementException(); 2623 } 2624 2625 private Map.Entry<K,V> lowestEntry() { 2626 for (;;) { 2627 ConcurrentSkipListMap.Node <K,V> n = loNode(); 2628 if (!isBeforeEnd(n)) 2629 return null; 2630 Map.Entry<K,V> e = n.createSnapshot(); 2631 if (e != null) 2632 return e; 2633 } 2634 } 2635 2636 private Map.Entry<K,V> highestEntry() { 2637 for (;;) { 2638 ConcurrentSkipListMap.Node <K,V> n = hiNode(); 2639 if (n == null || !inBounds(n.key)) 2640 return null; 2641 Map.Entry<K,V> e = n.createSnapshot(); 2642 if (e != null) 2643 return e; 2644 } 2645 } 2646 2647 private Map.Entry<K,V> removeLowest() { 2648 for (;;) { 2649 Node<K,V> n = loNode(); 2650 if (n == null) 2651 return null; 2652 K k = n.key; 2653 if (!inBounds(k)) 2654 return null; 2655 V v = m.doRemove(k, null); 2656 if (v != null) 2657 return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); 2658 } 2659 } 2660 2661 private Map.Entry<K,V> removeHighest() { 2662 for (;;) { 2663 Node<K,V> n = hiNode(); 2664 if (n == null) 2665 return null; 2666 K k = n.key; 2667 if (!inBounds(k)) 2668 return null; 2669 V v = m.doRemove(k, null); 2670 if (v != null) 2671 return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); 2672 } 2673 } 2674 2675 2678 private Map.Entry<K,V> getNearEntry(K key, int rel) { 2679 if (isDescending) { if ((rel & m.LT) == 0) 2681 rel |= m.LT; 2682 else 2683 rel &= ~m.LT; 2684 } 2685 if (tooLow(key)) 2686 return ((rel & m.LT) != 0)? null : lowestEntry(); 2687 if (tooHigh(key)) 2688 return ((rel & m.LT) != 0)? highestEntry() : null; 2689 for (;;) { 2690 Node<K,V> n = m.findNear(key, rel); 2691 if (n == null || !inBounds(n.key)) 2692 return null; 2693 K k = n.key; 2694 V v = n.getValidValue(); 2695 if (v != null) 2696 return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); 2697 } 2698 } 2699 2700 private K getNearKey(K key, int rel) { 2702 if (isDescending) { if ((rel & m.LT) == 0) 2704 rel |= m.LT; 2705 else 2706 rel &= ~m.LT; 2707 } 2708 if (tooLow(key)) { 2709 if ((rel & m.LT) == 0) { 2710 ConcurrentSkipListMap.Node <K,V> n = loNode(); 2711 if (isBeforeEnd(n)) 2712 return n.key; 2713 } 2714 return null; 2715 } 2716 if (tooHigh(key)) { 2717 if ((rel & m.LT) != 0) { 2718 ConcurrentSkipListMap.Node <K,V> n = hiNode(); 2719 if (n != null) { 2720 K last = n.key; 2721 if (inBounds(last)) 2722 return last; 2723 } 2724 } 2725 return null; 2726 } 2727 for (;;) { 2728 Node<K,V> n = m.findNear(key, rel); 2729 if (n == null || !inBounds(n.key)) 2730 return null; 2731 K k = n.key; 2732 V v = n.getValidValue(); 2733 if (v != null) 2734 return k; 2735 } 2736 } 2737 2738 2739 2740 public boolean containsKey(Object key) { 2741 if (key == null) throw new NullPointerException (); 2742 K k = (K)key; 2743 return inBounds(k) && m.containsKey(k); 2744 } 2745 2746 public V get(Object key) { 2747 if (key == null) throw new NullPointerException (); 2748 K k = (K)key; 2749 return ((!inBounds(k)) ? null : m.get(k)); 2750 } 2751 2752 public V put(K key, V value) { 2753 checkKeyBounds(key); 2754 return m.put(key, value); 2755 } 2756 2757 public V remove(Object key) { 2758 K k = (K)key; 2759 return (!inBounds(k))? null : m.remove(k); 2760 } 2761 2762 public int size() { 2763 long count = 0; 2764 for (ConcurrentSkipListMap.Node <K,V> n = loNode(); 2765 isBeforeEnd(n); 2766 n = n.next) { 2767 if (n.getValidValue() != null) 2768 ++count; 2769 } 2770 return count >= Integer.MAX_VALUE? Integer.MAX_VALUE : (int)count; 2771 } 2772 2773 public boolean isEmpty() { 2774 return !isBeforeEnd(loNode()); 2775 } 2776 2777 public boolean containsValue(Object value) { 2778 if (value == null) 2779 throw new NullPointerException (); 2780 for (ConcurrentSkipListMap.Node <K,V> n = loNode(); 2781 isBeforeEnd(n); 2782 n = n.next) { 2783 V v = n.getValidValue(); 2784 if (v != null && value.equals(v)) 2785 return true; 2786 } 2787 return false; 2788 } 2789 2790 public void clear() { 2791 for (ConcurrentSkipListMap.Node <K,V> n = loNode(); 2792 isBeforeEnd(n); 2793 n = n.next) { 2794 if (n.getValidValue() != null) 2795 m.remove(n.key); 2796 } 2797 } 2798 2799 2800 2801 public V putIfAbsent(K key, V value) { 2802 checkKeyBounds(key); 2803 return m.putIfAbsent(key, value); 2804 } 2805 2806 public boolean remove(Object key, Object value) { 2807 K k = (K)key; 2808 return inBounds(k) && m.remove(k, value); 2809 } 2810 2811 public boolean replace(K key, V oldValue, V newValue) { 2812 checkKeyBounds(key); 2813 return m.replace(key, oldValue, newValue); 2814 } 2815 2816 public V replace(K key, V value) { 2817 checkKeyBounds(key); 2818 return m.replace(key, value); 2819 } 2820 2821 2822 2823 public Comparator<? super K> comparator() { 2824 Comparator<? super K> cmp = m.comparator(); 2825 if (isDescending) 2826 return Collections.reverseOrder(cmp); 2827 else 2828 return cmp; 2829 } 2830 2831 2835 private SubMap<K,V> newSubMap(K fromKey, 2836 boolean fromInclusive, 2837 K toKey, 2838 boolean toInclusive) { 2839 if (isDescending) { K tk = fromKey; 2841 fromKey = toKey; 2842 toKey = tk; 2843 boolean ti = fromInclusive; 2844 fromInclusive = toInclusive; 2845 toInclusive = ti; 2846 } 2847 if (lo != null) { 2848 if (fromKey == null) { 2849 fromKey = lo; 2850 fromInclusive = loInclusive; 2851 } 2852 else { 2853 int c = m.compare(fromKey, lo); 2854 if (c < 0 || (c == 0 && !loInclusive && fromInclusive)) 2855 throw new IllegalArgumentException ("key out of range"); 2856 } 2857 } 2858 if (hi != null) { 2859 if (toKey == null) { 2860 toKey = hi; 2861 toInclusive = hiInclusive; 2862 } 2863 else { 2864 int c = m.compare(toKey, hi); 2865 if (c > 0 || (c == 0 && !hiInclusive && toInclusive)) 2866 throw new IllegalArgumentException ("key out of range"); 2867 } 2868 } 2869 return new SubMap<K,V>(m, fromKey, fromInclusive, 2870 toKey, toInclusive, isDescending); 2871 } 2872 2873 public SubMap<K,V> subMap(K fromKey, 2874 boolean fromInclusive, 2875 K toKey, 2876 boolean toInclusive) { 2877 if (fromKey == null || toKey == null) 2878 throw new NullPointerException (); 2879 return newSubMap(fromKey, fromInclusive, toKey, toInclusive); 2880 } 2881 2882 public SubMap<K,V> headMap(K toKey, 2883 boolean inclusive) { 2884 if (toKey == null) 2885 throw new NullPointerException (); 2886 return newSubMap(null, false, toKey, inclusive); 2887 } 2888 2889 public SubMap<K,V> tailMap(K fromKey, 2890 boolean inclusive) { 2891 if (fromKey == null) 2892 throw new NullPointerException (); 2893 return newSubMap(fromKey, inclusive, null, false); 2894 } 2895 2896 public SubMap<K,V> subMap(K fromKey, K toKey) { 2897 return subMap(fromKey, true, toKey, false); 2898 } 2899 2900 public SubMap<K,V> headMap(K toKey) { 2901 return headMap(toKey, false); 2902 } 2903 2904 public SubMap<K,V> tailMap(K fromKey) { 2905 return tailMap(fromKey, true); 2906 } 2907 2908 public SubMap<K,V> descendingMap() { 2909 return new SubMap<K,V>(m, lo, loInclusive, 2910 hi, hiInclusive, !isDescending); 2911 } 2912 2913 2914 2915 public Map.Entry<K,V> ceilingEntry(K key) { 2916 return getNearEntry(key, (m.GT|m.EQ)); 2917 } 2918 2919 public K ceilingKey(K key) { 2920 return getNearKey(key, (m.GT|m.EQ)); 2921 } 2922 2923 public Map.Entry<K,V> lowerEntry(K key) { 2924 return getNearEntry(key, (m.LT)); 2925 } 2926 2927 public K lowerKey(K key) { 2928 return getNearKey(key, (m.LT)); 2929 } 2930 2931 public Map.Entry<K,V> floorEntry(K key) { 2932 return getNearEntry(key, (m.LT|m.EQ)); 2933 } 2934 2935 public K floorKey(K key) { 2936 return getNearKey(key, (m.LT|m.EQ)); 2937 } 2938 2939 public Map.Entry<K,V> higherEntry(K key) { 2940 return getNearEntry(key, (m.GT)); 2941 } 2942 2943 public K higherKey(K key) { 2944 return getNearKey(key, (m.GT)); 2945 } 2946 2947 public K firstKey() { 2948 return isDescending? highestKey() : lowestKey(); 2949 } 2950 2951 public K lastKey() { 2952 return isDescending? lowestKey() : highestKey(); 2953 } 2954 2955 public Map.Entry<K,V> firstEntry() { 2956 return isDescending? highestEntry() : lowestEntry(); 2957 } 2958 2959 public Map.Entry<K,V> lastEntry() { 2960 return isDescending? lowestEntry() : highestEntry(); 2961 } 2962 2963 public Map.Entry<K,V> pollFirstEntry() { 2964 return isDescending? removeHighest() : removeLowest(); 2965 } 2966 2967 public Map.Entry<K,V> pollLastEntry() { 2968 return isDescending? removeLowest() : removeHighest(); 2969 } 2970 2971 2972 2973 public NavigableSet<K> keySet() { 2974 KeySet<K> ks = keySetView; 2975 return (ks != null) ? ks : (keySetView = new KeySet(this)); 2976 } 2977 2978 public NavigableSet<K> navigableKeySet() { 2979 KeySet<K> ks = keySetView; 2980 return (ks != null) ? ks : (keySetView = new KeySet(this)); 2981 } 2982 2983 public Collection<V> values() { 2984 Collection<V> vs = valuesView; 2985 return (vs != null) ? vs : (valuesView = new Values(this)); 2986 } 2987 2988 public Set<Map.Entry<K,V>> entrySet() { 2989 Set<Map.Entry<K,V>> es = entrySetView; 2990 return (es != null) ? es : (entrySetView = new EntrySet(this)); 2991 } 2992 2993 public NavigableSet<K> descendingKeySet() { 2994 return descendingMap().navigableKeySet(); 2995 } 2996 2997 Iterator<K> keyIterator() { 2998 return new SubMapKeyIterator(); 2999 } 3000 3001 Iterator<V> valueIterator() { 3002 return new SubMapValueIterator(); 3003 } 3004 3005 Iterator<Map.Entry<K,V>> entryIterator() { 3006 return new SubMapEntryIterator(); 3007 } 3008 3009 3012 abstract class SubMapIter<T> implements Iterator<T> { 3013 3014 Node<K,V> lastReturned; 3015 3016 Node<K,V> next; 3017 3018 V nextValue; 3019 3020 SubMapIter() { 3021 for (;;) { 3022 next = isDescending ? hiNode() : loNode(); 3023 if (next == null) 3024 break; 3025 Object x = next.value; 3026 if (x != null && x != next) { 3027 if (! inBounds(next.key)) 3028 next = null; 3029 else 3030 nextValue = (V) x; 3031 break; 3032 } 3033 } 3034 } 3035 3036 public final boolean hasNext() { 3037 return next != null; 3038 } 3039 3040 final void advance() { 3041 if (next == null) 3042 throw new NoSuchElementException(); 3043 lastReturned = next; 3044 if (isDescending) 3045 descend(); 3046 else 3047 ascend(); 3048 } 3049 3050 private void ascend() { 3051 for (;;) { 3052 next = next.next; 3053 if (next == null) 3054 break; 3055 Object x = next.value; 3056 if (x != null && x != next) { 3057 if (tooHigh(next.key)) 3058 next = null; 3059 else 3060 nextValue = (V) x; 3061 break; 3062 } 3063 } 3064 } 3065 3066 private void descend() { 3067 for (;;) { 3068 next = m.findNear(lastReturned.key, LT); 3069 if (next == null) 3070 break; 3071 Object x = next.value; 3072 if (x != null && x != next) { 3073 if (tooLow(next.key)) 3074 next = null; 3075 else 3076 nextValue = (V) x; 3077 break; 3078 } 3079 } 3080 } 3081 3082 public void remove() { 3083 Node<K,V> l = lastReturned; 3084 if (l == null) 3085 throw new IllegalStateException (); 3086 m.remove(l.key); 3087 lastReturned = null; 3088 } 3089 3090 } 3091 3092 final class SubMapValueIterator extends SubMapIter<V> { 3093 public V next() { 3094 V v = nextValue; 3095 advance(); 3096 return v; 3097 } 3098 } 3099 3100 final class SubMapKeyIterator extends SubMapIter<K> { 3101 public K next() { 3102 Node<K,V> n = next; 3103 advance(); 3104 return n.key; 3105 } 3106 } 3107 3108 final class SubMapEntryIterator extends SubMapIter<Map.Entry<K,V>> { 3109 public Map.Entry<K,V> next() { 3110 Node<K,V> n = next; 3111 V v = nextValue; 3112 advance(); 3113 return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v); 3114 } 3115 } 3116 } 3117} 3118 | Popular Tags |