1 7 8 package java.util; 9 10 80 81 public class TreeMap<K,V> 82 extends AbstractMap <K,V> 83 implements SortedMap <K,V>, Cloneable , java.io.Serializable 84 { 85 91 private Comparator <? super K> comparator = null; 92 93 private transient Entry<K,V> root = null; 94 95 98 private transient int size = 0; 99 100 103 private transient int modCount = 0; 104 105 private void incrementSize() { modCount++; size++; } 106 private void decrementSize() { modCount++; size--; } 107 108 121 public TreeMap() { 122 } 123 124 137 public TreeMap(Comparator <? super K> c) { 138 this.comparator = c; 139 } 140 141 155 public TreeMap(Map <? extends K, ? extends V> m) { 156 putAll(m); 157 } 158 159 168 public TreeMap(SortedMap <K, ? extends V> m) { 169 comparator = m.comparator(); 170 try { 171 buildFromSorted(m.size(), m.entrySet().iterator(), null, null); 172 } catch (java.io.IOException cannotHappen) { 173 } catch (ClassNotFoundException cannotHappen) { 174 } 175 } 176 177 178 180 185 public int size() { 186 return size; 187 } 188 189 203 public boolean containsKey(Object key) { 204 return getEntry(key) != null; 205 } 206 207 220 public boolean containsValue(Object value) { 221 return (root==null ? false : 222 (value==null ? valueSearchNull(root) 223 : valueSearchNonNull(root, value))); 224 } 225 226 private boolean valueSearchNull(Entry n) { 227 if (n.value == null) 228 return true; 229 230 return (n.left != null && valueSearchNull(n.left)) || 232 (n.right != null && valueSearchNull(n.right)); 233 } 234 235 private boolean valueSearchNonNull(Entry n, Object value) { 236 if (value.equals(n.value)) 238 return true; 239 240 return (n.left != null && valueSearchNonNull(n.left, value)) || 242 (n.right != null && valueSearchNonNull(n.right, value)); 243 } 244 245 264 public V get(Object key) { 265 Entry<K,V> p = getEntry(key); 266 return (p==null ? null : p.value); 267 } 268 269 276 public Comparator <? super K> comparator() { 277 return comparator; 278 } 279 280 286 public K firstKey() { 287 return key(firstEntry()); 288 } 289 290 296 public K lastKey() { 297 return key(lastEntry()); 298 } 299 300 313 public void putAll(Map <? extends K, ? extends V> map) { 314 int mapSize = map.size(); 315 if (size==0 && mapSize!=0 && map instanceof SortedMap ) { 316 Comparator c = ((SortedMap )map).comparator(); 317 if (c == comparator || (c != null && c.equals(comparator))) { 318 ++modCount; 319 try { 320 buildFromSorted(mapSize, map.entrySet().iterator(), 321 null, null); 322 } catch (java.io.IOException cannotHappen) { 323 } catch (ClassNotFoundException cannotHappen) { 324 } 325 return; 326 } 327 } 328 super.putAll(map); 329 } 330 331 343 private Entry<K,V> getEntry(Object key) { 344 Entry<K,V> p = root; 345 K k = (K) key; 346 while (p != null) { 347 int cmp = compare(k, p.key); 348 if (cmp == 0) 349 return p; 350 else if (cmp < 0) 351 p = p.left; 352 else 353 p = p.right; 354 } 355 return null; 356 } 357 358 364 private Entry<K,V> getCeilEntry(K key) { 365 Entry<K,V> p = root; 366 if (p==null) 367 return null; 368 369 while (true) { 370 int cmp = compare(key, p.key); 371 if (cmp == 0) { 372 return p; 373 } else if (cmp < 0) { 374 if (p.left != null) 375 p = p.left; 376 else 377 return p; 378 } else { 379 if (p.right != null) { 380 p = p.right; 381 } else { 382 Entry<K,V> parent = p.parent; 383 Entry<K,V> ch = p; 384 while (parent != null && ch == parent.right) { 385 ch = parent; 386 parent = parent.parent; 387 } 388 return parent; 389 } 390 } 391 } 392 } 393 394 399 private Entry<K,V> getPrecedingEntry(K key) { 400 Entry<K,V> p = root; 401 if (p==null) 402 return null; 403 404 while (true) { 405 int cmp = compare(key, p.key); 406 if (cmp > 0) { 407 if (p.right != null) 408 p = p.right; 409 else 410 return p; 411 } else { 412 if (p.left != null) { 413 p = p.left; 414 } else { 415 Entry<K,V> parent = p.parent; 416 Entry<K,V> ch = p; 417 while (parent != null && ch == parent.left) { 418 ch = parent; 419 parent = parent.parent; 420 } 421 return parent; 422 } 423 } 424 } 425 } 426 427 431 private static <K> K key(Entry<K,?> e) { 432 if (e==null) 433 throw new NoSuchElementException (); 434 return e.key; 435 } 436 437 455 public V put(K key, V value) { 456 Entry<K,V> t = root; 457 458 if (t == null) { 459 incrementSize(); 460 root = new Entry<K,V>(key, value, null); 461 return null; 462 } 463 464 while (true) { 465 int cmp = compare(key, t.key); 466 if (cmp == 0) { 467 return t.setValue(value); 468 } else if (cmp < 0) { 469 if (t.left != null) { 470 t = t.left; 471 } else { 472 incrementSize(); 473 t.left = new Entry<K,V>(key, value, t); 474 fixAfterInsertion(t.left); 475 return null; 476 } 477 } else { if (t.right != null) { 479 t = t.right; 480 } else { 481 incrementSize(); 482 t.right = new Entry<K,V>(key, value, t); 483 fixAfterInsertion(t.right); 484 return null; 485 } 486 } 487 } 488 } 489 490 505 public V remove(Object key) { 506 Entry<K,V> p = getEntry(key); 507 if (p == null) 508 return null; 509 510 V oldValue = p.value; 511 deleteEntry(p); 512 return oldValue; 513 } 514 515 518 public void clear() { 519 modCount++; 520 size = 0; 521 root = null; 522 } 523 524 530 public Object clone() { 531 TreeMap <K,V> clone = null; 532 try { 533 clone = (TreeMap <K,V>) super.clone(); 534 } catch (CloneNotSupportedException e) { 535 throw new InternalError (); 536 } 537 538 clone.root = null; 540 clone.size = 0; 541 clone.modCount = 0; 542 clone.entrySet = null; 543 544 try { 546 clone.buildFromSorted(size, entrySet().iterator(), null, null); 547 } catch (java.io.IOException cannotHappen) { 548 } catch (ClassNotFoundException cannotHappen) { 549 } 550 551 return clone; 552 } 553 554 555 557 562 private transient volatile Set <Map.Entry <K,V>> entrySet = null; 563 564 576 public Set <K> keySet() { 577 if (keySet == null) { 578 keySet = new AbstractSet <K>() { 579 public Iterator <K> iterator() { 580 return new KeyIterator(); 581 } 582 583 public int size() { 584 return TreeMap.this.size(); 585 } 586 587 public boolean contains(Object o) { 588 return containsKey(o); 589 } 590 591 public boolean remove(Object o) { 592 int oldSize = size; 593 TreeMap.this.remove(o); 594 return size != oldSize; 595 } 596 597 public void clear() { 598 TreeMap.this.clear(); 599 } 600 }; 601 } 602 return keySet; 603 } 604 605 618 public Collection <V> values() { 619 if (values == null) { 620 values = new AbstractCollection <V>() { 621 public Iterator <V> iterator() { 622 return new ValueIterator(); 623 } 624 625 public int size() { 626 return TreeMap.this.size(); 627 } 628 629 public boolean contains(Object o) { 630 for (Entry<K,V> e = firstEntry(); e != null; e = successor(e)) 631 if (valEquals(e.getValue(), o)) 632 return true; 633 return false; 634 } 635 636 public boolean remove(Object o) { 637 for (Entry<K,V> e = firstEntry(); e != null; e = successor(e)) { 638 if (valEquals(e.getValue(), o)) { 639 deleteEntry(e); 640 return true; 641 } 642 } 643 return false; 644 } 645 646 public void clear() { 647 TreeMap.this.clear(); 648 } 649 }; 650 } 651 return values; 652 } 653 654 668 public Set <Map.Entry <K,V>> entrySet() { 669 if (entrySet == null) { 670 entrySet = new AbstractSet <Map.Entry <K,V>>() { 671 public Iterator <Map.Entry <K,V>> iterator() { 672 return new EntryIterator(); 673 } 674 675 public boolean contains(Object o) { 676 if (!(o instanceof Map.Entry )) 677 return false; 678 Map.Entry <K,V> entry = (Map.Entry <K,V>) o; 679 V value = entry.getValue(); 680 Entry<K,V> p = getEntry(entry.getKey()); 681 return p != null && valEquals(p.getValue(), value); 682 } 683 684 public boolean remove(Object o) { 685 if (!(o instanceof Map.Entry )) 686 return false; 687 Map.Entry <K,V> entry = (Map.Entry <K,V>) o; 688 V value = entry.getValue(); 689 Entry<K,V> p = getEntry(entry.getKey()); 690 if (p != null && valEquals(p.getValue(), value)) { 691 deleteEntry(p); 692 return true; 693 } 694 return false; 695 } 696 697 public int size() { 698 return TreeMap.this.size(); 699 } 700 701 public void clear() { 702 TreeMap.this.clear(); 703 } 704 }; 705 } 706 return entrySet; 707 } 708 709 753 public SortedMap <K,V> subMap(K fromKey, K toKey) { 754 return new SubMap(fromKey, toKey); 755 } 756 757 794 public SortedMap <K,V> headMap(K toKey) { 795 return new SubMap(toKey, true); 796 } 797 798 833 public SortedMap <K,V> tailMap(K fromKey) { 834 return new SubMap(fromKey, false); 835 } 836 837 private class SubMap 838 extends AbstractMap <K,V> 839 implements SortedMap <K,V>, java.io.Serializable { 840 private static final long serialVersionUID = -6520786458950516097L; 841 842 846 private boolean fromStart = false, toEnd = false; 847 private K fromKey, toKey; 848 849 SubMap(K fromKey, K toKey) { 850 if (compare(fromKey, toKey) > 0) 851 throw new IllegalArgumentException ("fromKey > toKey"); 852 this.fromKey = fromKey; 853 this.toKey = toKey; 854 } 855 856 SubMap(K key, boolean headMap) { 857 compare(key, key); 859 if (headMap) { 860 fromStart = true; 861 toKey = key; 862 } else { 863 toEnd = true; 864 fromKey = key; 865 } 866 } 867 868 SubMap(boolean fromStart, K fromKey, boolean toEnd, K toKey) { 869 this.fromStart = fromStart; 870 this.fromKey= fromKey; 871 this.toEnd = toEnd; 872 this.toKey = toKey; 873 } 874 875 public boolean isEmpty() { 876 return entrySet.isEmpty(); 877 } 878 879 public boolean containsKey(Object key) { 880 return inRange((K) key) && TreeMap.this.containsKey(key); 881 } 882 883 public V get(Object key) { 884 if (!inRange((K) key)) 885 return null; 886 return TreeMap.this.get(key); 887 } 888 889 public V put(K key, V value) { 890 if (!inRange(key)) 891 throw new IllegalArgumentException ("key out of range"); 892 return TreeMap.this.put(key, value); 893 } 894 895 public Comparator <? super K> comparator() { 896 return comparator; 897 } 898 899 public K firstKey() { 900 TreeMap.Entry <K,V> e = fromStart ? firstEntry() : getCeilEntry(fromKey); 901 K first = key(e); 902 if (!toEnd && compare(first, toKey) >= 0) 903 throw(new NoSuchElementException ()); 904 return first; 905 } 906 907 public K lastKey() { 908 TreeMap.Entry <K,V> e = toEnd ? lastEntry() : getPrecedingEntry(toKey); 909 K last = key(e); 910 if (!fromStart && compare(last, fromKey) < 0) 911 throw(new NoSuchElementException ()); 912 return last; 913 } 914 915 private transient Set <Map.Entry <K,V>> entrySet = new EntrySetView(); 916 917 public Set <Map.Entry <K,V>> entrySet() { 918 return entrySet; 919 } 920 921 private class EntrySetView extends AbstractSet <Map.Entry <K,V>> { 922 private transient int size = -1, sizeModCount; 923 924 public int size() { 925 if (size == -1 || sizeModCount != TreeMap.this.modCount) { 926 size = 0; sizeModCount = TreeMap.this.modCount; 927 Iterator i = iterator(); 928 while (i.hasNext()) { 929 size++; 930 i.next(); 931 } 932 } 933 return size; 934 } 935 936 public boolean isEmpty() { 937 return !iterator().hasNext(); 938 } 939 940 public boolean contains(Object o) { 941 if (!(o instanceof Map.Entry )) 942 return false; 943 Map.Entry <K,V> entry = (Map.Entry <K,V>) o; 944 K key = entry.getKey(); 945 if (!inRange(key)) 946 return false; 947 TreeMap.Entry node = getEntry(key); 948 return node != null && 949 valEquals(node.getValue(), entry.getValue()); 950 } 951 952 public boolean remove(Object o) { 953 if (!(o instanceof Map.Entry )) 954 return false; 955 Map.Entry <K,V> entry = (Map.Entry <K,V>) o; 956 K key = entry.getKey(); 957 if (!inRange(key)) 958 return false; 959 TreeMap.Entry <K,V> node = getEntry(key); 960 if (node!=null && valEquals(node.getValue(),entry.getValue())){ 961 deleteEntry(node); 962 return true; 963 } 964 return false; 965 } 966 967 public Iterator <Map.Entry <K,V>> iterator() { 968 return new SubMapEntryIterator( 969 (fromStart ? firstEntry() : getCeilEntry(fromKey)), 970 (toEnd ? null : getCeilEntry(toKey))); 971 } 972 } 973 974 public SortedMap <K,V> subMap(K fromKey, K toKey) { 975 if (!inRange2(fromKey)) 976 throw new IllegalArgumentException ("fromKey out of range"); 977 if (!inRange2(toKey)) 978 throw new IllegalArgumentException ("toKey out of range"); 979 return new SubMap(fromKey, toKey); 980 } 981 982 public SortedMap <K,V> headMap(K toKey) { 983 if (!inRange2(toKey)) 984 throw new IllegalArgumentException ("toKey out of range"); 985 return new SubMap(fromStart, fromKey, false, toKey); 986 } 987 988 public SortedMap <K,V> tailMap(K fromKey) { 989 if (!inRange2(fromKey)) 990 throw new IllegalArgumentException ("fromKey out of range"); 991 return new SubMap(false, fromKey, toEnd, toKey); 992 } 993 994 private boolean inRange(K key) { 995 return (fromStart || compare(key, fromKey) >= 0) && 996 (toEnd || compare(key, toKey) < 0); 997 } 998 999 private boolean inRange2(K key) { 1001 return (fromStart || compare(key, fromKey) >= 0) && 1002 (toEnd || compare(key, toKey) <= 0); 1003 } 1004 } 1005 1006 1009 private abstract class PrivateEntryIterator<T> implements Iterator <T> { 1010 private int expectedModCount = TreeMap.this.modCount; 1011 private Entry<K,V> lastReturned = null; 1012 Entry<K,V> next; 1013 1014 PrivateEntryIterator() { 1015 next = firstEntry(); 1016 } 1017 1018 PrivateEntryIterator(Entry<K,V> first) { 1020 next = first; 1021 } 1022 1023 public boolean hasNext() { 1024 return next != null; 1025 } 1026 1027 final Entry<K,V> nextEntry() { 1028 if (next == null) 1029 throw new NoSuchElementException (); 1030 if (modCount != expectedModCount) 1031 throw new ConcurrentModificationException (); 1032 lastReturned = next; 1033 next = successor(next); 1034 return lastReturned; 1035 } 1036 1037 public void remove() { 1038 if (lastReturned == null) 1039 throw new IllegalStateException (); 1040 if (modCount != expectedModCount) 1041 throw new ConcurrentModificationException (); 1042 if (lastReturned.left != null && lastReturned.right != null) 1043 next = lastReturned; 1044 deleteEntry(lastReturned); 1045 expectedModCount++; 1046 lastReturned = null; 1047 } 1048 } 1049 1050 private class EntryIterator extends PrivateEntryIterator<Map.Entry <K,V>> { 1051 public Map.Entry <K,V> next() { 1052 return nextEntry(); 1053 } 1054 } 1055 1056 private class KeyIterator extends PrivateEntryIterator<K> { 1057 public K next() { 1058 return nextEntry().key; 1059 } 1060 } 1061 1062 private class ValueIterator extends PrivateEntryIterator<V> { 1063 public V next() { 1064 return nextEntry().value; 1065 } 1066 } 1067 1068 private class SubMapEntryIterator extends PrivateEntryIterator<Map.Entry <K,V>> { 1069 private final K firstExcludedKey; 1070 1071 SubMapEntryIterator(Entry<K,V> first, Entry<K,V> firstExcluded) { 1072 super(first); 1073 firstExcludedKey = (firstExcluded == null 1074 ? null 1075 : firstExcluded.key); 1076 } 1077 1078 public boolean hasNext() { 1079 return next != null && next.key != firstExcludedKey; 1080 } 1081 1082 public Map.Entry <K,V> next() { 1083 if (next == null || next.key == firstExcludedKey) 1084 throw new NoSuchElementException (); 1085 return nextEntry(); 1086 } 1087 } 1088 1089 1092 private int compare(K k1, K k2) { 1093 return (comparator==null ? ((Comparable <K>)k1).compareTo(k2) 1094 : comparator.compare((K)k1, (K)k2)); 1095 } 1096 1097 1101 private static boolean valEquals(Object o1, Object o2) { 1102 return (o1==null ? o2==null : o1.equals(o2)); 1103 } 1104 1105 private static final boolean RED = false; 1106 private static final boolean BLACK = true; 1107 1108 1112 1113 static class Entry<K,V> implements Map.Entry <K,V> { 1114 K key; 1115 V value; 1116 Entry<K,V> left = null; 1117 Entry<K,V> right = null; 1118 Entry<K,V> parent; 1119 boolean color = BLACK; 1120 1121 1125 Entry(K key, V value, Entry<K,V> parent) { 1126 this.key = key; 1127 this.value = value; 1128 this.parent = parent; 1129 } 1130 1131 1136 public K getKey() { 1137 return key; 1138 } 1139 1140 1145 public V getValue() { 1146 return value; 1147 } 1148 1149 1156 public V setValue(V value) { 1157 V oldValue = this.value; 1158 this.value = value; 1159 return oldValue; 1160 } 1161 1162 public boolean equals(Object o) { 1163 if (!(o instanceof Map.Entry )) 1164 return false; 1165 Map.Entry e = (Map.Entry )o; 1166 1167 return valEquals(key,e.getKey()) && valEquals(value,e.getValue()); 1168 } 1169 1170 public int hashCode() { 1171 int keyHash = (key==null ? 0 : key.hashCode()); 1172 int valueHash = (value==null ? 0 : value.hashCode()); 1173 return keyHash ^ valueHash; 1174 } 1175 1176 public String toString() { 1177 return key + "=" + value; 1178 } 1179 } 1180 1181 1185 private Entry<K,V> firstEntry() { 1186 Entry<K,V> p = root; 1187 if (p != null) 1188 while (p.left != null) 1189 p = p.left; 1190 return p; 1191 } 1192 1193 1197 private Entry<K,V> lastEntry() { 1198 Entry<K,V> p = root; 1199 if (p != null) 1200 while (p.right != null) 1201 p = p.right; 1202 return p; 1203 } 1204 1205 1208 private Entry<K,V> successor(Entry<K,V> t) { 1209 if (t == null) 1210 return null; 1211 else if (t.right != null) { 1212 Entry<K,V> p = t.right; 1213 while (p.left != null) 1214 p = p.left; 1215 return p; 1216 } else { 1217 Entry<K,V> p = t.parent; 1218 Entry<K,V> ch = t; 1219 while (p != null && ch == p.right) { 1220 ch = p; 1221 p = p.parent; 1222 } 1223 return p; 1224 } 1225 } 1226 1227 1236 1237 private static <K,V> boolean colorOf(Entry<K,V> p) { 1238 return (p == null ? BLACK : p.color); 1239 } 1240 1241 private static <K,V> Entry<K,V> parentOf(Entry<K,V> p) { 1242 return (p == null ? null: p.parent); 1243 } 1244 1245 private static <K,V> void setColor(Entry<K,V> p, boolean c) { 1246 if (p != null) 1247 p.color = c; 1248 } 1249 1250 private static <K,V> Entry<K,V> leftOf(Entry<K,V> p) { 1251 return (p == null) ? null: p.left; 1252 } 1253 1254 private static <K,V> Entry<K,V> rightOf(Entry<K,V> p) { 1255 return (p == null) ? null: p.right; 1256 } 1257 1258 1259 private void rotateLeft(Entry<K,V> p) { 1260 Entry<K,V> r = p.right; 1261 p.right = r.left; 1262 if (r.left != null) 1263 r.left.parent = p; 1264 r.parent = p.parent; 1265 if (p.parent == null) 1266 root = r; 1267 else if (p.parent.left == p) 1268 p.parent.left = r; 1269 else 1270 p.parent.right = r; 1271 r.left = p; 1272 p.parent = r; 1273 } 1274 1275 1276 private void rotateRight(Entry<K,V> p) { 1277 Entry<K,V> l = p.left; 1278 p.left = l.right; 1279 if (l.right != null) l.right.parent = p; 1280 l.parent = p.parent; 1281 if (p.parent == null) 1282 root = l; 1283 else if (p.parent.right == p) 1284 p.parent.right = l; 1285 else p.parent.left = l; 1286 l.right = p; 1287 p.parent = l; 1288 } 1289 1290 1291 1292 private void fixAfterInsertion(Entry<K,V> x) { 1293 x.color = RED; 1294 1295 while (x != null && x != root && x.parent.color == RED) { 1296 if (parentOf(x) == leftOf(parentOf(parentOf(x)))) { 1297 Entry<K,V> y = rightOf(parentOf(parentOf(x))); 1298 if (colorOf(y) == RED) { 1299 setColor(parentOf(x), BLACK); 1300 setColor(y, BLACK); 1301 setColor(parentOf(parentOf(x)), RED); 1302 x = parentOf(parentOf(x)); 1303 } else { 1304 if (x == rightOf(parentOf(x))) { 1305 x = parentOf(x); 1306 rotateLeft(x); 1307 } 1308 setColor(parentOf(x), BLACK); 1309 setColor(parentOf(parentOf(x)), RED); 1310 if (parentOf(parentOf(x)) != null) 1311 rotateRight(parentOf(parentOf(x))); 1312 } 1313 } else { 1314 Entry<K,V> y = leftOf(parentOf(parentOf(x))); 1315 if (colorOf(y) == RED) { 1316 setColor(parentOf(x), BLACK); 1317 setColor(y, BLACK); 1318 setColor(parentOf(parentOf(x)), RED); 1319 x = parentOf(parentOf(x)); 1320 } else { 1321 if (x == leftOf(parentOf(x))) { 1322 x = parentOf(x); 1323 rotateRight(x); 1324 } 1325 setColor(parentOf(x), BLACK); 1326 setColor(parentOf(parentOf(x)), RED); 1327 if (parentOf(parentOf(x)) != null) 1328 rotateLeft(parentOf(parentOf(x))); 1329 } 1330 } 1331 } 1332 root.color = BLACK; 1333 } 1334 1335 1338 1339 private void deleteEntry(Entry<K,V> p) { 1340 decrementSize(); 1341 1342 if (p.left != null && p.right != null) { 1345 Entry<K,V> s = successor (p); 1346 p.key = s.key; 1347 p.value = s.value; 1348 p = s; 1349 } 1351 Entry<K,V> replacement = (p.left != null ? p.left : p.right); 1353 1354 if (replacement != null) { 1355 replacement.parent = p.parent; 1357 if (p.parent == null) 1358 root = replacement; 1359 else if (p == p.parent.left) 1360 p.parent.left = replacement; 1361 else 1362 p.parent.right = replacement; 1363 1364 p.left = p.right = p.parent = null; 1366 1367 if (p.color == BLACK) 1369 fixAfterDeletion(replacement); 1370 } else if (p.parent == null) { root = null; 1372 } else { if (p.color == BLACK) 1374 fixAfterDeletion(p); 1375 1376 if (p.parent != null) { 1377 if (p == p.parent.left) 1378 p.parent.left = null; 1379 else if (p == p.parent.right) 1380 p.parent.right = null; 1381 p.parent = null; 1382 } 1383 } 1384 } 1385 1386 1387 private void fixAfterDeletion(Entry<K,V> x) { 1388 while (x != root && colorOf(x) == BLACK) { 1389 if (x == leftOf(parentOf(x))) { 1390 Entry<K,V> sib = rightOf(parentOf(x)); 1391 1392 if (colorOf(sib) == RED) { 1393 setColor(sib, BLACK); 1394 setColor(parentOf(x), RED); 1395 rotateLeft(parentOf(x)); 1396 sib = rightOf(parentOf(x)); 1397 } 1398 1399 if (colorOf(leftOf(sib)) == BLACK && 1400 colorOf(rightOf(sib)) == BLACK) { 1401 setColor(sib, RED); 1402 x = parentOf(x); 1403 } else { 1404 if (colorOf(rightOf(sib)) == BLACK) { 1405 setColor(leftOf(sib), BLACK); 1406 setColor(sib, RED); 1407 rotateRight(sib); 1408 sib = rightOf(parentOf(x)); 1409 } 1410 setColor(sib, colorOf(parentOf(x))); 1411 setColor(parentOf(x), BLACK); 1412 setColor(rightOf(sib), BLACK); 1413 rotateLeft(parentOf(x)); 1414 x = root; 1415 } 1416 } else { Entry<K,V> sib = leftOf(parentOf(x)); 1418 1419 if (colorOf(sib) == RED) { 1420 setColor(sib, BLACK); 1421 setColor(parentOf(x), RED); 1422 rotateRight(parentOf(x)); 1423 sib = leftOf(parentOf(x)); 1424 } 1425 1426 if (colorOf(rightOf(sib)) == BLACK && 1427 colorOf(leftOf(sib)) == BLACK) { 1428 setColor(sib, RED); 1429 x = parentOf(x); 1430 } else { 1431 if (colorOf(leftOf(sib)) == BLACK) { 1432 setColor(rightOf(sib), BLACK); 1433 setColor(sib, RED); 1434 rotateLeft(sib); 1435 sib = leftOf(parentOf(x)); 1436 } 1437 setColor(sib, colorOf(parentOf(x))); 1438 setColor(parentOf(x), BLACK); 1439 setColor(leftOf(sib), BLACK); 1440 rotateRight(parentOf(x)); 1441 x = root; 1442 } 1443 } 1444 } 1445 1446 setColor(x, BLACK); 1447 } 1448 1449 private static final long serialVersionUID = 919286545866124006L; 1450 1451 1463 private void writeObject(java.io.ObjectOutputStream s) 1464 throws java.io.IOException { 1465 s.defaultWriteObject(); 1467 1468 s.writeInt(size); 1470 1471 for (Iterator <Map.Entry <K,V>> i = entrySet().iterator(); i.hasNext(); ) { 1473 Map.Entry <K,V> e = i.next(); 1474 s.writeObject(e.getKey()); 1475 s.writeObject(e.getValue()); 1476 } 1477 } 1478 1479 1480 1481 1485 private void readObject(final java.io.ObjectInputStream s) 1486 throws java.io.IOException , ClassNotFoundException { 1487 s.defaultReadObject(); 1489 1490 int size = s.readInt(); 1492 1493 buildFromSorted(size, null, s, null); 1494 } 1495 1496 1497 void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal) 1498 throws java.io.IOException , ClassNotFoundException { 1499 buildFromSorted(size, null, s, defaultVal); 1500 } 1501 1502 1503 void addAllForTreeSet(SortedSet <Map.Entry <K,V>> set, V defaultVal) { 1504 try { 1505 buildFromSorted(set.size(), set.iterator(), null, defaultVal); 1506 } catch (java.io.IOException cannotHappen) { 1507 } catch (ClassNotFoundException cannotHappen) { 1508 } 1509 } 1510 1511 1512 1542 private 1543 void buildFromSorted(int size, Iterator it, 1544 java.io.ObjectInputStream str, 1545 V defaultVal) 1546 throws java.io.IOException , ClassNotFoundException { 1547 this.size = size; 1548 root = 1549 buildFromSorted(0, 0, size-1, computeRedLevel(size), 1550 it, str, defaultVal); 1551 } 1552 1553 1567 private final Entry<K,V> buildFromSorted(int level, int lo, int hi, 1568 int redLevel, 1569 Iterator it, 1570 java.io.ObjectInputStream str, 1571 V defaultVal) 1572 throws java.io.IOException , ClassNotFoundException { 1573 1584 1585 if (hi < lo) return null; 1586 1587 int mid = (lo + hi) / 2; 1588 1589 Entry<K,V> left = null; 1590 if (lo < mid) 1591 left = buildFromSorted(level+1, lo, mid - 1, redLevel, 1592 it, str, defaultVal); 1593 1594 K key; 1596 V value; 1597 if (it != null) { 1598 if (defaultVal==null) { 1599 Map.Entry <K,V> entry = (Map.Entry <K,V>)it.next(); 1600 key = entry.getKey(); 1601 value = entry.getValue(); 1602 } else { 1603 key = (K)it.next(); 1604 value = defaultVal; 1605 } 1606 } else { key = (K) str.readObject(); 1608 value = (defaultVal != null ? defaultVal : (V) str.readObject()); 1609 } 1610 1611 Entry<K,V> middle = new Entry<K,V>(key, value, null); 1612 1613 if (level == redLevel) 1615 middle.color = RED; 1616 1617 if (left != null) { 1618 middle.left = left; 1619 left.parent = middle; 1620 } 1621 1622 if (mid < hi) { 1623 Entry<K,V> right = buildFromSorted(level+1, mid+1, hi, redLevel, 1624 it, str, defaultVal); 1625 middle.right = right; 1626 right.parent = middle; 1627 } 1628 1629 return middle; 1630 } 1631 1632 1641 private static int computeRedLevel(int sz) { 1642 int level = 0; 1643 for (int m = sz - 1; m >= 0; m = m / 2 - 1) 1644 level++; 1645 return level; 1646 } 1647} 1648 | Popular Tags |